7 Replies Latest reply on Nov 16, 2007 3:53 PM by Tatyana Polnyi

    Problem testing seam using EntityManager

    Eric Ray Novice

      I've have test like so...

      package com.vicor.dcap.integration.test;
      
      import javax.persistence.EntityManagerFactory;
      import javax.persistence.Persistence;
      
      import org.junit.AfterClass;
      import org.junit.Assert;
      import org.junit.BeforeClass;
      import org.junit.Test;
      
      
      public class PackageActionTest {
      
       private static EntityManagerFactory emf;
      
       @BeforeClass
       public static void init() {
       try {
       emf = Persistence.createEntityManagerFactory("DC");
       }
       catch(Exception e) {
       e.printStackTrace(System.err);
       Assert.fail(e.getMessage());
       }
       }
      
       @AfterClass
       public static void destroy()
       {
       try {
       //emf.close();
       }
       catch(Exception e) {
       e.printStackTrace(System.err);
       Assert.fail(e.getMessage());
       }
       }
      
       @Test
       public void loadPackages() {
       Assert.fail("not implemented yet...");
       }
      }
      


      when I run it I get the following exception stack

      FATAL 28-07 12:40:35,250 (Log4JLogger.java:fatal:124) -default.persistence.propertiesO-:-Ofalse
      FATAL 28-07 12:40:35,250 (Log4JLogger.java:fatal:124) -ejb3-interceptors-aop.xmlO-:-Ofalse
      FATAL 28-07 12:40:35,250 (Log4JLogger.java:fatal:124) -embedded-jboss-beans.xmlO-:-Ofalse
      FATAL 28-07 12:40:35,250 (Log4JLogger.java:fatal:124) -jboss-jms-beans.xmlO-:-Ofalse
      FATAL 28-07 12:40:35,250 (Log4JLogger.java:fatal:124) -jndi.propertiesO-:-Ofalse
      FATAL 28-07 12:40:35,265 (Log4JLogger.java:fatal:124) -log4j.xmlO-:-Ofalse
      FATAL 28-07 12:40:35,265 (Log4JLogger.java:fatal:124) -META-INF/ejb-jar.xmlO-:-Ofalse
      FATAL 28-07 12:40:35,265 (Log4JLogger.java:fatal:124) -META-INF/jboss-app.xmlO-:-Ofalse
      FATAL 28-07 12:40:35,265 (Log4JLogger.java:fatal:124) -META-INF/MANIFEST.MFO-:-Ofalse
      FATAL 28-07 12:40:35,265 (Log4JLogger.java:fatal:124) -META-INF/persistence.xmlO-:-Ofalse
      ERROR 28-07 12:40:35,343 (Log4JLogger.java:error:119) -Could not obtain initial context
      javax.naming.NamingException: Local server is not initialized
       at org.jnp.interfaces.LocalOnlyContextFactory.getInitialContext(LocalOnlyContextFactory.java:45)
       at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
       at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
       at javax.naming.InitialContext.init(InitialContext.java:223)
       at javax.naming.InitialContext.<init>(InitialContext.java:175)
       at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:28)
       at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
       at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
       at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
       at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:61)
       at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
       at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:151)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:205)
       at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
       at com.vicor.dcap.integration.test.PackageActionTest.init(PackageActionTest.java:19)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.junit.internal.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
       at org.junit.internal.runners.BeforeAndAfterRunner.runBefores(BeforeAndAfterRunner.java:50)
       at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:33)
       at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
       at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
       at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
      javax.persistence.PersistenceException: org.hibernate.HibernateException: Could not find datasource
      FATAL 28-07 12:40:35,359 (Log4JLogger.java:fatal:129) -Could not find datasource: java:/PostgresDCAP
      javax.naming.NamingException: Local server is not initialized
       at org.jnp.interfaces.LocalOnlyContextFactory.getInitialContext(LocalOnlyContextFactory.java:45)
       at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
       at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
       at javax.naming.InitialContext.init(InitialContext.java:223)
       at javax.naming.InitialContext.<init>(InitialContext.java:175)
       at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:28)
       at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
       at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
       at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
       at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:61)
       at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
       at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:151)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:205)
       at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
       at com.vicor.dcap.integration.test.PackageActionTest.init(PackageActionTest.java:19)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.junit.internal.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
       at org.junit.internal.runners.BeforeAndAfterRunner.runBefores(BeforeAndAfterRunner.java:50)
       at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:33)
       at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
       at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
       at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:217)
       at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
       at com.vicor.dcap.integration.test.PackageActionTest.init(PackageActionTest.java:19)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.junit.internal.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
       at org.junit.internal.runners.BeforeAndAfterRunner.runBefores(BeforeAndAfterRunner.java:50)
       at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:33)
       at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
       at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
       at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
      Caused by: org.hibernate.HibernateException: Could not find datasource
       at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:56)
       at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
       at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
       at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:61)
       at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
       at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:151)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:205)
       ... 18 more
      Caused by: javax.naming.NamingException: Local server is not initialized
       at org.jnp.interfaces.LocalOnlyContextFactory.getInitialContext(LocalOnlyContextFactory.java:45)
       at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
       at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
       at javax.naming.InitialContext.init(InitialContext.java:223)
       at javax.naming.InitialContext.<init>(InitialContext.java:175)
       at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:28)
       at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
       ... 27 more
      


      I'm using eclipes and have set my test project to be an ejb project. I have the following persistence.xml which defines the datasource I want to use.
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
       version="1.0">
       <persistence-unit name="DC" transaction-type="JTA">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/PostgresDCAP</jta-data-source>
       <properties>
       <property name="show_sql" value="true"/>
       <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
       <property name="hibernate.hbm2ddl.auto" value="create-drop" />
       </properties>
       </persistence-unit>
      </persistence>
      


      I also have included as a source directory (which gets put in the classpath) the files defined in the seam/embedded-ejb3/conf directory and modified the embedded-jboss-beans.xml to have a bootstrap datasource that matches the persistence unit defined in my persistence.xml.

       <bean name="PostgresDCAPBootstrap" class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
       <property name="driverClass">org.postgres.Driver</property>
       <property name="connectionURL">jdbc:postgresql://localhost:5432/test</property>
       <property name="userName">dcap</property>
       <property name="password">dcap</property>
       <property name="jndiName">java:/PostgresDCAP</property>
       <property name="minSize">0</property>
       <property name="maxSize">10</property>
       <property name="blockingTimeout">1000</property>
       <property name="idleTimeout">100000</property>
       <property name="transactionManager"><inject bean="TransactionManager"/></property>
       <property name="cachedConnectionManager"><inject bean="CachedConnectionManager"/></property>
       <property name="initialContextProperties"><inject bean="InitialContextProperties"/></property>
       </bean>
      
       <bean name="PostgresDCAP" class="java.lang.Object">
       <constructor factoryMethod="getDatasource">
       <factory bean="PostgresDCAPBootstrap"/>
       </constructor>
       </bean>
      


      Any ideas what I'm doing wrong.

      Thanks.
      Eric Ray