KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > hibernate > test > HibernateIntgUnitTestCase


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 /*
23   * JBoss, Home of Professional Open Source
24   * Copyright 2005, JBoss Inc., and individual contributors as indicated
25   * by the @authors tag. See the copyright.txt in the distribution for a
26   * full listing of individual contributors.
27   *
28   * This is free software; you can redistribute it and/or modify it
29   * under the terms of the GNU Lesser General Public License as
30   * published by the Free Software Foundation; either version 2.1 of
31   * the License, or (at your option) any later version.
32   *
33   * This software is distributed in the hope that it will be useful,
34   * but WITHOUT ANY WARRANTY; without even the implied warranty of
35   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36   * Lesser General Public License for more details.
37   *
38   * You should have received a copy of the GNU Lesser General Public
39   * License along with this software; if not, write to the Free
40   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
41   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
42   */

43 package org.jboss.test.hibernate.test;
44
45 import org.jboss.test.JBossTestCase;
46 import org.jboss.test.hibernate.model.User;
47 import org.jboss.test.hibernate.model.Name;
48 import org.jboss.test.hibernate.ejb.interfaces.ProfileService;
49 import org.jboss.test.hibernate.ejb.interfaces.ProfileServiceHome;
50 import org.jboss.test.hibernate.ejb.interfaces.ProfileServiceUtil;
51
52 import java.util.GregorianCalendar JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Iterator JavaDoc;
55
56 import junit.framework.Test;
57
58 /**
59  * Implementation of HibernateIntgUnitTestCase.
60  *
61  * @author Steve Ebersole
62  */

63 public class HibernateIntgUnitTestCase extends JBossTestCase
64 {
65    public HibernateIntgUnitTestCase(String JavaDoc name) throws Exception JavaDoc
66    {
67       super(name);
68    }
69
70    /** Setup the test suite.
71     */

72    public static Test suite() throws Exception JavaDoc
73    {
74       return getDeploySetup(HibernateIntgUnitTestCase.class, "hib-test.ear");
75    }
76
77    public void testRedeployment() throws Throwable JavaDoc
78    {
79       Throwable JavaDoc initialThrowable = null;
80
81       // Do some work
82
try
83       {
84          ProfileServiceHome home = ProfileServiceUtil.getHome();
85          ProfileService service = null;
86          try
87          {
88             service = home.create();
89
90             User user = new User();
91             user.setEmail("nobody@nowhere.com");
92             user.setName( new Name() );
93             user.getName().setFirstName("John");
94             user.getName().setInitial( new Character JavaDoc('Q') );
95             user.getName().setLastName("Public");
96             user.setPassword("password");
97             user.setTimeOfCreation( new GregorianCalendar JavaDoc() );
98             user.setHandle("myHandle");
99
100             Long JavaDoc savedUserId = service.storeUser( user ).getId();
101             getLog().info("User created with id = " + savedUserId );
102
103             // make *sure* it gets loaded into cache. This is to check
104
// that JBossCache as 2nd-level cache is properly releasing
105
// resources on SF shutdown; I have manually verified this is
106
// the case w/o JBossCache as the 2nd-level cache (i.e. this
107
// test case passes w/o JBossCache in the mix).
108
List JavaDoc users = service.listUsers();
109             assertNotNull( users );
110             assertEquals( "Incorrect result size", 1, users.size() );
111          }
112          finally
113          {
114             if ( service != null )
115             {
116                try
117                {
118                   service.remove();
119                }
120                catch( Throwable JavaDoc t )
121                {
122                }
123             }
124          }
125       }
126       catch( Throwable JavaDoc t )
127       {
128          // ignore; does not really matter if this stuff fails/succeeds
129
// simply store the original failure so that we can use it later
130
initialThrowable = t;
131       }
132
133       // force a redeploy
134
delegate.redeploy( "hib-test.ear" );
135
136       // then, do some more work...
137
ProfileServiceHome home = ProfileServiceUtil.getHome();
138       ProfileService service = null;
139       try
140       {
141          service = home.create();
142
143          User user = new User();
144          user.setEmail("nobody@nowhere.com");
145          user.setName( new Name() );
146          user.getName().setFirstName("John");
147          user.getName().setInitial( new Character JavaDoc('Q') );
148          user.getName().setLastName("Public");
149          user.setPassword("password");
150          user.setTimeOfCreation( new GregorianCalendar JavaDoc() );
151          user.setHandle("myHandle");
152
153          Long JavaDoc savedUserId = service.storeUser( user ).getId();
154          getLog().info("User created with id = " + savedUserId );
155
156          List JavaDoc users = service.listUsers();
157          assertNotNull( users );
158          assertEquals( "Incorrect result size", 1, users.size() );
159       }
160       catch( Throwable JavaDoc t )
161       {
162          // it is possible for the initial code block (b4 the redeploy) and this
163
// (after redeploy) to fail for the same reason, which would not indicate
164
// a redeployment issue per-se; but how to detect that?
165
if ( initialThrowable == null )
166          {
167             fail( "Getting new exceptions after redeploy [" + t + "]" );
168          }
169
170          if ( !t.getClass().getName().equals( initialThrowable.getClass().getName() ) )
171          {
172             fail( "After redploy failing for different cause [" + t + "]" );
173          }
174       }
175       finally
176       {
177          if ( service != null )
178          {
179             try
180             {
181                service.remove();
182             }
183             catch( Throwable JavaDoc t )
184             {
185             }
186          }
187       }
188
189    }
190
191    public void testCurrentSession() throws Throwable JavaDoc {
192
193       ProfileServiceHome home = ProfileServiceUtil.getHome();
194       ProfileService service = null;
195
196       try
197       {
198          service = home.create();
199
200          User user = new User();
201          user.setEmail("nobody@nowhere.com");
202          user.setName( new Name() );
203          user.getName().setFirstName("John");
204          user.getName().setInitial( new Character JavaDoc('Q') );
205          user.getName().setLastName("Public");
206          user.setPassword("password");
207          user.setTimeOfCreation( new GregorianCalendar JavaDoc() );
208          user.setHandle("myHandle");
209
210          Long JavaDoc savedUserId = service.storeUser( user ).getId();
211          getLog().info("User created with id = " + savedUserId );
212
213          List JavaDoc users = service.listUsers();
214          assertNotNull( users );
215          assertEquals( "Incorrect result size", 1, users.size() );
216
217          Long JavaDoc userId = ( ( User ) users.get( 0 ) ).getId();
218          assertEquals( "Saved used not returned", savedUserId, userId );
219
220          user = service.loadUser( savedUserId );
221          assertNotNull( user );
222       }
223       finally
224       {
225          if ( service != null )
226          {
227             try
228             {
229                service.remove();
230             }
231             catch( Throwable JavaDoc t )
232             {
233                // ignore
234
}
235          }
236       }
237    }
238
239 }
240
Popular Tags