KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > testutil > TestUtil


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, 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
38 package net.sourceforge.cruisecontrol.testutil;
39
40 import java.util.Arrays JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.PrintWriter JavaDoc;
45 import java.io.BufferedWriter JavaDoc;
46 import java.io.FileWriter JavaDoc;
47 import java.io.IOException JavaDoc;
48
49 import junit.framework.Assert;
50
51 import org.jdom.Element;
52 import org.jdom.Document;
53
54 public final class TestUtil {
55
56     private TestUtil() {
57
58     }
59
60     public static Element createElement(boolean success, boolean lastBuildSuccess) {
61         return createElement(success, lastBuildSuccess, "2 minutes 20 seconds", 4, null);
62     }
63
64     public static Element createModsElement(int numMods) {
65         Element modificationsElement = new Element("modifications");
66         for (int i = 1; i <= numMods; i++) {
67             Element modificationElement = new Element("modification");
68
69             Element dateElement = new Element("date");
70             dateElement.addContent("10/30/2004 13:00:03"); // Put in dynamic value?
71
modificationElement.addContent(dateElement);
72
73             Element commentElement = new Element("comment");
74             commentElement.addContent("The comment");
75             modificationElement.addContent(commentElement);
76
77             Element revisionElement = new Element("revision");
78             revisionElement.addContent("1." + i);
79             modificationElement.addContent(revisionElement);
80
81             Element fileElement = new Element("file");
82             fileElement.setAttribute("action", "modified");
83
84             Element revision2 = new Element("revision");
85             revision2.addContent("1." + i);
86             fileElement.addContent(revision2);
87
88             Element fileName = new Element("filename");
89             fileName.addContent("filename" + i);
90             fileElement.addContent(fileName);
91             modificationElement.addContent(fileElement);
92
93             if (i != 1) {
94                 Element projectElement = new Element("project");
95                 projectElement.addContent("basedir/subdirectory" + i);
96                 fileElement.addContent(projectElement);
97             }
98
99             Element userElement = new Element("user");
100             int userNumber = (i > 2) ? i - 1 : i;
101             userElement.addContent("user" + userNumber);
102             modificationElement.addContent(userElement);
103
104             modificationsElement.addContent(modificationElement);
105         }
106         return modificationsElement;
107     }
108
109
110     public static Element createElement(
111         boolean success,
112         boolean lastBuildSuccess,
113         String JavaDoc time,
114         int modCount,
115         String JavaDoc failureReason) {
116         Element cruisecontrolElement = new Element("cruisecontrol");
117         Element buildElement = new Element("build");
118         buildElement.setAttribute("time", time);
119
120         if (!success) {
121             buildElement.setAttribute(
122                 "error",
123                 (failureReason == null) ? "Compile failed" : failureReason);
124         }
125
126         cruisecontrolElement.addContent(createModsElement(modCount));
127         cruisecontrolElement.addContent(buildElement);
128         cruisecontrolElement.addContent(
129             createInfoElement("somelabel", lastBuildSuccess));
130         Document doc = new Document();
131         doc.setRootElement(cruisecontrolElement);
132         return cruisecontrolElement;
133     }
134
135     public static Element createInfoElement(String JavaDoc label, boolean lastSuccessful) {
136         Element infoElement = new Element("info");
137
138         Hashtable JavaDoc properties = new Hashtable JavaDoc();
139         properties.put("projectname", "someproject");
140         properties.put("label", label);
141         properties.put("lastbuildtime", "");
142         properties.put("lastgoodbuildtime", "");
143         properties.put("lastbuildsuccessful", lastSuccessful + "");
144         properties.put("buildfile", "");
145         properties.put("builddate", "11/30/2005 12:07:27");
146         properties.put("target", "");
147         properties.put("logfile", "log20020313120000.xml");
148         properties.put("cctimestamp", "20020313120000");
149
150         Iterator JavaDoc propertyIterator = properties.keySet().iterator();
151         while (propertyIterator.hasNext()) {
152             String JavaDoc propertyName = (String JavaDoc) propertyIterator.next();
153             Element propertyElement = new Element("property");
154             propertyElement.setAttribute("name", propertyName);
155             propertyElement.setAttribute("value", (String JavaDoc) properties.get(propertyName));
156             infoElement.addContent(propertyElement);
157         }
158
159         return infoElement;
160     }
161
162     /**
163      * Return true when same.
164      */

165     public static void assertArray(String JavaDoc msg, String JavaDoc[] refarr, String JavaDoc[] testarr) {
166         Assert.assertNotNull(refarr);
167         Assert.assertNotNull(testarr);
168
169         Assert.assertNotNull(msg + " Reference array is null and test not", refarr);
170         Assert.assertNotNull(msg + " Test array is null and reference not", testarr);
171         int shorterLength = Math.min(refarr.length, testarr.length);
172
173         for (int i = 0; i < shorterLength; i++) {
174             Assert.assertEquals(msg + " Element " + i + " mismatch.", refarr[i], testarr[i]);
175         }
176         Assert.assertEquals(msg + " Arrays have different lengths", refarr.length, testarr.length);
177         Assert.assertEquals(msg, Arrays.asList(refarr), Arrays.asList(testarr));
178     }
179
180     public static void write(String JavaDoc text, File JavaDoc file) throws IOException JavaDoc {
181         PrintWriter JavaDoc out = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(file)));
182         out.print(text);
183         out.flush();
184         out.close();
185     }
186 }
187
Popular Tags