001: /*
002: * $Id$
003: */
004: package com.mycompany.myproject.tests;
005:
006: import java.io.File;
007:
008: import junit.framework.Test;
009: import junit.framework.TestCase;
010: import junit.framework.TestSuite;
011:
012: import org.xins.common.servlet.container.HTTPServletHandler;
013:
014: /**
015: * Testcase that includes all the tests for the myproject API.
016: *
017: * @version $Revision$ $Date$
018: */
019: public class APITests extends TestCase {
020:
021: /**
022: * The Servlet server running the API. The value is <code>null</code> if the server is not started.
023: */
024: private static HTTPServletHandler API_SERVER;
025:
026: /**
027: * Flag that indicates that the API has been started.
028: */
029: private static boolean API_STARTED = false;
030:
031: /**
032: * Constructs a new <code>APITests</code> test suite with
033: * the specified name. The name will be passed to the superconstructor.
034: *
035: * @param name
036: * the name for this test suite.
037: */
038: public APITests(String name) {
039: super (name);
040: }
041:
042: /**
043: * Returns a test suite with all test cases defined by this class.
044: *
045: * @return
046: * the test suite, never <code>null</code>.
047: */
048: public static Test suite() {
049:
050: TestSuite suite = new TestSuite();
051:
052: if ("true".equals(System.getProperty("test.start.server"))) {
053: suite.addTestSuite(StartServer.class);
054: }
055: // Add all tests
056: suite.addTestSuite(MyFunctionTests.class);
057: if ("true".equals(System.getProperty("test.start.server"))) {
058: suite.addTestSuite(StopServer.class);
059: }
060:
061: return suite;
062: }
063:
064: /**
065: * Starts the web server.
066: */
067: public static class StartServer extends TestCase {
068:
069: /**
070: * Constructs a new <code>StartServer</code> test suite with
071: * the specified name. The name will be passed to the superconstructor.
072: *
073: * @param name
074: * the name for this test suite.
075: */
076: public StartServer(String name) {
077: super (name);
078: }
079:
080: /**
081: * Returns a test suite with all test cases defined by this class.
082: *
083: * @return
084: * the test suite, never <code>null</code>.
085: */
086: public static Test suite() {
087: return new TestSuite(StartServer.class);
088: }
089:
090: /**
091: * Unit test that is only used to start the Servlet API if the
092: * test.start.server build properties is set to true.
093: *
094: * @throws Exception
095: * if the internal Servlet container cannot be started for any reasons.
096: */
097: public void testStartServer() throws Exception {
098:
099: String warLocation = "build/webapps/myproject/myproject.war"
100: .replace('/', File.separatorChar);
101: File warFile = new File(System.getProperty("user.dir"),
102: warLocation);
103: int port = 8080;
104: if (System.getProperty("servlet.port") != null
105: && !System.getProperty("servlet.port").equals("")) {
106: port = Integer.parseInt(System
107: .getProperty("servlet.port"));
108: }
109:
110: // Start the web server
111: API_SERVER = new HTTPServletHandler(warFile, port, true);
112: API_STARTED = true;
113: }
114: }
115:
116: /**
117: * Stops the web server.
118: */
119: public static class StopServer extends TestCase {
120:
121: /**
122: * Constructs a new <code>StopServer</code> test suite with
123: * the specified name. The name will be passed to the superconstructor.
124: *
125: * @param name
126: * the name for this test suite.
127: */
128: public StopServer(String name) {
129: super (name);
130: }
131:
132: /**
133: * Returns a test suite with all test cases defined by this class.
134: *
135: * @return
136: * the test suite, never <code>null</code>.
137: */
138: public static Test suite() {
139: return new TestSuite(StopServer.class);
140: }
141:
142: /**
143: * Unit test that is only used to stop the Servlet API if the servlet
144: * is started.
145: *
146: * @throws Exception
147: * if the internal Servlet container cannot be stopped for any reasons.
148: */
149: public void testStopServer() throws Exception {
150:
151: if (API_STARTED) {
152: API_SERVER.close();
153: }
154: }
155: }
156: }
|