001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, 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.testutil;
037:
038: import java.io.File;
039: import java.io.UnsupportedEncodingException;
040: import java.util.Arrays;
041: import java.util.Hashtable;
042: import java.util.Iterator;
043: import java.util.List;
044: import java.util.Vector;
045: import java.net.URL;
046: import java.net.URLDecoder;
047:
048: import junit.framework.Assert;
049: import net.sourceforge.cruisecontrol.util.IO;
050:
051: import org.jdom.Document;
052: import org.jdom.Element;
053:
054: public final class TestUtil {
055:
056: public static class FilesToDelete {
057: private final List files = new Vector();
058:
059: public void add(File file) {
060: files.add(file);
061: }
062:
063: public void delete() {
064: Iterator fileIterator = files.iterator();
065: while (fileIterator.hasNext()) {
066: File file = (File) fileIterator.next();
067: IO.delete(file);
068: }
069: files.clear();
070: }
071: }
072:
073: private static File targetDir;
074:
075: public static File getTargetDir() {
076: if (targetDir == null) {
077: final URL url;
078: url = TestUtil.class.getClassLoader().getResource(
079: "net/sourceforge/cruisecontrol/BuilderTest.class");
080: final File classFile;
081: try {
082: classFile = new File(URLDecoder.decode(url.getPath(),
083: "utf-8"));
084: } catch (UnsupportedEncodingException e) {
085: throw new RuntimeException(e);
086: }
087: targetDir = classFile.getParentFile().getParentFile()
088: .getParentFile().getParentFile().getParentFile();
089:
090: final String absPath = targetDir.getAbsolutePath();
091: Assert.assertTrue("Unit test 'target' dir not a dir: "
092: + absPath, targetDir.isDirectory());
093: Assert.assertTrue("Unit test 'target' does not exist: "
094: + absPath, targetDir.exists());
095: Assert.assertEquals(
096: "Unit test 'target' dir has unexpected name: "
097: + absPath, "target", targetDir.getName());
098: Assert.assertEquals(
099: "Unit test 'target' dir in unexpected location: "
100: + absPath, "main", targetDir
101: .getParentFile().getName());
102: }
103: return targetDir;
104: }
105:
106: private TestUtil() {
107:
108: }
109:
110: public static Element createElement(boolean success,
111: boolean lastBuildSuccess) {
112: return createElement(success, lastBuildSuccess,
113: "2 minutes 20 seconds", 4, null);
114: }
115:
116: public static Element createModsElement(int numMods) {
117: Element modificationsElement = new Element("modifications");
118: for (int i = 1; i <= numMods; i++) {
119: Element modificationElement = new Element("modification");
120:
121: Element dateElement = new Element("date");
122: dateElement.addContent("10/30/2004 13:00:03"); // Put in dynamic value?
123: modificationElement.addContent(dateElement);
124:
125: Element commentElement = new Element("comment");
126: commentElement.addContent("The comment");
127: modificationElement.addContent(commentElement);
128:
129: Element revisionElement = new Element("revision");
130: revisionElement.addContent("1." + i);
131: modificationElement.addContent(revisionElement);
132:
133: Element fileElement = new Element("file");
134: fileElement.setAttribute("action", "modified");
135:
136: Element revision2 = new Element("revision");
137: revision2.addContent("1." + i);
138: fileElement.addContent(revision2);
139:
140: Element fileName = new Element("filename");
141: fileName.addContent("filename" + i);
142: fileElement.addContent(fileName);
143: modificationElement.addContent(fileElement);
144:
145: if (i != 1) {
146: Element projectElement = new Element("project");
147: projectElement.addContent("basedir/subdirectory" + i);
148: fileElement.addContent(projectElement);
149: }
150:
151: Element userElement = new Element("user");
152: int userNumber = (i > 2) ? i - 1 : i;
153: userElement.addContent("user" + userNumber);
154: modificationElement.addContent(userElement);
155:
156: modificationsElement.addContent(modificationElement);
157: }
158: return modificationsElement;
159: }
160:
161: public static Element createElement(boolean success,
162: boolean lastBuildSuccess, String time, int modCount,
163: String failureReason) {
164: Element cruisecontrolElement = new Element("cruisecontrol");
165: Element buildElement = new Element("build");
166: buildElement.setAttribute("time", time);
167:
168: if (!success) {
169: buildElement.setAttribute("error",
170: (failureReason == null) ? "Compile failed"
171: : failureReason);
172: }
173:
174: cruisecontrolElement.addContent(createModsElement(modCount));
175: cruisecontrolElement.addContent(buildElement);
176: cruisecontrolElement.addContent(createInfoElement("somelabel",
177: lastBuildSuccess));
178: Document doc = new Document();
179: doc.setRootElement(cruisecontrolElement);
180: return cruisecontrolElement;
181: }
182:
183: public static Element createInfoElement(String label,
184: boolean lastSuccessful) {
185: Element infoElement = new Element("info");
186:
187: Hashtable properties = new Hashtable();
188: properties.put("projectname", "someproject");
189: properties.put("label", label);
190: properties.put("lastbuildtime", "");
191: properties.put("lastgoodbuildtime", "");
192: properties.put("lastbuildsuccessful", lastSuccessful + "");
193: properties.put("buildfile", "");
194: properties.put("builddate", "11/30/2005 12:07:27");
195: properties.put("target", "");
196: properties.put("logfile", "log20020313120000.xml");
197: properties.put("cctimestamp", "20020313120000");
198:
199: Iterator propertyIterator = properties.keySet().iterator();
200: while (propertyIterator.hasNext()) {
201: String propertyName = (String) propertyIterator.next();
202: Element propertyElement = new Element("property");
203: propertyElement.setAttribute("name", propertyName);
204: propertyElement.setAttribute("value", (String) properties
205: .get(propertyName));
206: infoElement.addContent(propertyElement);
207: }
208:
209: return infoElement;
210: }
211:
212: /**
213: * Return true when same.
214: */
215: public static void assertArray(String msg, Object[] refarr,
216: Object[] testarr) {
217: Assert.assertNotNull(refarr);
218: Assert.assertNotNull(testarr);
219:
220: Assert.assertNotNull(msg
221: + " Reference array is null and test not", refarr);
222: Assert.assertNotNull(msg
223: + " Test array is null and reference not", testarr);
224: int shorterLength = Math.min(refarr.length, testarr.length);
225:
226: for (int i = 0; i < shorterLength; i++) {
227: Assert.assertEquals(msg + " Element " + i + " mismatch.",
228: refarr[i], testarr[i]);
229: }
230: Assert.assertEquals(msg + " Arrays have different lengths",
231: refarr.length, testarr.length);
232: Assert.assertEquals(msg, Arrays.asList(refarr), Arrays
233: .asList(testarr));
234: }
235: }
|