6 Replies Latest reply on Jan 11, 2011 7:40 PM by hurzeler

    Using separate persistence.xml for test and dev with JBoss Tools seam generated project

    barakka

      Hello!


      My situation is the following:



      • Seam 2.1.0.1BETA1

      • Eclipse 3.4

      • Seam Tools (3.0.0ALPHA1)




      I have generated a new project using the Create new Seam Web-Project wizard, selecting Dynimic Web Project 2.0 and ear packaging. The wizard has created a structure of four eclipse projects: one with the web contents, a second for the ear, a third with the ejb and finally a fourth with for the tests. The ejb project, as it will contain the entity ejbs, includes a META-INF/persistence.xml and is exported by eclipse to an ejb.jar during deployment. As for my setup this project points to the normal data source i use in development, which is set up against a real (MSSQL) database and such that the db is not regenerated at each start.


      For my tests, run through the eclipse TestNG plugin, I'd like to use the default hibernate datasource with automatic drop and create of the database schema. In order to do that it is necessary to modify the persistence.xml accordingly.


      Generating the desired persistence.xml is not a problem, the problem is how to let TestNG and the embedded Jboss know about this new file file and do not consider the default one.


      The solution of placing the new persistence.xml in the META-INF directory of the test project does not work, as that is not project containing the entity ejbs, so they get not scanned. Using the jar-file tag (to explicitly include the ejb.jar) is not possible as eclipse does not generate it when launching the plug-in. The remaining solution is to include each entity with a class declaration, which of course doesn't scale very well.


      Seam-gen generated projects use the ant script to copy and rename the file depending on the environment, but when running the tests through the TestNG plugin this approach is not viable.


      So, what solution can be used to have a separate persistence.xml for test and development when using the TestNG plugin and a Seam Tools generated project?


      Thanks to all,
      R.

        • 1. Re: Using separate persistence.xml for test and dev with JBoss Tools seam generated project
          gjeudy

          If you use maven2 you have better control over test cycle versus regular compile/package cycle. However seam-gen is not integrated with  maven2 so you would get none of seam-gens advantages if you decide to go this way.


          I have found a workable solution with maven2 but I'm still not fully satisfied, in any case if you are interested I can give you more information on my solution.

          • 2. Re: Using separate persistence.xml for test and dev with JBoss Tools seam generated project
            ionixx

            Hello Riccardo,


            do you have a solution for this situation in the meanwhile? I have the same problem and don´t know how to solve it. A maven2-based approach would be (in my eyes) a pain during development time. I want to use the eclipse-plugin for TestNG and not start mvn test each time.


            Would be great to hear from you!

            • 3. Re: Using separate persistence.xml for test and dev with JBoss Tools seam generated project
              mcarrizo

              Riccardo:


                  I'm dealing with exactly the same problem. Did you find a good solution for this issue ?


                       Best regards !!


                              Maximiliano

              • 4. Re: Using separate persistence.xml for test and dev with JBoss Tools seam generated project
                barakka

                Hello everyone,


                I'm sorry, but I have no solution for the problem. Since then, I've moved to maved, forgot completely about seam-gen, and resorted to writing a customized classloader for the jboss embedder in order to have it play nice with maven, eclipse and testNG.


                Best,
                Riccardo.

                • 5. Re: Using separate persistence.xml for test and dev with JBoss Tools seam generated project
                  john.genoese
                  I'm using maven as well, and I just emulated what seam-gen did in order to solve the problem, because there wasn't any way I wanted to list each <class/> in persistence.xml.

                  My solution was
                  1. Copy ejb/target/classes to /tmp/classes, excluding META-INF (where the production persistence.xml lives).


                  `[EJB POM MAVEN CODE START]
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-resources-plugin</artifactId>
                      <version>2.3</version>
                      <executions>
                          <execution>
                             <id>process-ejb-classes</id>
                             <phase>process-test-classes</phase>
                             <goals>
                                 <goal>copy-resources</goal>
                             </goals>
                             <configuration>
                                  <outputDirectory>target/test-classes</outputDirectory>
                                  <resources>
                                      <resource>
                                          <directory>target/classes</directory>
                                          <filtering>false</filtering>
                                          <excludes>
                                              <exclude>META-INF/**</exclude>
                                          </excludes>
                                      </resource>
                                  </resources>
                              </configuration>
                          </execution>
                       </executions>
                  </plugin>
                  [EJB POM MAVEN CODE END]`


                  2. Point maven-surefire-plugin <classesDirectory/> and <testClassesDirectory/> to 'target/test-classes'.

                  `[EJB POM MAVEN CODE START]
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <configuration>
                          <classesDirectory>target/test-classes</classesDirectory>
                          <testClassesDirectory>target/test-classes</testClassesDirectory>
                          <testSourceDirectory>src/test/java</testSourceDirectory>
                          <suiteXmlFiles>
                              <suiteXmlFile> src/test/testng.xml </suiteXmlFile>
                          </suiteXmlFiles>
                          <additionalClasspathElements>
                              <additionalClasspathElement>
                                  src/test/bootstrap
                              </additionalClasspathElement>
                          </additionalClasspathElements>
                          <childDelegation>true</childDelegation>
                          <useSystemClassLoader>true</useSystemClassLoader>
                          <argLine>-Dsun.lang.ClassLoader.allowArraySyntax=true</argLine>
                      </configuration>
                  </plugin>
                  [EJB POM MAVEN CODE END]`

                  Additionally,I use seam-gen to generate proper bootstrap artifacts, then isolate said artifacts in a separate place.

                  In this way, Hibernate/JPA discovers my entities without my having to provide <class/> elements in persistence.xml, or write a classloader! However, anyone would admit that writing a classloader is much more interesting and fun than this stuff!

                  Grace and peace.

                  -- John
                  • 6. Re: Using separate persistence.xml for test and dev with JBoss Tools seam generated project
                    hurzeler

                    Can you ellaborate on your point:
                    "Additionally,I use seam-gen to generate proper bootstrap artifacts, then isolate said artifacts in a separate place."


                    please?


                    What artifacts are required for Seam to recognize the entity classes?


                    Cheers
                    Bernie