org.eclipse.jetty.toolchain.test
Class JettyDistro

java.lang.Object
  extended by org.eclipse.jetty.toolchain.test.JettyDistro

public class JettyDistro
extends Object

Basic process based executor for using the Jetty Distribution along with custom configurations to perform basic

Allows for a test specific directory, that is a copied jetty-distribution, and then modified for the test specific testing required.

Requires that you setup the maven-dependency-plugin appropriately for the base distribution you want to use, along with any other dependencies (wars, libs, etc..) that you may need from other maven projects.

Maven Dependency Plugin Setup:

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
   <!-- Common Destination Directories -->
 
   <properties>
     <test-wars-dir>${project.build.directory}/test-wars</test-wars-dir>
     <test-libs-dir>${project.build.directory}/test-libs</test-libs-dir>
     <test-distro-dir>${project.build.directory}/test-dist</test-distro-dir>
   </properties>
 
   <build>
     <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>2.1</version>
         <executions>
 
           <!-- Copy LIB and WAR dependencies into place that JettyDistro can use them -->
 
           <execution>
             <id>test-lib-war-copy</id>
             <phase>process-test-resources</phase>
             <goals>
               <goal>copy</goal>
             </goals>
             <configuration>
               <artifactItems>
                 <artifactItem>
                   <groupId>org.mortbay.jetty.testwars</groupId>
                   <artifactId>test-war-java_util_logging</artifactId>
                   <version>7.3.0</version>
                   <type>war</type>
                   <outputDirectory>${test-wars-dir}</outputDirectory>
                 </artifactItem>
                 <artifactItem>
                   <groupId>org.mortbay.jetty</groupId>
                   <artifactId>jetty-aspect-servlet-api-2.5</artifactId>
                   <version>7.3.0</version>
                   <type>jar</type>
                   <outputDirectory>${test-libs-dir}</outputDirectory>
                 </artifactItem>
               </artifactItems>
               <overWriteIfNewer>true</overWriteIfNewer>
               <overWrite>true</overWrite>
               <stripVersion>true</stripVersion>
             </configuration>
           </execution>
 
           <!-- Extract Jetty DISTRIBUTION into place that JettyDistro can use it -->
 
           <execution>
             <id>unpack-test-dist</id>
             <phase>process-test-resources</phase>
             <goals>
               <goal>unpack</goal>
             </goals>
             <configuration>
               <artifactItems>
                 <artifactItem>
                   <groupId>org.eclipse.jetty</groupId>
                   <artifactId>jetty-distribution</artifactId>
                   <version>7.3.0</version>
                   <type>zip</type>
                   <overWrite>true</overWrite>
                 </artifactItem>
               </artifactItems>
               <outputAbsoluteArtifactFilename>true</outputAbsoluteArtifactFilename>
               <outputDirectory>${test-distro-dir}</outputDirectory>
               <overWriteSnapshots>true</overWriteSnapshots>
               <overWriteIfNewer>true</overWriteIfNewer>
             </configuration>
           </execution>
         </executions>
       </plugin>
     </plugins>
   </build>
 
 </project>
 

If you have a specific configuration you want to setup, you'll want to prepare this configuration in an overlay directory underneath the src/test/resources/ directory.
Notes:

  1. The JettyDistro sets up a unique test directory (based on the constructor JettyDistro(Class) or JettyDistro(TestingDir)), by ensuring the directory is empty, then copying the target/test-dist directory into this new testing directory prior to the test specific changes to the configuration.
    Note: this testing directory is a complete jetty distribution, suitable for executing via the command line for additional testing needs.
  2. The directory name you choose in src/test/resources will be the name you use in the overlayConfig(String) method to provide replacement configurations for the Jetty Distribution.
  3. You'll want to delete(String) any files and/or directories from the standard distribution prior to using the overlayConfig(String) method.
  4. Use the copyLib(String, String) method to copy JAR files from the target/test-libs directory (created and managed above using the maven-dependency-plugin) to copy the lib into the test specific.
  5. Use the copyTestWar(String) method to copy WAR files from the target/test-wars directory (created and managed above using the maven-dependency-plugin) to copy the WAR into the test specific directory.

