org.jvnet.hudson.test
Class HudsonTestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.jvnet.hudson.test.HudsonTestCase
All Implemented Interfaces:
junit.framework.Test

public abstract class HudsonTestCase
extends junit.framework.TestCase

Base class for all Hudson test cases.

Author:
Kohsuke Kawaguchi
See Also:
Wiki article about unit testing in Hudson

Nested Class Summary
 class HudsonTestCase.WebClient
          Extends WebClient and provide convenience methods for accessing Hudson.
 
Field Summary
protected  java.lang.String contextPath
          Where in the Server is Hudson deploed?
protected  TestEnvironment env
           
protected  HudsonHomeLoader homeLoader
           
 Hudson hudson
           
protected  JavaScriptDebugger jsDebugger
          JavaScript "debugger" that provides you information about the JavaScript call stack and the current values of the local variables in those stack frame.
protected  int localPort
          TCP/IP port that the server is listening on.
protected static java.util.List<ToolProperty<?>> NO_PROPERTIES
           
protected  java.util.List<Recipe.Runner> recipes
           
protected  org.mortbay.jetty.Server server
           
protected  java.util.List<LenientRunnable> tearDowns
          Runnables to be invoked at tearDown().
 
Constructor Summary
protected HudsonTestCase()
           
protected HudsonTestCase(java.lang.String name)
           
 
Method Summary
<R extends Run>
R
assertBuildStatus(Result status, R r)
          Asserts that the outcome of the build is a specific outcome.
<R extends Run>
R
assertBuildStatusSuccess(R r)
           
 void assertEqualBeans(java.lang.Object lhs, java.lang.Object rhs, java.lang.String properties)
          Asserts that two JavaBeans are equal as far as the given list of properties are concerned.
 void assertLogContains(java.lang.String substring, Run run)
          Asserts that the console output of the build contains the given substring.
 void assertXPath(com.gargoylesoftware.htmlunit.html.HtmlPage page, java.lang.String xpath)
          Asserts that the XPath matches.
protected  Ant.AntInstallation configureDefaultAnt()
          Extracts Ant and configures it.
protected  Maven.MavenInstallation configureDefaultMaven()
          Locates Maven2 and configure that as the only Maven in the system.
protected  org.mortbay.jetty.security.UserRealm configureUserRealm()
          Configures a security realm for a test.
protected  FreeStyleProject createFreeStyleProject()
           
protected  FreeStyleProject createFreeStyleProject(java.lang.String name)
           
protected  Launcher.LocalLauncher createLocalLauncher()
          Creates Launcher.LocalLauncher.
protected  MatrixProject createMatrixProject()
           
protected  MatrixProject createMatrixProject(java.lang.String name)
           
protected  hudson.maven.MavenModuleSet createMavenProject()
          Creates a empty Maven project with an unique name.
protected  hudson.maven.MavenModuleSet createMavenProject(java.lang.String name)
          Creates a empty Maven project with the given name.
 DumbSlave createSlave()
           
 DumbSlave createSlave(Label l)
          Creates and launches a new slave on the local host.
 DumbSlave createSlave(Label l, EnvVars env)
          Creates a slave with certain additional environment variables
 TaskListener createTaskListener()
          Creates a TaskListener connected to stdout.
 java.io.File createTmpDir()
          Allocates a new temporary directory for the duration of this test.
protected  javax.servlet.ServletContext createWebServer()
          Prepares a webapp hosting environment to get ServletContext implementation that we need for testing.
protected  com.gargoylesoftware.htmlunit.html.HtmlInput findPreviousInputElement(com.gargoylesoftware.htmlunit.html.HtmlElement current, java.lang.String name)
           
protected
<T extends Descriptor<?>>
T
get(java.lang.Class<T> d)
          Gets the descriptor instance of the current Hudson by its type.
protected  com.gargoylesoftware.htmlunit.html.HtmlButton getButtonByCaption(com.gargoylesoftware.htmlunit.html.HtmlForm f, java.lang.String s)
           
 void interactiveBreak()
          Blocks until the ENTER key is hit.
protected
<T> T
last(java.util.List<T> items)
          Returns the last item in the list.
protected  Hudson newHudson()
          Creates a new instance of Hudson.
protected  void pause()
          Pauses the execution until ENTER is hit in the console.
protected  void recipe()
          Called during the setUp() to give a test case an opportunity to control the test environment in which Hudson is run.
protected  void recipeLoadCurrentPlugin()
          If this test harness is launched for a Hudson plugin, locate the target/test-classes/the.hpl and add a recipe to install that to the new Hudson.
protected  void runTest()
           
protected  com.gargoylesoftware.htmlunit.Page search(java.lang.String q)
          Performs a search from the search box.
