001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001-2003, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol;
037:
038: import java.io.File;
039:
040: import junit.framework.TestCase;
041: import net.sourceforge.cruisecontrol.report.BuildLoopMonitor;
042: import net.sourceforge.cruisecontrol.report.BuildLoopMonitorRepository;
043: import net.sourceforge.cruisecontrol.util.MainArgs;
044: import net.sourceforge.cruisecontrol.util.Util;
045:
046: import org.apache.log4j.Appender;
047: import org.apache.log4j.Layout;
048: import org.apache.log4j.Logger;
049: import org.apache.log4j.spi.ErrorHandler;
050: import org.apache.log4j.spi.Filter;
051: import org.apache.log4j.spi.LoggingEvent;
052:
053: public class MainTest extends TestCase {
054:
055: public void testParsePassword() {
056: String[] correctArgs = new String[] { "-password", "password" };
057: String[] missingValue = new String[] { "-password" };
058: String[] missingParam = new String[] { "" };
059: assertEquals("password", Main.parsePassword(correctArgs));
060: assertEquals(null, Main.parsePassword(missingValue));
061: assertEquals(null, Main.parsePassword(missingParam));
062: }
063:
064: public void testParseUser() {
065: String[] correctArgs = new String[] { "-user", "user" };
066: String[] missingValue = new String[] { "-user" };
067: String[] missingParam = new String[] { "" };
068: assertEquals("user", Main.parseUser(correctArgs));
069: assertEquals(null, Main.parseUser(missingValue));
070: assertEquals(null, Main.parseUser(missingParam));
071: }
072:
073: public void testParseConfigurationFileName() throws Exception {
074: String[] correctArgs = new String[] { "-configfile",
075: "myconfig.xml" };
076: String[] missingParam = new String[] { "" };
077: String[] missingValue = new String[] { "-configfile" };
078:
079: assertEquals("myconfig.xml", Main.parseConfigFileName(
080: correctArgs, null));
081: assertEquals("config.xml", Main.parseConfigFileName(
082: missingParam, "config.xml"));
083:
084: try {
085: Main.parseConfigFileName(missingValue, null);
086: fail("Expected CruiseControlException on missing configfile value");
087: } catch (CruiseControlException e) {
088: // expected
089: }
090:
091: }
092:
093: public void testParseDashboardUrl() throws Exception {
094: String[] customizedArgs = new String[] { "-dashboardurl",
095: "http://myserver:1234/dashboard" };
096: String[] missingParam = new String[] { "" };
097: String[] defaultValue = new String[] { "-dashboardurl" };
098:
099: assertEquals("http://myserver:1234/dashboard", Main
100: .parseDashboardUrl(customizedArgs));
101: assertEquals("http://localhost:8080/dashboard", Main
102: .parseDashboardUrl(missingParam));
103: assertEquals("http://localhost:8080/dashboard", Main
104: .parseDashboardUrl(defaultValue));
105: }
106:
107: public void testParseDashboardUrlWithWebport() throws Exception {
108: String[] onlyWebport = { "-webport", "8585" };
109: String[] webportAndUrl = { "-webport", "8585", "-dashboardurl",
110: "http://myserver:1234/dashboard" };
111: String[] webportAndDefaultUrl = { "-webport", "8585",
112: "-dashboardurl" };
113:
114: assertEquals("http://localhost:8585/dashboard", Main
115: .parseDashboardUrl(onlyWebport));
116: assertEquals("http://myserver:1234/dashboard", Main
117: .parseDashboardUrl(webportAndUrl));
118: assertEquals("http://localhost:8080/dashboard", Main
119: .parseDashboardUrl(webportAndDefaultUrl));
120: }
121:
122: public void testParseHttpPostingInterval() throws Exception {
123: String[] customizedArgs = new String[] { "-postinterval",
124: "1234" };
125: String[] missingParam = new String[] { "" };
126: String[] defaultValue = new String[] { "-postinterval" };
127:
128: assertEquals(1234, Main
129: .parseHttpPostingInterval(customizedArgs));
130: assertEquals(5, Main.parseHttpPostingInterval(missingParam));
131: assertEquals(5, Main.parseHttpPostingInterval(defaultValue));
132: }
133:
134: public void testParsePostingEnabled() throws Exception {
135: String[] customizedArgs = new String[] { "-postenabled",
136: "false" };
137: String[] missingParam = new String[] { "" };
138: String[] defaultValue = new String[] { "-postenabled" };
139:
140: assertFalse(Main.parseHttpPostingEnabled(customizedArgs));
141: assertTrue(Main.parseHttpPostingEnabled(missingParam));
142: assertTrue(Main.parseHttpPostingEnabled(defaultValue));
143: }
144:
145: public void testParseHttpPort() throws Exception {
146: String[] correctArgs = new String[] { "-jmxport", "123" };
147: String[] missingParam = new String[] { "" };
148: String[] defaultValue = new String[] { "-jmxport" };
149: String[] invalidArgs = new String[] { "-jmxport", "ABC" };
150: String[] deprecatedArgs = new String[] { "-port", "123" };
151: String[] deprecatedAndCorrectArgs = new String[] { "-port",
152: "123", "-jmxport", "123" };
153:
154: assertEquals(123, Main.parseJMXHttpPort(correctArgs));
155: assertEquals(MainArgs.NOT_FOUND, Main
156: .parseJMXHttpPort(missingParam));
157: assertEquals(8000, Main.parseJMXHttpPort(defaultValue));
158: assertEquals(123, Main.parseJMXHttpPort(deprecatedArgs));
159:
160: try {
161: Main.parseJMXHttpPort(invalidArgs);
162: fail("Expected IllegalArgumentException on non-int ABC");
163: } catch (IllegalArgumentException e) {
164: // expected
165: }
166:
167: try {
168: Main.parseJMXHttpPort(deprecatedAndCorrectArgs);
169: fail("Expected exception");
170: } catch (IllegalArgumentException expected) {
171: }
172: }
173:
174: public void testParseRmiPort() throws Exception {
175: String[] correctArgs = new String[] { "-rmiport", "123" };
176: String[] missingParam = new String[] { "" };
177: String[] defaultValue = new String[] { "-rmiport" };
178: String[] invalidArgs = new String[] { "-rmiport", "ABC" };
179:
180: assertEquals(123, Main.parseRmiPort(correctArgs));
181: assertEquals(MainArgs.NOT_FOUND, Main
182: .parseRmiPort(missingParam));
183: assertEquals(1099, Main.parseRmiPort(defaultValue));
184:
185: try {
186: Main.parseRmiPort(invalidArgs);
187: fail("Expected exception");
188: } catch (IllegalArgumentException e) {
189: // expected
190: }
191: }
192:
193: public void testParseWebPort() throws Exception {
194: String[] correctArgs = new String[] { "-webport", "123" };
195: String[] missingParam = new String[] { "" };
196: String[] defaultValue = new String[] { "-webport" };
197: String[] invalidArgs = new String[] { "-webport", "ABC" };
198:
199: assertEquals(123, Main.parseWebPort(correctArgs));
200: assertEquals(MainArgs.NOT_FOUND, Main
201: .parseWebPort(missingParam));
202: assertEquals(8080, Main.parseWebPort(defaultValue));
203:
204: try {
205: Main.parseWebPort(invalidArgs);
206: fail("Expected exception");
207: } catch (IllegalArgumentException e) {
208: // expected
209: }
210: }
211:
212: public void testParseWebappPath() throws Exception {
213: final String tempDirName = System.getProperty("java.io.tmpdir");
214: final File webappDir = new File(tempDirName, "testwebapp");
215: final File webinfDir = new File(webappDir, "WEB-INF");
216:
217: try {
218: String[] correctArgs = new String[] { "-webapppath",
219: webappDir.getAbsolutePath() };
220: String[] missingParam = new String[] { "" };
221: String[] missingValue = new String[] { "-webapppath" };
222: String[] invalidArgs = new String[] { "-webapppath",
223: "does_not_exist" };
224:
225: if (!Util.doMkDirs(webinfDir)) {
226: throw new Exception("Could not create test webapp dir");
227: }
228: webappDir.deleteOnExit();
229: webinfDir.deleteOnExit();
230:
231: Main theMainClass = new Main();
232: assertEquals(webappDir.getAbsolutePath(), theMainClass
233: .parseWebappPath(correctArgs));
234:
235: final String msg = "'webapppath' argument must specify an "
236: + "existing directory but was ./webapps/cruisecontrol";
237: try {
238: theMainClass.parseWebappPath(missingValue);
239: fail();
240: } catch (IllegalArgumentException expected) {
241: assertEquals(msg, expected.getMessage());
242: }
243: try {
244: theMainClass.parseWebappPath(missingParam);
245: fail();
246: } catch (IllegalArgumentException expected) {
247: assertEquals(msg, expected.getMessage());
248: }
249:
250: try {
251: theMainClass.parseWebappPath(invalidArgs);
252: fail();
253: } catch (IllegalArgumentException expected) {
254: }
255:
256: } finally {
257: webinfDir.delete();
258: webappDir.delete();
259: }
260:
261: }
262:
263: public void testParseXslPath() {
264: final String tempDirName = System.getProperty("java.io.tmpdir");
265: String[] correctArgs = new String[] { "-xslpath", tempDirName };
266: String[] missingParam = new String[] { "" };
267: String[] missingValue = new String[] { "-xslpath" };
268: final String invalidXsl = "does_Not_Exist";
269: String[] invalidArgs = new String[] { "-xslpath", invalidXsl };
270:
271: assertEquals(tempDirName, Main.parseXslPath(correctArgs));
272: assertNull(Main.parseXslPath(missingParam));
273: assertNull(Main.parseXslPath(missingValue));
274:
275: try {
276: Main.parseXslPath(invalidArgs);
277: fail();
278: } catch (IllegalArgumentException expected) {
279: assertEquals(
280: "'xslpath' argument must specify an existing directory but was "
281: + invalidXsl, expected.getMessage());
282: }
283: }
284:
285: public void testUsage() {
286: String[] usage = { "-?" };
287: String[] notusage = { "-port", "8000" };
288: assertTrue(Main.shouldPrintUsage(usage));
289: assertFalse(Main.shouldPrintUsage(notusage));
290: }
291:
292: public void testshouldStartController() throws Exception {
293: String[] bothArgs = new String[] { "-jmxport", "8085",
294: "-rmiport", "8086" };
295: String[] bothArgsWithDeprecated = new String[] { "-port",
296: "8085", "-rmiport", "8086" };
297: String[] rmiPort = new String[] { "-rmiport", "8086" };
298: String[] httpPort = new String[] { "-jmxport", "8085" };
299: String[] httpPortWithDefault = new String[] { "-jmxport" };
300: String[] neitherArg = new String[] { "-foo", "blah" };
301: String[] deprecatedHttpPort = new String[] { "-port", "8085" };
302:
303: assertTrue(Main.shouldStartJmxAgent(bothArgs));
304: assertTrue(Main.shouldStartJmxAgent(bothArgsWithDeprecated));
305: assertTrue(Main.shouldStartJmxAgent(rmiPort));
306: assertTrue(Main.shouldStartJmxAgent(httpPort));
307: assertTrue(Main.shouldStartJmxAgent(httpPortWithDefault));
308: assertTrue(Main.shouldStartJmxAgent(deprecatedHttpPort));
309: assertFalse(Main.shouldStartJmxAgent(neitherArg));
310: }
311:
312: public void testShouldStartEmbeddedServer() throws Exception {
313: String[] bothArgs = new String[] { "-webport", "1234",
314: "-webapppath", "/tmp/foo" };
315: String[] webPort = new String[] { "-webport", "1234" };
316: String[] webappPath = new String[] { "-webapppath", "/tmp/foo" };
317: String[] neitherArg = new String[] {};
318:
319: assertTrue(Main.shouldStartEmbeddedServer(bothArgs));
320: assertTrue(Main.shouldStartEmbeddedServer(webPort));
321: assertTrue(Main.shouldStartEmbeddedServer(webappPath));
322: assertFalse(Main.shouldStartEmbeddedServer(neitherArg));
323:
324: }
325:
326: public void testShouldStartBuildLoopMonitor() throws Exception {
327: BuildLoopMonitor buildLoopMonitor = BuildLoopMonitorRepository
328: .getBuildLoopMonitor();
329: assertNull(buildLoopMonitor);
330: new Main().startPostingToDashboard(new String[0]);
331: buildLoopMonitor = BuildLoopMonitorRepository
332: .getBuildLoopMonitor();
333: assertNotNull(buildLoopMonitor);
334:
335: BuildLoopMonitorRepository.cancelPosting();
336: }
337:
338: public void testShouldNOTRestartBuildLoopMonitorIfItAlreadyExisting()
339: throws Exception {
340: assertTrue(Main.shouldPostDataToDashboard(new String[0]));
341:
342: new Main().startPostingToDashboard(new String[0]);
343:
344: assertFalse(Main.shouldPostDataToDashboard(new String[0]));
345:
346: BuildLoopMonitorRepository.cancelPosting();
347: }
348:
349: public void testDeprecatedArgs() {
350: String[] args = { "-port", "8000" };
351:
352: StringBufferAppender appender = new StringBufferAppender();
353: Logger testLogger = Logger.getLogger(Main.class);
354: testLogger.addAppender(appender);
355: Main.checkDeprecatedArguments(args, testLogger);
356:
357: assertTrue(appender
358: .toString()
359: .indexOf(
360: "WARNING: The port argument is deprecated. Use jmxport instead.") >= 0);
361: }
362:
363: public static class StringBufferAppender implements Appender {
364: private final StringBuffer myBuffer = new StringBuffer();
365:
366: public void addFilter(Filter filter) {
367: throw new UnsupportedOperationException();
368: }
369:
370: public Filter getFilter() {
371: throw new UnsupportedOperationException();
372: }
373:
374: public void clearFilters() {
375: throw new UnsupportedOperationException();
376: }
377:
378: public void close() {
379: throw new UnsupportedOperationException();
380: }
381:
382: public void doAppend(LoggingEvent event) {
383: myBuffer.append(event.getMessage() + "\n");
384: }
385:
386: public String getName() {
387: throw new UnsupportedOperationException();
388: }
389:
390: public void setErrorHandler(ErrorHandler errorHandler) {
391: throw new UnsupportedOperationException();
392: }
393:
394: public ErrorHandler getErrorHandler() {
395: throw new UnsupportedOperationException();
396: }
397:
398: public void setLayout(Layout layout) {
399: throw new UnsupportedOperationException();
400: }
401:
402: public Layout getLayout() {
403: throw new UnsupportedOperationException();
404: }
405:
406: public void setName(String s) {
407: throw new UnsupportedOperationException();
408: }
409:
410: public boolean requiresLayout() {
411: throw new UnsupportedOperationException();
412: }
413:
414: public String toString() {
415: return myBuffer.toString();
416: }
417: }
418:
419: }
|