KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > XMLLogHelperTest


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 package net.sourceforge.cruisecontrol.util;
38
39 import java.text.SimpleDateFormat JavaDoc;
40 import java.util.Date JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.Set JavaDoc;
45
46 import junit.framework.TestCase;
47 import net.sourceforge.cruisecontrol.CruiseControlException;
48 import net.sourceforge.cruisecontrol.Modification;
49 import net.sourceforge.cruisecontrol.testutil.TestUtil;
50
51 import org.jdom.Element;
52
53 /**
54  * @version $Id: XMLLogHelperTest.java,v 1.12 2005/03/16 22:31:38 jfredrick Exp $
55  */

56 public class XMLLogHelperTest extends TestCase {
57
58     private Element successfulLogElement;
59     private Element failedLogElement;
60
61     private static final Date JavaDoc SOME_DATE = new Date JavaDoc(2333);
62     private static final SimpleDateFormat JavaDoc DATE_FORMAT = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
63
64     public XMLLogHelperTest(String JavaDoc name) {
65         super(name);
66     }
67
68     private Modification[] createModifications(String JavaDoc username1, String JavaDoc username2) {
69         Modification[] mods = new Modification[3];
70         mods[0] = createModification(username1, false);
71         mods[1] = createModification(username2, false);
72         mods[2] = createModification("user3", true);
73         return mods;
74     }
75
76     private Modification createModification(String JavaDoc name, boolean addemail) {
77         Modification mod = new Modification();
78         mod.userName = name;
79         mod.comment = "This is the checkin for " + name;
80         if (addemail) {
81             mod.emailAddress = name + "@host.com";
82         }
83         mod.modifiedTime = SOME_DATE;
84
85         Modification.ModifiedFile modfile = mod.createModifiedFile("file.txt", "myfolder");
86         modfile.action = "checkin";
87         return mod;
88     }
89
90     private Element createModificationsElement(String JavaDoc username1, String JavaDoc username2) {
91         Element modificationsElement = new Element("modifications");
92         Modification[] mods = createModifications(username1, username2);
93         modificationsElement.addContent(mods[0].toElement(DATE_FORMAT));
94         modificationsElement.addContent(mods[1].toElement(DATE_FORMAT));
95         modificationsElement.addContent(mods[2].toElement(DATE_FORMAT));
96         return modificationsElement;
97     }
98
99     private Element createBuildElement(boolean successful) {
100         Element buildElement = new Element("build");
101         if (!successful) {
102             buildElement.setAttribute("error", "No Build Necessary");
103         }
104         return buildElement;
105     }
106
107     public void setUp() {
108         successfulLogElement = new Element("cruisecontrol");
109         successfulLogElement.addContent(TestUtil.createInfoElement("1.0", false));
110         successfulLogElement.addContent(createBuildElement(true));
111         successfulLogElement.addContent(createModificationsElement("username1", "username2"));
112
113         failedLogElement = new Element("cruisecontrol");
114         failedLogElement.addContent(TestUtil.createInfoElement("1.1", true));
115         failedLogElement.addContent(createBuildElement(false));
116         failedLogElement.addContent(createModificationsElement("username3", "username4"));
117     }
118
119     public void testGetLabel() {
120         XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement);
121         XMLLogHelper failureHelper = new XMLLogHelper(failedLogElement);
122         try {
123             assertEquals("1.0", successHelper.getLabel());
124             assertEquals("1.1", failureHelper.getLabel());
125         } catch (CruiseControlException e) {
126             assertTrue(false);
127         }
128     }
129
130     public void testGetLogFileName() {
131         XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement);
132         try {
133             assertEquals("log20020313120000.xml", successHelper.getLogFileName());
134         } catch (CruiseControlException e) {
135             assertTrue(false);
136         }
137     }
138
139     public void testWasPreviousBuildSuccessful() {
140         XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement);
141         XMLLogHelper failureHelper = new XMLLogHelper(failedLogElement);
142         try {
143             assertEquals(false, successHelper.wasPreviousBuildSuccessful());
144             assertEquals(true, failureHelper.wasPreviousBuildSuccessful());
145         } catch (CruiseControlException e) {
146             assertTrue(false);
147         }
148     }
149
150     public void testGetCruiseControlInfoProperty() {
151         XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement);
152         try {
153             assertEquals("1.0", successHelper.getCruiseControlInfoProperty("label"));
154         } catch (CruiseControlException e) {
155             assertTrue(false);
156         }
157         try {
158             successHelper.getCruiseControlInfoProperty("notaproperty");
159         } catch (CruiseControlException e) {
160             assertEquals("Property: notaproperty not found.", e.getMessage());
161         }
162     }
163
164     public void testIsBuildNecessary() {
165         XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement);
166         XMLLogHelper failureHelper = new XMLLogHelper(failedLogElement);
167         assertEquals(true, successHelper.isBuildNecessary());
168         assertEquals(false, failureHelper.isBuildNecessary());
169     }
170
171     public void testGetProjectName() {
172         XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement);
173
174         try {
175             assertEquals("someproject", successHelper.getProjectName());
176         } catch (CruiseControlException e) {
177             assertTrue(false);
178         }
179     }
180
181     public void testIsBuildSuccessful() {
182         XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement);
183         XMLLogHelper failureHelper = new XMLLogHelper(failedLogElement);
184         assertEquals(true, successHelper.isBuildSuccessful());
185         assertEquals(false, failureHelper.isBuildSuccessful());
186     }
187
188     public void testGetBuildParticipants() {
189         XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement);
190         Set JavaDoc successHelperParticipants = successHelper.getBuildParticipants();
191         assertEquals(true, successHelperParticipants.contains("username1"));
192         assertEquals(true, successHelperParticipants.contains("username2"));
193         assertEquals(false, successHelperParticipants.contains("notaperson"));
194         assertEquals(true, successHelperParticipants.contains("user3@host.com"));
195
196         //test P4 changelist structure
197
Element ccElement = new Element("cruisecontrol");
198         Element modsElement = new Element("modifications");
199         Element cl1Element = new Element("changelist");
200         cl1Element.setAttribute("user", "user1");
201         Element cl2Element = new Element("changelist");
202         cl2Element.setAttribute("user", "user2");
203
204         modsElement.addContent(cl1Element);
205         modsElement.addContent(cl2Element);
206         ccElement.addContent(modsElement);
207
208         XMLLogHelper helper = new XMLLogHelper(ccElement);
209         Set JavaDoc p4Users = helper.getBuildParticipants();
210
211         assertEquals(true, p4Users.contains("user1"));
212         assertEquals(true, p4Users.contains("user2"));
213         assertEquals(false, p4Users.contains("notaperson"));
214     }
215
216     public void testGetModifications() {
217         //NOTE: There is an issue with dateformat if you convert
218
//a date to a string and parse it back to a date the milliseconds will
219
//be different. Therefore the test gets all of the modifications
220
//and sets the date on all of them to account for this, after it compares the date by string
221
XMLLogHelper successHelper = new XMLLogHelper(successfulLogElement, DATE_FORMAT);
222         Set JavaDoc modifications = successHelper.getModifications();
223         Modification[] mods = createModifications("username1", "username2");
224         Map JavaDoc map = createMapByUserName(mods);
225         for (Iterator JavaDoc iterator = modifications.iterator(); iterator.hasNext();) {
226             Modification actual = (Modification) iterator.next();
227             Modification expected = (Modification) map.get(actual.userName);
228             assertNotNull(actual.userName, expected);
229             assertModificationsEquals(expected, actual);
230         }
231     }
232
233     private Map JavaDoc createMapByUserName(Modification[] modifications) {
234         Map JavaDoc map = new HashMap JavaDoc();
235         for (int i = 0; i < modifications.length; i++) {
236             map.put(modifications[i].userName, modifications[i]);
237         }
238
239         return map;
240     }
241
242     private void assertModificationsEquals(Modification expected, Modification actual) {
243         assertDateEquals(expected.modifiedTime, actual.modifiedTime);
244         actual.modifiedTime = SOME_DATE;
245         assertEquals(expected, actual);
246     }
247
248     private void assertDateEquals(Date JavaDoc expected, Date JavaDoc actual) {
249         assertEquals(DATE_FORMAT.format(expected), DATE_FORMAT.format(actual));
250     }
251     
252 }
Popular Tags