2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.test.cargo;
19
20 import java.io.File;
21
22 import junit.extensions.TestSetup;
23 import junit.framework.Test;
24
25 import org.codehaus.cargo.container.InstalledLocalContainer;
26 import org.codehaus.cargo.container.ContainerType;
27 import org.codehaus.cargo.container.tomcat.Tomcat5xInstalledLocalContainer;
28 import org.codehaus.cargo.container.deployable.Deployable;
29 import org.codehaus.cargo.container.deployable.DeployableType;
30 import org.codehaus.cargo.container.configuration.LocalConfiguration;
31 import org.codehaus.cargo.container.configuration.ConfigurationType;
32 import org.codehaus.cargo.generic.deployable.DefaultDeployableFactory;
33 import org.codehaus.cargo.generic.configuration.ConfigurationFactory;
34 import org.codehaus.cargo.generic.configuration.DefaultConfigurationFactory;
35 import org.codehaus.cargo.generic.DefaultContainerFactory;
36 import org.codehaus.cargo.util.log.FileLogger;
37
38 /***
39 * <p>Convenience <code>TestSetup</code> class which uses Cargo to start
40 * and stop a Servlet container.</p>
41 */
42 public class CargoTestSetup extends TestSetup {
43
44
45
46 /***
47 * <p>Construct a new instance of this test setup.</p>
48 *
49 * @param test Tests to be run within this test setup.
50 */
51 public CargoTestSetup(Test test) {
52 super(test);
53 }
54
55
56
57 /***
58 * <p>The installed local container for this test setup.</p>
59 */
60 private InstalledLocalContainer container;
61
62
63
64 /***
65 * <p>Start the container prior to running the tests.</p>
66 * <p>The following System properties are used:
67 * <ul>
68 * <li>cargo.container.id - ID of the container to use. [tomcat5x]</li>
69 * <li>cargo.container.home - Full path to a local installation of the container.
70 * If not set, uses the value of the TOMCAT_HOME environment variable.
71 * One of cargo.container.home or TOMCAT_HOME is REQUIRED.</li>
72 * <li>cargo.deployable - Full path to the war file to deploy. REQUIRED.</li>
73 * <li>cargo.container.output - Full path to a file to use for output. [none]</li>
74 * <li>cargo.container.log - Full path to a file to use for logging. [none]</li>
75 * <li>cargo.servlet.port - The port on which the container should listen. [8080]</li>
76 * </ul>
77 * </p>
78 *
79 * @throws Exception if an error occurs.
80 */
81 protected void setUp() throws Exception {
82
83 super.setUp();
84
85
86 String containerId = System.getProperty("cargo.container.id");
87 if (containerId == null) {
88 containerId = Tomcat5xInstalledLocalContainer.ID;
89 }
90 System.out.println("[INFO] container id: " + containerId);
91
92
93 String deployablePath = System.getProperty("cargo.deployable");
94 System.out.println("[INFO] deployable: " + deployablePath);
95 Deployable war = new DefaultDeployableFactory().createDeployable(
96 containerId,
97 deployablePath,
98 DeployableType.WAR);
99
100
101 ConfigurationFactory configurationFactory =
102 new DefaultConfigurationFactory();
103
104 LocalConfiguration configuration =
105 (LocalConfiguration) configurationFactory.createConfiguration(
106 containerId,
107 ConfigurationType.STANDALONE);
108
109
110 String servletPort = System.getProperty("cargo.servlet.port");
111 if (servletPort != null) {
112 configuration.setProperty("cargo.servlet.port", servletPort);
113 System.out.println("[INFO] servlet port: " + servletPort);
114 }
115
116 configuration.addDeployable(war);
117
118 container = (InstalledLocalContainer)
119 new DefaultContainerFactory().createContainer(
120 containerId,
121 ContainerType.INSTALLED, configuration);
122
123
124
125 String containerHome = System.getProperty("cargo.container.home");
126 if (containerHome == null || containerHome.startsWith("$")) {
127 containerHome = System.getenv("TOMCAT_HOME");
128 }
129 System.out.println("[INFO] container home: " + containerHome);
130 container.setHome(new File(containerHome));
131
132
133 String containerLog = System.getProperty("cargo.container.log");
134 if (containerLog != null) {
135 System.out.println("[INFO] container log: " + containerLog);
136 container.setLogger(new FileLogger(containerLog, false));
137 }
138
139
140 String containerOutput = System.getProperty("cargo.container.output");
141 if (containerOutput != null) {
142 System.out.println("[INFO] container output: " + containerOutput);
143 container.setOutput(new File(containerOutput));
144 }
145
146 container.start();
147 }
148
149
150 /***
151 * Stop the container after running the tests.
152 *
153 * @throws Exception if an error occurs.
154 */
155 protected void tearDown() throws Exception {
156 container.stop();
157 super.tearDown();
158 }
159
160
161 /***
162 * Return the name of the test setup.
163 * (Temporarily required due to MSUREFIRE-119.)
164 *
165 * @return the name of the test setup.
166 * @deprecated No replacement.
167 */
168
169 public String getName() {
170 return "CargoTestSetup";
171 }
172
173 }
174