KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > MainTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol;
38
39 import junit.framework.TestCase;
40 import net.sourceforge.cruisecontrol.util.MainArgs;
41
42 import org.apache.log4j.Appender;
43 import org.apache.log4j.Layout;
44 import org.apache.log4j.Logger;
45 import org.apache.log4j.spi.ErrorHandler;
46 import org.apache.log4j.spi.Filter;
47 import org.apache.log4j.spi.LoggingEvent;
48
49 public class MainTest extends TestCase {
50     public void testParsePassword() {
51         String JavaDoc[] correctArgs = new String JavaDoc[]{"-password", "password"};
52         String JavaDoc[] missingValue = new String JavaDoc[]{"-password"};
53         String JavaDoc[] missingParam = new String JavaDoc[]{""};
54         assertEquals("password", Main.parsePassword(correctArgs));
55         assertEquals(null, Main.parseUser(missingValue));
56         assertEquals(null, Main.parseUser(missingParam));
57     }
58
59     public void testParseUser() {
60         String JavaDoc[] correctArgs = new String JavaDoc[]{"-user", "user"};
61         String JavaDoc[] missingValue = new String JavaDoc[]{"-user"};
62         String JavaDoc[] missingParam = new String JavaDoc[]{""};
63         assertEquals("user", Main.parseUser(correctArgs));
64         assertEquals(null, Main.parseUser(missingValue));
65         assertEquals(null, Main.parseUser(missingParam));
66     }
67
68     public void testParseConfigurationFileName() throws Exception JavaDoc {
69         String JavaDoc[] correctArgs = new String JavaDoc[]{"-configfile", "myconfig.xml"};
70         String JavaDoc[] missingParam = new String JavaDoc[]{""};
71         String JavaDoc[] missingValue = new String JavaDoc[]{"-configfile"};
72
73         assertEquals("myconfig.xml", Main.parseConfigFileName(correctArgs, null));
74         assertEquals("config.xml", Main.parseConfigFileName(missingParam, "config.xml"));
75
76         try {
77             Main.parseConfigFileName(missingValue, null);
78             fail("Expected CruiseControlException on missing configfile value");
79         } catch (CruiseControlException e) {
80             // expected
81
}
82
83     }
84
85     public void testParseHttpPort() throws Exception JavaDoc {
86         String JavaDoc[] correctArgs = new String JavaDoc[]{"-jmxport", "123"};
87         String JavaDoc[] missingParam = new String JavaDoc[]{""};
88         String JavaDoc[] defaultValue = new String JavaDoc[]{"-jmxport"};
89         String JavaDoc[] invalidArgs = new String JavaDoc[]{"-jmxport", "ABC"};
90         String JavaDoc[] deprecatedArgs = new String JavaDoc[]{"-port", "123"};
91         String JavaDoc[] deprecatedAndCorrectArgs = new String JavaDoc[]{"-port", "123", "-jmxport", "123"};
92
93         assertEquals(123, Main.parseJMXHttpPort(correctArgs));
94         assertEquals(MainArgs.NOT_FOUND, Main.parseJMXHttpPort(missingParam));
95         assertEquals(8000, Main.parseJMXHttpPort(defaultValue));
96         assertEquals(123, Main.parseJMXHttpPort(deprecatedArgs));
97
98         try {
99             Main.parseJMXHttpPort(invalidArgs);
100             fail("Expected IllegalArgumentException on non-int ABC");
101         } catch (IllegalArgumentException JavaDoc e) {
102             // expected
103
}
104
105         try {
106             Main.parseJMXHttpPort(deprecatedAndCorrectArgs);
107             fail("Expected exception");
108         } catch (IllegalArgumentException JavaDoc expected) {
109         }
110     }
111
112     public void testParseRmiPort() throws Exception JavaDoc {
113         String JavaDoc[] correctArgs = new String JavaDoc[]{"-rmiport", "123"};
114         String JavaDoc[] missingParam = new String JavaDoc[]{""};
115         String JavaDoc[] defaultValue = new String JavaDoc[]{"-rmiport"};
116         String JavaDoc[] invalidArgs = new String JavaDoc[]{"-rmiport", "ABC"};
117
118         assertEquals(123, Main.parseRmiPort(correctArgs));
119         assertEquals(MainArgs.NOT_FOUND, Main.parseRmiPort(missingParam));
120         assertEquals(1099, Main.parseRmiPort(defaultValue));
121
122         try {
123             Main.parseRmiPort(invalidArgs);
124             fail("Expected exception");
125         } catch (IllegalArgumentException JavaDoc e) {
126             // expected
127
}
128     }
129
130     public void testParseXslPath() {
131         final String JavaDoc tempDirName = System.getProperty("java.io.tmpdir");
132         String JavaDoc[] correctArgs = new String JavaDoc[]{"-xslpath", tempDirName};
133         String JavaDoc[] missingParam = new String JavaDoc[]{""};
134         String JavaDoc[] missingValue = new String JavaDoc[]{"-xslpath"};
135         final String JavaDoc invalidXsl = "does_Not_Exist";
136         String JavaDoc[] invalidArgs = new String JavaDoc[]{"-xslpath", invalidXsl};
137
138         assertEquals(tempDirName, Main.parseXslPath(correctArgs));
139         assertNull(Main.parseXslPath(missingParam));
140         assertNull(Main.parseXslPath(missingValue));
141
142         try {
143             Main.parseXslPath(invalidArgs);
144             fail();
145         } catch (IllegalArgumentException JavaDoc expected) {
146             assertEquals("'xslpath' argument must specify an existing directory but was " + invalidXsl,
147                     expected.getMessage());
148         }
149     }
150
151     public void testUsage() {
152         String JavaDoc[] usage = {"-?"};
153         String JavaDoc[] notusage = {"-port", "8000"};
154         assertTrue(Main.shouldPrintUsage(usage));
155         assertFalse(Main.shouldPrintUsage(notusage));
156     }
157
158     public void testshouldStartController() throws Exception JavaDoc {
159         String JavaDoc[] bothArgs = new String JavaDoc[]{"-jmxport", "8085", "-rmiport", "8086"};
160         String JavaDoc[] bothArgsWithDeprecated = new String JavaDoc[]{"-port", "8085", "-rmiport", "8086"};
161         String JavaDoc[] rmiPort = new String JavaDoc[]{"-rmiport", "8086"};
162         String JavaDoc[] httpPort = new String JavaDoc[]{"-jmxport", "8085"};
163         String JavaDoc[] httpPortWithDefault = new String JavaDoc[]{"-jmxport"};
164         String JavaDoc[] neitherArg = new String JavaDoc[]{"-foo", "blah"};
165         String JavaDoc[] deprecatedHttpPort = new String JavaDoc[]{"-port", "8085"};
166
167         assertTrue(Main.shouldStartController(bothArgs));
168         assertTrue(Main.shouldStartController(bothArgsWithDeprecated));
169         assertTrue(Main.shouldStartController(rmiPort));
170         assertTrue(Main.shouldStartController(httpPort));
171         assertTrue(Main.shouldStartController(httpPortWithDefault));
172         assertTrue(Main.shouldStartController(deprecatedHttpPort));
173         assertFalse(Main.shouldStartController(neitherArg));
174     }
175
176     public void testDeprecatedArgs() {
177         String JavaDoc[] args = {"-port", "8000"};
178
179         StringBufferAppender appender = new StringBufferAppender();
180         Logger testLogger = Logger.getLogger(Main.class);
181         testLogger.addAppender(appender);
182         Main.checkDeprecatedArguments(args, testLogger);
183
184         assertTrue(appender.toString().indexOf(
185                 "WARNING: The port argument is deprecated. Use jmxport instead.") >= 0);
186     }
187
188     public static class StringBufferAppender implements Appender {
189         private final StringBuffer JavaDoc myBuffer = new StringBuffer JavaDoc();
190
191         public void addFilter(Filter filter) {
192             throw new UnsupportedOperationException JavaDoc();
193         }
194
195         public Filter getFilter() {
196             throw new UnsupportedOperationException JavaDoc();
197         }
198
199         public void clearFilters() {
200             throw new UnsupportedOperationException JavaDoc();
201         }
202
203         public void close() {
204             throw new UnsupportedOperationException JavaDoc();
205         }
206
207         public void doAppend(LoggingEvent event) {
208             myBuffer.append(event.getMessage() + "\n");
209         }
210
211         public String JavaDoc getName() {
212             throw new UnsupportedOperationException JavaDoc();
213         }
214
215         public void setErrorHandler(ErrorHandler errorHandler) {
216             throw new UnsupportedOperationException JavaDoc();
217         }
218
219         public ErrorHandler getErrorHandler() {
220             throw new UnsupportedOperationException JavaDoc();
221         }
222
223         public void setLayout(Layout layout) {
224             throw new UnsupportedOperationException JavaDoc();
225         }
226
227         public Layout getLayout() {
228             throw new UnsupportedOperationException JavaDoc();
229         }
230
231         public void setName(String JavaDoc s) {
232             throw new UnsupportedOperationException JavaDoc();
233         }
234
235         public boolean requiresLayout() {
236             throw new UnsupportedOperationException JavaDoc();
237         }
238
239         public String JavaDoc toString() {
240             return myBuffer.toString();
241         }
242     }
243
244
245 }
Popular Tags