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:
ExtensionPoint, Action, ModelObject, RootAction, junit.framework.Test
Direct Known Subclasses:
GroovyHudsonTestCase

public abstract class HudsonTestCase
extends junit.framework.TestCase
implements RootAction

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.
 
Nested classes/interfaces inherited from interface hudson.ExtensionPoint
ExtensionPoint.LegacyInstancesAreScopedToHudson
 
Field Summary
protected  String contextPath
          Where in the Server is Hudson deployed?
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.
static org.mortbay.jetty.MimeTypes MIME_TYPES
           
protected static List<ToolProperty<?>> NO_PROPERTIES
           
protected  List<Recipe.Runner> recipes
           
protected  org.mortbay.jetty.Server server
           
static int SLAVE_DEBUG_PORT
          Specify this to a TCP/IP port number to have slaves started with the debugger.
protected  List<LenientRunnable> tearDowns
          Runnables to be invoked at tearDown().
 
Constructor Summary
protected HudsonTestCase()
           
protected HudsonTestCase(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(Future<? extends R> r)
           
<R extends Run>
R
assertBuildStatusSuccess(R r)
           
 void assertEqualBeans(Object lhs, Object rhs, String properties)
          Asserts that two JavaBeans are equal as far as the given list of properties are concerned.
 void assertGoodStatus(com.gargoylesoftware.htmlunit.Page page)
          Assert that the specifed page can be served with a "good" HTTP status, eg, the page is not missing and can be served without a server error
 void assertLogContains(String substring, Run run)
          Asserts that the console output of the build contains the given substring.
 void assertStringContains(String haystack, String needle)
           
 void assertStringContains(String message, String haystack, String needle)
           
 void assertXPath(com.gargoylesoftware.htmlunit.html.DomNode page, String xpath)
          Asserts that the XPath matches the contents of a DomNode page.
 void assertXPath(com.gargoylesoftware.htmlunit.html.HtmlPage page, String xpath)
          Asserts that the XPath matches.
 void assertXPathResultsContainText(com.gargoylesoftware.htmlunit.html.DomNode page, String xpath, String needle)
           
 void assertXPathValue(com.gargoylesoftware.htmlunit.html.DomNode page, String xpath, String expectedValue)
           
 void assertXPathValueContains(com.gargoylesoftware.htmlunit.html.DomNode page, String xpath, String needle)
           
<J extends AbstractProject<J,R>,R extends AbstractBuild<J,R>>
R
buildAndAssertSuccess(J job)
           
protected  Ant.AntInstallation configureDefaultAnt()
          Extracts Ant and configures it.
protected  Maven.MavenInstallation configureDefaultMaven()
          Returns the older default Maven, while still allowing specification of other bundled Mavens.
protected  Maven.MavenInstallation configureDefaultMaven(String mavenVersion, int mavenReqVersion)
          Locates Maven2 and configure that as the only Maven in the system.
protected  void configureUpdateCenter()
          Configures the update center setting for the test.
protected  org.mortbay.jetty.security.UserRealm configureUserRealm()
          Configures a security realm for a test.
 CommandLauncher createComputerLauncher(EnvVars env)
          Creates a CommandLauncher for launching a slave locally.
protected  FreeStyleProject createFreeStyleProject()
           
protected  FreeStyleProject createFreeStyleProject(String name)
           
protected  Launcher.LocalLauncher createLocalLauncher()
          Creates Launcher.LocalLauncher.
protected  MatrixProject createMatrixProject()
           
protected  MatrixProject createMatrixProject(String name)
           
protected  MavenModuleSet createMavenProject()
          Creates a empty Maven project with an unique name.
protected  MavenModuleSet createMavenProject(String name)
          Creates a empty Maven project with the given name.
 DumbSlave createOnlineSlave()
          Create a new slave on the local host and wait for it to come onilne before returning.
 DumbSlave createOnlineSlave(Label l)
          Create a new slave on the local host and wait for it to come onilne before returning.
 DumbSlave createOnlineSlave(Label l, EnvVars env)
          Create a new slave on the local host and wait for it to come online before returning
 PretendSlave createPretendSlave(FakeLauncher faker)
           
 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.
 File createTmpDir()
          Allocates a new temporary directory for the duration of this test.
 HudsonTestCase.WebClient createWebClient()
          This is to assist Groovy test clients who are incapable of instantiating the inner classes properly.
protected  javax.servlet.ServletContext createWebServer()
          Prepares a webapp hosting environment to get ServletContext implementation that we need for testing.
<V> V
executeOnServer(Callable<V> c)
          Executes the given closure on the server, in the context of an HTTP request.
protected  com.gargoylesoftware.htmlunit.html.HtmlInput findPreviousInputElement(com.gargoylesoftware.htmlunit.html.HtmlElement current, String name)
           
protected
<T extends Descriptor<?>>
T
get(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, String s)
           
 String getDisplayName()
           
 String getIconFileName()
           
protected static String getLog(Run run)
          Get entire log file (this method is deprecated in hudson.model.Run, but in tests it is OK to load entire log).
 URL getURL()
          Returns the URL of the webapp top page.
 String getUrlName()
           
 void interactiveBreak()
          Blocks until the ENTER key is hit.
 boolean isGoodHttpStatus(int status)
          Determines whether the specifed HTTP status code is generally "good"
protected  boolean isSomethingHappening()
          Returns true if Hudson is building something or going to build something.
protected
<T> T
last(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.
 void runBare()
           
protected  void runTest()
           
protected  com.gargoylesoftware.htmlunit.Page search(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, String name)
          Submits the form by clikcing the submit button of the given name.
protected  void tearDown()
           
protected  void waitUntilNoActivity()
          Waits until Hudson finishes building everything, including those in the queue.
protected  void waitUntilNoActivityUpTo(int timeout)
          Waits until Hudson finishes building everything, including those in the queue, or fail the test if the specified timeout milliseconds is
 HudsonTestCase with(HudsonHomeLoader homeLoader)
           
 HudsonTestCase withExistingHome(File source)
           
 HudsonTestCase withNewHome()
           
 HudsonTestCase withPresetData(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, 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 String contextPath
Where in the Server is Hudson deployed?

Just like ServletContext.getContextPath(), starts with '/' but doesn't end with '/'.


tearDowns

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


recipes

protected 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 List<ToolProperty<?>> NO_PROPERTIES

SLAVE_DEBUG_PORT

public static int SLAVE_DEBUG_PORT
Specify this to a TCP/IP port number to have slaves started with the debugger.


MIME_TYPES

public static final org.mortbay.jetty.MimeTypes MIME_TYPES
Constructor Detail

HudsonTestCase

protected HudsonTestCase(String name)

HudsonTestCase

protected HudsonTestCase()
Method Detail

runBare

public void runBare()
             throws Throwable
Overrides:
runBare in class junit.framework.TestCase
Throws:
Throwable

setUp

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

configureUpdateCenter

protected void configureUpdateCenter()
                              throws Exception
Configures the update center setting for the test. By default, we load updates from local proxy to avoid network traffic as much as possible.

Throws:
Exception

tearDown

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

runTest

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

getIconFileName

public String getIconFileName()
Specified by:
getIconFileName in interface Action

getDisplayName

public String getDisplayName()
Specified by:
getDisplayName in interface Action
Specified by:
getDisplayName in interface ModelObject

getUrlName

public String getUrlName()
Specified by:
getUrlName in interface Action

newHudson

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

Throws:
Exception

createWebServer

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

Throws:
Exception

configureUserRealm

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


configureDefaultMaven

protected Maven.MavenInstallation configureDefaultMaven()
                                                 throws Exception
Returns the older default Maven, while still allowing specification of other bundled Mavens.

Throws:
Exception

configureDefaultMaven

protected Maven.MavenInstallation configureDefaultMaven(String mavenVersion,
                                                        int mavenReqVersion)
                                                 throws Exception
Locates Maven2 and configure that as the only Maven in the system.

Throws:
Exception

configureDefaultAnt

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

Throws:
Exception

createFreeStyleProject

protected FreeStyleProject createFreeStyleProject()
                                           throws IOException
Throws:
IOException

createFreeStyleProject

protected FreeStyleProject createFreeStyleProject(String name)
                                           throws IOException
Throws:
IOException

createMatrixProject

protected MatrixProject createMatrixProject()
                                     throws IOException
Throws:
IOException

createMatrixProject

protected MatrixProject createMatrixProject(String name)
                                     throws IOException
Throws:
IOException

createMavenProject

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

Throws:
IOException
See Also:
configureDefaultMaven()

createMavenProject

protected MavenModuleSet createMavenProject(String name)
                                     throws IOException
Creates a empty Maven project with the given name.

Throws:
IOException
See Also:
configureDefaultMaven()

createLocalLauncher

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


createTmpDir

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

Throws:
IOException

createSlave

public DumbSlave createSlave()
                      throws Exception
Throws:
Exception

createSlave

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

Throws:
Exception

getURL

public URL getURL()
           throws IOException
Returns the URL of the webapp top page. URL ends with '/'.

Throws:
IOException

createSlave

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

Throws:
Exception

createPretendSlave

public PretendSlave createPretendSlave(FakeLauncher faker)
                                throws Exception
Throws:
Exception

createComputerLauncher

public CommandLauncher createComputerLauncher(EnvVars env)
                                       throws URISyntaxException,
                                              MalformedURLException
Creates a CommandLauncher for launching a slave locally.

Parameters:
env - Environment variables to add to the slave process. Can be null.
Throws:
URISyntaxException
MalformedURLException

createOnlineSlave

public DumbSlave createOnlineSlave()
                            throws Exception
Create a new slave on the local host and wait for it to come onilne before returning.

Throws:
Exception

createOnlineSlave

public DumbSlave createOnlineSlave(Label l)
                            throws Exception
Create a new slave on the local host and wait for it to come onilne before returning.

Throws:
Exception

createOnlineSlave

public DumbSlave createOnlineSlave(Label l,
                                   EnvVars env)
                            throws Exception
Create a new slave on the local host and wait for it to come online before returning

Throws:
Exception

interactiveBreak

public void interactiveBreak()
                      throws 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:
Exception

last

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


pause

protected void pause()
              throws 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:
IOException

search

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

Throws:
Exception

assertBuildStatus

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

Throws:
Exception

isGoodHttpStatus

public boolean isGoodHttpStatus(int status)
Determines whether the specifed HTTP status code is generally "good"


assertGoodStatus

public void assertGoodStatus(com.gargoylesoftware.htmlunit.Page page)
Assert that the specifed page can be served with a "good" HTTP status, eg, the page is not missing and can be served without a server error

Parameters:
page -

assertBuildStatusSuccess

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

assertBuildStatusSuccess

public <R extends Run> R assertBuildStatusSuccess(Future<? extends R> r)
                                       throws Exception
Throws:
Exception

buildAndAssertSuccess

public <J extends AbstractProject<J,R>,R extends AbstractBuild<J,R>> R buildAndAssertSuccess(J job)
                                                   throws Exception
Throws:
Exception

assertLogContains

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

Throws:
Exception

getLog

protected static String getLog(Run run)
                        throws IOException
Get entire log file (this method is deprecated in hudson.model.Run, but in tests it is OK to load entire log).

Throws:
IOException

assertXPath

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


assertXPath

public void assertXPath(com.gargoylesoftware.htmlunit.html.DomNode page,
                        String xpath)
Asserts that the XPath matches the contents of a DomNode page. This variant of assertXPath(HtmlPage page, String xpath) allows us to examine XmlPages.

Parameters:
page -
xpath -

assertXPathValue

public void assertXPathValue(com.gargoylesoftware.htmlunit.html.DomNode page,
                             String xpath,
                             String expectedValue)

assertXPathValueContains

public void assertXPathValueContains(com.gargoylesoftware.htmlunit.html.DomNode page,
                                     String xpath,
                                     String needle)

assertXPathResultsContainText

public void assertXPathResultsContainText(com.gargoylesoftware.htmlunit.html.DomNode page,
                                          String xpath,
                                          String needle)

assertStringContains

public void assertStringContains(String message,
                                 String haystack,
                                 String needle)

assertStringContains

public void assertStringContains(String haystack,
                                 String needle)

submit

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

Throws:
Exception

submit

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

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

findPreviousInputElement

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

getButtonByCaption

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

createTaskListener

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


assertEqualBeans

public void assertEqualBeans(Object lhs,
                             Object rhs,
                             String properties)
                      throws 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:
Exception
Since:
1.297

get

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


isSomethingHappening

protected boolean isSomethingHappening()
Returns true if Hudson is building something or going to build something.


waitUntilNoActivity

protected void waitUntilNoActivity()
                            throws Exception
Waits until Hudson finishes building everything, including those in the queue.

This method uses a default time out to prevent infinite hang in the automated test execution environment.

Throws:
Exception

waitUntilNoActivityUpTo

protected void waitUntilNoActivityUpTo(int timeout)
                                throws Exception
Waits until Hudson finishes building everything, including those in the queue, or fail the test if the specified timeout milliseconds is

Throws:
Exception

recipe

protected void recipe()
               throws 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:
Exception

recipeLoadCurrentPlugin

protected void recipeLoadCurrentPlugin()
                                throws 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:
Exception

withNewHome

public HudsonTestCase withNewHome()

withExistingHome

public HudsonTestCase withExistingHome(File source)
                                throws Exception
Throws:
Exception

withPresetData

public HudsonTestCase withPresetData(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)

executeOnServer

public <V> V executeOnServer(Callable<V> c)
                  throws Exception
Executes the given closure on the server, in the context of an HTTP request. This is useful for testing some methods that require StaplerRequest and StaplerResponse.

The closure will get the request and response as parameters.

Throws:
Exception

createWebClient

public HudsonTestCase.WebClient createWebClient()
This is to assist Groovy test clients who are incapable of instantiating the inner classes properly.



Copyright © 2010. All Rights Reserved.