001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001-2003, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol;
037:
038: import java.util.HashMap;
039: import java.util.Map;
040:
041: import junit.framework.TestCase;
042: import net.sourceforge.cruisecontrol.builders.AntBuilder;
043: import net.sourceforge.cruisecontrol.publishers.MockPublisher;
044: import net.sourceforge.cruisecontrol.publishers.email.MockMapping;
045:
046: import org.jdom.Element;
047:
048: public class PluginXMLHelperTest extends TestCase {
049: private PluginXMLHelper helper;
050: private ProjectXMLHelper projectXmlHelper;
051: private PluginRegistry registry;
052:
053: private static final int SOME_INT = 15;
054: private static final int SOME_OTHER_INT = 16;
055:
056: protected void setUp() throws CruiseControlException {
057: registry = PluginRegistry.loadDefaultPluginRegistry();
058: projectXmlHelper = new ProjectXMLHelper(new HashMap(), registry);
059: helper = new PluginXMLHelper(projectXmlHelper);
060: }
061:
062: protected void tearDown() throws Exception {
063: registry = null;
064: projectXmlHelper = null;
065: helper = null;
066: }
067:
068: public void testConfigure() throws Exception {
069: Element testElement = new Element("test");
070: testElement.setAttribute("somestring", "expectedString");
071: testElement.setAttribute("someint", Integer.toString(SOME_INT));
072: testElement.setAttribute("someboolean", "true");
073: Element childElement = new Element("mockpluginchild");
074: childElement.setAttribute("somestring", "childString");
075: childElement.setAttribute("someint", Integer
076: .toString(SOME_OTHER_INT));
077: testElement.addContent(childElement);
078: Element childMapper = new Element("mockMapper");
079: childMapper.setAttribute("address", "foo");
080: testElement.addContent(childMapper);
081:
082: Element pluginElement = new Element("plugin");
083: pluginElement.setAttribute("name", "mockMapper");
084: pluginElement
085: .setAttribute("classname",
086: "net.sourceforge.cruisecontrol.publishers.email.MockMapping");
087: // set a default value for the 'mockProperty' property of the MockMapping
088: pluginElement.setAttribute("mockProperty", "bar");
089:
090: registry.register(pluginElement);
091:
092: MockPublisher plugin = (MockPublisher) helper
093: .configure(
094: testElement,
095: Class
096: .forName("net.sourceforge.cruisecontrol.publishers.MockPublisher"),
097: false);
098:
099: assertEquals("expectedString", plugin.getSomeString());
100: assertEquals(SOME_INT, plugin.getSomeInt());
101: assertEquals(true, plugin.getSomeBoolean());
102: assertEquals("childString", plugin.getMockPluginChild()
103: .getSomeString());
104: assertEquals(SOME_OTHER_INT, plugin.getMockPluginChild()
105: .getSomeInt());
106: assertEquals("foo", plugin.getEmailMapping().getAddress());
107: assertEquals("bar", ((MockMapping) plugin.getEmailMapping())
108: .getMockProperty());
109: }
110:
111: public void testConfigureNoClass() {
112: try {
113: helper.configure(new Element("irrelevant"),
114: MockBadBuilder.class, false);
115: fail("Expected an exception because noclass shouldn't exist");
116: } catch (CruiseControlException expected) {
117: }
118: }
119:
120: public void testConfigureDefaultsForKnownPlugin()
121: throws CruiseControlException {
122: Element pluginElement = new Element("plugin");
123: pluginElement.setAttribute("name", "ant");
124: final String loggerClassName = "net.sourceforge.cruisecontrol.util.XmlLoggerWithStatus";
125: pluginElement.setAttribute("loggerClassName", loggerClassName);
126:
127: try {
128: registry.register(pluginElement);
129: } catch (CruiseControlException e) {
130: fail("Shouldn't get exception on missing classname for known plugin");
131: }
132:
133: AntBuilder antBuilder = (AntBuilder) projectXmlHelper
134: .configurePlugin(new Element("ant"), false);
135: assertEquals(loggerClassName, antBuilder.getLoggerClassName());
136: }
137: }
138:
139: /**
140: * Used to test an exception that gets thrown while instantiating a builder.
141: */
142: class MockBadBuilder extends Builder {
143:
144: /**
145: * A constructor that violates the rules for a Builder.
146: */
147: public MockBadBuilder(String ruleBreakingArg) {
148:
149: }
150:
151: public Element build(Map properties, Progress progress)
152: throws CruiseControlException {
153: return null;
154: }
155:
156: public Element buildWithTarget(Map properties, String target,
157: Progress progress) throws CruiseControlException {
158: return null;
159: }
160:
161: }
|