1 37 38 package net.sourceforge.cruisecontrol.sourcecontrols; 39 40 import java.io.File ; 41 import java.util.Calendar ; 42 import java.util.Date ; 43 import java.util.List ; 44 import junit.framework.TestCase; 45 import net.sourceforge.cruisecontrol.CruiseControlException; 46 import net.sourceforge.cruisecontrol.Log; 47 import net.sourceforge.cruisecontrol.util.DateUtil; 48 49 54 public class BuildStatusTest extends TestCase { 55 private BuildStatus buildStatus; 56 57 protected void setUp() throws Exception { 58 buildStatus = new BuildStatus(); 59 } 60 61 protected void tearDown() throws Exception { 62 buildStatus = null; 63 } 64 65 68 public void testValidate() throws Exception { 69 try { 71 buildStatus.validate(); 72 fail("Should have thrown exception indicating log directory is mandatory"); 73 } catch (CruiseControlException e) { 74 assertEquals("Wrong exception", "'logdir' is required for BuildStatus", e.getMessage()); 75 } 76 77 buildStatus.setLogDir("does not exist"); 79 80 try { 81 buildStatus.validate(); 82 fail("Should have thrown exception indicating log directory must exist"); 83 } catch (CruiseControlException e) { 84 assertTrue("Wrong exception", e.getMessage().startsWith("Log directory does not exist")); 85 } 86 87 File tempFile = File.createTempFile("temp", "txt"); 89 tempFile.deleteOnExit(); 90 buildStatus.setLogDir(tempFile.getAbsolutePath()); 91 92 try { 93 buildStatus.validate(); 94 fail("Should have thrown exception indicating log directory must be a directory"); 95 } catch (CruiseControlException e) { 96 assertTrue("Wrong exception", e.getMessage().startsWith("Log directory is not a directory")); 97 } 98 99 buildStatus.setLogDir(tempFile.getParentFile().getAbsolutePath()); 101 buildStatus.validate(); 102 } 103 104 107 public void testGetModifications() throws Exception { 108 File tempDir = new File (System.getProperty("java.io.tmpdir")); 109 110 buildStatus.setLogDir(tempDir.getAbsolutePath()); 111 buildStatus.validate(); 112 113 Calendar calendar = Calendar.getInstance(); 114 Date today = calendar.getTime(); 115 calendar.add(Calendar.DATE, -1); 116 Date yesterday = calendar.getTime(); 117 calendar.add(Calendar.DATE, -1); 118 Date twoDaysAgo = calendar.getTime(); 119 120 List modifications = buildStatus.getModifications(twoDaysAgo, null); 122 assertEquals("Wrong number of modifications", 0, modifications.size()); 123 124 File yesterdayLog = new File (tempDir, Log.formatLogFileName(yesterday)); 126 yesterdayLog.createNewFile(); 127 try { 128 modifications = buildStatus.getModifications(twoDaysAgo, null); 129 assertEquals("Wrong number of modifications", 0, modifications.size()); 130 } finally { 131 yesterdayLog.delete(); 132 } 133 134 File yesterdayLog2 = new File (tempDir, Log.formatLogFileName(yesterday, "good.1")); 136 yesterdayLog2.createNewFile(); 137 try { 138 modifications = buildStatus.getModifications(twoDaysAgo, null); 139 assertEquals("Wrong number of modifications", 1, modifications.size()); 140 141 assertEquals("Property was not set correctly", tempDir.getAbsolutePath(), 143 buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGDIR_KEY)); 144 assertEquals("Property was not set correctly", yesterdayLog2.getName(), 145 buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGFILE_KEY)); 146 assertEquals("Property was not set correctly", DateUtil.getFormattedTime(yesterday), 147 buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGTIME_KEY)); 148 assertEquals("Property was not set correctly", "good.1", 149 buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGLABEL_KEY)); 150 151 modifications = buildStatus.getModifications(today, null); 153 assertEquals("Wrong number of modifications", 0, modifications.size()); 154 155 File todayLog = new File (tempDir, Log.formatLogFileName(today, "good.2")); 157 todayLog.createNewFile(); 158 try { 159 modifications = buildStatus.getModifications(twoDaysAgo, null); 160 assertEquals("Wrong number of modifications", 2, modifications.size()); 161 162 assertEquals("Property was not set correctly", tempDir.getAbsolutePath(), 164 buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGDIR_KEY)); 165 assertEquals("Property was not set correctly", todayLog.getName(), 166 buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGFILE_KEY)); 167 assertEquals("Property was not set correctly", DateUtil.getFormattedTime(today), 168 buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGTIME_KEY)); 169 assertEquals("Property was not set correctly", "good.2", 170 buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGLABEL_KEY)); 171 } finally { 172 todayLog.delete(); 173 } 174 } finally { 175 yesterdayLog2.delete(); 176 } 177 } 178 } 179 | Popular Tags |