1 package net.sourceforge.cruisecontrol; 2 3 import java.io.File ; 4 import java.io.FileWriter ; 5 import java.io.IOException ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Vector ; 9 10 import junit.framework.TestCase; 11 12 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 PROJECT_NAME = "testProject"; 19 private static final String STATUS_FILENAME = "buildStatus.txt"; 20 private static final String TEXT = "the test status"; 21 private static final String TIME = "12/17/2005 20:11:25"; 22 23 private static final String XML_LOGGER_DATA = TIME + " [" + PROJECT_NAME + "]\n"; 24 private static final String XML_LOGGER_TEXT = "<br>" + XML_LOGGER_DATA; 25 26 private static final String PLAIN_TEXT = TEXT + "\n" + TIME + "\n"; 27 private static final String PLAIN_WITH_LOGGER = TEXT + "\n" + TIME + XML_LOGGER_TEXT; 28 29 private static final String HTML_TEXT = TEXT + "\n<br/>" + TIME + "\n<br/>"; 30 private static final String HTML_WITH_LOGGER = HTML_TEXT + XML_LOGGER_DATA + "<br/>"; 31 32 private File logDir; 33 34 private FilesToDelete files = new FilesToDelete(); 35 36 protected void setUp() throws Exception { 37 logDir = new File ("testresults/"); 39 if (!logDir.exists()) { 40 assertTrue("Failed to create test result dir", logDir.mkdir()); 41 files.addFile(logDir); 42 } 43 44 File projectLogDir = new File (logDir, PROJECT_NAME + "/"); 46 if (!projectLogDir.exists()) { 47 assertTrue("Failed to create project log dir", projectLogDir.mkdir()); 48 files.addFile(logDir); 49 } 50 51 File file = new File (logDir, STATUS_FILENAME); 53 prepareFile(file, PLAIN_WITH_LOGGER); 54 55 file = new File (projectLogDir, STATUS_FILENAME); 57 prepareFile(file, PLAIN_WITH_LOGGER); 58 } 59 60 protected void tearDown() throws Exception { 61 files.delete(); 62 } 63 64 public void testStatusFileNotFound() { 65 String 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 msg, boolean isSingleProject, int readLines, String expected) { 108 String 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 msg, boolean isSingleProject, int readLines, String expected) { 117 String 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 file, String body) throws IOException { 126 FileWriter writer = new FileWriter (file); 127 writer.write(body); 128 writer.close(); 129 files.addFile(file); 130 } 131 132 class FilesToDelete { 133 private List files = new Vector (); 134 135 void addFile(File file) { 136 files.add(file); 137 } 138 139 void delete() { 140 Iterator fileIterator = files.iterator(); 141 while (fileIterator.hasNext()) { 142 File file = (File ) fileIterator.next(); 143 file.delete(); 144 } 145 files.clear(); 146 } 147 } 148 } 149 | Popular Tags |