1 37 38 package net.sourceforge.cruisecontrol; 39 40 import java.text.DateFormat ; 41 import java.text.ParseException ; 42 import java.text.SimpleDateFormat ; 43 import java.util.ArrayList ; 44 import java.util.Date ; 45 import java.util.Hashtable ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 49 import junit.framework.TestCase; 50 import net.sourceforge.cruisecontrol.sourcecontrols.MockSourceControl; 51 import net.sourceforge.cruisecontrol.sourcecontrols.Vss; 52 53 import org.jdom.Element; 54 import org.jdom.output.XMLOutputter; 55 56 public class ModificationSetTest extends TestCase { 57 58 private ModificationSet modSet; 59 60 public void testIsLastModificationInQuietPeriod() throws ParseException { 61 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); 62 Modification mod1 = new Modification(); 63 mod1.modifiedTime = formatter.parse("20020621140000"); 64 Modification mod2 = new Modification(); 65 mod2.modifiedTime = formatter.parse("20020621140100"); 66 67 Modification modInFuture = new Modification(); 70 modInFuture.modifiedTime = formatter.parse("30020731150000"); 71 72 List mods1 = new ArrayList (); 73 mods1.add(mod1); 74 mods1.add(mod2); 75 76 List mods2 = new ArrayList (); 77 mods2.add(mod1); 78 79 List hasModInFuture = new ArrayList (); 80 hasModInFuture.add(mod1); 81 hasModInFuture.add(mod2); 82 hasModInFuture.add(modInFuture); 83 84 Date now = formatter.parse("20020621140103"); 85 86 modSet.setQuietPeriod(5); 87 88 assertEquals(true, modSet.isLastModificationInQuietPeriod(now, mods1)); 89 assertEquals(false, modSet.isLastModificationInQuietPeriod(now, mods2)); 90 assertEquals(false, modSet.isLastModificationInQuietPeriod(now, hasModInFuture)); 91 } 92 93 public void testGetLastModificationMillis() throws ParseException { 94 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); 95 Modification mod1 = new Modification(); 96 mod1.modifiedTime = formatter.parse("20020621140000"); 97 Modification mod2 = new Modification(); 98 mod2.modifiedTime = formatter.parse("20020621140100"); 99 100 List mods1 = new ArrayList (); 101 mods1.add(mod2); 102 mods1.add(mod1); 103 104 assertEquals(mod2.modifiedTime.getTime(), modSet.getLastModificationMillis(mods1)); 105 } 106 107 public void testGetQuietPeriodDifference() throws ParseException { 108 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); 109 Date now = formatter.parse("20020621140103"); 110 Modification mod1 = new Modification(); 111 mod1.modifiedTime = formatter.parse("20020621140000"); 112 Modification mod2 = new Modification(); 113 mod2.modifiedTime = formatter.parse("20020621140100"); 114 115 List mods1 = new ArrayList (); 116 mods1.add(mod1); 117 118 List mods2 = new ArrayList (); 119 mods2.add(mod2); 120 121 modSet.setQuietPeriod(5); 122 123 assertEquals(0, modSet.getQuietPeriodDifference(now, mods1)); 124 assertEquals(2000, modSet.getQuietPeriodDifference(now, mods2)); 125 } 126 127 public void testGetModifications() throws Exception { 128 MockSourceControl mock1 = new MockSourceControl(); 129 mock1.setType(1); 130 MockSourceControl mock2 = new MockSourceControl(); 131 mock2.setType(2); 132 133 modSet.add(mock1); 134 modSet.add(mock2); 135 136 Element modSetResults = modSet.getModifications(new Date ()); 138 DateFormat formatter = DateFormatFactory.getDateFormat(); 139 Element modificationsElement = new Element("modifications"); 140 Iterator mock1ModificationsIterator = mock1.getModifications(new Date (), new Date ()).iterator(); 141 while (mock1ModificationsIterator.hasNext()) { 142 Modification modification = (Modification) mock1ModificationsIterator.next(); 143 modificationsElement.addContent(modification.toElement(formatter)); 144 } 145 Iterator mock2ModificationsIterator = mock2.getModifications(new Date (), new Date ()).iterator(); 146 while (mock2ModificationsIterator.hasNext()) { 147 Modification modification = (Modification) mock2ModificationsIterator.next(); 148 modificationsElement.addContent(modification.toElement(formatter)); 149 } 150 151 XMLOutputter outputter = new XMLOutputter(); 152 assertEquals("XML data differ", 153 outputter.outputString(modificationsElement), 154 outputter.outputString(modSetResults)); 155 } 156 157 162 public void testGetMixedModifications() throws ParseException { 163 DateFormat formatter = DateFormatFactory.getDateFormat(); 164 165 Modification mod1 = new Modification(); 166 mod1.userName = "user3"; 167 mod1.modifiedTime = formatter.parse("04/04/2004 17:23:50"); 168 mod1.comment = "comment3"; 169 170 Modification.ModifiedFile mod1file = mod1.createModifiedFile("file3", "dir3"); 171 mod1file.action = "Checkin"; 172 173 Modification mod2 = new Modification(); 174 mod2.userName = "user4"; 175 mod2.modifiedTime = formatter.parse("02/02/2002 17:23:50"); 176 mod2.comment = "comment4"; 177 178 Modification.ModifiedFile mod2file = mod1.createModifiedFile("file4", "dir4"); 179 mod2file.action = "Checkin"; 180 181 final List result = new ArrayList (); 182 result.add(mod1.toElement(formatter)); 183 result.add(mod2); 184 185 assertEquals(mod1.modifiedTime.getTime(), modSet.getLastModificationMillis(result)); 186 187 modSet.add(new MockSourceControl() { 188 public List getModifications(Date lastBuild, Date now) { 189 return result; 190 } 191 }); 192 193 Element actual = modSet.getModifications(new Date ()); 194 195 Element expected = new Element("modifications"); 196 expected.addContent(mod1.toElement(formatter)); 197 expected.addContent(mod2.toElement(formatter)); 198 199 XMLOutputter outputter = new XMLOutputter(); 200 assertEquals("XML data differ", outputter.outputString(expected), outputter.outputString(actual)); 201 202 } 203 204 public void testGetProperties() throws Exception { 205 MockSourceControl mock1 = new MockSourceControl(); 206 mock1.setType(1); 207 MockSourceControl mock2 = new MockSourceControl(); 208 mock2.setType(2); 209 210 modSet.add(mock1); 211 modSet.add(mock2); 212 213 modSet.getModifications(new Date ()); 215 Hashtable table = modSet.getProperties(); 216 assertNotNull("Properties shouldn't be null.", table); 217 assertEquals("Properties should be empty.", 0, table.size()); 218 219 modSet = new ModificationSet(); 220 modSet.setQuietPeriod(0); 221 mock1 = new MockSourceControl(); 222 mock2 = new MockSourceControl(); 223 mock1.setType(1); 224 mock2.setType(2); 225 mock1.setProperty("property"); 226 mock2.setPropertyOnDelete("propertyOnDelete"); 227 228 modSet.add(mock1); 229 modSet.add(mock2); 230 231 modSet.getModifications(new Date ()); 233 table = modSet.getProperties(); 234 assertNotNull("Properties shouldn't be null.", table); 235 assertEquals("Properties should should have 2 entries.", 2, table.size()); 236 assertTrue("Property not found.", table.containsKey("property")); 237 assertTrue("PropertyOnDelete not found.", table.containsKey("propertyOnDelete")); 238 239 modSet = new ModificationSet(); 240 modSet.setQuietPeriod(0); 241 mock1 = new MockSourceControl(); 242 mock1.setType(1); 243 mock1.setProperty("property"); 244 245 modSet.add(mock1); 246 modSet.getModifications(new Date ()); table = modSet.getProperties(); 248 assertNotNull("Properties shouldn't be null.", table); 249 assertEquals("Properties should should have 1 entry.", 1, table.size()); 250 assertTrue("Property not found.", table.containsKey("property")); 251 } 252 253 public void testValidate() throws CruiseControlException { 254 try { 255 modSet.validate(); 256 fail("modificationset should require at least one sourcecontrol"); 257 } catch (CruiseControlException e) { 258 } 259 260 modSet.add(new Vss()); 261 modSet.validate(); 262 } 263 264 public void testSetRequireModification() { 265 modSet.getModifications(new Date ()); 266 assertFalse(modSet.isModified()); 267 modSet.setRequireModification(false); 268 assertTrue(modSet.isModified()); 269 } 270 271 public void testSetIgnoreFiles() { 272 273 final String correctPattern = "*.txt,dir1/*/file*.txt"; 274 try { 275 modSet.setIgnoreFiles(correctPattern); 276 } catch (CruiseControlException e) { 277 fail ("Exception while setting pattern"); 278 } 279 280 final List globPatterns = modSet.getIgnoreFiles(); 281 assertEquals("The number of parsed patterns is not correct", 2, globPatterns.size()); 282 283 } 284 285 public void testFilterIgnoredFiles() throws CruiseControlException, ParseException { 286 287 final DateFormat formatter = DateFormatFactory.getDateFormat(); 288 final List modifications = new ArrayList (); 289 290 final Modification mod1 = new Modification(); 291 mod1.type = "Checkin"; 292 mod1.userName = "user1"; 293 mod1.modifiedTime = formatter.parse("02/02/2002 17:23:50"); 294 mod1.comment = "comment1"; 295 mod1.createModifiedFile("file1", "dir1"); 296 modifications.add(mod1); 297 298 final Modification mod2 = new Modification(); 299 mod2.type = "Checkin"; 300 mod2.userName = "user2"; 301 mod2.modifiedTime = formatter.parse("02/02/2002 17:23:50"); 302 mod2.comment = "comment2"; 303 mod2.createModifiedFile("file1", "dir2"); 304 modifications.add(mod2); 305 306 final Modification mod3 = new Modification(); 307 mod3.type = "Checkin"; 308 mod3.userName = "user3"; 309 mod3.modifiedTime = formatter.parse("02/02/2002 17:23:50"); 310 mod3.comment = "comment1"; 311 mod3.createModifiedFile("file3", "dir1"); 312 modifications.add(mod3); 313 314 modSet.filterIgnoredModifications(modifications); 315 assertEquals ("No modification should have been filtered out", 3, modifications.size()); 316 317 modSet.setIgnoreFiles("dir2/file3,di?1/f*3"); 319 modSet.filterIgnoredModifications(modifications); 320 assertEquals ("No modification have been filtered out", 2, modifications.size()); 321 322 final List expectedModifications = new ArrayList (); 323 expectedModifications.add(mod1); 324 expectedModifications.add(mod2); 325 326 assertEquals ("The wrong modification has been filtered out", expectedModifications, modifications); 327 328 } 329 330 protected void setUp() throws Exception { 331 modSet = new ModificationSet(); 332 modSet.setQuietPeriod(0); 333 } 334 335 protected void tearDown() throws Exception { 336 modSet = null; 337 } 338 339 } 340 | Popular Tags |