1 37 package net.sourceforge.cruisecontrol; 38 39 import java.io.File ; 40 import java.io.FileWriter ; 41 import java.io.IOException ; 42 import java.util.ArrayList ; 43 import java.util.List ; 44 45 import junit.framework.TestCase; 46 import net.sourceforge.cruisecontrol.config.XMLConfigManager; 47 import net.sourceforge.cruisecontrol.listeners.ListenerTestPlugin; 48 49 53 public class CruiseControlControllerTest extends TestCase { 54 55 private File dir = new File ("target"); 56 private File configFile = new File (dir, "_tempConfigFile"); 57 private File configFile2 = new File (dir, "_tempConfigFile2"); 58 private CruiseControlController ccController; 59 60 protected void setUp() throws CruiseControlException { 61 dir.mkdirs(); 62 ccController = new CruiseControlController(); 63 ensureFileDoesntExist(configFile); 64 ensureFileDoesntExist(configFile2); 65 } 66 67 public void tearDown() { 68 ccController = null; 69 ensureFileDoesntExist(configFile); 70 ensureFileDoesntExist(configFile2); 71 } 72 73 private void ensureFileDoesntExist(File file) { 74 final long start = System.currentTimeMillis(); 75 final long oneMinute = 60 * 1000; 76 while (file.exists()) { 77 file.delete(); 78 if (System.currentTimeMillis() > (start + oneMinute)) { 79 fail("unable to delete file " + file.getPath()); 80 } 81 } 82 assertFalse(file.exists()); 83 } 84 85 public void testSetFileFailsWithNull() { 86 try { 87 ccController.setConfigFile(null); 88 fail("Allowed to not set a config file"); 89 } catch (CruiseControlException expected) { 90 assertEquals("No config file", expected.getMessage()); 91 } 92 } 93 94 public void testSetFileFailsIfFileDoesntExist() { 95 try { 96 ccController.setConfigFile(configFile); 97 fail("Config file must exist"); 98 } catch (CruiseControlException expected) { 99 assertEquals("Config file not found: " + configFile.getAbsolutePath(), expected.getMessage()); 100 } 101 } 102 103 public void testLoadEmptyProjects() throws IOException , CruiseControlException { 104 FileWriter configOut = new FileWriter (configFile); 105 writeHeader(configOut); 106 writeFooterAndClose(configOut); 107 108 ccController.setConfigFile(configFile); 109 assertEquals(configFile, ccController.getConfigFile()); 110 assertTrue(ccController.getProjects().isEmpty()); 111 } 112 113 public void testLoadThreadCount() throws IOException , CruiseControlException { 114 FileWriter configOut = new FileWriter (configFile); 115 writeHeader(configOut); 116 configOut.write("<system><configuration>" 117 + "<threads count=\"2\"/>" 118 + "</configuration></system>"); 119 writeFooterAndClose(configOut); 120 121 ccController.setConfigFile(configFile); 122 assertEquals(configFile, ccController.getConfigFile()); 123 assertTrue(ccController.getProjects().isEmpty()); 124 } 125 126 public void testLoadSomeProjects() throws IOException , CruiseControlException { 127 ccController = new CruiseControlController(); 128 129 FileWriter configOut = new FileWriter (configFile); 130 writeHeader(configOut); 131 writeProjectDetails(configOut, "testProject1", 30); 132 writeProjectDetails(configOut, "testProject2", 30); 133 writeFooterAndClose(configOut); 134 135 ccController.setConfigFile(configFile); 136 assertEquals(configFile, ccController.getConfigFile()); 137 assertEquals(2, ccController.getProjects().size()); 138 } 139 140 public void testSetConfigFileShouldFailWithDuplicateProjects() throws IOException , CruiseControlException { 141 ccController = new CruiseControlController(); 142 143 FileWriter configOut = new FileWriter (configFile); 144 writeHeader(configOut); 145 writeProjectDetails(configOut, "testProject1", 30); 146 writeProjectDetails(configOut, "testProject1", 30); 147 writeFooterAndClose(configOut); 148 149 try { 150 ccController.setConfigFile(configFile); 151 fail("duplicate project names should fail"); 152 } catch (CruiseControlException expected) { 153 assertEquals("Duplicate entries in config file for project name testProject1", expected.getMessage()); 154 } 155 } 156 157 public void testLoadSomeProjectsWithParametrizedNames() throws IOException , CruiseControlException { 159 ccController = new CruiseControlController(); 160 161 FileWriter configOut = new FileWriter (configFile); 162 writeHeader(configOut); 163 configOut.write(" <property name='name' value='testProject'/>\n"); 165 configOut.write(" <property name='encoding' value='utf8'/>\n"); 166 configOut.write(" <plugin name='testlistener' " 168 + "classname='net.sourceforge.cruisecontrol.listeners.ListenerTestPlugin' " 169 + "string='listener for ${project.name}'/>\n"); 170 171 configOut.write(" <project name='${name}1'>\n"); 173 configOut.write(" <modificationset><alwaysbuild/></modificationset>\n"); 174 configOut.write(" <schedule><ant/></schedule>\n"); 175 configOut.write(" <listeners><testlistener/></listeners>\n"); 176 configOut.write(" <log dir='logs/${project.name}' encoding='${encoding}'/>\n"); 177 configOut.write(" </project>\n"); 178 writeFooterAndClose(configOut); 179 180 ccController.setConfigFile(configFile); 181 assertEquals(configFile, ccController.getConfigFile()); 182 assertEquals(1, ccController.getProjects().size()); 183 final Project project = ((Project) ccController.getProjects().get(0)); 184 assertEquals("project name can be resolved", "testProject1", project.getName()); 185 186 assertEquals("project name can be resolved", "testProject1", project.getLog().getProjectName()); 187 188 List listeners = project.getListeners(); 189 assertEquals(1, listeners.size()); 190 Listener listener = (Listener) listeners.get(0); 191 assertEquals(ListenerTestPlugin.class, listener.getClass()); 192 assertEquals("listener for testProject1", ((ListenerTestPlugin) listener).getString()); 193 194 assertEquals("logs/testProject1", project.getLogDir()); 195 assertEquals("utf8", project.getLog().getLogXmlEncoding()); 196 } 197 198 public void testConfigReloading() throws IOException , CruiseControlException { 199 MyListener listener = new MyListener(); 200 201 ccController = new CruiseControlController(); 202 203 ccController.addListener(listener); 204 FileWriter configOut = new FileWriter (configFile); 205 writeHeader(configOut); 206 writeProjectDetails(configOut, "testProject1", 30); 207 writeProjectDetails(configOut, "testProject2", 30); 208 writeFooterAndClose(configOut); 209 210 ccController.setConfigFile(configFile); 211 212 assertEquals(configFile, ccController.getConfigFile()); 213 assertEquals(2, ccController.getProjects().size()); 214 assertEquals(2, listener.added.size()); 215 assertEquals(0, listener.removed.size()); 216 217 listener.clear(); 218 219 assertFalse(ccController.parseConfigFileIfNecessary()); 221 222 assertEquals(0, listener.added.size()); 224 assertEquals(0, listener.removed.size()); 225 226 228 listener.clear(); 229 230 sleep(1200); 231 configOut = new FileWriter (configFile); 232 writeHeader(configOut); 233 writeProjectDetails(configOut, "testProject1", 30); 234 writeProjectDetails(configOut, "testProject2", 30); 235 writeProjectDetails(configOut, "testProject3", 30); 236 writeFooterAndClose(configOut); 237 238 assertTrue(ccController.parseConfigFileIfNecessary()); 239 240 assertEquals(3, ccController.getProjects().size()); 241 assertEquals(1, listener.added.size()); 242 assertEquals(0, listener.removed.size()); 243 244 246 listener.clear(); 247 248 sleep(1200); 249 configOut = new FileWriter (configFile); 250 writeHeader(configOut); 251 writeProjectDetails(configOut, "testProject3", 30); 252 writeFooterAndClose(configOut); 253 254 assertTrue(ccController.parseConfigFileIfNecessary()); 256 257 assertEquals(1, ccController.getProjects().size()); 258 assertEquals(0, listener.added.size()); 259 assertEquals(2, listener.removed.size()); 260 } 261 262 public void testConfigReloadingWithXmlInclude() throws IOException , CruiseControlException { 263 MyListener listener = new MyListener(); 264 265 ccController = new CruiseControlController(); 266 267 ccController.addListener(listener); 268 269 FileWriter configOut2 = new FileWriter (configFile2); 270 writeProjectDetails(configOut2, "testProject1", 30); 271 writeProjectDetails(configOut2, "testProject2", 30); 272 configOut2.close(); 273 274 FileWriter wrapperConfigOut = new FileWriter (configFile); 275 wrapperConfigOut.write("<?xml version=\"1.0\" ?>\n"); 276 wrapperConfigOut.write("<!DOCTYPE cruisecontrol [ \n"); 277 wrapperConfigOut.write("<!ENTITY projects SYSTEM \"" + configFile2.getName() + "\"> \n"); 278 wrapperConfigOut.write("]> \n"); 279 wrapperConfigOut.write("<cruisecontrol>\n"); 280 wrapperConfigOut.write("&projects;"); 281 writeFooterAndClose(wrapperConfigOut); 282 283 ccController.setConfigFile(configFile); 284 285 assertEquals(configFile, ccController.getConfigFile()); 286 assertEquals(2, ccController.getProjects().size()); 287 assertEquals(2, listener.added.size()); 288 assertEquals(0, listener.removed.size()); 289 290 listener.clear(); 291 292 assertFalse(ccController.parseConfigFileIfNecessary()); 294 295 assertEquals(0, listener.added.size()); 297 assertEquals(0, listener.removed.size()); 298 299 301 listener.clear(); 302 303 sleep(1200); 304 configOut2 = new FileWriter (configFile2); 305 writeProjectDetails(configOut2, "testProject1", 30); 306 writeProjectDetails(configOut2, "testProject2", 30); 307 writeProjectDetails(configOut2, "testProject3", 30); 308 configOut2.close(); 309 310 assertTrue(ccController.parseConfigFileIfNecessary()); 311 312 assertEquals(3, ccController.getProjects().size()); 313 assertEquals(1, listener.added.size()); 314 assertEquals(0, listener.removed.size()); 315 316 318 listener.clear(); 319 320 sleep(1200); 321 configOut2 = new FileWriter (configFile2); 322 writeProjectDetails(configOut2, "testProject3", 30); 323 configOut2.close(); 324 325 assertTrue(ccController.parseConfigFileIfNecessary()); 327 328 assertEquals(1, ccController.getProjects().size()); 329 assertEquals(0, listener.added.size()); 330 assertEquals(2, listener.removed.size()); 331 } 332 333 public void testShouldReloadConfigurationWhenPluginAttributesChange() throws Exception { 334 ccController = new CruiseControlController(); 335 336 FileWriter configOut = new FileWriter (configFile); 337 writeHeader(configOut); 338 writeProjectDetails(configOut, "testProject1", 30); 339 writeFooterAndClose(configOut); 340 341 ccController.setConfigFile(configFile); 342 343 assertFalse(ccController.parseConfigFileIfNecessary()); 345 346 sleep(1200); 347 configOut = new FileWriter (configFile); 348 writeHeader(configOut); 349 writeProjectDetails(configOut, "testProject1", 60); 350 writeFooterAndClose(configOut); 351 352 assertTrue(ccController.parseConfigFileIfNecessary()); 353 } 354 355 public void testReadProject() { 356 String tempDir = System.getProperty("java.io.tmpdir"); 357 Project project = ccController.readProject(tempDir); 358 assertNotNull(project); 359 assertTrue(project.getBuildForced()); 360 } 361 362 public void testRegisterPlugins() throws IOException , CruiseControlException { 363 FileWriter configOut = new FileWriter (configFile); 364 writeHeader(configOut); 365 configOut.write(" <plugin name='testname' " 366 + "classname='net.sourceforge.cruisecontrol.CruiseControllerTest'/>\n"); 367 configOut.write(" <plugin name='labelincrementer' classname='my.global.Incrementer'/>\n"); 368 writeFooterAndClose(configOut); 369 370 ccController.setConfigFile(configFile); 371 XMLConfigManager configManager = (XMLConfigManager) ccController.getConfigManager(); 372 CruiseControlConfig config = configManager.getCruiseControlConfig(); 373 374 PluginRegistry newRegistry = config.getRootPlugins(); 375 assertTrue(newRegistry.isPluginRegistered("testname")); 376 assertFalse(newRegistry.isPluginRegistered("unknown_plugin")); 377 assertEquals(newRegistry.getPluginClassname("labelincrementer"), "my.global.Incrementer"); 378 } 379 380 public void testSetConfigFileShouldValidateAllElements() throws IOException { 381 FileWriter configOut = new FileWriter (configFile); 382 writeHeader(configOut); 383 configOut.write(" <project name='buildlogger'>\n"); 384 configOut.write(" <modificationset><alwaysbuild/></modificationset>\n"); 385 configOut.write(" <schedule><ant/></schedule>\n"); 386 configOut.write(" <log>\n"); 387 configOut.write(" <merge/>\n"); 388 configOut.write(" </log>\n"); 389 configOut.write(" </project>\n"); 390 writeFooterAndClose(configOut); 391 392 try { 393 ccController.setConfigFile(configFile); 394 fail("BuildLogger.validate() was not called"); 395 } catch (CruiseControlException ccex) { 396 assertEquals("one of file or dir are required attributes", 397 ccex.getMessage()); 398 } 399 } 400 401 private void writeHeader(FileWriter configOut) throws IOException { 402 configOut.write("<?xml version=\"1.0\" ?>\n"); 403 configOut.write("<cruisecontrol>\n"); 404 } 405 406 private void writeFooterAndClose(FileWriter configOut) throws IOException { 407 configOut.write("</cruisecontrol>\n"); 408 configOut.close(); 409 } 410 411 private void sleep(long l) { 412 try { 413 Thread.sleep(l); 414 } catch (InterruptedException dontCare) { 415 System.out.println("dontCare happened"); 416 } 417 } 418 419 private void writeProjectDetails(FileWriter configOut, final String projectName, int interval) throws IOException { 420 configOut.write("<project name='" + projectName + "'>\n"); 421 configOut.write(" <modificationset><alwaysbuild/></modificationset>\n"); 422 configOut.write(" <schedule interval=\"" + interval + "\"><ant/></schedule>\n"); 423 configOut.write("</project>\n"); 424 } 425 426 class MyListener implements CruiseControlController.Listener { 427 private List added = new ArrayList (); 428 private List removed = new ArrayList (); 429 public void clear() { 430 added.clear(); 431 removed.clear(); 432 } 433 public void projectAdded(Project project) { 434 added.add(project); 435 } 436 public void projectRemoved(Project project) { 437 removed.add(project); 438 } 439 } 440 } 441 | Popular Tags |