1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import java.io.BufferedReader ; 40 import java.io.InputStreamReader ; 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.text.ParseException ; 44 import java.text.SimpleDateFormat ; 45 import java.util.Date ; 46 import java.util.List ; 47 import java.util.ArrayList ; 48 49 import junit.framework.TestCase; 50 import net.sourceforge.cruisecontrol.CruiseControlException; 51 import net.sourceforge.cruisecontrol.Modification; 52 import net.sourceforge.cruisecontrol.util.ManagedCommandline; 53 54 60 public class AlienBrainTest extends TestCase { 61 62 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat ("M/d/yyyy z"); 63 private static final Date NT_TIME_ZERO; 64 private static final Date JAVA_TIME_ZERO; 65 66 static { 67 try { 68 NT_TIME_ZERO = DATE_FORMAT.parse("1/1/1601 UTC"); 69 JAVA_TIME_ZERO = DATE_FORMAT.parse("1/1/1970 UTC"); 70 } catch (ParseException e) { 71 throw new RuntimeException (e.getMessage()); 72 } 73 } 74 75 78 public void testConstruction() { 79 new AlienBrain(); 80 } 81 82 84 public void testValidate() { 85 AlienBrain ab = new AlienBrain(); 86 87 try { 88 ab.validate(); 89 fail("AlienBrain should throw exceptions when required " 90 + "attributes are not set."); 91 } catch (CruiseControlException expected) { 92 } 93 94 ab.setPath("Module1"); 95 96 try { 97 ab.validate(); 98 } catch (CruiseControlException expected) { 99 fail("AlienBrain should not throw exceptions when required " 100 + "attributes are set.\n" + expected); 101 } 102 103 } 104 105 public void testDateToFiletime() throws ParseException { 106 assertEquals(0L, AlienBrain.dateToFiletime(NT_TIME_ZERO)); 107 assertEquals(116444736000000000L, AlienBrain.dateToFiletime(JAVA_TIME_ZERO)); 108 assertEquals(127610208000000000L, AlienBrain.dateToFiletime(DATE_FORMAT.parse("5/20/2005 UTC"))); 109 } 110 111 public void testFiletimeToDate() throws ParseException { 112 assertEquals(NT_TIME_ZERO, AlienBrain.filetimeToDate(0L)); 113 assertEquals(JAVA_TIME_ZERO, AlienBrain.filetimeToDate(116444736000000000L)); 114 assertEquals(DATE_FORMAT.parse("5/20/2005 UTC"), AlienBrain.filetimeToDate(127610208000000000L)); 115 116 Date now = new Date (); 117 assertEquals(now, 118 AlienBrain.filetimeToDate(AlienBrain.dateToFiletime(now))); 119 } 120 121 public void testBuildGetModificationsCommand() throws ParseException { 122 AlienBrain ab = new AlienBrain(); 123 124 ab.setUser("FooUser"); 125 ab.setPath("FooProject"); 126 127 Date date = DATE_FORMAT.parse("5/20/2005 -0400"); 128 ManagedCommandline cmdLine = ab.buildGetModificationsCommand(date, date); 129 130 assertEquals("ab -u FooUser find FooProject -regex \"SCIT > " 131 + "127610352000000000\" " 132 + "-format \"#SCIT#|#DbPath#|#Changed By#|#CheckInComment#\"" 133 , cmdLine.toString()); 134 } 135 136 public void testParseModificationDescription() throws ParseException { 137 Modification m = AlienBrain.parseModificationDescription( 138 "127610352000000000|/a/path/to/a/file.cpp|sjacobs|" 139 + "A change that probably breaks everything."); 140 141 assertEquals(DATE_FORMAT.parse("5/20/2005 -0400"), m.modifiedTime); 142 assertEquals("sjacobs", m.userName); 143 assertEquals("A change that probably breaks everything.", m.comment); 144 assertEquals(1, m.files.size()); 148 assertEquals("/a/path/to/a/file.cpp", 149 ((Modification.ModifiedFile) (m.files.get(0))).fileName); 150 } 151 152 155 private List loadTestLog(String name) throws IOException { 156 InputStream testStream = getClass().getResourceAsStream(name); 157 assertNotNull("failed to load resource " + name + " in class " 158 + getClass().getName(), testStream); 159 160 List lines = new ArrayList (); 161 String line; 162 BufferedReader reader = new BufferedReader (new InputStreamReader (testStream)); 163 while ((line = reader.readLine()) != null) { 164 lines.add(line); 165 } 166 167 return lines; 168 } 169 170 public void testParseModifications() throws IOException , ParseException { 171 List results = loadTestLog("alienbrain_modifications.txt"); 172 173 AlienBrain ab = new AlienBrain(); 174 175 List modifications = ab.parseModifications(results); 176 177 assertEquals( 178 "Returned wrong number of modifications.", 179 7, 180 modifications.size()); 181 182 SimpleDateFormat dateFormat = new SimpleDateFormat ("M/d/yyyy HH:mm:ss z"); 183 assertEquals("Wrong modification time", 184 dateFormat.parse("4/19/2005 16:51:55 -0400"), 185 ((Modification) modifications.get(0)).modifiedTime); 186 187 assertEquals("Wrong path", 188 "/FooProject/Code/Vehicles/Src/Position.cpp", 189 ((Modification.ModifiedFile) (((Modification) modifications.get(0)).files.get(0))).fileName); 190 191 assertEquals("Wrong user", 192 "User 1", 193 ((Modification) modifications.get(0)).userName); 194 195 assertEquals("Wrong comment", 196 "Passenger Animatoin", 197 ((Modification) modifications.get(0)).comment); 198 199 assertEquals("Wrong modification time", 200 dateFormat.parse("5/7/2005 7:44:45 -0400"), 201 ((Modification) modifications.get(6)).modifiedTime); 202 203 assertEquals("Wrong path", 204 "/FooProject/Code/Vehicles/Src/Materialnfo.cpp", 205 ((Modification.ModifiedFile) (((Modification) modifications.get(6)).files.get(0))).fileName); 206 207 assertEquals("Wrong user", 208 "User 1", 209 ((Modification) modifications.get(6)).userName); 210 211 assertEquals("Wrong comment", 212 "Import from 2004", 213 ((Modification) modifications.get(6)).comment); 214 } 215 216 218 public void testParseNoModifications() throws IOException { 219 List results = loadTestLog("alienbrain_nomodifications.txt"); 220 221 AlienBrain ab = new AlienBrain(); 222 223 List modifications = ab.parseModifications(results); 224 assertEquals(0, modifications.size()); 225 } 226 227 } 267 | Popular Tags |