1 37 package net.sourceforge.cruisecontrol; 38 39 import java.util.HashMap ; 40 import java.util.Map ; 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 (), registry); 60 helper = new PluginXMLHelper(projectXmlHelper); 61 } 62 63 protected void tearDown() throws Exception { 64 registry = null; 65 projectXmlHelper = null; 66 helper = null; 67 } 68 69 public void testConfigure() throws Exception { 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 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 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 130 class MockBadBuilder extends Builder { 131 132 135 public MockBadBuilder(String ruleBreakingArg) { 136 137 } 138 139 public Element build(Map properties) throws CruiseControlException { 140 return null; 141 } 142 143 public Element buildWithTarget(Map properties, String target) throws CruiseControlException { 144 return null; 145 } 146 147 } 148 | Popular Tags |