1 2 Previous Next 17 Replies Latest reply on Jul 19, 2011 10:28 AM by Andrey Osipov

    Preconfiguring embedded Glassfish

    Piotr Nizio Newbie

      Hi! First of all I'd like to say you're doing great job with this project and I hope it'll be most successful




      Currently I'm trying to set up Arquillian for testing a JEE6 project intended to run on Glassfish 3. I managed to configure everything to run a simple test in an embedded container. While trying to move on to some serious EJB testing I encountered a problem with configuring a server instance. Before I can deploy my test jar I need to set up some JDBC connection pools and relevant JNDI resources. The best way to do it is to use a sun-resources.xml file, but how can I tell the embedded container to read this file before deploying the app?




      I tried modifying org.jboss.arquillian.glassfish.GlassFishEmbeddedContainer source file but the solution is more complex than just invoking Glassfish CLI API to run 'add-resources' command, because the server instance needs a proper dtd to read the sun-resources.xml.

        • 1. Re: Preconfiguring embedded Glassfish
          Aslak Knutsen Master

          Thank you!


          I don't know to much about GlassFish, but new in Alpha2 is container configuration. The GlassFish container can be pointed to a specific instanceRoot, in that instanceRoot you can put what ever you want. I would assume you could place your resources file there.


          For the configuration options see http://docs.jboss.org/arquillian/reference/latest/en-US/html_single/#container.glassfish-embedded-30

          and  http://docs.jboss.org/arquillian/reference/latest/en-US/html_single/#containers.configuration for how to configure Arquillian.



          This covers how it could be done pre Alpha2, but now you can use the Configuration instead of changing the DeployableContainer.


          • 2. Re: Preconfiguring embedded Glassfish
            Andrew Rubinger Master

            I'm also curious about the GFv3 deployment vs. configuration mechanism.  Is it possible to drop your "sun-resources.xml" somewhere inside of your JAR/WAR/EAR?  Or deployed on its own, separately?




            • 3. Re: Preconfiguring embedded Glassfish
              Iskandar Salim Newbie

              Hi Piotr,


              Glassfish embedded/full will refer to a domain.xml file where can specify datasources, connection pools etc. that you can use for your tests.


              1) Configure the test container by placing arquillian.xml in the root classpath (of your tests). Arquillian will build the configuration from this file instead of using the defaults. (applicable with trunk or Alpha2 versions, as Aslak mentioned)


              Sample arquillian.xml


              <?xml version="1.0"?>
               <arquillian xmlns="http://jboss.com/arquillian"


              2) You will want the embedded GlassFish container instance root to be at a  fixed location so that you can setup your connection pools, jndi resources and other stuff in the domain.xml file e.g. src/test/glassfish/config/domain.xml (create folder manually if you have to) You can copy from your GlassFish installation e.g. ${glassfish}/glassfish/domains/domain1/config/domain.xml and edit where necessary.


              Example section with the jndi datasource setup for a test database


                  <applications />
                      <jdbc-resource jndi-name="jdbc/helpdesk" pool-name="HelpdeskPool" object-type="user" enabled="true"/>
                      <jdbc-connection-pool datasource-classname="org.h2.jdbcx.JdbcDataSource"
                          <property name="Password" value="helpdesk"/>
                          <property name="User" value="helpdesk"/>
                          <property name="serverName" value="localhost"/>
                          <property name="DatabaseName" value="helpdesk"/>
                          <property name="url" value="jdbc:h2:tcp://localhost/~/helpdesk"/>


              Now, for example, you can refer to the test datasource e.g. jdbc/helpdesk from your persistence.xml

              1 of 1 people found this helpful
              • 4. Re: Preconfiguring embedded Glassfish
                Piotr Nizio Newbie

                Aslak, Iskandar > Thank you! That's exactly what I need and what Arquillian lacked in my opinion


                Andrew > I recently did some research in this topic and it seems it is not possible for Glassfish to automatically configure resources from sun-resources.xml bundled in archive together with an app. At least I couldn't find it in the docs and my attempts of putting the file in META-INF inside a jar and deploying it failed (no resources were created). AFAIK the only reasons for keeping that file in a project are:

                • facilitating resource creation using CLI ('asadmin create-resources' command)
                • allowing the IDE <-> AppServer integration plugin to create resources before deploying the app (as it is done e.g. in NetBeans)
                • 5. Re: Preconfiguring embedded Glassfish
                  Aslak Knutsen Master

                  Piotr Nizio wrote:


                  Aslak, Iskandar > Thank you! That's exactly what I need and what Arquillian lacked in my opinion


                  We have a couple of issues that should improve this:




                  • 6. Re: Preconfiguring embedded Glassfish
                    Aslak Knutsen Master

                    If any of your guys are interested in contributing to Arquillian, this issue might be right up your alley.





                    • 8. Re: Preconfiguring embedded Glassfish
                      John Ament Master

                      so one question/request i've had is if it's at all possible to not put the domain.xml under source - e.g. whend oing the build have arquillian point to a location on the file system not necessarily on the classpath to make glassfish? What we'd end up doing is having an admin go in and create the necessary JMS/JDBC/JNDI resources and point to that during our builds.  This would all be assuming that the build is local of course.

                      • 9. Re: Preconfiguring embedded Glassfish
                        Aslak Knutsen Master

                        If I understand you correctly, that is what the instanceRoot option gives you.




                        instanceRoot can be pointed to your GlassFish 3.0 install Domain folder, e.g, /opt/glassfish/domains/domain1

                        • 10. Re: Preconfiguring embedded Glassfish
                          Iskandar Salim Newbie

                          Gave it a go


                          Profile for JUnit and TestNG examples. For TestNG, bsh.jar needs to be included in GlassFish e.g. ${glassfish}/glassfish/domains/domain/lib


                                    <!-- JUnit examples-->
                                    <!-- TestNG examples -->
                          • 11. Re: Preconfiguring embedded Glassfish
                            Aslak Knutsen Master

                            Excellent Iskandar!


                            Reviewing the patch, and will get it in for Alpha-3

                            • 12. Re: Preconfiguring embedded Glassfish
                              Dan Allen Master

                              Better yet, with my latest patch on ARQ-156, you can now supply a sun-resources.xml file that will be passed to the add-resources asadmin command before the Embedded GlassFish instance is started. So you can work with a fresh instance and still get all the JMS/JDBC/JNDI resources you want!


                              Here's a preview, starting with the Arquillian configuration:


                              <arquillian xmlns="http://jboss.com/arquillian"


                              Here is the sun-resources.xml file that configures an embedded Derby instance named jdbc/tests:


                              <?xml version="1.0" encoding="UTF-8"?>
                              <!DOCTYPE resources PUBLIC
                                 "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN"
                                 <jdbc-resource pool-name="TestsEmbeddedDerbyPool"
                                 <jdbc-connection-pool name="TestsEmbeddedDerbyPool"
                                    <property name="databaseName" value="target/databases/derby"/>
                                    <property name="createDatabase" value="create"/>



                              If you add a JDBC driver to your test classpath, such as H2, you have the option of using it instead of embedded Derby:



                              <?xml version="1.0" encoding="UTF-8"?>
                              <!DOCTYPE resources PUBLIC
                                 "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN"
                                 <jdbc-resource pool-name="TestsEmbeddedH2Pool"
                                 <jdbc-connection-pool name="TestsEmbeddedH2Pool"
                                    <property name="user" value="sa"/>
                                    <property name="password" value=""/>
                                    <property name="url" value="jdbc:h2:file:target/databases/h2/db"/>



                              Excited for Alpha3 yet?

                              • 13. Re: Preconfiguring embedded Glassfish
                                Iskandar Salim Newbie

                                Testing with GlassFish containers made cleaner. This is neat!

                                Really looking forward to Alpha3

                                • 14. Re: Preconfiguring embedded Glassfish
                                  Dan Allen Master

                                  Indeed! I'm working to ensure that regardless of what container you are targeting, there is an easy way to configure resources. I'm working on a tutorial that will cover JBoss AS as well.


                                  I'm also hoping that the JSR 88 container is a foot in the door for other containers to be supported (hint, WebLogic). While there is a benefit to using something more advanced than JSR 88, it gets the job done.

                                  1 2 Previous Next