1 37 package net.sourceforge.cruisecontrol; 38 39 import java.util.List ; 40 import java.util.Map ; 41 import java.util.Set ; 42 import java.util.TreeSet ; 43 44 import junit.framework.TestCase; 45 import net.sourceforge.cruisecontrol.bootstrappers.CVSBootstrapper; 46 import net.sourceforge.cruisecontrol.builders.AntBuilder; 47 import net.sourceforge.cruisecontrol.listeners.ListenerTestPlugin; 48 import net.sourceforge.cruisecontrol.publishers.ExecutePublisher; 49 import net.sourceforge.cruisecontrol.publishers.SCPPublisher; 50 import net.sourceforge.cruisecontrol.sourcecontrols.ConcurrentVersionsSystem; 51 import net.sourceforge.cruisecontrol.sourcecontrols.SVN; 52 53 import org.jdom.Element; 54 55 public class PluginRegistryTest extends TestCase { 56 57 private PluginRegistry defaultRegistry; 58 59 protected void setUp() throws Exception { 60 super.setUp(); 61 62 defaultRegistry = PluginRegistry.loadDefaultPluginRegistry(); 63 } 64 65 68 public void testGetDefaultPropertiesNoPlugin() { 69 PluginRegistry registry = PluginRegistry.createRegistry(); 70 assertEquals(0, registry.getDefaultProperties("qwertyuiop").size()); 71 } 72 73 public void testGettingPluginClass() throws Exception { 74 PluginRegistry registry = PluginRegistry.createRegistry(); 75 76 assertNotNull(registry.getPluginClass("ant")); 77 } 78 79 public void testRegisteringPluginNoClass() { 80 PluginRegistry registry = PluginRegistry.createRegistry(); 81 82 final String nonExistentClassname = 83 "net.sourceforge.cruisecontrol.Foo" + System.currentTimeMillis(); 84 registry.register("foo", nonExistentClassname); 85 try { 86 registry.getPluginClass("foo"); 87 fail("Expected an exception when getting a plugin" 88 + " class that isn't loadable."); 89 } catch (CruiseControlException expected) { 90 } 91 } 92 93 public void testAddingPlugin() throws Exception { 94 PluginRegistry registry = PluginRegistry.createRegistry(); 95 96 final Class antBuilderClass = AntBuilder.class; 98 final String antBuilderClassname = antBuilderClass.getName(); 99 registry.register("ant", antBuilderClassname); 100 101 assertTrue(registry.isPluginRegistered("ant")); 103 assertEquals(antBuilderClassname, registry.getPluginClassname("ant")); 104 assertEquals(antBuilderClass, registry.getPluginClass("ant")); 105 } 106 107 public void testRootRegistryAndClassnameOverrideOverwrite() throws Exception { 108 PluginRegistry registry = PluginRegistry.createRegistry(); 109 String antClassName = registry.getPluginClassname("ant"); 110 String nonExistentClassname = 111 "net.sourceforge.cruisecontrol.Foo" + System.currentTimeMillis(); 112 Element pluginElement = new Element("plugin"); 113 pluginElement.setAttribute("name", "ant"); 114 pluginElement.setAttribute("classname", nonExistentClassname); 115 PluginRegistry.registerToRoot(pluginElement); 116 assertNotSame(antClassName, registry.getPluginClassname("ant")); 118 registry.register("ant", antClassName); 120 assertEquals(antClassName, registry.getPluginClassname("ant")); 122 123 pluginElement.setAttribute("classname", antClassName); 125 PluginRegistry.registerToRoot(pluginElement); 126 } 127 128 135 public void testGetPluginConfigOverride() throws Exception { 136 137 PluginRegistry registry = PluginRegistry.createRegistry(); 138 139 Element rootPluginElement = new Element("plugin"); 141 rootPluginElement.setAttribute("name", "testlistener"); 142 rootPluginElement.setAttribute("classname", ListenerTestPlugin.class.getName()); 143 rootPluginElement.setAttribute("string", "default"); 144 rootPluginElement.setAttribute("string2", "otherdefault"); 145 146 Element rootStringWrapper = new Element("stringwrapper"); 147 rootStringWrapper.setAttribute("string", "wrapper"); 148 rootPluginElement.addContent(rootStringWrapper); 149 150 PluginRegistry.registerToRoot(rootPluginElement); 151 152 Element otherRootPluginElement = new Element("plugin"); 153 otherRootPluginElement.setAttribute("name", "testlistener2"); 154 otherRootPluginElement.setAttribute("classname", ListenerTestPlugin.class.getName()); 155 otherRootPluginElement.setAttribute("string", "default2"); 156 157 PluginRegistry.registerToRoot(otherRootPluginElement); 158 159 Map defaultProperties; 160 defaultProperties = registry.getDefaultProperties("testlistener"); 162 assertEquals(2, defaultProperties.size()); 163 assertEquals("default", defaultProperties.get("string")); 164 assertEquals("otherdefault", defaultProperties.get("string2")); 165 166 defaultProperties = registry.getDefaultProperties("testlistener2"); 167 assertEquals(1, defaultProperties.size()); 168 assertEquals("default2", defaultProperties.get("string")); 169 170 Element pluginElement = new Element("plugin"); 172 pluginElement.setAttribute("name", "testlistener"); 173 pluginElement.setAttribute("string", "overriden"); 174 175 Element stringWrapper = new Element("stringwrapper"); 176 stringWrapper.setAttribute("string", "overriden"); 177 pluginElement.addContent(stringWrapper); 178 179 registry.register(pluginElement); 180 181 defaultProperties = registry.getDefaultProperties("testlistener"); 183 assertEquals(2, defaultProperties.size()); 184 assertEquals("overriden", defaultProperties.get("string")); 185 assertEquals("otherdefault", defaultProperties.get("string2")); 186 187 Element pluginConfig = registry.getPluginConfig("testlistener"); 188 assertEquals("testlistener", pluginConfig.getName()); 189 assertEquals("overriden", pluginConfig.getAttributeValue("string")); 190 assertEquals("otherdefault", pluginConfig.getAttributeValue("string2")); 191 192 List wrappers = pluginConfig.getChildren("stringwrapper"); 193 assertEquals(2, wrappers.size()); 194 Set expectedWrapperAttributes = new TreeSet (); 195 expectedWrapperAttributes.add("wrapper"); 196 expectedWrapperAttributes.add("overriden"); 197 198 Set wrapperAttributes = new TreeSet (); 199 for (int i = 0; i < wrappers.size(); i++) { 200 org.jdom.Element element = (org.jdom.Element) wrappers.get(i); 201 wrapperAttributes.add(element.getAttributeValue("string")); 202 } 203 assertEquals(expectedWrapperAttributes, wrapperAttributes); 204 205 defaultProperties = registry.getDefaultProperties("testlistener2"); 207 assertEquals(1, defaultProperties.size()); 208 assertEquals("default2", defaultProperties.get("string")); 209 210 Element otherPluginConfig = registry.getPluginConfig("testlistener2"); 211 assertEquals("testlistener2", otherPluginConfig.getName()); 212 assertEquals("default2", otherPluginConfig.getAttributeValue("string")); 213 assertEquals(null, otherPluginConfig.getAttributeValue("string2")); 214 215 PluginRegistry.resetRootRegistry(); 216 } 217 218 public void testCaseInsensitivityPluginNames() throws Exception { 219 PluginRegistry registry = PluginRegistry.createRegistry(); 222 223 final String antBuilderClassname = AntBuilder.class.getName(); 225 registry.register("testname", antBuilderClassname); 226 227 assertTrue(registry.isPluginRegistered("testname")); 229 230 assertTrue(registry.isPluginRegistered("TESTNAME")); 233 assertTrue(registry.isPluginRegistered("Testname")); 234 assertTrue(registry.isPluginRegistered("TestName")); 235 assertTrue(registry.isPluginRegistered("tESTnAME")); 236 } 237 238 public void testPluginRegistry() throws Exception { 239 240 verifyPluginClass( 241 "currentbuildstatusbootstrapper", 242 "net.sourceforge.cruisecontrol.bootstrappers.CurrentBuildStatusBootstrapper"); 243 verifyPluginClass( 244 "cvsbootstrapper", 245 "net.sourceforge.cruisecontrol.bootstrappers.CVSBootstrapper"); 246 verifyPluginClass( 247 "p4bootstrapper", 248 "net.sourceforge.cruisecontrol.bootstrappers.P4Bootstrapper"); 249 verifyPluginClass( 250 "svnbootstrapper", 251 "net.sourceforge.cruisecontrol.bootstrappers.SVNBootstrapper"); 252 verifyPluginClass( 253 "vssbootstrapper", 254 "net.sourceforge.cruisecontrol.bootstrappers.VssBootstrapper"); 255 verifyPluginClass("alienbrain", "net.sourceforge.cruisecontrol.sourcecontrols.AlienBrain"); 256 verifyPluginClass("clearcase", "net.sourceforge.cruisecontrol.sourcecontrols.ClearCase"); 257 verifyPluginClass("cvs", 258 "net.sourceforge.cruisecontrol.sourcecontrols.ConcurrentVersionsSystem"); 259 verifyPluginClass("filesystem", "net.sourceforge.cruisecontrol.sourcecontrols.FileSystem"); 260 verifyPluginClass("httpfile", "net.sourceforge.cruisecontrol.sourcecontrols.HttpFile"); 261 verifyPluginClass("mks", "net.sourceforge.cruisecontrol.sourcecontrols.MKS"); 262 verifyPluginClass("p4", "net.sourceforge.cruisecontrol.sourcecontrols.P4"); 263 verifyPluginClass("pvcs", "net.sourceforge.cruisecontrol.sourcecontrols.PVCS"); 264 verifyPluginClass("svn", "net.sourceforge.cruisecontrol.sourcecontrols.SVN"); 265 verifyPluginClass("vss", "net.sourceforge.cruisecontrol.sourcecontrols.Vss"); 268 verifyPluginClass("vssjournal", "net.sourceforge.cruisecontrol.sourcecontrols.VssJournal"); 269 verifyPluginClass("compound", "net.sourceforge.cruisecontrol.sourcecontrols.Compound"); 270 verifyPluginClass("triggers", "net.sourceforge.cruisecontrol.sourcecontrols.Triggers"); 271 verifyPluginClass("targets", "net.sourceforge.cruisecontrol.sourcecontrols.Targets"); 272 verifyPluginClass("ant", "net.sourceforge.cruisecontrol.builders.AntBuilder"); 273 verifyPluginClass("maven", "net.sourceforge.cruisecontrol.builders.MavenBuilder"); 274 verifyPluginClass("pause", "net.sourceforge.cruisecontrol.PauseBuilder"); 275 verifyPluginClass( 276 "labelincrementer", 277 "net.sourceforge.cruisecontrol.labelincrementers.DefaultLabelIncrementer"); 278 verifyPluginClass( 279 "artifactspublisher", 280 "net.sourceforge.cruisecontrol.publishers.ArtifactsPublisher"); 281 verifyPluginClass( 282 "currentbuildstatuspublisher", 283 "net.sourceforge.cruisecontrol.publishers.CurrentBuildStatusPublisher"); 284 verifyPluginClass("email", "net.sourceforge.cruisecontrol.publishers.LinkEmailPublisher"); 285 verifyPluginClass( 286 "htmlemail", 287 "net.sourceforge.cruisecontrol.publishers.HTMLEmailPublisher"); 288 verifyPluginClass("execute", "net.sourceforge.cruisecontrol.publishers.ExecutePublisher"); 289 verifyPluginClass("scp", "net.sourceforge.cruisecontrol.publishers.SCPPublisher"); 290 verifyPluginClass("modificationset", "net.sourceforge.cruisecontrol.ModificationSet"); 291 verifyPluginClass("schedule", "net.sourceforge.cruisecontrol.Schedule"); 292 verifyPluginClass("buildstatus", "net.sourceforge.cruisecontrol.sourcecontrols.BuildStatus"); 293 verifyPluginClass("clearcasebootstrapper", "net.sourceforge.cruisecontrol.bootstrappers.ClearCaseBootstrapper"); 294 verifyPluginClass("xsltlogpublisher", "net.sourceforge.cruisecontrol.publishers.XSLTLogPublisher"); 295 verifyPluginClass("httpfile", "net.sourceforge.cruisecontrol.sourcecontrols.HttpFile"); 296 verifyPluginClass("currentbuildstatuslistener", 297 "net.sourceforge.cruisecontrol.listeners.CurrentBuildStatusListener"); 298 verifyPluginClass("onsuccess", "net.sourceforge.cruisecontrol.publishers.OnSuccessPublisher"); 299 } 300 301 public void testCanLoadDefaultRegistry() { 302 assertTrue(defaultRegistry.isPluginRegistered("cvs")); 303 assertTrue(defaultRegistry.isPluginRegistered("antbootstrapper")); 304 } 305 306 public void testCanGetPluginName() { 307 PluginRegistry registry = defaultRegistry; 308 309 assertEquals("cvs", registry.getPluginName(ConcurrentVersionsSystem.class)); 310 assertEquals("cvsbootstrapper", registry.getPluginName(CVSBootstrapper.class)); 311 assertEquals("svn", registry.getPluginName(SVN.class)); 312 assertEquals("execute", registry.getPluginName(ExecutePublisher.class)); 313 assertEquals("scp", registry.getPluginName(SCPPublisher.class)); 314 } 315 316 public void testCanGetPluginDetails() throws CruiseControlException { 317 PluginDetail[] defaultPlugins = defaultRegistry.getPluginDetails(); 318 319 assertNotNull(defaultPlugins); 320 assertTrue(0 < defaultPlugins.length); 321 } 322 323 public void testCanGetPluginTypes() { 324 PluginType[] defaultTypes = defaultRegistry.getPluginTypes(); 325 326 assertNotNull(defaultTypes); 327 assertTrue(0 < defaultTypes.length); 328 } 329 330 static void verifyPluginClass(String pluginName, String expectedName) 331 throws Exception { 332 PluginRegistry registry = PluginRegistry.loadDefaultPluginRegistry(); 333 334 assertTrue(registry.isPluginRegistered(pluginName)); 335 336 String className = registry.getPluginClassname(pluginName); 337 assertEquals(expectedName, className); 338 339 Class pluginClass = Class.forName(className); 340 pluginClass.getConstructor((Class []) null).newInstance((Object []) null); 342 } 343 } 344 | Popular Tags |