Now using connections managed by JPA instead of using JNDI mocking.
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / JpaTester.java
index 52e284ee7e8ee073b389c892c87c9e9f282bba65..19f7f187d5838f5ef1272db9d67d8934d3a1bb48 100644 (file)
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- */ 
+ */
 package org.wamblee.support.persistence;
 
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import javax.sql.DataSource;
 
 import org.dbunit.IDatabaseTester;
+import org.wamblee.support.jndi.StubInitialContextFactory;
 
 /**
  * This class is the entry point for JPA tests. Test code should construct a
@@ -80,12 +83,23 @@ public class JpaTester {
     public void start() throws Exception {
         db = DatabaseBuilder.getDatabase();
         dataSource = db.start();
+        
+        // NOTE: adding datasource to JNDI is no longer needed for 
+        //       JPA testing, but is nice to have available for other uses.
+        StubInitialContextFactory.register();
+        try {
+            InitialContext ctx = new InitialContext();
+            ctx.bind(persistenceUnit.getJndiName(), dataSource);
+        } catch (NamingException e) {
+            throw new RuntimeException("JNDI problem", e);
+        }
 
         dbUtils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
+        dbUtils.start();
         dbUtils.dropTables();
         dbUtils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
 
-        jpaBuilder = new JpaBuilder(dataSource, persistenceUnit);
+        jpaBuilder = new JpaBuilder(db.getJdbcUrl(), db.getUsername(), db.getPassword(), persistenceUnit);
         jpaBuilder.start();
 
         // db tester should be created after Jpa builder because jpa builder
@@ -101,6 +115,9 @@ public class JpaTester {
         if (jpaBuilder != null) {
             jpaBuilder.stop();
         }
+        if (dbUtils != null) {
+            dbUtils.stop();
+        }
         if (db != null) {
             db.stop();
         }