org.hd.d.pg2k.test.dev
Class Main

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.hd.d.pg2k.test.dev.Main
All Implemented Interfaces:
junit.framework.Test

public final class Main
extends junit.framework.TestCase

This is the main entry point for development-time tests. This is also a valid top-level JUnit test class.

They can be run from the command-line or IDE to test most self-contained features, or from a WAR to allow self-test in an HTTP environment.

(In a WAR environment the out/err values and exitWhenDone/inWAR flags should be set appropriately.)


Nested Class Summary
static class Main.TestThreadFactory
          A factory for creating daemon pool threads.
 
Field Summary
private static boolean _accessToFilesystem
          If true, this indicates that we have access to the filesystem for tests that need it.
private static java.util.concurrent.locks.Lock _rtLock
          Lock to prevent re-entering the runTests() method; never null.
private static int AVAILABLE_PROCESSORS
          Number of available CPUs.
(package private) static java.util.concurrent.ExecutorService computeIntensiveTestThreadPool
          Shared thread pool for compute-bound tests.
private static java.io.PrintStream err
          The PrintStream to direct standard output to.
private static java.net.URL loopbackURL
          URL for our mount-point if running as a WAR; null if none.
private static java.io.PrintStream out
          The PrintStream to direct standard output to.
private static java.lang.Object servletContext
          Current ServletContext if running in a WAR; null if none.
private static java.lang.Class<?>[] testClasses
          List of classes containing test suites to be run.
 
Constructor Summary
Main()
           
 
Method Summary
private static void flattenSuite(junit.framework.TestSuite dest, junit.framework.TestSuite src)
          Flattens test case structure from src to dest.
static java.io.PrintStream getErr()
          Get stream to use in lieu of System.err for jUnit tests; never null.
static java.net.URL getLoopbackURL()
          Get URL for our mount-point if running as a WAR; or null if none.
static java.io.PrintStream getOut()
          Get stream to use in lieu of System.out for jUnit tests; never null.
static java.lang.Object getServletContext()
          Get current ServletContext if running in a WAR; null if none.
static boolean isAccessToFilesystem()
          If true, this indicates that we have access to the filesystem for tests that need it.
static void main(java.lang.String[] args)
          Main entry point from command-line.
static boolean runTests(boolean forever, java.io.PrintStream o, java.io.PrintStream e, java.net.URL loopbackURL, java.lang.Object servletContext)
          Actually runs the unit tests.
static void runTestsFromServlet(java.io.Writer writer, java.net.URL loopbackURL, java.lang.Object servletContext)
          Run the unit tests from a servlet.
private static void setErr(java.io.PrintStream e)
          Set the err stream; never null.
static void setLoopbackURL(java.net.URL loopbackURL)
          Set URL for our mount-point if running as a WAR; or null if none.
private static void setOut(java.io.PrintStream o)
          Set the out stream; never null.
static void setServletContext(java.lang.Object servletContext)
          Set current ServletContext if running in a WAR; null if none.
static junit.framework.TestSuite suite()
          This returns a suite of all the top-level tests that we want to perform.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, setUp, tearDown, 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

_accessToFilesystem

private static final boolean _accessToFilesystem
If true, this indicates that we have access to the filesystem for tests that need it.


loopbackURL

private static volatile java.net.URL loopbackURL
URL for our mount-point if running as a WAR; null if none. This is used for loop-back testing when running in a WAR.

Marked volatile for thread-safe lock-free access.


servletContext

private static volatile java.lang.Object servletContext
Current ServletContext if running in a WAR; null if none. Declared as an Object to avoid making Main depend on Servlet API.

Marked volatile for thread-safe lock-free access.


out

private static volatile java.io.PrintStream out
The PrintStream to direct standard output to. Defaults to System.out. Could be over-ridden by a servlet etc to send output back to the client.

This is package-visible only for safety.

Marked volatile for thread-safe lock-free access.


err

private static volatile java.io.PrintStream err
The PrintStream to direct standard output to. Defaults to System.out. Could be over-ridden by a servlet etc to send output back to the client.

This is package-visible only for safety.

Marked volatile for thread-safe lock-free access.


_rtLock

