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.test.hibernate.test;
023:
024: import org.jboss.test.JBossTestCase;
025: import org.jboss.test.hibernate.model.User;
026: import org.jboss.test.hibernate.model.Name;
027: import org.jboss.test.hibernate.ejb.interfaces.ProfileService;
028: import org.jboss.test.hibernate.ejb.interfaces.ProfileServiceHome;
029: import org.jboss.test.hibernate.ejb.interfaces.ProfileServiceUtil;
030: import org.jboss.test.hibernate.ejb.interfaces.AggregateProfileServiceHome;
031: import org.jboss.test.hibernate.ejb.interfaces.AggregateProfileService;
032: import org.jboss.test.hibernate.ejb.interfaces.AggregateProfileServiceUtil;
033:
034: import java.util.GregorianCalendar;
035: import java.util.List;
036: import java.util.Iterator;
037:
038: import junit.framework.Test;
039:
040: /**
041: * Implementation of HibernateIntgUnitTestCase.
042: *
043: * @author Steve Ebersole
044: */
045: public class HibernateIntgUnitTestCase extends JBossTestCase {
046: public HibernateIntgUnitTestCase(String name) throws Exception {
047: super (name);
048: }
049:
050: /** Setup the test suite.
051: */
052: public static Test suite() throws Exception {
053: return getDeploySetup(HibernateIntgUnitTestCase.class,
054: "hib-test.ear");
055: }
056:
057: public void testRedeployment() throws Throwable {
058: Throwable initialThrowable = null;
059:
060: // Do some work
061: try {
062: ProfileServiceHome home = ProfileServiceUtil.getHome();
063: ProfileService service = null;
064: try {
065: service = home.create();
066:
067: User user = new User();
068: user.setEmail("nobody@nowhere.com");
069: user.setName(new Name());
070: user.getName().setFirstName("John");
071: user.getName().setInitial(new Character('Q'));
072: user.getName().setLastName("Public");
073: user.setPassword("password");
074: user.setTimeOfCreation(new GregorianCalendar());
075: user.setHandle("redeploy-1-handle");
076:
077: Long savedUserId = service.storeUser(user).getId();
078: getLog().info("User created with id = " + savedUserId);
079:
080: // make *sure* it gets loaded into cache. This is to check
081: // that JBossCache as 2nd-level cache is properly releasing
082: // resources on SF shutdown; I have manually verified this is
083: // the case w/o JBossCache as the 2nd-level cache (i.e. this
084: // test case passes w/o JBossCache in the mix).
085: List users = service.listUsers();
086: assertNotNull(users);
087: assertEquals("Incorrect result size", 1, users.size());
088: } finally {
089: if (service != null) {
090: try {
091: service.remove();
092: } catch (Throwable t) {
093: }
094: }
095: }
096: } catch (Throwable t) {
097: // ignore; does not really matter if this stuff fails/succeeds
098: // simply store the original failure so that we can use it later
099: initialThrowable = t;
100: }
101:
102: // force a redeploy
103: delegate.redeploy("hib-test.ear");
104:
105: // then, do some more work...
106: ProfileServiceHome home = ProfileServiceUtil.getHome();
107: ProfileService service = null;
108: try {
109: service = home.create();
110:
111: User user = new User();
112: user.setEmail("nobody@nowhere.com");
113: user.setName(new Name());
114: user.getName().setFirstName("John");
115: user.getName().setInitial(new Character('Q'));
116: user.getName().setLastName("Public");
117: user.setPassword("password");
118: user.setTimeOfCreation(new GregorianCalendar());
119: user.setHandle("redeploy-1-handle");
120:
121: Long savedUserId = service.storeUser(user).getId();
122: getLog().info("User created with id = " + savedUserId);
123:
124: List users = service.listUsers();
125: assertNotNull(users);
126: assertEquals("Incorrect result size", 1, users.size());
127: } catch (Throwable t) {
128: // it is possible for the initial code block (b4 the redeploy) and this
129: // (after redeploy) to fail for the same reason, which would not indicate
130: // a redeployment issue per-se; but how to detect that?
131: if (initialThrowable == null) {
132: fail("Getting new exceptions after redeploy [" + t
133: + "]");
134: }
135:
136: if (!t.getClass().getName().equals(
137: initialThrowable.getClass().getName())) {
138: fail("After redploy failing for different cause [" + t
139: + "]");
140: }
141: } finally {
142: if (service != null) {
143: try {
144: service.remove();
145: } catch (Throwable t) {
146: }
147: }
148: }
149:
150: }
151:
152: public void testCurrentSession() throws Throwable {
153:
154: ProfileServiceHome home = ProfileServiceUtil.getHome();
155: ProfileService service = null;
156:
157: try {
158: service = home.create();
159:
160: int initialCount = service.listUsers().size();
161:
162: User user = new User();
163: user.setEmail("nobody@nowhere.com");
164: user.setName(new Name());
165: user.getName().setFirstName("John");
166: user.getName().setInitial(new Character('Q'));
167: user.getName().setLastName("Public");
168: user.setPassword("password");
169: user.setTimeOfCreation(new GregorianCalendar());
170: user.setHandle("current-handle");
171:
172: Long savedUserId = service.storeUser(user).getId();
173: getLog().info("User created with id = " + savedUserId);
174:
175: user = service.loadUser(savedUserId);
176:
177: List users = service.listUsers();
178: assertNotNull(users);
179: assertEquals("Incorrect result size", initialCount + 1,
180: users.size());
181:
182: boolean found = false;
183: Iterator itr = users.iterator();
184: while (itr.hasNext()) {
185: user = (User) itr.next();
186: if (savedUserId.equals(user.getId())) {
187: found = true;
188: }
189: }
190: assertTrue("Saved used not found in list", found);
191:
192: user = service.loadUser(savedUserId);
193: assertNotNull(user);
194: } finally {
195: if (service != null) {
196: try {
197: service.remove();
198: } catch (Throwable t) {
199: // ignore
200: }
201: }
202: }
203: }
204:
205: public void testNestedEjbCalls() throws Throwable {
206: AggregateProfileServiceHome home = AggregateProfileServiceUtil
207: .getHome();
208: AggregateProfileService service = null;
209:
210: try {
211: service = home.create();
212:
213: int initialCount = service.listUsers().size();
214:
215: User user = new User();
216: user.setEmail("nobody@nowhere.com");
217: user.setName(new Name());
218: user.getName().setFirstName("John");
219: user.getName().setInitial(new Character('Q'));
220: user.getName().setLastName("Public");
221: user.setPassword("password");
222: user.setTimeOfCreation(new GregorianCalendar());
223: user.setHandle("nested-handle");
224:
225: Long savedUserId = service.storeUser(user).getId();
226: getLog().info("User created with id = " + savedUserId);
227:
228: List users = service.listUsers();
229: assertNotNull(users);
230: assertEquals("Incorrect result size", initialCount + 1,
231: users.size());
232:
233: boolean found = false;
234: Iterator itr = users.iterator();
235: while (itr.hasNext()) {
236: user = (User) itr.next();
237: if (savedUserId.equals(user.getId())) {
238: found = true;
239: }
240: }
241: assertTrue("Saved used not found in list", found);
242:
243: user = service.loadUser(savedUserId);
244: assertNotNull(user);
245: } finally {
246: if (service != null) {
247: try {
248: service.remove();
249: } catch (Throwable t) {
250: // ignore
251: }
252: }
253: }
254: }
255:
256: }
|