1 37 package net.sourceforge.cruisecontrol; 38 39 import junit.framework.TestCase; 40 41 import java.util.List ; 42 import java.util.Locale ; 43 import java.util.Vector ; 44 import java.util.Iterator ; 45 import java.io.File ; 46 import java.io.FileWriter ; 47 import java.io.IOException ; 48 49 54 public class StatusHelperTest extends TestCase { 55 private static final String LOG_DIR = "target/testresults/"; 56 private static final String PROJECT_NAME = "testProject"; 57 private static final String STATUS_FILENAME = "buildStatus.txt"; 58 59 private static final String TEXT = "the test status"; 60 private static final String TIME = "12/17/2005 20:11:25"; 61 private static final String PLAIN_TEXT = TEXT + "\n" + TIME + "\n"; 62 private static final String HTML_TEXT = TEXT + "\n<br/>" + TIME + "\n<br/>"; 63 private static final String XML_LOGGER_WITH_STATUS_OUTPUT = TEXT + "\n" + TIME 64 + "<br><span class=\"link\">11:47:33 [-force-atriuum-stop] </span>" 65 + "<br><span class=\"link\">11:47:34 [-clean] </span>" 66 + "<br><span class=\"link\">11:47:34 [-checkout] </span>" 67 + "<br><span class=\"link\">11:47:34 [-update] </span>" 68 + "<br><span class=\"link\">11:48:29 [-clean-old-test-data] </span>" 69 + "<br><span class=\"link\">11:48:29 [clean] </span>" 70 + "<br><span class=\"link\">11:48:30 [checkstyle] </span>" 71 + "<br><span class=\"link\">11:48:34 [-init] </span>" 72 + "<br><span class=\"link\">11:48:34 [-build] </span>"; 73 74 private static final String LOG_CONTENTS = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 75 + "<cruisecontrol></cruisecontrol>"; 76 77 private StatusHelper helper; 78 private File logDir; 79 private FilesToDelete files = new FilesToDelete(); 80 81 protected void setUp() throws Exception { 82 helper = new StatusHelper(); 83 84 logDir = new File (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 File projectLogDir = new File (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 file = new File (logDir, "log20040102030405.xml"); 102 prepareFile(file, LOG_CONTENTS); 103 104 file = new File (logDir, STATUS_FILENAME); 106 prepareFile(file, PLAIN_TEXT); 107 108 file = new File (projectLogDir, STATUS_FILENAME); 110 prepareFile(file, PLAIN_TEXT); 111 } 112 113 protected void tearDown() throws Exception { 114 helper = null; 115 files.delete(); 116 } 117 118 public void testGetLastBuildResult() throws IOException { 119 assertNull(helper.getLastBuildResult()); 120 121 helper.setProjectDirectory(logDir); 122 assertEquals("failed", helper.getLastBuildResult()); 123 124 File file = new File (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 { 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 file = new File (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 { 143 assertNull(helper.getLastSuccessfulBuildLabel()); 144 145 helper.setProjectDirectory(logDir); 146 assertNull(helper.getLastSuccessfulBuildLabel()); 147 148 File file = new File (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 { 155 assertNull(helper.getLastSuccessfulBuildTimeString(Locale.US)); 156 157 helper.setProjectDirectory(logDir); 158 assertNull(helper.getLastSuccessfulBuildTimeString(Locale.US)); 159 160 File file = new File (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 logDirPath = logDir.getAbsolutePath(); 168 String 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 { 176 File projectLogDir = new File (logDir, "xmlusingproject"); 177 projectLogDir.mkdir(); 178 File file = new File (projectLogDir, "status.txt"); 179 prepareFile(file, XML_LOGGER_WITH_STATUS_OUTPUT); 180 181 String actual = helper.getCurrentStatus("false", logDir.getAbsolutePath(), "xmlusingproject", "status.txt"); 182 assertEquals(HTML_TEXT, actual); 183 } 184 185 private void prepareFile(File file, String body) throws IOException { 186 FileWriter writer = new FileWriter (file); 187 writer.write(body); 188 writer.close(); 189 files.addFile(file); 190 } 191 192 class FilesToDelete { 193 private List files = new Vector (); 194 195 void addFile(File file) { 196 files.add(file); 197 } 198 199 void delete() { 200 Iterator fileIterator = files.iterator(); 201 while (fileIterator.hasNext()) { 202 File file = (File ) fileIterator.next(); 203 file.delete(); 204 } 205 files.clear(); 206 } 207 } 208 } 209 | Popular Tags |