KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sourceforge.cruisecontrol;
2
3 import java.io.File JavaDoc;
4 import java.io.FileWriter JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Vector JavaDoc;
9
10 import junit.framework.TestCase;
11
12 /**
13  * @author <a HREF="mailto:jeffjensen@upstairstechnology.com">Jeff Jensen </a>
14  */

15 public class BuildStatusTest extends TestCase {
16     private static final boolean MULTIPLE_PROJECT_MODE = false;
17     private static final boolean SINGLE_PROJECT_MODE = true;
18     private static final String JavaDoc PROJECT_NAME = "testProject";
19     private static final String JavaDoc STATUS_FILENAME = "buildStatus.txt";
20     private static final String JavaDoc TEXT = "the test status";
21     private static final String JavaDoc TIME = "12/17/2005 20:11:25";
22
23     private static final String JavaDoc XML_LOGGER_DATA = TIME + " [" + PROJECT_NAME + "]\n";
24     private static final String JavaDoc XML_LOGGER_TEXT = "<br>" + XML_LOGGER_DATA;
25     
26     private static final String JavaDoc PLAIN_TEXT = TEXT + "\n" + TIME + "\n";
27     private static final String JavaDoc PLAIN_WITH_LOGGER = TEXT + "\n" + TIME + XML_LOGGER_TEXT;
28     
29     private static final String JavaDoc HTML_TEXT = TEXT + "\n<br/>" + TIME + "\n<br/>";
30     private static final String JavaDoc HTML_WITH_LOGGER = HTML_TEXT + XML_LOGGER_DATA + "<br/>";
31
32     private File JavaDoc logDir;
33
34     private FilesToDelete files = new FilesToDelete();
35
36     protected void setUp() throws Exception JavaDoc {
37         // make base log dir
38
logDir = new File JavaDoc("testresults/");
39         if (!logDir.exists()) {
40             assertTrue("Failed to create test result dir", logDir.mkdir());
41             files.addFile(logDir);
42         }
43
44         // make multi project log dir
45
File JavaDoc projectLogDir = new File JavaDoc(logDir, PROJECT_NAME + "/");
46         if (!projectLogDir.exists()) {
47             assertTrue("Failed to create project log dir", projectLogDir.mkdir());
48             files.addFile(logDir);
49         }
50
51         // for single project
52
File JavaDoc file = new File JavaDoc(logDir, STATUS_FILENAME);
53         prepareFile(file, PLAIN_WITH_LOGGER);
54
55         // for multi project
56
file = new File JavaDoc(projectLogDir, STATUS_FILENAME);
57         prepareFile(file, PLAIN_WITH_LOGGER);
58     }
59
60     protected void tearDown() throws Exception JavaDoc {
61         files.delete();
62     }
63
64     public void testStatusFileNotFound() {
65         String JavaDoc status = BuildStatus.getStatusHtml(SINGLE_PROJECT_MODE,
66             logDir.getAbsolutePath(),
67             PROJECT_NAME,
68             "badfilename.txt",
69             BuildStatus.READ_ALL_LINES);
70         assertEquals("(build status file not found)", status);
71         status = BuildStatus.getStatusHtml(MULTIPLE_PROJECT_MODE,
72             logDir.getAbsolutePath(),
73             PROJECT_NAME, "badfilename.txt",
74             BuildStatus.READ_ALL_LINES);
75         assertEquals("(build status file not found)", status);
76     }
77
78     public void testShouldThrowExceptionWithDirectory() {
79         try {
80             BuildStatus.getStatusHtml(SINGLE_PROJECT_MODE,
81                 logDir.getAbsolutePath(),
82                 null,
83                 PROJECT_NAME,
84                 BuildStatus.READ_ALL_LINES);
85             fail("Expected exception for build status file not found.");
86         } catch (CruiseControlWebAppException expected) {
87             assertTrue(expected.getMessage().indexOf("is a directory") > 0);
88         }
89     }
90
91     public void testGetCurrentStatusSingleProject() {
92         coreTestPlain("status only: ", SINGLE_PROJECT_MODE, BuildStatus.READ_ONLY_STATUS_LINES, PLAIN_TEXT);
93         coreTestHtml("status only: ", SINGLE_PROJECT_MODE, BuildStatus.READ_ONLY_STATUS_LINES, HTML_TEXT);
94         
95         coreTestPlain("all lines: ", SINGLE_PROJECT_MODE, BuildStatus.READ_ALL_LINES, PLAIN_TEXT + XML_LOGGER_DATA);
96         coreTestHtml("all lines: ", SINGLE_PROJECT_MODE, BuildStatus.READ_ALL_LINES, HTML_WITH_LOGGER);
97     }
98
99     public void testGetCurrentStatusMultiProject() {
100         coreTestPlain("status only: ", MULTIPLE_PROJECT_MODE, BuildStatus.READ_ONLY_STATUS_LINES, PLAIN_TEXT);
101         coreTestHtml("status only: ", MULTIPLE_PROJECT_MODE, BuildStatus.READ_ONLY_STATUS_LINES, HTML_TEXT);
102         
103         coreTestPlain("all lines: ", MULTIPLE_PROJECT_MODE, BuildStatus.READ_ALL_LINES, PLAIN_TEXT + XML_LOGGER_DATA);
104         coreTestHtml("all lines: ", MULTIPLE_PROJECT_MODE, BuildStatus.READ_ALL_LINES, HTML_WITH_LOGGER);
105     }
106
107     private void coreTestPlain(String JavaDoc msg, boolean isSingleProject, int readLines, String JavaDoc expected) {
108         String JavaDoc actual = BuildStatus.getStatusPlain(isSingleProject,
109                                                    logDir.getAbsolutePath(),
110                                                    PROJECT_NAME,
111                                                    STATUS_FILENAME,
112                                                    readLines);
113         assertEquals("plain:" + msg, expected, actual);
114     }
115
116     private void coreTestHtml(String JavaDoc msg, boolean isSingleProject, int readLines, String JavaDoc expected) {
117         String JavaDoc actual = BuildStatus.getStatusHtml(isSingleProject,
118                                                   logDir.getAbsolutePath(),
119                                                   PROJECT_NAME,
120                                                   STATUS_FILENAME,
121                                                   readLines);
122         assertEquals("html:" + msg, expected, actual);
123     }
124
125     private void prepareFile(File JavaDoc file, String JavaDoc body) throws IOException JavaDoc {
126         FileWriter JavaDoc writer = new FileWriter JavaDoc(file);
127         writer.write(body);
128         writer.close();
129         files.addFile(file);
130     }
131
132     class FilesToDelete {
133         private List JavaDoc files = new Vector JavaDoc();
134
135         void addFile(File JavaDoc file) {
136             files.add(file);
137         }
138
139         void delete() {
140             Iterator JavaDoc fileIterator = files.iterator();
141             while (fileIterator.hasNext()) {
142                 File JavaDoc file = (File JavaDoc) fileIterator.next();
143                 file.delete();
144             }
145             files.clear();
146         }
147     }
148 }
149
Popular Tags