KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > LogTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, 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;
38
39 import java.io.File JavaDoc;
40 import java.io.FilenameFilter JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.StringReader JavaDoc;
43 import java.text.ParseException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Calendar JavaDoc;
46 import java.util.Date JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49 import junit.framework.TestCase;
50 import net.sourceforge.cruisecontrol.logmanipulators.DeleteManipulator;
51 import net.sourceforge.cruisecontrol.logmanipulators.GZIPManipulator;
52
53 import org.jdom.Element;
54 import org.jdom.JDOMException;
55 import org.jdom.input.SAXBuilder;
56 import org.jdom.output.Format;
57 import org.jdom.output.XMLOutputter;
58
59
60 public class LogTest extends TestCase {
61     private final List JavaDoc filesToClear = new ArrayList JavaDoc();
62
63     public void tearDown() {
64         for (Iterator JavaDoc iterator = filesToClear.iterator(); iterator.hasNext();) {
65             File JavaDoc file = (File JavaDoc) iterator.next();
66             if (file.exists()) {
67                 file.delete();
68             }
69         }
70     }
71
72     public void testCreatingLog() {
73         //Cannot create a Log instance with a null project name
74
try {
75             Log log = new Log();
76             log.setProjectName(null);
77             fail("Expected an exception when creating a Log instance with "
78                     + "a null Project name.");
79         } catch (NullPointerException JavaDoc npe) {
80             //Good, expected this exception.
81
}
82
83         //Cannot create a Log instance with a null project name
84
try {
85             Log log = new Log();
86             log.validate();
87             fail("Expected an exception when creating a Log instance with "
88                     + "a null Project name.");
89         } catch (IllegalStateException JavaDoc npe) {
90             // Good, expected this exception.
91
} catch (CruiseControlException cce) {
92             fail("unepected: " + cce.getMessage());
93         }
94     }
95     
96     public void testDefaultLogLocation() {
97         Log log = new Log();
98         log.setProjectName("foo");
99         assertEquals("logs" + File.separatorChar + "foo", log.getLogDir());
100     }
101
102     public void testFormatLogFileName() {
103         Calendar JavaDoc augTweleveCalendar = Calendar.getInstance();
104         augTweleveCalendar.set(2004, 7, 12, 1, 1, 1);
105         Date JavaDoc augTweleve = augTweleveCalendar.getTime();
106
107         String JavaDoc expected = "log20040812010101.xml";
108         String JavaDoc actual = Log.formatLogFileName(augTweleve);
109         assertEquals(
110             expected + "--" + actual,
111             expected, actual);
112         assertEquals("log20040812010101Lbuild.1.xml", Log.formatLogFileName(augTweleve, "build.1"));
113     }
114
115     public void testWasSuccessfulBuild() {
116         assertTrue(Log.wasSuccessfulBuild("log20040812010101Lbuild.1.xml"));
117         assertFalse(Log.wasSuccessfulBuild("log20040812010101.xml"));
118         assertFalse(Log.wasSuccessfulBuild(null));
119     }
120
121     public void testParseDateFromLogFileName() throws ParseException JavaDoc {
122         Calendar JavaDoc augTweleveCalendar = Calendar.getInstance();
123         augTweleveCalendar.set(2004, 7, 12, 1, 1, 1);
124         Date JavaDoc augTweleve = augTweleveCalendar.getTime();
125
126         assertEquals(augTweleve.toString(), Log.parseDateFromLogFileName("log20040812010101Lbuild.1.xml").toString());
127         assertEquals(augTweleve.toString(), Log.parseDateFromLogFileName("log20040812010101.xml").toString());
128     }
129
130     public void testParseLabelFromLogFileName() {
131         assertEquals("build.1", Log.parseLabelFromLogFileName("log20040812010101Lbuild.1.xml"));
132         assertEquals("", Log.parseLabelFromLogFileName("log20040812010101.xml"));
133     }
134
135     public void testXMLEncoding()
136             throws CruiseControlException, IOException JavaDoc, JDOMException {
137         String JavaDoc[] encodings = { "UTF-8", "ISO-8859-1", null };
138
139         SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
140         XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
141         for (int i = 0; i < encodings.length; i++) {
142             Log log = new Log();
143             log.setProjectName("testXMLEncoding");
144             log.setDir("target");
145             if (encodings[i] != null) {
146                 log.setEncoding(encodings[i]);
147             }
148             log.validate();
149
150             // Add a minimal buildLog
151
log.addContent(getBuildLogInfo());
152             Element build = new Element("build");
153             log.addContent(build);
154             log.addContent(new Element("modifications"));
155
156             // Add 8-bit characters
157
build.setText("Something with special characters: \u00c6\u00d8\u00c5");
158
159             // Write and read the file
160
log.writeLogFile(new Date JavaDoc());
161             File JavaDoc logFile = log.getLastLogFile();
162             filesToClear.add(logFile);
163             Element actualContent = builder.build(logFile).getRootElement();
164
165             // content.toString() only returns the root element but not the
166
// children: [Element: <cruisecontrol/>]
167
// Use an XMLOutputter (that trims whitespace) instead.
168
String JavaDoc expected = outputter.outputString(log.getContent());
169             String JavaDoc actual = outputter.outputString(actualContent);
170             assertEquals(expected, actual);
171         }
172     }
173
174     public void testManipulateLog() throws Exception JavaDoc {
175         String JavaDoc testProjectName = "testBackupLog";
176         String JavaDoc testLogDir = "target";
177
178         // Test backup of 12 Months
179
Calendar JavaDoc date = Calendar.getInstance();
180         date.set(Calendar.YEAR, date.get(Calendar.YEAR) - 1);
181         date.set(Calendar.MONTH, date.get(Calendar.MONTH) - 13);
182         GZIPManipulator gzip = new GZIPManipulator();
183         gzip.setEvery(12);
184         gzip.setUnit("month");
185         Log log = getWrittenTestLog(testProjectName, testLogDir, date.getTime());
186         log = getWrittenTestLog(testProjectName, testLogDir, new Date JavaDoc());
187         log.add(gzip);
188         log.validate();
189         assertBackupsHelper(log, 2, 1, 1);
190
191         // Test Backup of 2 days
192
date = Calendar.getInstance();
193         date.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH) - 2);
194         gzip = new GZIPManipulator();
195         gzip.setEvery(2);
196         gzip.setUnit("day");
197         log = getWrittenTestLog(testProjectName, testLogDir, date.getTime());
198         log.add(gzip);
199         log.validate();
200         assertBackupsHelper(log, 3, 1, 2);
201         
202         // Test delete of logfile
203
date = Calendar.getInstance();
204         date.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH) - 2);
205         DeleteManipulator deleteManipulator = new DeleteManipulator();
206         deleteManipulator.setEvery(2);
207         deleteManipulator.setUnit("day");
208         log = getWrittenTestLog(testProjectName, testLogDir, date.getTime());
209         log.add(deleteManipulator);
210         log.validate();
211         assertBackupsHelper(log, 3, 1, 2);
212
213         date = Calendar.getInstance();
214         date.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH) - 2);
215         deleteManipulator = new DeleteManipulator();
216         deleteManipulator.setEvery(2);
217         deleteManipulator.setUnit("day");
218         // This should delete the gz-files too
219
deleteManipulator.setIgnoreSuffix(true);
220         log = getWrittenTestLog(testProjectName, testLogDir, date.getTime());
221         log.add(deleteManipulator);
222         log.validate();
223         assertBackupsHelper(log, 1, 1, 0);
224         
225         //Validation Error
226
gzip = new GZIPManipulator();
227         gzip.setUnit("day");
228         log = getWrittenTestLog(testProjectName, testLogDir, date.getTime());
229         log.add(gzip);
230         try {
231             log.validate();
232             fail("Validation should fail!");
233         } catch (CruiseControlException e) {
234             assertTrue(true);
235         }
236         
237         
238     }
239
240     private void assertBackupsHelper(Log log, int expectedLength, int expectedXML, int expectedGZIP) {
241         log.callManipulators();
242         File JavaDoc[] logfiles = new File JavaDoc(log.getLogDir()).listFiles(new FilenameFilter JavaDoc() {
243
244             public boolean accept(File JavaDoc file, String JavaDoc fileName) {
245                 return fileName.startsWith("log20")
246                         && (fileName.endsWith(".xml") || fileName
247                                 .endsWith(".gz"));
248             }
249
250         });
251         int countGzip = 0;
252         int countXML = 0;
253         for (int i = 0; i < logfiles.length; i++) {
254             File JavaDoc file = logfiles[i];
255             if (file.getName().endsWith(".gz")) {
256                 filesToClear.add(file);
257                 countGzip++;
258             } else if (file.getName().endsWith(".xml")) {
259                 countXML++;
260             } else {
261                 fail("Other log files exists");
262             }
263         }
264         assertEquals(expectedLength, logfiles.length);
265         assertEquals(expectedXML, countXML);
266         assertEquals(expectedGZIP, countGzip);
267     }
268
269     private Log getWrittenTestLog(String JavaDoc projectName, String JavaDoc testLogDir,
270             Date JavaDoc date) throws CruiseControlException, JDOMException,
271             IOException JavaDoc {
272         Log log;
273         Element build;
274         log = new Log();
275         log.setProjectName(projectName);
276         log.setDir(testLogDir);
277         log.addContent(getBuildLogInfo());
278         build = new Element("build");
279         log.addContent(build);
280         log.addContent(new Element("modifications"));
281         log.writeLogFile(date);
282         filesToClear.add(log.getLastLogFile());
283         return log;
284     }
285
286     // Get a minimal info element for the buildLog
287
private Element getBuildLogInfo() throws JDOMException, IOException JavaDoc {
288         SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
289         String JavaDoc infoXML = "<info><property name=\"label\" value=\"\"/>"
290                 + "<property name=\"lastbuildtime\" value=\"\"/>"
291                 + "<property name=\"lastgoodbuildtime\" value=\"\"/>"
292                 + "<property name=\"lastbuildsuccessful\" value=\"\"/>"
293                 + "<property name=\"buildfile\" value=\"\"/>"
294                 + "<property name=\"buildtarget\" value=\"\"/>"
295                 + "</info>";
296         Element info = builder.build(new StringReader JavaDoc(infoXML)).getRootElement();
297         return (Element) info.clone();
298     }
299 }
300
Popular Tags