KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > standalone > unit > POJOEnvironmentTestCase


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.unit;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.sql.DataSource JavaDoc;
30 import javax.transaction.TransactionManager JavaDoc;
31 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34 import junit.framework.TestCase;
35 import junit.textui.TestRunner;
36
37 /**
38  * POJO Environment tests
39  *
40  * @TODO XMLKernelDeployer -> BeanXMLDeployer
41  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 40696 $
43  */

44 public class POJOEnvironmentTestCase extends TestCase
45 {
46    private static boolean booted = false;
47
48    public POJOEnvironmentTestCase(String JavaDoc name)
49    {
50       super(name);
51    }
52
53    protected void setUp() throws Exception JavaDoc
54    {
55       // set bad properties to make sure that we're injecting InitialContext correct
56
// System.setProperty("java.naming.factory.initial", "ERROR");
57
// System.setProperty("java.naming.factory.url.pkgs", "ERROR");
58

59       super.setUp();
60       long start = System.currentTimeMillis();
61       try
62       {
63          if (!booted)
64          {
65             booted = true;
66             EJB3StandaloneBootstrap.boot("");
67          }
68       }
69       catch (Exception JavaDoc e)
70       {
71          throw e;
72       }
73       catch (Throwable JavaDoc t)
74       {
75          throw new RuntimeException JavaDoc(t);
76       }
77    }
78
79    @Override JavaDoc
80    protected void tearDown() throws Exception JavaDoc
81    {
82       super.tearDown();
83       EJB3StandaloneBootstrap.shutdown();
84    }
85
86
87    protected void configureLoggingAfterBootstrap()
88    {
89    }
90
91    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
92    {
93       return new InitialContext JavaDoc(getInitialContextProperties());
94    }
95
96    protected Hashtable JavaDoc getInitialContextProperties()
97    {
98       return EJB3StandaloneBootstrap.getInitialContextProperties();
99    }
100
101
102    public void testTxDataSource() throws Throwable JavaDoc
103    {
104       InitialContext JavaDoc ctx = getInitialContext();
105       DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("java:/DefaultDS");
106       TransactionManager JavaDoc tm = (TransactionManager JavaDoc) ctx.lookup("java:/TransactionManager");
107
108       Connection JavaDoc c = ds.getConnection();
109       try
110       {
111          Statement JavaDoc s = c.createStatement();
112          s.execute("create table test (key integer, value char(50))");
113       }
114       finally
115       {
116          c.close();
117       }
118
119       tm.begin();
120       try
121       {
122          c = ds.getConnection();
123          try
124          {
125             Statement JavaDoc s = c.createStatement();
126             s.execute("insert into test (key, value) values(1, 'Hello')");
127          }
128          finally
129          {
130             c.close();
131          }
132       }
133       finally
134       {
135          tm.rollback();
136       }
137
138       c = ds.getConnection();
139       try
140       {
141          Statement JavaDoc s = c.createStatement();
142          ResultSet JavaDoc r = s.executeQuery("select count(*) from test");
143          if (r.next())
144          {
145             assertEquals(0, r.getInt(1));
146          }
147          else
148             fail("Should not be here");
149       }
150       finally
151       {
152          c.close();
153       }
154
155       tm.begin();
156       try
157       {
158          c = ds.getConnection();
159          try
160          {
161             Statement JavaDoc s = c.createStatement();
162             s.execute("insert into test (key, value) values(1, 'Goodbye')");
163          }
164          finally
165          {
166             c.close();
167          }
168       }
169       finally
170       {
171          tm.commit();
172       }
173
174       c = ds.getConnection();
175       try
176       {
177          Statement JavaDoc s = c.createStatement();
178          ResultSet JavaDoc r = s.executeQuery("select value from test where key=1");
179          if (r.next())
180          {
181             assertEquals("Goodbye", r.getString(1));
182          }
183          else
184             fail("Should not be here");
185       }
186       finally
187       {
188          c.close();
189       }
190    }
191
192    public static void main(String JavaDoc[] args)
193    {
194       TestRunner.run(suite());
195    }
196
197    public static Test suite()
198    {
199       TestSuite suite = new TestSuite("POJOEnvironment");
200       suite.addTestSuite(POJOEnvironmentTestCase.class);
201       return suite;
202    }
203
204
205 }
Popular Tags