KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > standalone > flushmodenever > unit > FactoryUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.standalone.flushmodenever.unit;
23
24 import java.util.Hashtable JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.transaction.UserTransaction JavaDoc;
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 /**
42  * Sample client for the jboss container.
43  *
44  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
45  * @version $Id: FactoryUnitTestCase.java 42263 2006-03-15 03:26:18Z bill $
46  */

47
48 public class FactoryUnitTestCase
49         extends TestCase
50 {
51
52    public FactoryUnitTestCase(String JavaDoc name)
53    {
54
55       super(name);
56
57    }
58
59    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
60    {
61       return new InitialContext JavaDoc(getInitialContextProperties());
62    }
63
64    protected Hashtable JavaDoc getInitialContextProperties()
65    {
66       return EJB3StandaloneBootstrap.getInitialContextProperties();
67    }
68
69    public void testUserTransaction() throws Exception JavaDoc
70    {
71       UserTransaction JavaDoc tx = (UserTransaction JavaDoc)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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
191    {
192       TestSuite suite = new TestSuite();
193       suite.addTestSuite(FactoryUnitTestCase.class);
194
195
196       // setup test so that embedded JBoss is started/stopped once for all tests here.
197
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