0 Replies Latest reply on Mar 6, 2011 8:39 AM by Sam W

    Manual binding data source in Junit test

    Sam W Expert

      Hello,

       

      After a day's struggling, I am still unable to resolve this error. My junit test still throwing exception complaining about datasource is not found.

       

      Here is my code:

       

      public class RoleFacadeTest {   

          private static EntityManagerFactory emf;

          private static EntityManager em;

       

          public RoleFacadeTest() {

          }

       

       

          // copied from org.jboss.naming.Util

          private static Context createSubcontext(Context ctx, String cxtName)

               throws NamingException {

           //System.out.println(" creating subcontext " + cxtName);

           Context subctx = ctx;

           Name name = ctx.getNameParser("").parse(cxtName);

           for (int pos = 0; pos < name.size(); pos++) {

               String ctxName = name.get(pos);

               try {

                   subctx = (Context) ctx.lookup(ctxName);

               } catch (NameNotFoundException e) {

                   subctx = ctx.createSubcontext(ctxName);

               }

               // The current subctx will be the ctx for the next name component

               ctx = subctx;

           }

           return subctx;

          }

       

          @Before

          public void setUp() {

      //        Map<String, String> config = new HashMap<String, String>();

      //        config.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/houseware");

      //        config.put("hibernate.connection.username", "root");

      //        config.put("hibernate.connection.password", "admin");

      //        config.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");

      //        config.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

      //        config.put("hibernate.show_sql", "true");

              // rcarver - setup the jndi context and the datasource

              try {

                  // Create initial context

                  System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

                  System.setProperty(Context.URL_PKG_PREFIXES,  "org.jboss.naming");

       

                  MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();

       

                  ds.setURL("jdbc:mysql://localhost:3306/houseware");

                  ds.setUser("root");

                  ds.setPassword("admin");

       

                  InitialContext ic = new InitialContext();

       

       

                  String dsName = "jdbc:/Houseware";

       

       

                  String[] cxts = dsName.split("/");

                  String inCxt = cxts[0];

                  createSubcontext(ic, inCxt);

                   for (int i = 1; i < cxts.length - 1; i++) {

                       // if the data source name is like java:/comp/mysqldatasource

                       // this takes care of creating subcontexts in jndi

                       inCxt = inCxt + "/" + cxts[i];

                       createSubcontext(ic, inCxt);

                   }    

       

       

                   ic.bind(dsName, ds);

       

               } catch (NamingException ex) {

                  Logger.getLogger(RoleFacadeTest.class.getName()).log(Level.SEVERE, null, ex);

              }

              emf = Persistence.createEntityManagerFactory("HousewareEnterpriseApplication-v3-ejbPU");

              em = (EntityManager) emf.createEntityManager();

       

       

          }

       

       

          @Test

          public void testCreate() throws Exception {

              Context initContext = new InitialContext();

              Context webContext = (Context)initContext.lookup("java:/comp/env");

       

       

              DataSource ds = (DataSource) webContext.lookup("jdbc/Houseware");

          }

       

      }

       

          I am not sure how to define a correct persistence.xml for this junit test, here is the file:

       

       

      <?xml version="1.0" encoding="UTF-8"?>

      <persistence version="2.0" 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_2_0.xsd">

        <persistence-unit name="HousewareEnterpriseApplication-v3-ejbPU" transaction-type="RESOURCE_LOCAL">

          <non-jta-data-source>jdbc/Houseware</non-jta-data-source>

       

       

       

              <class>au.com.houseware.server.ejb.entity.bean.RoleFacade</class>

              <exclude-unlisted-classes>false</exclude-unlisted-classes>

              <properties>

                  <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/housewar"/>

                  <property name="hibernate.connection.username" value="root"/>

                  <property name="hibernate.connection.password" value="admin"/>

                  <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

                  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

                  <property name="hibernate.show_sql" value="true"/>

                  <property name="hibernate.hbm2ddl.auto" value="update"/>

              </properties>

        </persistence-unit>

      </persistence>

       

       

      Note, I don't knwo where to define data source xml file. I got a jboss-ds.xml file is running perfectly with jboss as, but since this is junit test. I dont' know where I should put it.

       

      Here is the full exception:

       

      Testsuite: au.com.houseware.server.ejb.entity.bean.RoleFacadeTest

      07/03/2011 12:26:01 AM au.com.houseware.server.ejb.entity.bean.RoleFacadeTest setUp

      SEVERE: null

      javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]

                at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1690)

                at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1812)

                at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:695)

                at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688)

                at javax.naming.InitialContext.lookup(InitialContext.java:392)

                at au.com.houseware.server.ejb.entity.bean.RoleFacadeTest.createSubcontext(RoleFacadeTest.java:58)

                at au.com.houseware.server.ejb.entity.bean.RoleFacadeTest.setUp(RoleFacadeTest.java:98)

                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:597)

                at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

                at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

                at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

                at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)

                at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)

                at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)

                at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)

                at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)

                at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)

                at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)

                at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)

                at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)

                at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

                at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)

                at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518)

                at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052)

                at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906)

      Caused by: java.net.SocketTimeoutException: Receive timed out

                at java.net.PlainDatagramSocketImpl.receive0(Native Method)

                at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)

                at java.net.DatagramSocket.receive(DatagramSocket.java:725)

                at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1659)

                ... 27 more

      log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).

      log4j:WARN Please initialize the log4j system properly.

      Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 13.307 sec

       

       

      Your help is very much appreciated.

      Thanks

      Sam