/* * Copyright 2006 The Apache Software Foundation. * * 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.apache.openjpa.persistence.simple; import java.util.HashMap; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.FlushModeType; import javax.persistence.LockModeType; import javax.persistence.Persistence; import junit.framework.TestCase; import junit.textui.TestRunner; import org.apache.openjpa.persistence.OpenJPAEntityManager; /** * Negative test case to verify that EntityManager throws required exceptions * after close. * * @author Craig Russell */ public class TestEntityManagerMethodsThrowAfterClose extends TestCase { private EntityManagerFactory emf; private EntityManager em; private AllFieldTypes aft = new AllFieldTypes(); public void setUp() { Map props = new HashMap(System.getProperties()); props.put("openjpa.MetaDataFactory", "jpa(Types=" + AllFieldTypes.class.getName() + ")"); emf = Persistence.createEntityManagerFactory("test", props); em = emf.createEntityManager(); em.close(); } public void tearDown() { if (emf == null) return; try { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); em.createQuery("delete from AllFieldTypes").executeUpdate(); em.getTransaction().commit(); em.close(); emf.close(); } catch (Exception e) { } } public void testPersistAfterClose() { try { em.persist(aft); fail("Expected exception for method persist after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testMergeAfterClose() { try { em.merge(aft); fail("Expected exception for method merge after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testRemoveAfterClose() { try { em.remove(aft); fail("Expected exception for method remove after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testFindAfterClose() { try { em.find(AllFieldTypes.class, Integer.valueOf(1)); fail("Expected exception for method find after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testGetReferenceAfterClose() { try { em.getReference(AllFieldTypes.class, Integer.valueOf(1)); fail("Expected exception for method getReference after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testFlushAfterClose() { try { em.flush(); fail("Expected exception for method flush after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testSetFlushModeAfterClose() { try { em.setFlushMode(FlushModeType.AUTO); fail("Expected exception for method setFlushMode after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testGetFlushModeAfterClose() { try { em.getFlushMode(); fail("Expected exception for method getFlushMode after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testLockAfterClose() { try { em.lock(aft, LockModeType.WRITE); fail("Expected exception for method lock after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testRefreshAfterClose() { try { em.refresh(aft); fail("Expected exception for method refresh after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testClearAfterClose() { try { em.clear(); fail("Expected exception for method clear after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testContainsAfterClose() { try { em.contains(aft); fail("Expected exception for method contains after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testCreateQueryAfterClose() { try { em.createQuery("SELECT Object(aft) FROM AllFieldTypes aft"); fail("Expected exception for method createQuery after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testCreateNamedQueryAfterClose() { try { em.createNamedQuery("NamedQuery"); fail("Expected exception for method persist after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testCreateNativeQueryAfterClose() { try { em.createNativeQuery("SELECT * FROM ALLFIELDTYPES"); fail("Expected exception for method createNativeQuery after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testCreateNativeQueryMappingAfterClose() { try { em.createNativeQuery("SELECT * FROM ALLFIELDTYPES", AllFieldTypes.class); fail("Expected exception for method createNativeQuery with Mapping after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testJoinTransactionAfterClose() { try { em.joinTransaction(); fail("Expected exception for method joinTransaction after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testGetDelegateAfterClose() { try { em.getDelegate(); fail("Expected exception for method getDelegate after em.close()"); } catch (IllegalStateException ex) { // expected } } public void testCloseAfterClose() { try { em.close(); fail("Expected exception for method close after em.close()"); } catch (IllegalStateException ex) { // expected } } public static void main(String[] args) { TestRunner.run(TestPersistence.class); } }