Skip to content

Commit

Permalink
switch to testNG, improve error handling, change package structure an…
Browse files Browse the repository at this point in the history
…d group id
  • Loading branch information
jsimone committed Jan 12, 2012
1 parent 47dccae commit 8a6352c
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ tomcat.8080
.project
.settings/
tomcat.*/
test-output/
51 changes: 51 additions & 0 deletions README.md
Expand Up @@ -22,3 +22,54 @@ or
java -jar target/tomcat-runner.jar help

Prints out all arguments accepted

## Using with Maven in your project

You can use the Maven dependency plugin to download tomcat-runner as part of your build. This will eliminate the need for any external dependencies other than those specified in your build to run your application.

### pom.xml

Add the following to your pom.xml:

...
<repositories>
<repository>
<id>tomcat-runner-repo</id>
<name>tomcat runner repository on GitHub</name>
<url>http://jsimone.github.com/tomcat-runner/repository/</url>
</repository>
</repositories>
...
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>heroku.runner</groupId>
<artifactId>tomcat-runner</artifactId>
<version>0.0.5</version>
<destFileName>tomcat-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>

### launching

Now when you run `maven package` tomcat runner will be downloaded for you. You can then launch your application with:

$ java -jar target/dependency/tomcat-runner.jar target/<appname>.war
16 changes: 8 additions & 8 deletions pom.xml
@@ -1,9 +1,9 @@
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>heroku.runner</groupId>
<groupId>tomcat.runner</groupId>
<artifactId>tomcat-runner</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
<name>tomcat-runner</name>
<url>http://maven.apache.org</url>

