1 37 package net.sourceforge.cruisecontrol; 38 39 import java.io.File ; 40 import java.net.URL ; 41 import java.net.URLDecoder ; 42 import java.util.Enumeration ; 43 import java.util.Iterator ; 44 import java.util.List ; 45 import java.util.Properties ; 46 import java.util.Set ; 47 48 import junit.framework.TestCase; 49 import net.sourceforge.cruisecontrol.labelincrementers.DefaultLabelIncrementer; 50 import net.sourceforge.cruisecontrol.listeners.ListenerTestNestedPlugin; 51 import net.sourceforge.cruisecontrol.listeners.ListenerTestOtherNestedPlugin; 52 import net.sourceforge.cruisecontrol.listeners.ListenerTestPlugin; 53 import net.sourceforge.cruisecontrol.listeners.ListenerTestSelfConfiguringPlugin; 54 import net.sourceforge.cruisecontrol.util.Util; 55 56 import org.jdom.Element; 57 58 public class CruiseControlConfigTest extends TestCase { 59 60 private CruiseControlConfig config; 61 private File configFile; 62 private File tempDirectory; 63 private File propertiesFile; 64 65 private static final int ONE_SECOND = 1000; 66 67 protected void setUp() throws Exception { 68 URL url; 69 url = this.getClass().getClassLoader().getResource("net/sourceforge/cruisecontrol/test.properties"); 70 propertiesFile = new File (URLDecoder.decode(url.getPath())); 71 72 url = this.getClass().getClassLoader().getResource("net/sourceforge/cruisecontrol/testconfig.xml"); 74 configFile = new File (URLDecoder.decode(url.getPath())); 75 tempDirectory = configFile.getParentFile(); 76 77 Element rootElement = Util.loadConfigFile(configFile); 78 Properties globalProperties = new Properties (); 79 globalProperties.put("test.properties.dir", propertiesFile.getParentFile().getAbsolutePath()); 80 config = new CruiseControlConfig(globalProperties); 81 config.configure(rootElement); 82 } 83 84 protected void tearDown() { 85 File fooDirectory = new File (tempDirectory, "foo"); 90 fooDirectory.delete(); 91 } 92 93 public void testProjectNamesShouldMatchOrderInFile() { 94 Set names = config.getProjectNames(); 95 Iterator iter = names.iterator(); 96 assertEquals("project1", (String ) iter.next()); 97 assertEquals("project2", (String ) iter.next()); 98 assertEquals("project3", (String ) iter.next()); 99 assertEquals("project3bis", (String ) iter.next()); 100 assertEquals("project4", (String ) iter.next()); 101 } 102 103 public void testGetProjectNames() { 104 assertEquals(16, config.getProjectNames().size()); 105 } 106 107 public void testGlobalProperty() throws CruiseControlException { 108 ProjectConfig projConfig = config.getConfig("simpleprops"); 109 Properties props = projConfig.getProperties(); 110 assertEquals(5, props.size()); assertEquals("works!", props.getProperty("global")); 112 } 113 114 public void testProjectNameProperty() throws CruiseControlException { 115 ProjectConfig projConfig = config.getConfig("project1"); 116 Properties props = projConfig.getProperties(); 117 assertEquals(4, props.size()); 118 assertEquals("project1", props.getProperty("project.name")); 119 } 120 121 public void testProjectNameInGlobalProperty() throws CruiseControlException { 122 ProjectConfig projConfig = config.getConfig("project1"); 123 Properties props = projConfig.getProperties(); 124 assertEquals(4, props.size()); 125 assertEquals("works!", props.getProperty("global")); 126 assertEquals("project1", props.getProperty("project.name")); 127 assertEquals("project=project1", props.getProperty("project.global")); 128 } 129 130 public void testSimpleProperty() throws CruiseControlException { 131 ProjectConfig projConfig = config.getConfig("simpleprops"); 132 Properties props = projConfig.getProperties(); 133 assertEquals(5, props.size()); 134 assertEquals("success!", props.getProperty("simple")); 135 } 136 137 public void testMultipleProperties() throws CruiseControlException { 138 ProjectConfig projConfig = config.getConfig("multiprops"); 139 Properties props = projConfig.getProperties(); 140 assertEquals(8, props.size()); 141 assertEquals("one", props.getProperty("first")); 142 assertEquals("two", props.getProperty("second")); 143 assertEquals("three", props.getProperty("third")); 144 assertEquals("one.two$three", props.getProperty("multi")); 145 } 146 147 public void testNestedProperties() throws CruiseControlException { 148 ProjectConfig projConfig = config.getConfig("nestedprops"); 149 Properties props = projConfig.getProperties(); 150 assertEquals(10, props.size()); 151 assertEquals("one", props.getProperty("first")); 152 assertEquals("two", props.getProperty("second")); 153 assertEquals("three", props.getProperty("third")); 154 assertEquals("almost", props.getProperty("one.two.three")); 155 assertEquals("threeLevelsDeep", props.getProperty("almost")); 156 assertEquals("threeLevelsDeep", props.getProperty("nested")); 157 } 158 159 public void testPropertyEclipsing() throws CruiseControlException { 160 ProjectConfig projConfig = config.getConfig("eclipseprop"); 161 Properties props = projConfig.getProperties(); 162 assertEquals(4, props.size()); 163 assertEquals("eclipsed", props.getProperty("global")); 164 } 165 166 public void testLoadPropertiesFromFile() throws CruiseControlException { 167 ProjectConfig projConfig = config.getConfig("propsfromfile"); 168 Properties props = projConfig.getProperties(); 169 assertEquals(9, props.size()); 170 assertEquals("/home/cruise", props.getProperty("dir1")); 171 assertEquals("/home/cruise/logs", props.getProperty("dir2")); 172 assertEquals("temp", props.getProperty("tempdir")); 173 assertEquals("/home/cruise/logs/temp", props.getProperty("multi")); 174 } 175 176 public void testPropertiesInProperties() throws CruiseControlException { 178 ProjectConfig projConfig = config.getConfig("propsinpropsdef"); 179 Properties props = projConfig.getProperties(); 180 assertEquals("true", props.getProperty("env.toupper")); 182 assertEquals("env", props.getProperty("env.prefix")); 183 184 assertEquals("Resolving property file name attribute worked", 185 "/home/cruise", props.getProperty("dir1")); 186 assertEquals("Resolving property name attribute worked", 187 "test1", props.getProperty("test1")); 188 int nbEnvPropertiesFound = 0; 189 for (Enumeration propertyNames = props.propertyNames(); propertyNames.hasMoreElements(); ) { 190 String name = (String ) propertyNames.nextElement(); 191 if (name.startsWith("env.")) { 192 nbEnvPropertiesFound++; 193 } 194 } 195 assertTrue("Resolving environment prefix attribute worked", 196 nbEnvPropertiesFound > 0); 197 assertNotNull("Resolving environment prefix and touuper attributes worked", 198 props.getProperty("env.PATH")); 199 } 200 201 222 223 public void testGetPluginConfigNoOverride() throws Exception { 225 ProjectConfig projConfig = config.getConfig("project1"); 226 PluginRegistry registry = config.getProjectPlugins("project1"); 227 228 assertEquals(ListenerTestNestedPlugin.class, registry.getPluginClass("testnested")); 229 230 final ProjectXMLHelper helper 231 = new ProjectXMLHelper(projConfig.getProperties(), registry); 232 233 PluginXMLHelper pluginHelper = new PluginXMLHelper(helper); 234 Object plugin; 235 236 plugin = helper.getConfiguredPlugin(pluginHelper, "testnested"); 237 assertEquals(ListenerTestNestedPlugin.class, plugin.getClass()); 238 ListenerTestNestedPlugin plug1 = (ListenerTestNestedPlugin) plugin; 239 assertEquals("default", plug1.getString()); 240 assertEquals("otherdefault", plug1.getOtherString()); 241 242 plugin = helper.getConfiguredPlugin(pluginHelper, "testselfconfiguring"); 243 assertEquals(null, plugin); 244 245 plugin = helper.getConfiguredPlugin(pluginHelper, "testlistener"); 246 assertEquals(null, plugin); 247 } 248 249 public void testGetPluginConfig() throws Exception { 251 ProjectConfig projConfig = config.getConfig("project4"); 252 PluginRegistry registry = config.getProjectPlugins("project4"); 253 254 final ProjectXMLHelper helper 255 = new ProjectXMLHelper(projConfig.getProperties(), registry); 256 257 PluginXMLHelper pluginHelper = new PluginXMLHelper(helper); 258 Object plugin; 259 260 plugin = helper.getConfiguredPlugin(pluginHelper, "testnested"); 261 assertEquals(ListenerTestNestedPlugin.class, plugin.getClass()); 262 ListenerTestNestedPlugin plug1 = (ListenerTestNestedPlugin) plugin; 263 assertEquals("overriden", plug1.getString()); 264 assertEquals("otherdefault", plug1.getOtherString()); 266 267 plugin = helper.getConfiguredPlugin(pluginHelper, "testselfconfiguring"); 268 assertEquals(ListenerTestSelfConfiguringPlugin.class, plugin.getClass()); 269 ListenerTestSelfConfiguringPlugin plug2 = (ListenerTestSelfConfiguringPlugin) plugin; 270 assertEquals(null, plug2.getString()); 271 assertEquals(null, plug2.getNested()); 272 273 plugin = helper.getConfiguredPlugin(pluginHelper, "testlistener"); 274 assertEquals(ListenerTestPlugin.class, plugin.getClass()); 275 ListenerTestPlugin plug3 = (ListenerTestPlugin) plugin; 276 assertEquals("project4-0", plug3.getString()); 277 } 278 279 public void testPluginConfiguration() throws Exception { 280 ProjectConfig projConfig = config.getConfig("project4"); 281 PluginRegistry plugins = config.getProjectPlugins("project4"); 282 283 assertEquals(ListenerTestPlugin.class, plugins.getPluginClass("testlistener")); 284 assertEquals(ListenerTestNestedPlugin.class, plugins.getPluginClass("testnested")); 285 assertEquals(ListenerTestSelfConfiguringPlugin.class, plugins.getPluginClass("testselfconfiguring")); 286 287 List listeners = projConfig.getListeners(); 288 assertEquals(3, listeners.size()); 289 290 Listener listener0 = (Listener) listeners.get(0); 291 assertEquals(ListenerTestPlugin.class, listener0.getClass()); 292 ListenerTestPlugin testListener0 = (ListenerTestPlugin) listener0; 293 assertEquals("project4-0", testListener0.getString()); 294 295 Listener listener1 = (Listener) listeners.get(1); 296 assertEquals(ListenerTestPlugin.class, listener1.getClass()); 297 ListenerTestPlugin testListener1 = (ListenerTestPlugin) listener1; 298 assertEquals("listener1", testListener1.getString()); 299 assertEquals("wrapper1", testListener1.getStringWrapper().getString()); 300 301 Listener listener2 = (Listener) listeners.get(2); 302 assertEquals(ListenerTestPlugin.class, listener2.getClass()); 303 ListenerTestPlugin testListener2 = (ListenerTestPlugin) listener2; 304 assertEquals("listener2", testListener2.getString()); 305 final String wrapper = testListener2.getStringWrapper().getString(); 310 assertTrue("wrapper2-works!", "wrapper2-works!".equals(wrapper) 311 || "wrapper1".equals(wrapper)); 312 } 313 314 public void testPluginConfigurationClassOverride() throws Exception { 315 ProjectConfig projConfig = config.getConfig("project5"); 316 PluginRegistry plugins = config.getProjectPlugins("project5"); 317 318 assertEquals(ListenerTestPlugin.class, plugins.getPluginClass("testlistener")); 319 assertEquals(ListenerTestOtherNestedPlugin.class, plugins.getPluginClass("testnested")); 320 321 List listeners = projConfig.getListeners(); 322 assertEquals(1, listeners.size()); 323 324 Listener listener0 = (Listener) listeners.get(0); 325 assertEquals(ListenerTestPlugin.class, listener0.getClass()); 326 ListenerTestPlugin testListener0 = (ListenerTestPlugin) listener0; 327 assertEquals("default", testListener0.getString()); 328 ListenerTestNestedPlugin nested = testListener0.getNested(); 329 assertTrue(nested instanceof ListenerTestOtherNestedPlugin); 330 assertEquals("notshadowing", ((ListenerTestOtherNestedPlugin) nested).getString()); 331 assertEquals(null, ((ListenerTestOtherNestedPlugin) nested).getOtherString()); 332 assertEquals("otherother", ((ListenerTestOtherNestedPlugin) nested).getOtherOtherString()); 333 } 334 335 359 360 public void testGetBootstrappers() throws CruiseControlException { 361 ProjectConfig projConfig = config.getConfig("project1"); 362 363 List bootstrappers = projConfig.getBootstrappers(); 364 assertEquals(0, bootstrappers.size()); 365 366 projConfig = config.getConfig("project2"); 367 bootstrappers = projConfig.getBootstrappers(); 368 assertEquals(1, bootstrappers.size()); 369 } 370 371 public void testGetSchedule() throws CruiseControlException { 372 ProjectConfig projConfig; 373 382 383 projConfig = config.getConfig("project2"); 384 Schedule schedule = projConfig.getSchedule(); 385 assertEquals(20 * ONE_SECOND, schedule.getInterval()); 386 } 387 388 public void testGetModificationSet() throws CruiseControlException { 389 ProjectConfig projConfig; 390 399 400 projConfig = config.getConfig("project2"); 401 ModificationSet modSet = projConfig.getModificationSet(); 402 assertEquals(10 * ONE_SECOND, modSet.getQuietPeriod()); 403 } 404 405 public void testGetLabelIncrementer() throws CruiseControlException { 406 Element pluginElement = new Element("plugin"); 407 pluginElement.setAttribute("name", CruiseControlConfig.LABEL_INCREMENTER); 408 pluginElement.setAttribute("classname", DefaultLabelIncrementer.class.getName()); 409 PluginRegistry.registerToRoot(pluginElement); 410 ProjectConfig projConfig = config.getConfig("project2"); 411 DefaultLabelIncrementer incrementer = (DefaultLabelIncrementer) projConfig.getLabelIncrementer(); 412 assertTrue(incrementer.isValidLabel("build#9")); 413 414 projConfig = config.getConfig("project1"); 415 incrementer = (DefaultLabelIncrementer) projConfig.getLabelIncrementer(); 416 assertFalse(incrementer.isValidLabel("build#9")); 417 } 418 419 public void testGetLog() throws CruiseControlException { 420 ProjectConfig projConfig = config.getConfig("project1"); 421 assertEquals("logs" + File.separatorChar + "project1", projConfig.getLog().getLogDir()); 422 423 projConfig = config.getConfig("project2"); 424 assertEquals(tempDirectory.getAbsolutePath() + "/foo", projConfig.getLog().getLogDir()); 425 426 projConfig = config.getConfig("project3"); 427 assertEquals("logs" + File.separatorChar + "project3", projConfig.getLog().getLogDir()); 428 429 projConfig = config.getConfig("project3bis"); 430 assertEquals("logs/project3bis", projConfig.getLog().getLogDir()); 431 assertNull(projConfig.getLog().getLogXmlEncoding()); 432 433 projConfig = config.getConfig("project2"); 434 assertEquals("utf-8", projConfig.getLog().getLogXmlEncoding()); 435 } 436 437 public void testPreconfigureLog() throws Exception { 438 ProjectConfig projConfig = config.getConfig("logpreconfigured"); 439 440 final Log log = projConfig.getLog(); 441 assertEquals("mylogs/logpreconfigured", log.getLogDir()); 442 assertEquals("utf128", log.getLogXmlEncoding()); 443 assertEquals("logpreconfigured", log.getProjectName()); 444 445 BuildLogger[] loggers = log.getLoggers(); 446 assertEquals(2, loggers.length); 447 } 448 449 public void testGetListeners() throws CruiseControlException { 450 ProjectConfig projConfig = config.getConfig("project1"); 451 List listeners = projConfig.getListeners(); 452 assertEquals(0, listeners.size()); 453 454 projConfig = config.getConfig("project2"); 455 listeners = projConfig.getListeners(); 456 assertEquals(1, listeners.size()); 457 } 458 } 459 | Popular Tags |