protected  void setUp()
           
 com.gargoylesoftware.htmlunit.html.HtmlPage submit(com.gargoylesoftware.htmlunit.html.HtmlForm form)
          Submits the form.
 com.gargoylesoftware.htmlunit.html.HtmlPage submit(com.gargoylesoftware.htmlunit.html.HtmlForm form, java.lang.String name)
          Submits the form by clikcing the submit button of the given name.
protected  void tearDown()
           
 HudsonTestCase with(HudsonHomeLoader homeLoader)
           
 HudsonTestCase withExistingHome(java.io.File source)
           
 HudsonTestCase withNewHome()
           
 HudsonTestCase withPresetData(java.lang.String name)
          Declares that this test case expects to start with one of the preset data sets.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

hudson

public Hudson hudson

env

protected final TestEnvironment env

homeLoader

protected HudsonHomeLoader homeLoader

localPort

protected int localPort
TCP/IP port that the server is listening on.


server

protected org.mortbay.jetty.Server server

contextPath

protected java.lang.String contextPath
Where in the Server is Hudson deploed?


tearDowns

protected java.util.List<LenientRunnable> tearDowns
Runnables to be invoked at tearDown().


recipes

protected java.util.List<Recipe.Runner> recipes

jsDebugger

protected JavaScriptDebugger jsDebugger
JavaScript "debugger" that provides you information about the JavaScript call stack and the current values of the local variables in those stack frame.

Unlike Java debugger, which you as a human interfaces directly and interactively, this JavaScript debugger is to be interfaced by your program (or through the expression evaluation capability of your Java debugger.)


NO_PROPERTIES

protected static final java.util.List<ToolProperty<?>> NO_PROPERTIES
Constructor Detail

HudsonTestCase

protected HudsonTestCase(java.lang.String name)

HudsonTestCase

protected HudsonTestCase()
Method Detail

setUp

protected void setUp()
              throws java.lang.Exception
Overrides:
setUp in class junit.framework.TestCase
Throws:
java.lang.Exception

tearDown

protected void tearDown()
                 throws java.lang.Exception
Overrides:
tearDown in class junit.framework.TestCase
Throws:
java.lang.Exception

runTest

protected void runTest()
                throws java.lang.Throwable
Overrides:
runTest in class junit.framework.TestCase
Throws:
java.lang.Throwable

newHudson

protected Hudson newHudson()
                    throws java.lang.Exception
Creates a new instance of Hudson. If the derived class wants to create it in a different way, you can override it.

Throws:
java.lang.Exception

createWebServer

protected javax.servlet.ServletContext createWebServer()
                                                throws java.lang.Exception
Prepares a webapp hosting environment to get ServletContext implementation that we need for testing.

Throws:
java.lang.Exception

configureUserRealm

protected org.mortbay.jetty.security.UserRealm configureUserRealm()
Configures a security realm for a test.


configureDefaultMaven

protected Maven.MavenInstallation configureDefaultMaven()
                                                 throws java.lang.Exception
Locates Maven2 and configure that as the only Maven in the system.

Throws:
java.lang.Exception

configureDefaultAnt

protected Ant.AntInstallation configureDefaultAnt()
                                           throws java.lang.Exception
Extracts Ant and configures it.

Throws:
java.lang.Exception

createFreeStyleProject

protected FreeStyleProject createFreeStyleProject()
                                           throws java.io.IOException
Throws:
java.io.IOException

createFreeStyleProject

protected FreeStyleProject createFreeStyleProject(java.lang.String name)
                                           throws java.io.IOException
Throws:
java.io.IOException

createMatrixProject

protected MatrixProject createMatrixProject()
                                     throws java.io.IOException
Throws:
java.io.IOException

createMatrixProject

protected MatrixProject createMatrixProject(java.lang.String name)
                                     throws java.io.IOException
Throws:
java.io.IOException

createMavenProject

protected hudson.maven.MavenModuleSet createMavenProject()
                                                  throws java.io.IOException
Creates a empty Maven project with an unique name.

Throws:
java.io.IOException
See Also:
configureDefaultMaven()

createMavenProject

protected hudson.maven.MavenModuleSet createMavenProject(java.lang.String name)
                                                  throws java.io.IOException
Creates a empty Maven project with the given name.

Throws:
java.io.IOException
See Also:
configureDefaultMaven()

createLocalLauncher

protected Launcher.LocalLauncher createLocalLauncher()
Creates Launcher.LocalLauncher. Useful for launching processes.


createTmpDir

public java.io.File createTmpDir()
                          throws java.io.IOException
Allocates a new temporary directory for the duration of this test.

Throws:
java.io.IOException

createSlave

public DumbSlave createSlave()
                      throws java.lang.Exception
Throws:
java.lang.Exception

createSlave

public DumbSlave createSlave(Label l)
                      throws java.lang.Exception
Creates and launches a new slave on the local host.

Throws:
java.lang.Exception

createSlave

public DumbSlave createSlave(Label l,
                             EnvVars env)
                      throws java.lang.Exception
