KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 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 junit.framework.TestCase;
40
41 import java.util.List JavaDoc;
42 import java.util.Locale JavaDoc;
43 import java.util.Vector JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.io.File JavaDoc;
46 import java.io.FileWriter JavaDoc;
47 import java.io.IOException JavaDoc;
48
49 /**
50  * User: jfredrick Date: Jan 31, 2004 Time: 5:18:43 PM
51  *
52  * @author <a HREF="mailto:jeffjensen@upstairstechnology.com">Jeff Jensen </a>
53  */

54 public class StatusHelperTest extends TestCase {
55     private static final String JavaDoc LOG_DIR = "target/testresults/";
56     private static final String JavaDoc PROJECT_NAME = "testProject";
57     private static final String JavaDoc STATUS_FILENAME = "buildStatus.txt";
58
59     private static final String JavaDoc TEXT = "the test status";
60     private static final String JavaDoc TIME = "12/17/2005 20:11:25";
61     private static final String JavaDoc PLAIN_TEXT = TEXT + "\n" + TIME + "\n";
62     private static final String JavaDoc HTML_TEXT = TEXT + "\n<br/>" + TIME + "\n<br/>";
63     private static final String JavaDoc XML_LOGGER_WITH_STATUS_OUTPUT = TEXT + "\n" + TIME
64                 + "<br><span class=\"link\">11:47:33&nbsp;[-force-atriuum-stop]&nbsp;</span>"
65                 + "<br><span class=\"link\">11:47:34&nbsp;[-clean]&nbsp;</span>"
66                 + "<br><span class=\"link\">11:47:34&nbsp;[-checkout]&nbsp;</span>"
67                 + "<br><span class=\"link\">11:47:34&nbsp;[-update]&nbsp;</span>"
68                 + "<br><span class=\"link\">11:48:29&nbsp;[-clean-old-test-data]&nbsp;</span>"
69                 + "<br><span class=\"link\">11:48:29&nbsp;[clean]&nbsp;</span>"
70                 + "<br><span class=\"link\">11:48:30&nbsp;[checkstyle]&nbsp;</span>"
71                 + "<br><span class=\"link\">11:48:34&nbsp;[-init]&nbsp;</span>"
72                 + "<br><span class=\"link\">11:48:34&nbsp;[-build]&nbsp;</span>";
73
74     private static final String JavaDoc LOG_CONTENTS = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
75         + "<cruisecontrol></cruisecontrol>";
76
77     private StatusHelper helper;
78     private File JavaDoc logDir;
79     private FilesToDelete files = new FilesToDelete();
80
81     protected void setUp() throws Exception JavaDoc {
82         helper = new StatusHelper();
83
84         // make base log dir
85
logDir = new File JavaDoc(LOG_DIR);
86         if (logDir.isFile()) {
87             logDir.delete();
88         }
89         if (!logDir.isDirectory()) {
90             assertTrue("Failed to create test result dir " + logDir.getAbsolutePath(), logDir.mkdirs());
91             files.addFile(logDir);
92         }
93
94         // make multi project log dir
95
File JavaDoc projectLogDir = new File JavaDoc(logDir, PROJECT_NAME + "/");
96         if (!projectLogDir.exists()) {
97             assertTrue("Failed to create project log dir", projectLogDir.mkdir());
98             files.addFile(logDir);
99         }
100
101         File JavaDoc file = new File JavaDoc(logDir, "log20040102030405.xml");
102         prepareFile(file, LOG_CONTENTS);
103
104         // for single project
105
file = new File JavaDoc(logDir, STATUS_FILENAME);
106         prepareFile(file, PLAIN_TEXT);
107
108         // for multi project
109
file = new File JavaDoc(projectLogDir, STATUS_FILENAME);
110         prepareFile(file, PLAIN_TEXT);
111     }
112
113     protected void tearDown() throws Exception JavaDoc {
114         helper = null;
115         files.delete();
116     }
117
118     public void testGetLastBuildResult() throws IOException JavaDoc {
119         assertNull(helper.getLastBuildResult());
120
121         helper.setProjectDirectory(logDir);
122         assertEquals("failed", helper.getLastBuildResult());
123
124         File JavaDoc file = new File JavaDoc(logDir, "log20040203040506Lbuild.2.xml");
125         prepareFile(file, LOG_CONTENTS);
126         helper.setProjectDirectory(logDir);
127         assertEquals("passed", helper.getLastBuildResult());
128     }
129
130     public void testGetLastBuildTimeString() throws IOException JavaDoc {
131         assertNull(helper.getLastBuildTimeString(Locale.US));
132
133         helper.setProjectDirectory(logDir);
134         assertEquals("01/02/2004 03:04:05", helper.getLastBuildTimeString(Locale.US));
135
136         File JavaDoc file = new File JavaDoc(logDir, "log20040203040506Lbuild.2.xml");
137         prepareFile(file, LOG_CONTENTS);
138         helper.setProjectDirectory(logDir);
139         assertEquals("02/03/2004 04:05:06", helper.getLastBuildTimeString(Locale.US));
140     }
141
142     public void testGetLastSuccessfulBuildLabel() throws IOException JavaDoc {
143         assertNull(helper.getLastSuccessfulBuildLabel());
144
145         helper.setProjectDirectory(logDir);
146         assertNull(helper.getLastSuccessfulBuildLabel());
147
148         File JavaDoc file = new File JavaDoc(logDir, "log20040203040506Lbuild.2.xml");
149         prepareFile(file, LOG_CONTENTS);
150         helper.setProjectDirectory(logDir);
151         assertEquals("build.2", helper.getLastSuccessfulBuildLabel());
152     }
153
154     public void testGetLastSuccessfulBuildTimeString() throws IOException JavaDoc {
155         assertNull(helper.getLastSuccessfulBuildTimeString(Locale.US));
156
157         helper.setProjectDirectory(logDir);
158         assertNull(helper.getLastSuccessfulBuildTimeString(Locale.US));
159
160         File JavaDoc file = new File JavaDoc(logDir, "log20040203040506Lbuild.2.xml");
161         prepareFile(file, LOG_CONTENTS);
162         helper.setProjectDirectory(logDir);
163         assertEquals("02/03/2004 04:05:06", helper.getLastSuccessfulBuildTimeString(Locale.US));
164     }
165
166     public void testGetCurrentStatus() {
167         String JavaDoc logDirPath = logDir.getAbsolutePath();
168         String JavaDoc actual = helper.getCurrentStatus("true", logDirPath, PROJECT_NAME, STATUS_FILENAME);
169         assertEquals("testing single project: ", HTML_TEXT, actual);
170         
171         actual = helper.getCurrentStatus("false", logDirPath, PROJECT_NAME, STATUS_FILENAME);
172         assertEquals("testing multi project: ", HTML_TEXT, actual);
173     }
174
175     public void testWithXmlLoggerWithStatusOutput() throws IOException JavaDoc {
176         File JavaDoc projectLogDir = new File JavaDoc(logDir, "xmlusingproject");
177         projectLogDir.mkdir();
178         File JavaDoc file = new File JavaDoc(projectLogDir, "status.txt");
179         prepareFile(file, XML_LOGGER_WITH_STATUS_OUTPUT);
180
181         String JavaDoc actual = helper.getCurrentStatus("false", logDir.getAbsolutePath(), "xmlusingproject", "status.txt");
182         assertEquals(HTML_TEXT, actual);
183     }
184
185     private void prepareFile(File JavaDoc file, String JavaDoc body) throws IOException JavaDoc {
186         FileWriter JavaDoc writer = new FileWriter JavaDoc(file);
187         writer.write(body);
188         writer.close();
189         files.addFile(file);
190     }
191
192     class FilesToDelete {
193         private List JavaDoc files = new Vector JavaDoc();
194
195         void addFile(File JavaDoc file) {
196             files.add(file);
197         }
198
199         void delete() {
200             Iterator JavaDoc fileIterator = files.iterator();
201             while (fileIterator.hasNext()) {
202                 File JavaDoc file = (File JavaDoc) fileIterator.next();
203                 file.delete();
204             }
205             files.clear();
206         }
207     }
208 }
209
Popular Tags