Mantis - Resin
Viewing Issue Advanced Details
4502 minor always 04-13-11 13:07 04-26-11 16:54
reza  
ferg  
normal  
closed 4.0.17  
fixed  
none    
none 4.0.18  
0004502: Loading JPA fails in JUnit due to data source JNDI registry ordring issues
Loading JPA is failing in JUnit because the JPA bootstrap happens before the data source is registered in JNDI. For example, the following code will fail to deploy:

================================================================================
<beans>
  <database>
    <jndi-name>jdbc/test</jndi-name>
    <driver>
      <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type>
      <url>jdbc:mysql://localhost:3306/test</url> [^]
      <user></user>
      <password></password>
    </driver>
  </database>
</beans>
================================================================================

================================================================================
<persistence>
  <persistence-unit name="test" transaction-type="JTA">
    <jta-data-source>jdbc/test</jta-data-source>
    <properties>
      <property name="eclipselink.ddl-generation" value="create-tables" />
      <property name="eclipselink.logging.level" value="FINEST" />
    </properties>
  </persistence-unit>
</persistence>
================================================================================

================================================================================
package qa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class TestBean {
  @PersistenceContext
  private EntityManager entityManager;

  public void hit() {
    System.out.println("hit(" + entityManager + ")");
  }
}
================================================================================

================================================================================
package qa;

import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.caucho.junit.ResinBeanContainerRunner;

@RunWith(ResinBeanContainerRunner.class)
public class JUnitTest {
  @Inject
  private TestBean bean;

  @Test
  public void test() {
    bean.hit();
  }
}
================================================================================

This is definitely a timing issue specific to JUnit/the bean container because the same code works fine in a web application, loaded via a Servlet:

================================================================================
package qa;



import java.io.IOException;



import javax.inject.Inject;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



@WebServlet(name = "TestServlet", urlPatterns = { "/test" })

public class TestServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;



  @Inject

  private TestBean bean;



  @Override

  protected void doGet(HttpServletRequest request,

      HttpServletResponse response) throws ServletException, IOException {

    bean.hit();

  }

}
================================================================================

This is pretty serious since loading JPA is critical to most Java EE integration testing.

Notes
(0005207)
ferg   
04-26-11 16:54   
env/0e81