1 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 [] correctArgs = new String []{"-password", "password"}; 52 String [] missingValue = new String []{"-password"}; 53 String [] missingParam = new String []{""}; 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 [] correctArgs = new String []{"-user", "user"}; 61 String [] missingValue = new String []{"-user"}; 62 String [] missingParam = new String []{""}; 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 { 69 String [] correctArgs = new String []{"-configfile", "myconfig.xml"}; 70 String [] missingParam = new String []{""}; 71 String [] missingValue = new String []{"-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 } 82 83 } 84 85 public void testParseHttpPort() throws Exception { 86 String [] correctArgs = new String []{"-jmxport", "123"}; 87 String [] missingParam = new String []{""}; 88 String [] defaultValue = new String []{"-jmxport"}; 89 String [] invalidArgs = new String []{"-jmxport", "ABC"}; 90 String [] deprecatedArgs = new String []{"-port", "123"}; 91 String [] deprecatedAndCorrectArgs = new String []{"-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 e) { 102 } 104 105 try { 106 Main.parseJMXHttpPort(deprecatedAndCorrectArgs); 107 fail("Expected exception"); 108 } catch (IllegalArgumentException expected) { 109 } 110 } 111 112 public void testParseRmiPort() throws Exception { 113 String [] correctArgs = new String []{"-rmiport", "123"}; 114 String [] missingParam = new String []{""}; 115 String [] defaultValue = new String []{"-rmiport"}; 116 String [] invalidArgs = new String []{"-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 e) { 126 } 128 } 129 130 public void testParseXslPath() { 131 final String tempDirName = System.getProperty("java.io.tmpdir"); 132 String [] correctArgs = new String []{"-xslpath", tempDirName}; 133 String [] missingParam = new String []{""}; 134 String [] missingValue = new String []{"-xslpath"}; 135 final String invalidXsl = "does_Not_Exist"; 136 String [] invalidArgs = new String []{"-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 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 [] usage = {"-?"}; 153 String [] notusage = {"-port", "8000"}; 154 assertTrue(Main.shouldPrintUsage(usage)); 155 assertFalse(Main.shouldPrintUsage(notusage)); 156 } 157 158 public void testshouldStartController() throws Exception { 159 String [] bothArgs = new String []{"-jmxport", "8085", "-rmiport", "8086"}; 160 String [] bothArgsWithDeprecated = new String []{"-port", "8085", "-rmiport", "8086"}; 161 String [] rmiPort = new String []{"-rmiport", "8086"}; 162 String [] httpPort = new String []{"-jmxport", "8085"}; 163 String [] httpPortWithDefault = new String []{"-jmxport"}; 164 String [] neitherArg = new String []{"-foo", "blah"}; 165 String [] deprecatedHttpPort = new String []{"-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 [] 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 myBuffer = new StringBuffer (); 190 191 public void addFilter(Filter filter) { 192 throw new UnsupportedOperationException (); 193 } 194 195 public Filter getFilter() { 196 throw new UnsupportedOperationException (); 197 } 198 199 public void clearFilters() { 200 throw new UnsupportedOperationException (); 201 } 202 203 public void close() { 204 throw new UnsupportedOperationException (); 205 } 206 207 public void doAppend(LoggingEvent event) { 208 myBuffer.append(event.getMessage() + "\n"); 209 } 210 211 public String getName() { 212 throw new UnsupportedOperationException (); 213 } 214 215 public void setErrorHandler(ErrorHandler errorHandler) { 216 throw new UnsupportedOperationException (); 217 } 218 219 public ErrorHandler getErrorHandler() { 220 throw new UnsupportedOperationException (); 221 } 222 223 public void setLayout(Layout layout) { 224 throw new UnsupportedOperationException (); 225 } 226 227 public Layout getLayout() { 228 throw new UnsupportedOperationException (); 229 } 230 231 public void setName(String s) { 232 throw new UnsupportedOperationException (); 233 } 234 235 public boolean requiresLayout() { 236 throw new UnsupportedOperationException (); 237 } 238 239 public String toString() { 240 return myBuffer.toString(); 241 } 242 } 243 244 245 } | Popular Tags |