001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2003, 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;
037:
038: import java.io.File;
039: import java.io.IOException;
040: import java.util.Locale;
041:
042: import junit.framework.TestCase;
043: import net.sourceforge.cruisecontrol.testutil.TestUtil.FilesToDelete;
044: import net.sourceforge.cruisecontrol.util.IO;
045: import net.sourceforge.cruisecontrol.util.Util;
046:
047: /**
048: * User: jfredrick Date: Jan 31, 2004 Time: 5:18:43 PM
049: *
050: * @author <a href="mailto:jeffjensen@upstairstechnology.com">Jeff Jensen </a>
051: */
052: public class StatusHelperTest extends TestCase {
053: private static final String LOG_DIR = "target/testresults/";
054: private static final String PROJECT_NAME = "testProject";
055: private static final String STATUS_FILENAME = "buildStatus.txt";
056:
057: private static final String TEXT = "the test status";
058: private static final String TIME = "12/17/2005 20:11:25";
059: private static final String PLAIN_TEXT = TEXT + "\n" + TIME + "\n";
060: private static final String HTML_TEXT = TEXT + "\n<br/>" + TIME
061: + "\n<br/>";
062: private static final String XML_LOGGER_WITH_STATUS_OUTPUT = TEXT
063: + "\n"
064: + TIME
065: + "<br><span class=\"link\">11:47:33 [-force-atriuum-stop] </span>"
066: + "<br><span class=\"link\">11:47:34 [-clean] </span>"
067: + "<br><span class=\"link\">11:47:34 [-checkout] </span>"
068: + "<br><span class=\"link\">11:47:34 [-update] </span>"
069: + "<br><span class=\"link\">11:48:29 [-clean-old-test-data] </span>"
070: + "<br><span class=\"link\">11:48:29 [clean] </span>"
071: + "<br><span class=\"link\">11:48:30 [checkstyle] </span>"
072: + "<br><span class=\"link\">11:48:34 [-init] </span>"
073: + "<br><span class=\"link\">11:48:34 [-build] </span>";
074:
075: private static final String LOG_CONTENTS = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
076: + "<cruisecontrol></cruisecontrol>";
077:
078: private StatusHelper helper;
079: private File logDir;
080: private final FilesToDelete filesToDelete = new FilesToDelete();
081:
082: protected void setUp() throws Exception {
083: helper = new StatusHelper();
084:
085: // make base log dir
086: logDir = new File(LOG_DIR);
087: if (logDir.isFile()) {
088: logDir.delete();
089: }
090: if (!logDir.isDirectory()) {
091: assertTrue("Failed to create test result dir "
092: + logDir.getAbsolutePath(), Util.doMkDirs(logDir));
093: filesToDelete.add(logDir);
094: }
095:
096: // make multi project log dir
097: File projectLogDir = new File(logDir, PROJECT_NAME + "/");
098: if (!projectLogDir.exists()) {
099: assertTrue("Failed to create project log dir: "
100: + projectLogDir.getAbsolutePath(), Util
101: .doMkDirs(projectLogDir));
102: filesToDelete.add(logDir);
103: }
104:
105: File file = new File(logDir, "log20040102030405.xml");
106: prepareFile(file, LOG_CONTENTS);
107:
108: // for single project
109: file = new File(logDir, STATUS_FILENAME);
110: prepareFile(file, PLAIN_TEXT);
111:
112: // for multi project
113: file = new File(projectLogDir, STATUS_FILENAME);
114: prepareFile(file, PLAIN_TEXT);
115: }
116:
117: protected void tearDown() throws Exception {
118: helper = null;
119: filesToDelete.delete();
120: }
121:
122: public void testGetLastBuildResult() throws IOException,
123: CruiseControlException {
124: assertNull(helper.getLastBuildResult());
125:
126: helper.setProjectDirectory(logDir);
127: assertEquals("failed", helper.getLastBuildResult());
128:
129: File file = new File(logDir, "log20040203040506Lbuild.2.xml");
130: prepareFile(file, LOG_CONTENTS);
131: helper.setProjectDirectory(logDir);
132: assertEquals("passed", helper.getLastBuildResult());
133: }
134:
135: public void testGetLastBuildTimeString() throws IOException,
136: CruiseControlException {
137: assertNull(helper.getLastBuildTimeString(Locale.US));
138:
139: helper.setProjectDirectory(logDir);
140: assertEquals("01/02/2004 03:04:05", helper
141: .getLastBuildTimeString(Locale.US));
142:
143: File file = new File(logDir, "log20040203040506Lbuild.2.xml");
144: prepareFile(file, LOG_CONTENTS);
145: helper.setProjectDirectory(logDir);
146: assertEquals("02/03/2004 04:05:06", helper
147: .getLastBuildTimeString(Locale.US));
148: }
149:
150: public void testGetLastSuccessfulBuildLabel() throws IOException,
151: CruiseControlException {
152: assertNull(helper.getLastSuccessfulBuildLabel());
153:
154: helper.setProjectDirectory(logDir);
155: assertNull(helper.getLastSuccessfulBuildLabel());
156:
157: File file = new File(logDir, "log20040203040506Lbuild.2.xml");
158: prepareFile(file, LOG_CONTENTS);
159: helper.setProjectDirectory(logDir);
160: assertEquals("build.2", helper.getLastSuccessfulBuildLabel());
161: }
162:
163: public void testGetLastSuccessfulBuildTimeString()
164: throws IOException, CruiseControlException {
165: assertNull(helper.getLastSuccessfulBuildTimeString(Locale.US));
166:
167: helper.setProjectDirectory(logDir);
168: assertNull(helper.getLastSuccessfulBuildTimeString(Locale.US));
169:
170: File file = new File(logDir, "log20040203040506Lbuild.2.xml");
171: prepareFile(file, LOG_CONTENTS);
172: helper.setProjectDirectory(logDir);
173: assertEquals("02/03/2004 04:05:06", helper
174: .getLastSuccessfulBuildTimeString(Locale.US));
175: }
176:
177: public void testGetCurrentStatus() {
178: String logDirPath = logDir.getAbsolutePath();
179: String actual = helper.getCurrentStatus("true", logDirPath,
180: PROJECT_NAME, STATUS_FILENAME);
181: assertEquals("testing single project: ", HTML_TEXT, actual);
182:
183: actual = helper.getCurrentStatus("false", logDirPath,
184: PROJECT_NAME, STATUS_FILENAME);
185: assertEquals("testing multi project: ", HTML_TEXT, actual);
186: }
187:
188: public void testWithXmlLoggerWithStatusOutput() throws IOException,
189: CruiseControlException {
190: File projectLogDir = new File(logDir, "xmlusingproject");
191: projectLogDir.mkdir();
192: File file = new File(projectLogDir, "status.txt");
193: prepareFile(file, XML_LOGGER_WITH_STATUS_OUTPUT);
194:
195: String actual = helper.getCurrentStatus("false", logDir
196: .getAbsolutePath(), "xmlusingproject", "status.txt");
197: assertEquals(HTML_TEXT, actual);
198: }
199:
200: private void prepareFile(File file, String body)
201: throws CruiseControlException {
202: IO.write(file, body);
203: filesToDelete.add(file);
204: }
205: }
|