private static final java.util.concurrent.locks.Lock _rtLock
Lock to prevent re-entering the runTests() method; never null.


AVAILABLE_PROCESSORS

private static final int AVAILABLE_PROCESSORS
Number of available CPUs.


computeIntensiveTestThreadPool

static final java.util.concurrent.ExecutorService computeIntensiveTestThreadPool
Shared thread pool for compute-bound tests. Suitable for almost-entirely CPU-bound threads with the pool size exactly the number of CPUs available to this JVM (at creation of this pool).

Some work can be queued for this pool to maximise throughput and excess work (with no free pool threads or queue space available) will be run directly in the caller's thread. In simple uses this will allow for a little parallel slackness.

This puts an upper bound on Thread resources (eg memory for stack) consumed by threads in this pool.

The threads in the pool are daemon threads, so should not prevent the JVM from exiting.

Only available to test routines in this package.


testClasses

private static final java.lang.Class<?>[] testClasses
List of classes containing test suites to be run. They should be run in the order specified by default.

Constructor Detail

Main

public Main()
Method Detail

isAccessToFilesystem

public static boolean isAccessToFilesystem()
If true, this indicates that we have access to the filesystem for tests that need it. We deduce this by testing if we can load the local properties file, which we only test at most once per run to avoid lots of log messages. Access to the file can be arranged by having the tests run in the correct working directory, or by setting a system property to indicate its location.

Tests which expect access to configuration files, test data, etc, should avoid running when this does not return true.


getLoopbackURL

public static java.net.URL getLoopbackURL()
Get URL for our mount-point if running as a WAR; or null if none. This is used for loop-back testing when running in a WAR.

Usually a top-level URL such as "http://localhost:8080/".


setLoopbackURL

public static void setLoopbackURL(java.net.URL loopbackURL)
Set URL for our mount-point if running as a WAR; or null if none. This is used for loop-back testing when running in a WAR.


getServletContext

public static java.lang.Object getServletContext()
Get current ServletContext if running in a WAR; null if none. Declared as an Object to avoid making Main depend on Servlet API.


setServletContext

public static void setServletContext(java.lang.Object servletContext)
Set current ServletContext if running in a WAR; null if none. Declared as an Object to avoid making Main depend on Servlet API.


getOut

public static java.io.PrintStream getOut()
Get stream to use in lieu of System.out for jUnit tests; never null.


setOut

private static void setOut(java.io.PrintStream o)
Set the out stream; never null.


getErr

public static java.io.PrintStream getErr()
Get stream to use in lieu of System.err for jUnit tests; never null.


setErr

private static void setErr(java.io.PrintStream e)
Set the err stream; never null.


main

public static final void main(java.lang.String[] args)
Main entry point from command-line. Normally we try to run all tests and see how many produced errors.

If the first arg is -forever then the tests are run continuously until the process is killed.

If one or more test classes are given on the command-line then those tests alone are run, else all tests are run.

Only one top-level test routine may run at any one time.


runTestsFromServlet

public static void runTestsFromServlet(java.io.Writer writer,
                                       java.net.URL loopbackURL,
                                       java.lang.Object servletContext)
Run the unit tests from a servlet. Takes a Writer argument (assumed to be the servlet output stream) and wraps it to take output from both err and out streams.


flattenSuite

private static void flattenSuite(junit.framework.TestSuite dest,
                                 junit.framework.TestSuite src)
Flattens test case structure from src to dest.


runTests

public static boolean runTests(boolean forever,
                               java.io.PrintStream o,
                               java.io.PrintStream e,
                               java.net.URL loopbackURL,
                               java.lang.Object servletContext)
Actually runs the unit tests. This is synchronized to only also one set of tests to be run at once to ensure consistent behaviour and limit resource usage if run from a servlet.

Output and error streams supplied replace the normal ones for the duration of the run; they must not be null. The previous values are restored upon completion.

Parameters:
forever - if true, tests are run repeatedly until an error is found
Returns:
true if all tests ran OK

suite

public static junit.framework.TestSuite suite()
This returns a suite of all the top-level tests that we want to perform. This returns a flattened suite of tests.


DHD Multimedia Gallery V1.57.21

Copyright (c) 1996-2011, Damon Hart-Davis. All rights reserved.