Built-in GlassFish ignores Maven test resources

I have several beans sessions for which I wrote unit tests. I installed Maven to include persistence.xml in the src / main / resources / META-INF directory, which references the local MySQL database for development purposes. I have another persistence.xml file in the src / test / resources / META-INF directory that references the Derby __default built-in database. Tests are deployed to the GlassFish 3.1 integrated container.

When I run the tests, I get the following error:

java.lang.RuntimeException: javax.naming.NamingException: Lookup failed for 'jdbc/mylog' 

jdbc / mylog is the MySQL database referenced by the persistence unit in the main directory. Obviously, it ignores the persistence block in the test directory, but I don't know why.

Maven sets the class path correctly, as far as I can tell, with the test classes in front of the classes and a look in the actual target / test-classes / META-INF directory shows that it copied the correct Derby built-in block, persistence.

[DEBUG] Test Classpath :
[DEBUG]   C:\Users\Laurens\Documents\Projects\Mylog\target\test-classes
[DEBUG]   C:\Users\Laurens\Documents\Projects\Mylog\target\classes
[DEBUG]   C:\Users\Laurens\.m2\repository\org\eclipse\persistence\eclipselink\2.2.0\eclipselink-2.2.0.jar
[DEBUG]   C:\Users\Laurens\.m2\repository\org\eclipse\persistence\javax.persistence\2.0.3\javax.persistence-2.0.3.jar
[DEBUG]   C:\Users\Laurens\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.jpa.modelgen.processor\2.2.0\org.eclipse.persistence.jpa.modelgen.processor-2.2.0.jar
[DEBUG]   C:\Users\Laurens\.m2\repository\org\glassfish\extras\glassfish-embedded-all\3.1\glassfish-embedded-all-3.1.jar
[DEBUG]   C:\Users\Laurens\.m2\repository\javax\javaee-web-api\6.0\javaee-web-api-6.0.jar
[DEBUG]   C:\Users\Laurens\.m2\repository\junit\junit\4.8.1\junit-4.8.1.jar

Any hint of how GlassFish uses the right persistence system is greatly appreciated! Thank!

+5
source share
3 answers

Glassfish JPA , , maven-surefire-plugin ( ). Embedded Glassfish , , ScatteredArchive. java.io.tmpdir, gfembed<a_random_number>tmp, Glassfish Glassfish Glassfish.

Glassfish , , , , ( ), . GF_EMBED_DOMAIN_HOME/applications/<application_name>. persistence.xml src/main/resources/META-INF src/test/resources/META-INF <application-name>/META-INF. , , , , , , JPA . src/main/resources/META-INF.

:

1. Glassfish

(domain.xml), jdbc/mylog. , , , . :

Map<String, Object> props = new HashMap<String, Object>();
props.put("org.glassfish.ejb.embedded.glassfish.installation.root", "./glassfish-install/glassfish");
container = EJBContainer.createEJBContainer(props);
context = container.getContext();
datasource = (DataSource) context.lookup("jdbc/mylog"); //You can lookup the datasource too, to confirm that your setup is successful.

glassfish-install glassfish Maven ( ); glassfish domain1/config Glassfish domain1. . (JAR- JDBC- JAR ..) Glassfish, Glassfish, .

Glassfish domain configuration file location

Glassfish , Glassfish, ( , , , ):

<domain log-root="${com.sun.aas.instanceRoot}/logs" application-root="${com.sun.aas.instanceRoot}/applications" version="10.0">
  <system-applications/>
  <applications/>
  <resources>
    <jdbc-resource pool-name="MyPool" jndi-name="jdbc/mylog"/>
    ...
    <jdbc-connection-pool driver-classname="" datasource-classname="org.apache.derby.jdbc.ClientDataSource" res-type="javax.sql.DataSource" description="" name="MyPool" ping="true">
      <property name="User" value="APP"></property>
      <property name="RetrieveMessageText" value="true"></property>
      <property name="CreateDatabase" value="true"></property>
      <property name="ServerName" value="localhost"></property>
      <property name="Ssl" value="off"></property>
      <property name="SecurityMechanism" value="4"></property>
      <property name="TraceFileAppend" value="false"></property>
      <property name="TraceLevel" value="-1"></property>
      <property name="PortNumber" value="1527"></property>
      <property name="LoginTimeout" value="0"></property>
      <property name="Password" value="APP"></property>
      <property name="databaseName" value="MYDB"></property>
    </jdbc-connection-pool>
    ...
  </resources>
  <servers>
    <server name="server" config-ref="server-config"> 
      <resource-ref ref="jdbc/__TimerPool"/>
      <resource-ref ref="jdbc/__default"/>
      <resource-ref ref="jdbc/mylog"/>
    </server>
  </servers>
  ...
...

domain.xml java.net , , Glassfish.

2. persistence.xml

Maven MOM persistence.xml src/test/resources/META-INF src/main/resources/META-INF, test. . , fooobar.com/questions/86862/.... , , , persistence.xml, . - , JPA persistence.xml target/classes target/test-classes, , . Hibernate JPA, TRACE org.hibernate.ejb ( Ejb3Configuration ) , test-classes .


:

Glassfish 3.1, .

+11

" ", maven, ? maven , maven test. , .

0

This answer may seem silly, but I was looking for a way to run those tests from eclipse to Run AsJUnit Test. Here is how I did it:

@BeforeClass
public static void setUp() throws IOException {
    Files.copy(new File("target/test-classes/META-INF/persistence.xml"), new File("target/classes/META-INF/persistence.xml"));
    // ...
}

I just copy test / persistence.xml to classes / persistence.xml. It works.

0
source

All Articles