Expand Down Expand Up @@ -45,12 +45,12 @@
<artifactId>tomcat-jsp-api</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>tomcat-runner-no-dep</finalName>
Expand Down
@@ -1,4 +1,4 @@
package com.heroku.launch;
package tomcat.runner.launch;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -19,12 +19,17 @@ public class ArgParser {
* @return
* @throws ArgumentNotFoundException
*/
public static Map<Argument, String> parseArgs(String[] args) throws ArgumentNotFoundException {
public static Map<Argument, String> parseArgs(String[] args) throws ArgumentNotFoundException, MissingAppException {
Map<Argument, String> argMap = new HashMap<Argument, String>();

for(int i=0; i < args.length-1; i+=2) {
argMap.put(Argument.getArgFor(args[i]), args[i+1]);
}

//if there are even number of arguments then we didn't define the app location
if(args.length % 2 == 0 || args[args.length-1].startsWith("--")) {
throw new MissingAppException();
}

argMap.put(Argument.APPLICATION_DIR, args[args.length-1]);

Expand Down
@@ -1,4 +1,4 @@
package com.heroku.launch;
package tomcat.runner.launch;

/**
* The argument enum holds which arguments are supported.
Expand All @@ -9,6 +9,7 @@
public enum Argument {
SESSION_TIMEOUT ("--session-timeout", "The number of minutes of inactivity before a user's session is timed out"),
PORT ("--port", "The port that the server will accept http requests on"),
CONTEXT_XML ("--context_xml", "The parth to the context xml to use"),
APPLICATION_DIR ("", "");

private String argName;
Expand Down
@@ -1,4 +1,4 @@
package com.heroku.launch;
package tomcat.runner.launch;

/**
* ArgumentNotFoundException is thrown when an argument that isn't understood
Expand Down
@@ -1,5 +1,6 @@
package com.heroku.launch;
package tomcat.runner.launch;
import java.io.File;
import java.net.URL;
import java.util.Map;

import org.apache.catalina.Context;
Expand All @@ -22,14 +23,14 @@ public static void printHelp() {
System.out.println("Usage: java -jar tomcat-runner.jar [arguments...] path/to/webapp");
System.out.println("Arguments:");
for (Argument argument : Argument.values()) {
System.out.println(argument.argName() + " " + argument.helpText());
System.out.format("%-30s%-90s%n", argument.argName(), argument.helpText());
}
}

public static void main(String[] args) throws Exception {

//print help text when asked for
if("help".equals(args[0]) || "--help".equals(args[0])) {
if(args.length == 0 || "help".equals(args[0]) || "--help".equals(args[0])) {
printHelp();
System.exit(0);
}
Expand All @@ -42,6 +43,10 @@ public static void main(String[] args) throws Exception {
System.out.println("Unexpected Argument: " + e.getArgName());
System.out.println("For usage information run `java -jar tomcat-runner.jar help`");
System.exit(1);
} catch (MissingAppException e) {
System.out.println("Application location not defined");
System.out.println("For usage information run `java -jar tomcat-runner.jar help`");
System.exit(1);
}

Tomcat tomcat = new Tomcat();
Expand All @@ -60,7 +65,7 @@ public static void main(String[] args) throws Exception {
ctx.setSessionTimeout(Integer.valueOf(argMap.get(Argument.SESSION_TIMEOUT)));
}

System.out.println("configuring app at: " + new File(argMap.get(Argument.APPLICATION_DIR)).getAbsolutePath());
System.out.println("deploying app from: " + new File(argMap.get(Argument.APPLICATION_DIR)).getAbsolutePath());

//start the server
tomcat.start();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tomcat/runner/launch/MissingAppException.java
@@ -0,0 +1,5 @@
package tomcat.runner.launch;

public class MissingAppException extends Exception {

}
39 changes: 39 additions & 0 deletions src/main/test/tomcat/runner/launch/ArgParserNegativeTest.java
@@ -0,0 +1,39 @@
package tomcat.runner.launch;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import tomcat.runner.launch.ArgParser;
import tomcat.runner.launch.Argument;
import tomcat.runner.launch.ArgumentNotFoundException;
import tomcat.runner.launch.MissingAppException;

public class ArgParserNegativeTest {

@DataProvider(name = "Negative-Test-Provider")
public Object[][] parameterIntTestProvider() {
return new Object[][]{
{ new String[] {"--fake-arg", "45", "/path/to/project"}, ArgumentNotFoundException.class },
{ new String[] {Argument.SESSION_TIMEOUT.argName(), "45", "--fake-arg", "test", "/path/to/project"}, ArgumentNotFoundException.class },
{ new String[] {Argument.SESSION_TIMEOUT.argName(), "45", "--fake-arg", "/path/to/project"}, ArgumentNotFoundException.class },
{ new String[] {Argument.SESSION_TIMEOUT.argName(), "45", "test", "/path/to/project"}, ArgumentNotFoundException.class },
{ new String[] {Argument.SESSION_TIMEOUT.argName(), "--fake-arg", "test", "/path/to/project"}, ArgumentNotFoundException.class },
{ new String[] {"/path/to/project", Argument.SESSION_TIMEOUT.argName()}, ArgumentNotFoundException.class },
{ new String[] {Argument.SESSION_TIMEOUT.argName(), "--fake-arg", "45", "test", "/path/to/project"}, ArgumentNotFoundException.class },
{ new String[] {Argument.SESSION_TIMEOUT.argName(), "45", Argument.PORT.argName(), "9090"}, MissingAppException.class },
{ new String[] {Argument.SESSION_TIMEOUT.argName(), "45", Argument.PORT.argName()}, MissingAppException.class }
};
}

@Test(dataProvider = "Negative-Test-Provider")
public void testFakeArgument(String[] args, Class<Exception> excpectedException) {
try {
ArgParser.parseArgs(args);
Assert.fail("Parsing of arguments should have thrown exception");
} catch (Exception e) {
Assert.assertTrue(e.getClass().isAssignableFrom(excpectedException), "Wrong exception thrown for negative test");
}
}

}
34 changes: 34 additions & 0 deletions src/main/test/tomcat/runner/launch/ArgParserTest.java
@@ -0,0 +1,34 @@
package tomcat.runner.launch;

import java.util.Map;

import org.testng.Assert;
import org.testng.annotations.Test;

import tomcat.runner.launch.ArgParser;
import tomcat.runner.launch.Argument;
import tomcat.runner.launch.ArgumentNotFoundException;
import tomcat.runner.launch.MissingAppException;


public class ArgParserTest {

@Test
public void testArgumentParsingWithArg() throws ArgumentNotFoundException, MissingAppException {
String[] args = {Argument.SESSION_TIMEOUT.argName(), "45", Argument.PORT.argName(), "8888", "/path/to/project"};
Map<Argument, String> argMap = ArgParser.parseArgs(args);

Assert.assertEquals(argMap.get(Argument.SESSION_TIMEOUT), "45", "Expected session timeout to match that which was set");
Assert.assertEquals(argMap.get(Argument.PORT), "8888", "Expected port to match that which was set");
Assert.assertEquals(argMap.get(Argument.APPLICATION_DIR), "/path/to/project", "Expected directory to math that which was set");
}

@Test
public void testArgumentParsingNoArg() throws ArgumentNotFoundException, MissingAppException {
String[] args = {"/path/to/project"};
Map<Argument, String> argMap = ArgParser.parseArgs(args);

Assert.assertEquals(argMap.get(Argument.APPLICATION_DIR), "/path/to/project", "Expected directory to math that which was set");
}

}

0 comments on commit 8a6352c

Please sign in to comment.