Next you'll want to use Junit 4.8+ and the @BeforeClass and @AfterClass annotations to setup the JettyDistro class for setting up your testing configuration.

Example Test Case using JettyDistro class

 public class MySampleTest
 {
     private static JettyDistro jetty;
 
     @BeforeClass
     public static void initJetty() throws Exception
     {
         jetty = new JettyDistro(MySampleTest.class);
 
         jetty.copyTestWar("test-war-java_util_logging.war");
         jetty.copyTestWar("test-war-policy.war");
 
         jetty.delete("webapps/test.war");
         jetty.delete("contexts/test.d");
         jetty.delete("contexts/javadoc.xml");
         jetty.delete("contexts/test.xml");
 
         jetty.overlayConfig("no_security");
 
         jetty.setDebug(true);
 
         jetty.start();
     }
 
     @AfterClass
     public static void shutdownJetty() throws Exception
     {
         if (jetty != null)
         {
             jetty.stop();
         }
     }
 
     @Test
     public void testRequest() throws Exception
     {
         SimpleRequest request = new SimpleRequest(jetty.getBaseUri());
         String path = "/test-war-policy/security/PRACTICAL/testFilsystem");
         String response = request.getString(path);
         Assert.assertEquals("Success", response);
     }
 }
 


Constructor Summary
JettyDistro(Class<?> clazz)
          Setup the JettyHome as belonging in a testing directory associated with a testing clazz.
JettyDistro(Class<?> clazz, String artifact)
          Setup the JettyHome as belonging in a testing directory associated with a testing clazz.
JettyDistro(TestingDir testdir)
          Setup the JettyHome as belonging to a specific testing method directory
JettyDistro(TestingDir testdir, String artifact)
          Setup the JettyHome as belonging to a specific testing method directory
 
Method Summary
 void copyLib(String libFilename, String outputPath)
          Copy an arbitrary file from target/test-libs/${libFilename} to the testing directory.
 void copyProjectMainConfig()
          Copy the ${project.basedir}/src/main/config/ tree into the testing directory.
 void copyResource(String resourcePath, String outputPath)
          Copy an arbitrary file from src/test/resources/${resourcePath} to the testing directory.
 void copyTestWar(String testWarFilename)
          Copy a war file from ${project.basedir}/target/test-wars/${testWarFilename} into the ${jetty.home}/webapps/ directory
 void createProjectLib(String jarFilename)
          Create a ${jetty.home}/lib/self/${jarFilename} jar file from the content in the ${project.basedir}/target/classes/ directory.
 void delete(String path)
          Delete a File or Directory found in the ${jetty.home} directory.
 URI getBaseUri()
          Return the baseUri being used for this Jetty Process Instance.
 File getJettyHomeDir()
          Return the $(jetty.home) directory being used for this JettyDistro
 String getJmxUrl()
          Return the JMX URL being used for this Jetty Process Instance.
 void overlayConfig(String testConfigName)
          Take the directory contents from ${project.basedir}/src/test/resources/${testConfigName}/ and copy it over whatever happens to be at ${jetty.home}
 void setDebug(boolean debug)
          enable debug on the jetty process
 void setStartTime(long startTime, TimeUnit timeUnit)
           
static List<String> splitAndUnescapeCommandLine(CharSequence rawCmdLine)
           
 void start()
          Start the jetty server
 void stop()
          Stop the jetty server
 void unpackConfig(String configFilename)
          Unpack an arbitrary config from target/test-configs/${configFilename} to the testing directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JettyDistro

public JettyDistro(Class<?> clazz)
            throws IOException
Setup the JettyHome as belonging in a testing directory associated with a testing clazz.

Parameters:
clazz - the testing class using this JettyDistro
Throws:
IOException - if unable to copy unpacked distribution into place for the provided testing directory

JettyDistro

public JettyDistro(Class<?> clazz,
                   String artifact)
            throws IOException
Setup the JettyHome as belonging in a testing directory associated with a testing clazz.

Parameters:
clazz - the testing class using this JettyDistro
artifact - name of jetty distribution artifact
Throws:
IOException - if unable to copy unpacked distribution into place for the provided testing directory

