<commit_before>/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.orm.jpa.support;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import junit.framework.TestCase;
import org.easymock.MockControl;

import org.springframework.mock.web.MockFilterConfig;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;

/**
 * @author <NAME>
 */
public class OpenEntityManagerInViewTests extends TestCase {

	private MockControl factoryControl, managerControl, txControl;

	private EntityManager manager;

	private EntityTransaction tx;

	private EntityManagerFactory factory;

	private JpaTemplate template;

	@Override
	protected void setUp() throws Exception {
		factoryControl = MockControl.createControl(EntityManagerFactory.class);
		factory = (EntityManagerFactory) factoryControl.getMock();
		managerControl = MockControl.createControl(EntityManager.class);
		manager = (EntityManager) managerControl.getMock();
		txControl = MockControl.createControl(EntityTransaction.class);
		tx = (EntityTransaction) txControl.getMock();

		template = new JpaTemplate(factory);
		template.afterPropertiesSet();

		factoryControl.expectAndReturn(factory.createEntityManager(), manager);
	}

	@Override
	protected void tearDown() throws Exception {
		assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
		assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
		assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
		assertFalse(TransactionSynchronizationManager.isActualTransactionActive());

		factoryControl = null;
		managerControl = null;
		txControl = null;
		manager = null;
		factory = null;
		template = null;
		tx = null;
	}

	public void testOpenEntityManagerInterceptorInView() throws Exception {
		OpenEntityManagerInViewInterceptor rawInterceptor = new OpenEntityManagerInViewInterceptor();
		rawInterceptor.setEntityManagerFactory(factory);
		HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

		MockServletContext sc = new MockServletContext();
		MockHttpServletRequest request = new MockHttpServletRequest(sc);
		MockHttpServletResponse response = new MockHttpServletResponse();
		
		managerControl.replay();
		factoryControl.replay();
		
		interceptor.preHandle(request, response, "handler");
		assertTrue(TransactionSynchronizationManager.hasResource(factory));
		
		// check that further invocations simply participate
		interceptor.preHandle(request, response, "handler");

		interceptor.preHandle(request, response, "handler");
		interceptor.postHandle(request, response, "handler", null);
		interceptor.afterCompletion(request, response, "handler", null);

		interceptor.postHandle(request, response, "handler", null);
		interceptor.afterCompletion(request, response, "handler", null);

		interceptor.preHandle(request, response, "handler");
		interceptor.postHandle(request, response, "handler", null);
		interceptor.afterCompletion(request, response, "handler", null);
		<commit_msg>polishing<commit_after>/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.orm.jpa.support;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import junit.framework.TestCase;
import org.easymock.MockControl;

import org.springframework.mock.web.MockFilterConfig;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;

/**
 * @author <NAME>
 */
public class OpenEntityManagerInViewTests extends TestCase {

	private MockControl factoryControl, managerControl;

	private EntityManager manager;

	private EntityManagerFactory factory;

	private JpaTemplate template;


	@Override
	protected void setUp() throws Exception {
		factoryControl = MockControl.createControl(EntityManagerFactory.class);
		factory = (EntityManagerFactory) factoryControl.getMock();
		managerControl = MockControl.createControl(EntityManager.class);
		manager = (EntityManager) managerControl.getMock();

		template = new JpaTemplate(factory);
		template.afterPropertiesSet();

		factoryControl.expectAndReturn(factory.createEntityManager(), manager);
	}

	@Override
	protected void tearDown() throws Exception {
		assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
		assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
		assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
		assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
	}

	public void testOpenEntityManagerInterceptorInView() throws Exception {
		OpenEntityManagerInViewInterceptor rawInterceptor = new OpenEntityManagerInViewInterceptor();
		rawInterceptor.setEntityManagerFactory(factory);
		HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

		MockServletContext sc = new MockServletContext();
		MockHttpServletRequest request = new MockHttpServletRequest(sc);
		MockHttpServletResponse response = new MockHttpServletResponse();
		
		managerControl.replay();
		factoryControl.replay();
		
		interceptor.preHandle(request, response, "handler");
		assertTrue(TransactionSynchronizationManager.hasResource(factory));
		
		// check that further invocations simply participate
		interceptor.preHandle(request, response, "handler");

		interceptor.preHandle(request, response, "handler");
		interceptor.postHandle(request, response, "handler", null);
		interceptor.afterCompletion(request, response, "handler", null);

		interceptor.postHandle(request, response, "handler", null);
		interceptor.afterCompletion(request, response, "handler", null);

		interceptor.preHandle(request, response, "handler");
		interceptor.postHandle(request, response, "handler", null);
		interceptor.afterCompletion(request, response, "handler", null);
		
{'fraction_non_alphanumeric': 0.08745111980092428, 'fraction_numerical': 0.0028439388553146107, 'mean_word_length': 6.672727272727273, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6819642', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2071, 'n_words': 523}