Mittwoch, 2. März 2011

Integration test with jetty & maven

Hey all,
these days I have tested some integration test stuff with the jetty plugin. First time I found a "how to" for maven-jetty-plugin:6.1.26 which work well for me. Then I would to extend my integration test and have found the "jetty-maven-plugin". Between version 6 and 7 the plugin was renamed. That is a little bit confusion if you searches the internet and find different "how to's". 

However, I find out the solution for my test cases. All my integration tests are standalone test projects, so there is no dependency (without using classes and webapps) to the business components.
  1. start a webApp and send some HTTP stuff to the webApp.
  2. start two webApps, webApp "A" depends (via HTTP) on webApp "B". Then send some HTTP stuff to the webApp "A". WebApp "A" then calls webApp "B".
  3. same as 2. but the webApps are on different ports.
 At first, you have to get your webApps to be started. Therefore I've used the "maven-dependency-plugin":

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-dependency-plugin</artifactId>
 <version>2.2</version>
 <executions>
  <execution>
   <id>copy</id>
   <phase>package</phase>
   <goals>
    <goal>copy</goal>
   </goals>
   <configuration>
    <artifactItems>
     <artifactItem>
      <groupId>com.foo.bar<groupId>
      <artifactId>webAppA</artifactId>
      <version>00.02-SNAPSHOT</version>
      <type>war</type>
      <overWrite>true</overWrite>
      <outputDirectory>target/webapps</outputDirectory>
      <destFileName>webAppA.war</destFileName>
     </artifactItem>
     <artifactItem>
      <groupId>com.foo.bar</groupId>
      <artifactId>webAppB</artifactId>
      <version>00.02-SNAPSHOT</version>
      <type>war</type>
      <overWrite>true</overWrite>
      <outputDirectory>target/webapps</outputDirectory>
      <destFileName>webAppB</destFileName>
     </artifactItem>
    </artifactItems>
   </configuration>
  </execution>
 </executions> 

</plugin>

As you can see, the "maven-dependency-plugin" is execute while the "package phase"!.
The next step is to start the container with the apps. I do not explain the scenarios for testcase 1. and 2. because I'm sure everybody can derive the first 2 cases from the 3. example ;-)
  1. Define two connectors on different ports and give them a name.
  2. Define the contextHandlers and configure the dependency to the connector.
  3. Define the loginService.
  4. Configure the Execution of the jetty plugin.
<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.0.0.M2</version>
    <configuration>
        <connectors>
            <connector implementation= "org.eclipse.jetty.server.nio.SelectChannelConnector">
                <port>8090</port>
                <name>instance_8090</name>
            </connector>
            <connector implementation= "org.eclipse.jetty.server.nio.SelectChannelConnector">
                <port>8080</port>
                <name>instance_8080</name>
            </connector>
        </connectors>
        <contextHandlers>
            <contextHandler implementation= "org.eclipse.jetty.webapp.WebAppContext">
                <contextPath>/webAppA</contextPath>
                <war>${basedir}/target/webapps/webAppA.war</war>
                <connectorNames><item>instance_8090</item></connectorNames>
            </contextHandler>
            <contextHandler implementation= "org.eclipse.jetty.webapp.WebAppContext">
                <contextPath>/webAppB</contextPath>
                <war>${basedir}/target/webapps/webAppB.war</war>
                <connectorNames><item>instance_8090</item></connectorNames>
            </contextHandler>
        </contextHandlers>
        <loginServices>
            <loginService implementation= "org.eclipse.jetty.security.HashLoginService">
                <name>Test Realm</name>
                <config>${basedir}/src/etc/realm.properties</config>
            </loginService>
        </loginServices>
        <stopKey>foo</stopKey>
        <stopPort>9999</stopPort>
    </configuration>
    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <scanIntervalSeconds>0</scanIntervalSeconds>
                <daemon>true</daemon>
            </configuration>
        </execution>
        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>                   
        <dependency>
            <groupId>oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.2.0</version>
        </dependency>
    </dependencies>
</plugin>
 

If you have some dependencies that are not available in you webApps (for example the jdbc driver which should be provided from the container), so add the dependencies to the jetty plugin as shown in the sample above.


I don't know why the plugIn needs the realm, but without it I've trouble.The content of my "realm.properties" is:
username: test[manager]


Now we should add the ability to execute the integration tests. Therefore add the "maven-failsafe-plugin":

<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Hint: I've problems to find the right maven repo for the yetti plugIn, so maybe you can save a bit time with that: "http://repo2.maven.org/maven2"

After configured your integration test project as above, you can start jetty with "mvn jetty:run" or you can execute your integration test incl. starting and stopping yetti with "mvn clean install".

You fiind the documentation of the plugIn here: http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin.

and no have some fun...
          ...while testing your apps.

Keine Kommentare:

Kommentar veröffentlichen