JettyDistro

public JettyDistro(TestingDir testdir)
            throws IOException
Setup the JettyHome as belonging to a specific testing method directory

Parameters:
testdir - the testing directory to use as the JettyHome for this JettyDistro
Throws:
IOException - if unable to copy unpacked distribution into place for the provided testing directory

JettyDistro

public JettyDistro(TestingDir testdir,
                   String artifact)
            throws IOException
Setup the JettyHome as belonging to a specific testing method directory

Parameters:
testdir - the testing directory to use as the JettyHome for this JettyDistro
artifact - name of jetty distribution artifact
Throws:
IOException - if unable to copy unpacked distribution into place for the provided testing directory
Method Detail

getJettyHomeDir

public File getJettyHomeDir()
Return the $(jetty.home) directory being used for this JettyDistro

Returns:
the jetty.home directory being used

copyTestWar

public void copyTestWar(String testWarFilename)
                 throws IOException
Copy a war file from ${project.basedir}/target/test-wars/${testWarFilename} into the ${jetty.home}/webapps/ directory

Parameters:
testWarFilename - the war file to copy (must exist)
Throws:
IOException - if unable to copy the war file.

copyResource

public void copyResource(String resourcePath,
                         String outputPath)
                  throws IOException
Copy an arbitrary file from src/test/resources/${resourcePath} to the testing directory.

Parameters:
resourcePath - the relative path for file content within the src/test/resources directory.
outputPath - the testing directory relative output path for the file output (will result in a file with the outputPath name being created)
Throws:
IOException - if unable to copy resource file

copyLib

public void copyLib(String libFilename,
                    String outputPath)
             throws IOException
Copy an arbitrary file from target/test-libs/${libFilename} to the testing directory.

Parameters:
libFilename - the target/test-libs/${libFilename} to copy
outputPath - the destination testing directory relative output path for the lib. (will result in a file with the outputPath name being created)
Throws:
IOException - if unable to copy lib

copyProjectMainConfig

public void copyProjectMainConfig()
                           throws IOException
Copy the ${project.basedir}/src/main/config/ tree into the testing directory.

Throws:
IOException - if unable to copy the directory tree

createProjectLib

public void createProjectLib(String jarFilename)
                      throws IOException
Create a ${jetty.home}/lib/self/${jarFilename} jar file from the content in the ${project.basedir}/target/classes/ directory.

Throws:
IOException - if unable to copy the directory tree

unpackConfig

public void unpackConfig(String configFilename)
                  throws IOException
Unpack an arbitrary config from target/test-configs/${configFilename} to the testing directory.

Parameters:
configFilename - the target/test-configs/${configFilename} to copy
Throws:
IOException - if unable to unpack config file

delete

public void delete(String path)
Delete a File or Directory found in the ${jetty.home} directory.

Parameters:
path - the path to delete. (can be a file or directory)

getBaseUri

public URI getBaseUri()
Return the baseUri being used for this Jetty Process Instance.

Returns:
the base URI for this Jetty Process Instance.

getJmxUrl

public String getJmxUrl()
Return the JMX URL being used for this Jetty Process Instance.

Returns:
the JMX URL for this Jetty Process Instance.

overlayConfig

public void overlayConfig(String testConfigName)
                   throws IOException
Take the directory contents from ${project.basedir}/src/test/resources/${testConfigName}/ and copy it over whatever happens to be at ${jetty.home}

Parameters:
testConfigName - the src/test/resources/ directory name to use as the source diretory for the configuration we are interested in.
Throws:
IOException - if unable to copy directory.

start

public void start()
           throws IOException
Start the jetty server

Throws:
IOException - if unable to start the server.

splitAndUnescapeCommandLine

public static List<String> splitAndUnescapeCommandLine(CharSequence rawCmdLine)

setDebug

public void setDebug(boolean debug)
enable debug on the jetty process

Parameters:
debug -

stop

public void stop()
Stop the jetty server


setStartTime

public void setStartTime(long startTime,
                         TimeUnit timeUnit)


Copyright © 1995-2012 Mort Bay Consulting. All Rights Reserved.