KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, 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.HashMap JavaDoc;
40 import java.util.Map JavaDoc;
41
42 import junit.framework.TestCase;
43 import net.sourceforge.cruisecontrol.builders.AntBuilder;
44 import net.sourceforge.cruisecontrol.publishers.MockPublisher;
45 import net.sourceforge.cruisecontrol.publishers.email.MockMapping;
46
47 import org.jdom.Element;
48
49 public class PluginXMLHelperTest extends TestCase {
50     private PluginXMLHelper helper;
51     private ProjectXMLHelper projectXmlHelper;
52     private PluginRegistry registry;
53
54     private static final int SOME_INT = 15;
55     private static final int SOME_OTHER_INT = 16;
56
57     protected void setUp() throws CruiseControlException {
58         registry = PluginRegistry.loadDefaultPluginRegistry();
59         projectXmlHelper = new ProjectXMLHelper(new HashMap JavaDoc(), registry);
60         helper = new PluginXMLHelper(projectXmlHelper);
61     }
62
63     protected void tearDown() throws Exception JavaDoc {
64         registry = null;
65         projectXmlHelper = null;
66         helper = null;
67     }
68
69     public void testConfigure() throws Exception JavaDoc {
70         Element testElement = new Element("test");
71         testElement.setAttribute("somestring", "expectedString");
72         testElement.setAttribute("someint", Integer.toString(SOME_INT));
73         testElement.setAttribute("someboolean", "true");
74         Element childElement = new Element("mockpluginchild");
75         childElement.setAttribute("somestring", "childString");
76         childElement.setAttribute("someint", Integer.toString(SOME_OTHER_INT));
77         testElement.addContent(childElement);
78         Element childMapper = new Element("mockMapper");
79         childMapper.setAttribute("address", "foo");
80         testElement.addContent(childMapper);
81
82         Element pluginElement = new Element("plugin");
83         pluginElement.setAttribute("name", "mockMapper");
84         pluginElement.setAttribute("classname", "net.sourceforge.cruisecontrol.publishers.email.MockMapping");
85         // set a default value for the 'mockProperty' property of the MockMapping
86
pluginElement.setAttribute("mockProperty", "bar");
87
88         registry.register(pluginElement);
89
90         MockPublisher plugin = (MockPublisher) helper.configure(testElement,
91                 Class.forName("net.sourceforge.cruisecontrol.publishers.MockPublisher"), false);
92
93         assertEquals("expectedString", plugin.getSomeString());
94         assertEquals(SOME_INT, plugin.getSomeInt());
95         assertEquals(true, plugin.getSomeBoolean());
96         assertEquals("childString", plugin.getMockPluginChild().getSomeString());
97         assertEquals(SOME_OTHER_INT, plugin.getMockPluginChild().getSomeInt());
98         assertEquals("foo", plugin.getEmailMapping().getAddress());
99         assertEquals("bar", ((MockMapping) plugin.getEmailMapping()).getMockProperty());
100     }
101
102     public void testConfigureNoClass() {
103         try {
104             helper.configure(new Element("irrelevant"), MockBadBuilder.class, false);
105             fail("Expected an exception because noclass shouldn't exist");
106         } catch (CruiseControlException expected) {
107         }
108     }
109
110     public void testConfigureDefaultsForKnownPlugin() throws CruiseControlException {
111         Element pluginElement = new Element("plugin");
112         pluginElement.setAttribute("name", "ant");
113         final String JavaDoc loggerClassName = "net.sourceforge.cruisecontrol.util.XmlLoggerWithStatus";
114         pluginElement.setAttribute("loggerClassName", loggerClassName);
115
116         try {
117             registry.register(pluginElement);
118         } catch (CruiseControlException e) {
119             fail("Shouldn't get exception on missing classname for known plugin");
120         }
121         
122         AntBuilder antBuilder = (AntBuilder) projectXmlHelper.configurePlugin(new Element("ant"), false);
123         assertEquals(loggerClassName, antBuilder.getLoggerClassName());
124     }
125 }
126
127 /**
128  * Used to test an exception that gets thrown while instantiating a builder.
129  */

130 class MockBadBuilder extends Builder {
131
132     /**
133      * A constructor that violates the rules for a Builder.
134      */

135     public MockBadBuilder(String JavaDoc ruleBreakingArg) {
136
137     }
138
139     public Element build(Map JavaDoc properties) throws CruiseControlException {
140         return null;
141     }
142     
143     public Element buildWithTarget(Map JavaDoc properties, String JavaDoc target) throws CruiseControlException {
144         return null;
145     }
146
147 }
148
Popular Tags