6 Replies Latest reply on Mar 17, 2009 2:46 AM by Nikolay Elenkov

    Integration test environmnet not loading import.sql successfully, maven build

    Troy Sellers Newbie

      Hi All,


      I am running into a problem trying to build 2.1.1.GA and richfaces project using Maven 2.0.9.


      My production database is Oracle 9i and we have just migrated our development environment from HSQL to Oracle (started dev before Oracle dbase was available).  Everything is fine in production however the testing environment, which still tries to use an HSQL database, no longer loads the import.sql file correctly.


      It seems that the following happens during the test phase..


      1 - Persistence is loaded from test/resources/META-INF and the import.sql that lives in test resources is trying to run.  This fails as the Entity classes in main project are not yet loaded.
      2 - Persistence is then loaded from main/resources/META-INF, this loads the persistence unit that leverages th Oracle Dialect. 
      3 - Entity classes now try and load using this oracle PU (not the test using HSQL) and fail on data types.


      I wonder if anyone knows what to do to fix the load order of maven so that the test PU over writes the main one?


      Any help / suggestions would be appreciated.


      Cheers,
      Troy

        • 1. Re: Integration test environmnet not loading import.sql successfully, maven build
          Nikolay Elenkov Master

          It would probably be easier to use Maven filters to set up persistence.xml for test and production
          instead of juggling the classpath. Something like:


          persitence.xml:


          <persistence-unit name="pu">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/MyPuDS</jta-data-source>
            <properties>
             <property name="hibernate.dialect" value="${hibernate.dialect}" />
             ...
            </properties>
            ...
          </persistence.unit>



          src/main/filters/dev.properties:


          hibernate.dialect=org.hibernate.dialect.HSQLDialect



          src/main/filters/prod.properties:


          hibernate.dialect=org.hibernate.dialect.Oracle10gDialect



          Of course you will also need to set up your pom.xml to use filters for this to work.

          • 2. Re: Integration test environmnet not loading import.sql successfully, maven build
            Troy Sellers Newbie

            Hi Nikolay,


            Thanks for the response on that, sounds like a good solution to the problem.
            I found this http://jira.codehaus.org/browse/SUREFIRE-443 that talks about why this doesn't work very well for JBoss and Maven.  The Maven hack that is described in here works for me, but you have proposed a far more elegant solution i think.


            Cheers,
            Troy

            • 3. Re: Integration test environmnet not loading import.sql successfully, maven build
              Troy Sellers Newbie

              Hi,


              After some investigation this doesn't seem as promising as I had hoped.  I wanted to apply the filters during the test deployment, then again for the build of the artifact. 
              Can't see a way to do this..


              Cheers,
              Troy

              • 4. Re: Integration test environmnet not loading import.sql successfully, maven build
                Nikolay Elenkov Master

                Troy Sellers wrote on Mar 16, 2009 05:25:

                I wanted to apply the filters during the test deployment, then again for the build of the artifact. 
                e a way to do this..




                Why do you need to filter twice? You can setup a filter per resource, so you can have one for your
                presistence.xml (see below) and for any other resources you may need.


                You can also have profiles, so that dev.properties is used by default, and prod.properties when you
                build a production version (say: mvn -Pprod package).


                <resource>
                  <filtering>true</filtering>
                  <directory>src/main/resources/META-INF</directory>
                  <includes>
                    <include>**/*.xml</include>
                  </includes>
                  <targetPath>META-INF</targetPath>
                </resource>


                • 5. Re: Integration test environmnet not loading import.sql successfully, maven build
                  Troy Sellers Newbie

                  Hi,


                  I think i might have confused the issue, problem is not changing this to target a specific environment, rather that it needs to run for a test phase in the build (using Hypersonic) and then be ready for deployment to an Oracle environment (dev / test / prod.. doesn't matter).


                  The persistence.xml needs to be pointing to an HSQL database while the integration tests are running, then, once build is finished, it needs to be pointing to the Oracle datasource that is used for dev / prod.


                  The problem seems to lie with the way Maven packages its compiled test resources, using two directories, classes and test-classes.  Adding a persistence.xml in with the test resources doesn't overwrite the persistence.xml in the main resources.
                  As both of these are copied to the classpath, and given that jboss embedded seems to load both of these directories using separate class loaders (just a guess from witnessing behavior!), when the entities of my application are trying to load they are failing because the main persistence.xml specifies an Oracle9iDialect.


                  This is probably something that the maven surefire plugin should handle (maybe a 'combine-resources' flag?) although they have listed this as a minor priority.


                  Cheers,
                  Troy

                  • 6. Re: Integration test environmnet not loading import.sql successfully, maven build
                    Nikolay Elenkov Master

                    Troy Sellers wrote on Mar 16, 2009 23:30:


                    I think i might have confused the issue, problem is not changing this to target a specific environment, rather that it needs to run for a test phase in the build (using Hypersonic) and then be ready for deployment to an Oracle environment (dev / test / prod.. doesn't matter).


                    OK, I see what you are trying to do. While this maybe doable, it is probably not worth the trouble. Since you are running your
                    integration tests in HSQL (not Oracle) your test application is actually different from your production one (different configuration).
                    So instead of trying to replace presistence.xml and friends before deployment, simply build with a different profile. Yes, it requires
                    an additional step (or two), but it is actually cleaner and ensures that none of your test code/configurations end up in production.


                    To test:


                    mvn -Pdev test



                    To deploy production:


                    mvn -Pprod deploy



                    HTH