Creates a slave with certain additional environment variables

Throws:
java.lang.Exception

interactiveBreak

public void interactiveBreak()
                      throws java.lang.Exception
Blocks until the ENTER key is hit. This is useful during debugging a test so that one can inspect the state of Hudson through the web browser.

Throws:
java.lang.Exception

last

protected <T> T last(java.util.List<T> items)
Returns the last item in the list.


pause

protected void pause()
              throws java.io.IOException
Pauses the execution until ENTER is hit in the console.

This is often very useful so that you can interact with Hudson from an browser, while developing a test case.

Throws:
java.io.IOException

search

protected com.gargoylesoftware.htmlunit.Page search(java.lang.String q)
                                             throws java.lang.Exception
Performs a search from the search box.

Throws:
java.lang.Exception

assertBuildStatus

public <R extends Run> R assertBuildStatus(Result status,
                                           R r)
                                throws java.lang.Exception
Asserts that the outcome of the build is a specific outcome.

Throws:
java.lang.Exception

assertBuildStatusSuccess

public <R extends Run> R assertBuildStatusSuccess(R r)
                                       throws java.lang.Exception
Throws:
java.lang.Exception

assertLogContains

public void assertLogContains(java.lang.String substring,
                              Run run)
                       throws java.lang.Exception
Asserts that the console output of the build contains the given substring.

Throws:
java.lang.Exception

assertXPath

public void assertXPath(com.gargoylesoftware.htmlunit.html.HtmlPage page,
                        java.lang.String xpath)
Asserts that the XPath matches.


submit

public com.gargoylesoftware.htmlunit.html.HtmlPage submit(com.gargoylesoftware.htmlunit.html.HtmlForm form)
                                                   throws java.lang.Exception
Submits the form.

Throws:
java.lang.Exception

submit

public com.gargoylesoftware.htmlunit.html.HtmlPage submit(com.gargoylesoftware.htmlunit.html.HtmlForm form,
                                                          java.lang.String name)
                                                   throws java.lang.Exception
Submits the form by clikcing the submit button of the given name.

Parameters:
name - This corresponds to the @name of <f:submit />
Throws:
java.lang.Exception

findPreviousInputElement

protected com.gargoylesoftware.htmlunit.html.HtmlInput findPreviousInputElement(com.gargoylesoftware.htmlunit.html.HtmlElement current,
                                                                                java.lang.String name)

getButtonByCaption

protected com.gargoylesoftware.htmlunit.html.HtmlButton getButtonByCaption(com.gargoylesoftware.htmlunit.html.HtmlForm f,
                                                                           java.lang.String s)

createTaskListener

public TaskListener createTaskListener()
Creates a TaskListener connected to stdout.


assertEqualBeans

public void assertEqualBeans(java.lang.Object lhs,
                             java.lang.Object rhs,
                             java.lang.String properties)
                      throws java.lang.Exception
Asserts that two JavaBeans are equal as far as the given list of properties are concerned.

This method takes two objects that have properties (getXyz, isXyz, or just the public xyz field), and makes sure that the property values for each given property are equals (by using Assert.assertEquals(Object, Object))

Property values can be null on both objects, and that is OK, but passing in a property that doesn't exist will fail an assertion.

This method is very convenient for comparing a large number of properties on two objects, for example to verify that the configuration is identical after a config screen roundtrip.

Parameters:
lhs - One of the two objects to be compared.
rhs - The other object to be compared
properties - ','-separated list of property names that are compared.
Throws:
java.lang.Exception
Since:
1.297

get

protected <T extends Descriptor<?>> T get(java.lang.Class<T> d)
Gets the descriptor instance of the current Hudson by its type.


recipe

protected void recipe()
               throws java.lang.Exception
Called during the setUp() to give a test case an opportunity to control the test environment in which Hudson is run.

One could override this method and call a series of withXXX methods, or you can use the annotations with Recipe meta-annotation.

Throws:
java.lang.Exception

recipeLoadCurrentPlugin

protected void recipeLoadCurrentPlugin()
                                throws java.lang.Exception
If this test harness is launched for a Hudson plugin, locate the target/test-classes/the.hpl and add a recipe to install that to the new Hudson.

This file is created by maven-hpi-plugin at the testCompile phase when the current packaging is hpi.

Throws:
java.lang.Exception

withNewHome

public HudsonTestCase withNewHome()

withExistingHome

public HudsonTestCase withExistingHome(java.io.File source)
                                throws java.lang.Exception
Throws:
java.lang.Exception

withPresetData

public HudsonTestCase withPresetData(java.lang.String name)
Declares that this test case expects to start with one of the preset data sets. See https://svn.dev.java.net/svn/hudson/trunk/hudson/main/test/src/main/preset-data/ for available datasets and what they mean.


with

public HudsonTestCase with(HudsonHomeLoader homeLoader)


Copyright © 2009. All Rights Reserved.