1 22 package org.jboss.ejb3.test.standalone.flushmodenever.unit; 23 24 import java.util.Hashtable ; 25 import javax.naming.InitialContext ; 26 import javax.transaction.UserTransaction ; 27 import javax.persistence.EntityManagerFactory; 28 import javax.persistence.EntityManager; 29 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; 30 import org.jboss.ejb3.test.standalone.flushmodenever.Entity1; 31 import org.jboss.ejb3.test.standalone.flushmodenever.Entity2; 32 import org.jboss.ejb3.test.standalone.flushmodenever.Session2; 33 import org.jboss.ejb3.test.standalone.flushmodenever.Stateful1; 34 import org.jboss.ejb3.test.standalone.flushmodenever.Util; 35 import org.jboss.ejb3.test.standalone.flushmodenever.Session1; 36 import junit.extensions.TestSetup; 37 import junit.framework.Test; 38 import junit.framework.TestCase; 39 import junit.framework.TestSuite; 40 41 47 48 public class FactoryUnitTestCase 49 extends TestCase 50 { 51 52 public FactoryUnitTestCase(String name) 53 { 54 55 super(name); 56 57 } 58 59 protected InitialContext getInitialContext() throws Exception 60 { 61 return new InitialContext (getInitialContextProperties()); 62 } 63 64 protected Hashtable getInitialContextProperties() 65 { 66 return EJB3StandaloneBootstrap.getInitialContextProperties(); 67 } 68 69 public void testUserTransaction() throws Exception 70 { 71 UserTransaction tx = (UserTransaction )getInitialContext().lookup("UserTransaction"); 72 EntityManagerFactory factory1 = (EntityManagerFactory)getInitialContext().lookup("java:/Manager1Factory"); 73 74 tx.begin(); 75 Entity1 one = new Entity1(); 76 one.setString("UserTransaction"); 77 EntityManager em1 = factory1.createEntityManager(); 78 em1.persist(one); 79 tx.commit(); 80 em1.close(); 81 82 EntityManager em = factory1.createEntityManager(); 83 one = em.find(Entity1.class, one.getId()); 84 assertNotNull(one); 85 em.close(); 86 87 } 88 89 public void testMe() throws Exception 90 { 91 Session1 session1 = (Session1) this.getInitialContext().lookup("Session1Bean/remote"); 92 Session2 session2 = (Session2) this.getInitialContext().lookup("Session2Bean/remote"); 93 94 int oneF = session1.create1FromFactory(); 95 int oneM = session1.create1FromManager(); 96 int twoF = session1.create2FromFactory(); 97 int twoM = session1.create2FromManager(); 98 session1.doUtil(new Util()); 99 100 session2.find1FromFactory(oneF); 101 assertNotNull(session2.find1FromManager(oneM)); 102 session2.find2FromFactory(twoF); 103 assertNotNull(session2.find2FromManager(twoM)); 104 assertNotNull(session2.findUtil1FromManager(1)); 105 assertNotNull(session2.findUtil2FromManager(2)); 106 107 } 108 109 public void testExtended() throws Exception 110 { 111 Stateful1 stateful1 = (Stateful1) this.getInitialContext().lookup("Stateful1Bean/remote"); 112 Session2 session2 = (Session2) this.getInitialContext().lookup("Session2Bean/remote"); 113 114 int oneId = stateful1.create1(); 115 int twoId = stateful1.create2(); 116 117 stateful1.update1(); 118 stateful1.update2(); 119 120 { 121 Entity1 one = session2.find1FromManager(oneId); 122 assertEquals(one.getString(), "changed"); 123 124 Entity2 two = session2.find2FromManager(twoId); 125 assertEquals(two.getString(), "changed"); 126 } 127 128 stateful1.never(); 129 130 { 131 Entity1 one = session2.find1FromManager(oneId); 132 assertEquals(one.getString(), "changed"); 133 134 Entity2 two = session2.find2FromManager(twoId); 135 assertEquals(two.getString(), "changed"); 136 } 137 138 stateful1.checkout(); 139 140 { 141 Entity1 one = session2.find1FromManager(oneId); 142 assertEquals(one.getString(), "never"); 143 144 Entity2 two = session2.find2FromManager(twoId); 145 assertEquals(two.getString(), "never"); 146 } 147 } 148 149 public void testExtended2() throws Exception 150 { 151 Stateful1 stateful1 = (Stateful1) this.getInitialContext().lookup("Stateful1Bean/remote"); 152 Session2 session2 = (Session2) this.getInitialContext().lookup("Session2Bean/remote"); 153 154 int oneId = stateful1.create1(); 155 int twoId = stateful1.create2(); 156 157 stateful1.update1(); 158 stateful1.update2(); 159 160 Entity1 one = session2.find1FromManager(oneId); 161 assertEquals(one.getString(), "changed"); 162 163 Entity2 two = session2.find2FromManager(twoId); 164 assertEquals(two.getString(), "changed"); 165 one.setString("never2"); 166 two.setString("never2"); 167 stateful1.never2(one, two); 168 169 170 171 { 172 Entity1 uno = session2.find1FromManager(oneId); 173 assertEquals(uno.getString(), "changed"); 174 175 Entity2 dos = session2.find2FromManager(twoId); 176 assertEquals(dos.getString(), "changed"); 177 } 178 179 stateful1.checkout(); 180 181 { 182 Entity1 uno = session2.find1FromManager(oneId); 183 assertEquals(uno.getString(), "never2"); 184 185 Entity2 dos = session2.find2FromManager(twoId); 186 assertEquals(dos.getString(), "never2"); 187 } 188 } 189 190 public static Test suite() throws Exception 191 { 192 TestSuite suite = new TestSuite(); 193 suite.addTestSuite(FactoryUnitTestCase.class); 194 195 196 TestSetup wrapper = new TestSetup(suite) 198 { 199 protected void setUp() 200 { 201 startupEmbeddedJboss(); 202 } 203 204 protected void tearDown() 205 { 206 shutdownEmbeddedJboss(); 207 } 208 }; 209 210 return wrapper; 211 } 212 213 public static void startupEmbeddedJboss() 214 { 215 EJB3StandaloneBootstrap.boot(null); 216 EJB3StandaloneBootstrap.scanClasspath("flushmodenever-session1.jar, flushmodenever-session2.jar"); 217 } 218 219 public static void shutdownEmbeddedJboss() 220 { 221 EJB3StandaloneBootstrap.shutdown(); 222 } 223 224 } 225 | Popular Tags |