KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, 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 java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
42 import java.util.TreeSet JavaDoc;
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 JavaDoc {
60         super.setUp();
61         
62         defaultRegistry = PluginRegistry.loadDefaultPluginRegistry();
63     }
64     
65     /**
66      * @deprecated Testing deprecated code
67      */

68     public void testGetDefaultPropertiesNoPlugin() {
69         PluginRegistry registry = PluginRegistry.createRegistry();
70         assertEquals(0, registry.getDefaultProperties("qwertyuiop").size());
71     }
72  
73     public void testGettingPluginClass() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
94         PluginRegistry registry = PluginRegistry.createRegistry();
95
96         //Add a plugin with a classname that exists
97
final Class JavaDoc antBuilderClass = AntBuilder.class;
98         final String JavaDoc antBuilderClassname = antBuilderClass.getName();
99         registry.register("ant", antBuilderClassname);
100
101         //It should be registered
102
assertTrue(registry.isPluginRegistered("ant"));
103         assertEquals(antBuilderClassname, registry.getPluginClassname("ant"));
104         assertEquals(antBuilderClass, registry.getPluginClass("ant"));
105     }
106
107     public void testRootRegistryAndClassnameOverrideOverwrite() throws Exception JavaDoc {
108         PluginRegistry registry = PluginRegistry.createRegistry();
109         String JavaDoc antClassName = registry.getPluginClassname("ant");
110         String JavaDoc 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         // did the overwrite work?
117
assertNotSame(antClassName, registry.getPluginClassname("ant"));
118         // now override the root definition in the child registry
119
registry.register("ant", antClassName);
120         // does it mask the parent definition?
121
assertEquals(antClassName, registry.getPluginClassname("ant"));
122
123         // restore the root definition, or we'll wreck the other tests
124
pluginElement.setAttribute("classname", antClassName);
125         PluginRegistry.registerToRoot(pluginElement);
126     }
127
128     /**
129      * 2 levels of plugin registry, 1 plugin, 2 properties defined in
130      * parent, one overriden in child
131      *
132      * @deprecated Testing deprecated code
133      * @throws Exception on failure
134      */

135     public void testGetPluginConfigOverride() throws Exception JavaDoc {
136
137         PluginRegistry registry = PluginRegistry.createRegistry();
138
139         // 2 plugins in the root, with the same plugin class, but different names
140
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 JavaDoc defaultProperties;
160         // check getDefaultProperties
161
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         // now let's make some 'overrides'
171
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         // test the first plugin
182
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 JavaDoc wrappers = pluginConfig.getChildren("stringwrapper");
193         assertEquals(2, wrappers.size());
194         Set JavaDoc expectedWrapperAttributes = new TreeSet JavaDoc();
195         expectedWrapperAttributes.add("wrapper");
196         expectedWrapperAttributes.add("overriden");
197
198         Set JavaDoc wrapperAttributes = new TreeSet JavaDoc();
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         // test the second plugin
206
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 JavaDoc {
219         //Plugin names are treated as case-insensitive by CruiseControl, so
220
// a plugin named TestName and testname are the same.
221
PluginRegistry registry = PluginRegistry.createRegistry();
222
223         //Add a plugin with an all lowercase name
224
final String JavaDoc antBuilderClassname = AntBuilder.class.getName();
225         registry.register("testname", antBuilderClassname);
226
227         //It should be registered
228
assertTrue(registry.isPluginRegistered("testname"));
229
230         //If we ask if other case versions are registered, it should
231
// say "yes"
232
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 JavaDoc {
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         // skipped because not everyone has starteam api jar
266
// verifyPluginClass("starteam", "net.sourceforge.cruisecontrol.sourcecontrols.StarTeam");
267
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 JavaDoc pluginName, String JavaDoc expectedName)
331             throws Exception JavaDoc {
332         PluginRegistry registry = PluginRegistry.loadDefaultPluginRegistry();
333
334         assertTrue(registry.isPluginRegistered(pluginName));
335
336         String JavaDoc className = registry.getPluginClassname(pluginName);
337         assertEquals(expectedName, className);
338
339         Class JavaDoc pluginClass = Class.forName(className);
340         // casts to suppress varargs warnings under sdk >= 1.5
341
pluginClass.getConstructor((Class JavaDoc[]) null).newInstance((Object JavaDoc[]) null);
342     }
343 }
344
Popular Tags