KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bmp > test > BmpUnitTestCase


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.test.bmp.test;
23
24 import java.util.Iterator JavaDoc;
25
26 import javax.naming.InitialContext JavaDoc;
27
28 import junit.framework.Test;
29
30 import org.jboss.test.JBossTestCase;
31 import org.jboss.test.bmp.interfaces.BMPHelperSession;
32 import org.jboss.test.bmp.interfaces.BMPHelperSessionHome;
33 import org.jboss.test.bmp.interfaces.SimpleBMP;
34 import org.jboss.test.bmp.interfaces.SimpleBMPHome;
35
36 public class BmpUnitTestCase
37     extends JBossTestCase
38 {
39     public BmpUnitTestCase(String JavaDoc name)
40     {
41         super(name);
42     }
43
44     public void testBMP() throws Exception JavaDoc
45    {
46       BMPHelperSessionHome sessionHome = (BMPHelperSessionHome)new InitialContext JavaDoc ().lookup ("bmp.BMPHelperSession");
47       BMPHelperSession session = sessionHome.create ();
48       
49       getLog().debug ("looking up table:");
50       boolean exists = session.existsSimpleBeanTable ();
51       if (exists)
52       {
53          getLog().debug ("table exists.");
54          getLog().debug ("delete it...");
55          session.dropSimpleBeanTable();
56          getLog().debug ("done.");
57       }
58       
59       getLog().debug ("table does not exist.");
60       getLog().debug ("create it...");
61       session.createSimpleBeanTable();
62       try
63       {
64          getLog().debug ("done.");
65          
66          getLog().debug ("start playing with bmp beans.");
67          SimpleBMPHome home = (SimpleBMPHome)new InitialContext JavaDoc ().lookup ("bmp.SimpleBMP");
68          getLog().debug ("create bean1: 1, Daniel");
69          SimpleBMP b1 = home.create (1, "Daniel");
70          getLog().debug ("getName (): "+b1.getName ());
71
72          getLog().debug ("create bean2: 2, Robert");
73          b1 = home.createMETHOD (2, "Robert");
74          getLog().debug ("getName (): "+b1.getName ());
75
76          try
77          {
78             getLog().debug ("trying to create one with same primkey: 1, Patrick");
79             b1 = home.create (1, "Patrick");
80          }
81          catch (Exception JavaDoc _e)
82          {
83             getLog().debug (_e.toString ());
84          }
85          
86          getLog().debug ("create some more dummys:");
87          for (int i = 0; i < 50; ++i)
88             home.create (i + 3, ("Dummy "+i));
89
90          try
91          {
92             getLog().debug ("trying to find Robert again");
93             b1 = home.findByPrimaryKey (new Integer JavaDoc (2));
94             getLog().debug ("getName (): "+b1.getName ());
95          }
96          catch (Exception JavaDoc _e)
97          {
98             getLog().debug (_e.toString ());
99          }
100
101          try
102          {
103             getLog().debug ("trying to find an not existing bean");
104             b1 = home.findByPrimaryKey (new Integer JavaDoc (0));
105             getLog().debug ("getName (): "+b1.getName ());
106          }
107          catch (Exception JavaDoc _e)
108          {
109             getLog().debug (_e.toString ());
110          }
111          
112          
113          getLog().debug ("rename Daniel to Maria: 1, Daniel");
114          b1 = home.findByPrimaryKey (new Integer JavaDoc (1));
115          getLog().debug ("name old: " + b1.getName ());
116          b1.setName ("Maria");
117          getLog().debug ("name new: " + b1.getName ());
118         
119          
120          getLog().debug ("find all beans:");
121          Iterator JavaDoc it = home.findAll ().iterator ();
122          while (it.hasNext ())
123          {
124             getLog().debug ("found:"+((SimpleBMP)it.next ()).getName ());
125          }
126
127
128          getLog().debug ("*******Now trying from within the Session bean (to be able to rollback):");
129          getLog().debug (session.doTest ());
130          getLog().debug ("Getting the name after a rollback:");
131          getLog().debug (session.doTestAfterRollback ());
132
133          getLog().debug ("removing all beans:");
134          it = home.findAll ().iterator ();
135          while (it.hasNext ())
136             ((SimpleBMP)it.next ()).remove ();
137       }
138       finally
139       {
140          getLog().debug ("table exists.");
141          getLog().debug ("delete it...");
142          session.dropSimpleBeanTable();
143          getLog().debug ("done.");
144       }
145    }
146
147    public static Test suite() throws Exception JavaDoc
148    {
149       return getDeploySetup(BmpUnitTestCase.class, "bmp.jar");
150    }
151 }
152
Popular Tags