1 22 package org.jboss.ejb3.test.standalone.unit; 23 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.sql.Statement ; 27 import java.util.Hashtable ; 28 import javax.naming.InitialContext ; 29 import javax.sql.DataSource ; 30 import javax.transaction.TransactionManager ; 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 44 public class POJOEnvironmentTestCase extends TestCase 45 { 46 private static boolean booted = false; 47 48 public POJOEnvironmentTestCase(String name) 49 { 50 super(name); 51 } 52 53 protected void setUp() throws Exception 54 { 55 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 e) 70 { 71 throw e; 72 } 73 catch (Throwable t) 74 { 75 throw new RuntimeException (t); 76 } 77 } 78 79 @Override 80 protected void tearDown() throws Exception 81 { 82 super.tearDown(); 83 EJB3StandaloneBootstrap.shutdown(); 84 } 85 86 87 protected void configureLoggingAfterBootstrap() 88 { 89 } 90 91 protected InitialContext getInitialContext() throws Exception 92 { 93 return new InitialContext (getInitialContextProperties()); 94 } 95 96 protected Hashtable getInitialContextProperties() 97 { 98 return EJB3StandaloneBootstrap.getInitialContextProperties(); 99 } 100 101 102 public void testTxDataSource() throws Throwable 103 { 104 InitialContext ctx = getInitialContext(); 105 DataSource ds = (DataSource ) ctx.lookup("java:/DefaultDS"); 106 TransactionManager tm = (TransactionManager ) ctx.lookup("java:/TransactionManager"); 107 108 Connection c = ds.getConnection(); 109 try 110 { 111 Statement 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 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 s = c.createStatement(); 142 ResultSet 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 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 s = c.createStatement(); 178 ResultSet 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 [] 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 |