1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import java.io.BufferedInputStream ; 40 import java.io.IOException ; 41 import java.io.InputStream ; 42 import java.text.DateFormat ; 43 import java.text.ParseException ; 44 import java.text.SimpleDateFormat ; 45 import java.util.Calendar ; 46 import java.util.Date ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 50 import junit.framework.TestCase; 51 import net.sourceforge.cruisecontrol.CruiseControlException; 52 import net.sourceforge.cruisecontrol.Modification; 53 import net.sourceforge.cruisecontrol.util.Commandline; 54 55 import org.apache.oro.text.regex.MalformedPatternException; 56 import org.apache.oro.text.regex.Perl5Compiler; 57 import org.apache.oro.text.regex.Perl5Matcher; 58 59 64 public class P4Test extends TestCase { 65 66 69 static class MockP4 extends P4 { 70 private final long timeOffset; 71 72 public MockP4(long offset) { 73 this.timeOffset = offset; 74 } 75 protected long calculateServerTimeOffset() { 76 return timeOffset; 77 } 78 } 79 80 public void testGetQuoteChar() { 81 boolean windows = true; 82 String quoteChar = P4.getQuoteChar(windows); 83 assertEquals("\"", quoteChar); 84 85 quoteChar = P4.getQuoteChar(!windows); 86 assertEquals("'", quoteChar); 87 } 88 89 public void testValidate() { 90 P4 p4 = new P4(); 91 92 try { 93 p4.validate(); 94 fail("P4 should throw exceptions when required attributes are not set."); 95 } catch (CruiseControlException expected) { 96 } 97 98 p4.setUser("user"); 99 p4.setPort("port"); 100 p4.setClient("client"); 101 p4.setView("view"); 102 103 try { 104 p4.validate(); 105 } catch (CruiseControlException e) { 106 fail("P4 should not throw exceptions when required attributes are set."); 107 } 108 } 109 110 public void testBuildChangesCommand() throws ParseException { 111 P4 p4 = new MockP4(0); 112 p4.setView("foo"); 113 114 DateFormat dateFormat = new SimpleDateFormat ("MM/dd/yyyy"); 115 Date date = dateFormat.parse("12/30/2004"); 116 Commandline cmdLine = p4.buildChangesCommand(date, date, true); 117 118 String [] args = cmdLine.getCommandline(); 119 StringBuffer cmd = new StringBuffer (); 120 cmd.append(args[ 0 ]); 121 for (int i = 1; i < args.length; i++) { 122 cmd.append(" " + args[ i ]); 123 } 124 125 assertEquals("p4 -s changes -s submitted foo@2004/12/30:00:00:00,@2004/12/30:00:00:00", cmd.toString()); 126 } 127 128 public void testBuildChangesCommand_Unix() throws ParseException { 129 P4 p4 = new MockP4(0); 130 p4.setView("foo"); 131 132 DateFormat dateFormat = new SimpleDateFormat ("MM/dd/yyyy"); 133 Date date = dateFormat.parse("12/30/2004"); 134 Commandline cmdLine = p4.buildChangesCommand(date, date, false); 135 136 String [] args = cmdLine.getCommandline(); 137 StringBuffer cmd = new StringBuffer (); 138 cmd.append(args[ 0 ]); 139 for (int i = 1; i < args.length; i++) { 140 cmd.append(" " + args[ i ]); 141 } 142 143 assertEquals("p4 -s changes -s submitted foo@2004/12/30:00:00:00,@2004/12/30:00:00:00", cmd.toString()); 144 } 145 146 private InputStream loadTestLog(String name) { 147 InputStream testStream = getClass().getResourceAsStream(name); 148 assertNotNull("failed to load resource " + name + " in class " + getClass().getName(), testStream); 149 return testStream; 150 } 151 152 public void testParseChangelists() throws IOException { 153 BufferedInputStream input = 154 new BufferedInputStream (loadTestLog("p4_changes.txt")); 155 156 P4 p4 = new P4(); 157 String [] changelists = p4.parseChangelistNumbers(input); 158 input.close(); 159 assertNotNull("No changelists returned", changelists); 160 assertEquals("Returned wrong number of changelists", 161 4, 162 changelists.length); 163 String [] expectedChangelists = new String []{"14", "12", "11"}; 164 for (int i = 0; i < expectedChangelists.length; i++) { 165 assertEquals("Returned wrong changelist number", 166 expectedChangelists[ i ], 167 changelists[ i ]); 168 } 169 } 170 171 public void testParseChangeDescriptions() throws IOException , MalformedPatternException { 172 BufferedInputStream input = 173 new BufferedInputStream (loadTestLog("p4_describe.txt")); 174 175 P4 p4 = new P4(); 176 List changelists = p4.parseChangeDescriptions(input); 177 input.close(); 178 assertEquals("Returned wrong number of changelists", 179 3, 180 changelists.size()); 181 182 assertEquals("Wrong description", 183 "Fixed support for db2. This is now the default database shipped" 184 + " with HPDoc. For now that is. Still has to be tested on" 185 + " PostgreSQL to see that it is still working there. The sea rch" 186 + " mechanism is also upgraded to now ALMOST support AND/OR" 187 + " expressions. There are thoughtsabout this, but not yet implemented (however prepared for)", 188 ((Modification) changelists.get(0)) 189 .comment); 190 checkModifications((Modification) changelists.get(0), 191 "//depot/hpdoc/main", 33); 192 193 assertEquals("Wrong description", 194 "ok, tests running smooth. Checking in mostly for backup. Not" 195 + " finished yet. CIMD is comming on great and I'm starting to see a framework developing.", 196 ((Modification) changelists.get(1)) 197 .comment); 198 checkModifications((Modification) changelists.get(1), 199 "//depot/k4j/main", 65); 200 assertEquals("Wrong description", 201 "Testing ..\nSome ..\nLinebreaks.", 202 ((Modification) changelists.get(2)) 203 .comment); 204 checkModifications((Modification) changelists.get(2), "", 0); 205 } 214 215 224 public void checkModifications(Modification modification, 225 String depotPrefix, int modCount) throws MalformedPatternException { 226 List changeList = modification.files; 227 assertEquals("Wrong number of entries", modCount, changeList.size()); 228 for (Iterator i = changeList.iterator(); i.hasNext();) { 229 Modification.ModifiedFile file 230 = (Modification.ModifiedFile) i.next(); 231 assertTrue("Filename doesn't start with prefix " + depotPrefix, 232 file.fileName.startsWith(depotPrefix)); 233 assertEquals("Filename has # at bad index", -1, 234 file.fileName.indexOf("#")); 235 assertTrue("Revision doesn't match regexp ^\\d+$: " + file.revision, 236 matches(file.revision, "^\\d+$")); 237 assertTrue("Unknown action type: " + file.action, 238 matches(file.action, "(edit|add)")); 239 } 240 } 241 242 public void testParseInfoResponse() throws IOException { 243 BufferedInputStream input = new BufferedInputStream (loadTestLog("p4_info.txt")); 244 245 P4 p4 = new P4(); 246 p4.setCorrectForServerTime(true); 247 248 Calendar cal = Calendar.getInstance(); 249 cal.set(2005, 6, 29, 20, 39, 06); 251 long p4ServerTime = cal.getTime().getTime(); 252 long ccServerTime = System.currentTimeMillis(); 253 long expectedOffset = p4ServerTime - ccServerTime; 254 255 long offset = p4.parseServerInfo(input); 256 input.close(); 257 258 long maxOffset = 1000 * 60; 262 long offsetDifference = Math.abs(offset - expectedOffset); 263 264 assertTrue("Server time offset wasn't calculated accurately. Expected " 265 + expectedOffset + " but got " + offset + ". Maximum allowed" 266 + "difference in these values is " + maxOffset 267 + ". P4 server time (from test input) is " + p4ServerTime 268 + "; CC server time is " + ccServerTime, offsetDifference < maxOffset); 269 } 270 271 public boolean matches(String str, String pattern) throws MalformedPatternException { 272 return new Perl5Matcher().matches(str, new Perl5Compiler().compile(pattern)); 273 } 274 275 285 public static void main(String [] args) { 286 junit.textui.TestRunner.run(P4Test.class); 287 } 288 289 } 290 | Popular Tags |