Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
CargoTestSetup |
|
| 2.25;2.25 |
1 | /* |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to you under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
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 | // ------------------------------------------------------------ Constructors |
|
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 | 0 | super(test); |
53 | 0 | } |
54 | ||
55 | // ------------------------------------------------------ Instance Variables |
|
56 | ||
57 | /** |
|
58 | * <p>The installed local container for this test setup.</p> |
|
59 | */ |
|
60 | private InstalledLocalContainer container; |
|
61 | ||
62 | // ------------------------------------------------------ Test Setup Methods |
|
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 | 0 | super.setUp(); |
84 | ||
85 | // If there is no container id, default to Tomcat 5x |
|
86 | 0 | String containerId = System.getProperty("cargo.container.id"); |
87 | 0 | if (containerId == null) { |
88 | 0 | containerId = Tomcat5xInstalledLocalContainer.ID; |
89 | } |
|
90 | 0 | System.out.println("[INFO] container id: " + containerId); |
91 | ||
92 | // Construct the war, using the container id and the path to the war file |
|
93 | 0 | String deployablePath = System.getProperty("cargo.deployable"); |
94 | 0 | System.out.println("[INFO] deployable: " + deployablePath); |
95 | 0 | Deployable war = new DefaultDeployableFactory().createDeployable( |
96 | containerId, |
|
97 | deployablePath, |
|
98 | DeployableType.WAR); |
|
99 | ||
100 | // Container configuration |
|
101 | 0 | ConfigurationFactory configurationFactory = |
102 | new DefaultConfigurationFactory(); |
|
103 | ||
104 | 0 | LocalConfiguration configuration = |
105 | (LocalConfiguration) configurationFactory.createConfiguration( |
|
106 | containerId, |
|
107 | ConfigurationType.STANDALONE); |
|
108 | ||
109 | // Find and (if provided) set the port to use for the container. |
|
110 | 0 | String servletPort = System.getProperty("cargo.servlet.port"); |
111 | 0 | if (servletPort != null) { |
112 | 0 | configuration.setProperty("cargo.servlet.port", servletPort); |
113 | 0 | System.out.println("[INFO] servlet port: " + servletPort); |
114 | } |
|
115 | ||
116 | 0 | configuration.addDeployable(war); |
117 | ||
118 | 0 | container = (InstalledLocalContainer) |
119 | new DefaultContainerFactory().createContainer( |
|
120 | containerId, |
|
121 | ContainerType.INSTALLED, configuration); |
|
122 | ||
123 | // If 'cargo.container.home' is not set, or if an expression was |
|
124 | // passed through, try to use the TOMCAT_HOME environment variable. |
|
125 | 0 | String containerHome = System.getProperty("cargo.container.home"); |
126 | 0 | if (containerHome == null || containerHome.startsWith("$")) { |
127 | 0 | containerHome = System.getenv("TOMCAT_HOME"); |
128 | } |
|
129 | 0 | System.out.println("[INFO] container home: " + containerHome); |
130 | 0 | container.setHome(new File(containerHome)); |
131 | ||
132 | // Find and (if provided) set the path to a log file |
|
133 | 0 | String containerLog = System.getProperty("cargo.container.log"); |
134 | 0 | if (containerLog != null) { |
135 | 0 | System.out.println("[INFO] container log: " + containerLog); |
136 | 0 | container.setLogger(new FileLogger(containerLog, false)); |
137 | } |
|
138 | ||
139 | // Find and (if provided) set the path to an output file |
|
140 | 0 | String containerOutput = System.getProperty("cargo.container.output"); |
141 | 0 | if (containerOutput != null) { |
142 | 0 | System.out.println("[INFO] container output: " + containerOutput); |
143 | 0 | container.setOutput(new File(containerOutput)); |
144 | } |
|
145 | ||
146 | 0 | container.start(); |
147 | 0 | } |
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 | 0 | container.stop(); |
157 | 0 | super.tearDown(); |
158 | 0 | } |
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 | 0 | return "CargoTestSetup"; |
171 | } |
|
172 | ||
173 | } |
|
174 |