001: package net.sourceforge.cruisecontrol;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.InputStream;
005:
006: import junit.framework.TestCase;
007: import net.sourceforge.cruisecontrol.config.XmlResolver;
008: import net.sourceforge.cruisecontrol.util.Util;
009:
010: import org.jdom.Element;
011:
012: public class CruiseControlConfigIncludeTest extends TestCase {
013:
014: private Element rootElement;
015: private Element includeElement;
016: private XmlResolver resolver;
017:
018: protected void setUp() throws Exception {
019: StringBuffer configText = new StringBuffer(200);
020: configText.append("<cruisecontrol>");
021: configText.append(" <plugin name='foo.project'");
022: configText
023: .append(" classname='net.sourceforge.cruisecontrol.MockProjectInterface'/>");
024: configText.append(" <include.projects file='include.xml'/>");
025: configText.append(" <foo.project name='in.root'/>");
026: configText.append("</cruisecontrol>");
027: rootElement = elementFromString(configText.toString());
028:
029: StringBuffer includeText = new StringBuffer(200);
030: includeText.append("<cruisecontrol>");
031: includeText.append(" <foo.project name='in.include'/>");
032: includeText.append("</cruisecontrol>");
033: includeElement = elementFromString(includeText.toString());
034:
035: resolver = new IncludeXmlResolver(includeElement);
036: }
037:
038: protected void tearDown() throws Exception {
039: rootElement = null;
040: includeElement = null;
041: resolver = null;
042: }
043:
044: public void testShouldLoadIncludedProjects() throws Exception {
045: CruiseControlConfig config = new CruiseControlConfig(
046: rootElement, resolver, null);
047: assertEquals(2, config.getProjectNames().size());
048: assertIsFooProject(config.getProject("in.root"));
049: assertIsFooProject(config.getProject("in.include"));
050: }
051:
052: public void testShouldLoadedNestedIncludes() throws Exception {
053: StringBuffer includeText = new StringBuffer(200);
054: includeText.append("<cruisecontrol>");
055: includeText.append(" <include.projects file='include.xml'/>");
056: includeText.append(" <foo.project name='in.first.include'/>");
057: includeText.append("</cruisecontrol>");
058: Element includeWithNestedInclude = elementFromString(includeText
059: .toString());
060:
061: Element[] elements = new Element[2];
062: elements[0] = includeWithNestedInclude;
063: elements[1] = includeElement;
064: resolver = new IncludeXmlResolver(elements);
065:
066: CruiseControlConfig config = new CruiseControlConfig(
067: rootElement, resolver, null);
068: assertEquals(3, config.getProjectNames().size());
069: assertIsFooProject(config.getProject("in.root"));
070: assertIsFooProject(config.getProject("in.first.include"));
071: assertIsFooProject(config.getProject("in.include"));
072: }
073:
074: public void testIncludesCanDefinePlugins()
075: throws CruiseControlException {
076: String newProjectTag = "new.project.type";
077:
078: Element pluginElement = new Element("plugin");
079: pluginElement.setAttribute("name", newProjectTag);
080: pluginElement.setAttribute("classname",
081: MockProjectInterface.class.getName());
082: includeElement.addContent(pluginElement);
083:
084: Element barElement = new Element(newProjectTag);
085: barElement.setAttribute("name", "bar");
086: includeElement.addContent(barElement);
087:
088: CruiseControlConfig config = new CruiseControlConfig(
089: rootElement, resolver, null);
090: assertEquals(3, config.getProjectNames().size());
091: assertIsFooProject(config.getProject("bar"));
092: }
093:
094: public void testPropertiesShouldBeAvailableToIncludedProjects()
095: throws CruiseControlException {
096: Element property = new Element("property");
097: property.setAttribute("name", "baz");
098: property.setAttribute("value", "goo");
099: rootElement.addContent(property);
100:
101: Element project = new Element("foo.project");
102: project.setAttribute("name", "${baz}");
103: includeElement.addContent(project);
104:
105: CruiseControlConfig config = new CruiseControlConfig(
106: rootElement, resolver);
107: assertEquals(3, config.getProjectNames().size());
108: assertIsFooProject(config.getProject("goo"));
109: }
110:
111: public void testErrorsInIncludeShouldBeContained()
112: throws CruiseControlException {
113: Element unknownPlugin = new Element("unknown.plugin.error");
114: includeElement.addContent(unknownPlugin);
115:
116: CruiseControlConfig config = new CruiseControlConfig(
117: rootElement, resolver);
118: assertEquals(1, config.getProjectNames().size());
119: assertIsFooProject(config.getProject("in.root"));
120: }
121:
122: public void testErrorsParsingIncludeShouldBeContained()
123: throws CruiseControlException {
124: XmlResolver resolverHitsError = new XmlResolver() {
125: public Element getElement(String path)
126: throws CruiseControlException {
127: throw new CruiseControlException("simulate parse error");
128: }
129: };
130:
131: CruiseControlConfig config = new CruiseControlConfig(
132: rootElement, resolverHitsError);
133: assertEquals(1, config.getProjectNames().size());
134: assertIsFooProject(config.getProject("in.root"));
135: }
136:
137: public void testIncludeFilenameContainsProperty()
138: throws CruiseControlException {
139: XmlResolver includeFOOXmlResolver = new XmlResolver() {
140: private final Element includePropertyElement;
141: {
142: StringBuffer includeText = new StringBuffer(200);
143: includeText.append("<cruisecontrol>");
144: includeText
145: .append(" <foo.project name='in.include.withproperty'/>");
146: includeText.append("</cruisecontrol>");
147: includePropertyElement = elementFromString(includeText
148: .toString());
149: }
150:
151: public Element getElement(String path)
152: throws CruiseControlException {
153: assertEquals("include_FOO_.xml", path);
154: return includePropertyElement;
155: }
156: };
157:
158: Element propertyElement = new Element("property");
159: propertyElement.setAttribute("name", "filenameswitch");
160: propertyElement.setAttribute("value", "_FOO_");
161: rootElement.addContent(propertyElement);
162: rootElement.removeChild("include.projects");
163: Element includeTagElement = new Element("include.projects");
164: includeTagElement.setAttribute("file",
165: "include${filenameswitch}.xml");
166: rootElement.addContent(includeTagElement);
167:
168: CruiseControlConfig config = new CruiseControlConfig(
169: rootElement, includeFOOXmlResolver);
170: assertEquals(2, config.getProjectNames().size());
171: assertIsFooProject(config.getProject("in.root"));
172: assertIsFooProject(config.getProject("in.include.withproperty"));
173: }
174:
175: public void testIncludeFilenameContainsUnsetProperty()
176: throws CruiseControlException {
177: XmlResolver includeUnknownXmlResolver = new XmlResolver() {
178: public Element getElement(String path)
179: throws CruiseControlException {
180: throw new CruiseControlException(
181: "failed to load file []");
182: }
183: };
184:
185: rootElement.removeChild("include.projects");
186: Element includeTagElement = new Element("include.projects");
187: includeTagElement.setAttribute("file",
188: "include${filenameswitch}.xml");
189: rootElement.addContent(includeTagElement);
190:
191: CruiseControlConfig config = new CruiseControlConfig(
192: rootElement, includeUnknownXmlResolver);
193: assertEquals(1, config.getProjectNames().size());
194: assertIsFooProject(config.getProject("in.root"));
195: }
196:
197: public static Element elementFromString(String text)
198: throws CruiseControlException {
199: InputStream is = new ByteArrayInputStream(text.getBytes());
200: return Util.loadRootElement(is);
201: }
202:
203: private void assertIsFooProject(ProjectInterface project) {
204: assertNotNull(project);
205: assertEquals(MockProjectInterface.class.getName(), project
206: .getClass().getName());
207: }
208:
209: private class IncludeXmlResolver implements XmlResolver {
210:
211: private final Element[] includeElements;
212: private int count = 0;
213:
214: IncludeXmlResolver(Element element) {
215: includeElements = new Element[] { element };
216: }
217:
218: IncludeXmlResolver(Element[] elements) {
219: includeElements = elements;
220: }
221:
222: public Element getElement(String path)
223: throws CruiseControlException {
224: assertEquals("include.xml", path);
225: Element element = includeElements[count];
226: count++;
227: return element;
228: }
229: }
230:
231: }
|