001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.test.standalone.flushmodenever.unit;
023:
024: import java.util.Hashtable;
025: import javax.naming.InitialContext;
026: import javax.transaction.UserTransaction;
027: import javax.persistence.EntityManagerFactory;
028: import javax.persistence.EntityManager;
029: import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
030: import org.jboss.ejb3.test.standalone.flushmodenever.Entity1;
031: import org.jboss.ejb3.test.standalone.flushmodenever.Entity2;
032: import org.jboss.ejb3.test.standalone.flushmodenever.Session2;
033: import org.jboss.ejb3.test.standalone.flushmodenever.Stateful1;
034: import org.jboss.ejb3.test.standalone.flushmodenever.Util;
035: import org.jboss.ejb3.test.standalone.flushmodenever.Session1;
036: import junit.extensions.TestSetup;
037: import junit.framework.Test;
038: import junit.framework.TestCase;
039: import junit.framework.TestSuite;
040:
041: /**
042: * Sample client for the jboss container.
043: *
044: * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
045: * @version $Id: FactoryUnitTestCase.java 60233 2007-02-03 10:13:23Z wolfc $
046: */
047:
048: public class FactoryUnitTestCase extends TestCase {
049:
050: public FactoryUnitTestCase(String name) {
051:
052: super (name);
053:
054: }
055:
056: protected InitialContext getInitialContext() throws Exception {
057: return new InitialContext(getInitialContextProperties());
058: }
059:
060: protected Hashtable getInitialContextProperties() {
061: return EJB3StandaloneBootstrap.getInitialContextProperties();
062: }
063:
064: public void testUserTransaction() throws Exception {
065: UserTransaction tx = (UserTransaction) getInitialContext()
066: .lookup("UserTransaction");
067: EntityManagerFactory factory1 = (EntityManagerFactory) getInitialContext()
068: .lookup("java:/Manager1Factory");
069:
070: tx.begin();
071: Entity1 one = new Entity1();
072: one.setString("UserTransaction");
073: EntityManager em1 = factory1.createEntityManager();
074: em1.persist(one);
075: tx.commit();
076: em1.close();
077:
078: EntityManager em = factory1.createEntityManager();
079: one = em.find(Entity1.class, one.getId());
080: assertNotNull(one);
081: em.close();
082:
083: }
084:
085: public void testMe() throws Exception {
086: Session1 session1 = (Session1) this .getInitialContext().lookup(
087: "Session1Bean/remote");
088: Session2 session2 = (Session2) this .getInitialContext().lookup(
089: "Session2Bean/remote");
090:
091: int oneF = session1.create1FromFactory();
092: int oneM = session1.create1FromManager();
093: int twoF = session1.create2FromFactory();
094: int twoM = session1.create2FromManager();
095: session1.doUtil(new Util());
096:
097: session2.find1FromFactory(oneF);
098: assertNotNull(session2.find1FromManager(oneM));
099: session2.find2FromFactory(twoF);
100: assertNotNull(session2.find2FromManager(twoM));
101: assertNotNull(session2.findUtil1FromManager(1));
102: assertNotNull(session2.findUtil2FromManager(2));
103:
104: }
105:
106: public void testExtended() throws Exception {
107: Stateful1 stateful1 = (Stateful1) this .getInitialContext()
108: .lookup("Stateful1Bean/remote");
109: Session2 session2 = (Session2) this .getInitialContext().lookup(
110: "Session2Bean/remote");
111:
112: int oneId = stateful1.create1();
113: int twoId = stateful1.create2();
114:
115: stateful1.update1();
116: stateful1.update2();
117:
118: {
119: Entity1 one = session2.find1FromManager(oneId);
120: assertEquals(one.getString(), "changed");
121:
122: Entity2 two = session2.find2FromManager(twoId);
123: assertEquals(two.getString(), "changed");
124: }
125:
126: stateful1.never();
127:
128: {
129: Entity1 one = session2.find1FromManager(oneId);
130: assertEquals(one.getString(), "changed");
131:
132: Entity2 two = session2.find2FromManager(twoId);
133: assertEquals(two.getString(), "changed");
134: }
135:
136: stateful1.checkout();
137:
138: {
139: Entity1 one = session2.find1FromManager(oneId);
140: assertEquals(one.getString(), "never");
141:
142: Entity2 two = session2.find2FromManager(twoId);
143: assertEquals(two.getString(), "never");
144: }
145: }
146:
147: public void testExtended2() throws Exception {
148: Stateful1 stateful1 = (Stateful1) this .getInitialContext()
149: .lookup("Stateful1Bean/remote");
150: Session2 session2 = (Session2) this .getInitialContext().lookup(
151: "Session2Bean/remote");
152:
153: int oneId = stateful1.create1();
154: int twoId = stateful1.create2();
155:
156: stateful1.update1();
157: stateful1.update2();
158:
159: Entity1 one = session2.find1FromManager(oneId);
160: assertEquals(one.getString(), "changed");
161:
162: Entity2 two = session2.find2FromManager(twoId);
163: assertEquals(two.getString(), "changed");
164: one.setString("never2");
165: two.setString("never2");
166: stateful1.never2(one, two);
167:
168: {
169: Entity1 uno = session2.find1FromManager(oneId);
170: assertEquals(uno.getString(), "changed");
171:
172: Entity2 dos = session2.find2FromManager(twoId);
173: assertEquals(dos.getString(), "changed");
174: }
175:
176: stateful1.checkout();
177:
178: {
179: Entity1 uno = session2.find1FromManager(oneId);
180: assertEquals(uno.getString(), "never2");
181:
182: Entity2 dos = session2.find2FromManager(twoId);
183: assertEquals(dos.getString(), "never2");
184: }
185: }
186:
187: public static Test suite() throws Exception {
188: TestSuite suite = new TestSuite();
189: suite.addTestSuite(FactoryUnitTestCase.class);
190:
191: // setup test so that embedded JBoss is started/stopped once for all tests here.
192: TestSetup wrapper = new TestSetup(suite) {
193: protected void setUp() {
194: startupEmbeddedJboss();
195: }
196:
197: protected void tearDown() {
198: shutdownEmbeddedJboss();
199: }
200: };
201:
202: return wrapper;
203: }
204:
205: public static void startupEmbeddedJboss() {
206: EJB3StandaloneBootstrap.boot(null);
207: EJB3StandaloneBootstrap
208: .scanClasspath("flushmodenever-session1.jar, flushmodenever-session2.jar");
209: }
210:
211: public static void shutdownEmbeddedJboss() {
212: EJB3StandaloneBootstrap.shutdown();
213: }
214:
215: }
|