1 37 package net.sourceforge.cruisecontrol.labelincrementers; 38 39 import java.io.ByteArrayInputStream ; 40 import java.io.File ; 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.text.ParseException ; 44 import java.util.Iterator ; 45 import java.util.LinkedList ; 46 import java.util.List ; 47 48 import org.apache.tools.ant.Project; 49 import org.apache.tools.ant.taskdefs.Delete; 50 import org.apache.tools.ant.types.FileSet; 51 import org.apache.tools.ant.types.PatternSet.NameEntry; 52 53 import junit.framework.TestCase; 54 import net.sourceforge.cruisecontrol.CruiseControlException; 55 import net.sourceforge.cruisecontrol.util.Commandline; 56 57 64 public class P4ChangelistLabelIncrementerTest extends TestCase { 65 66 static class MockP4ChangelistLabelIncrementer 67 extends P4ChangelistLabelIncrementer { 68 public String inputText; 69 public String exceptionText; 70 public InputStream in; 71 public Commandline cmd; 72 73 protected void runP4Cmd(Commandline cmd, P4CmdParser parser) 74 throws CruiseControlException { 75 this.cmd = cmd; 76 if (exceptionText != null) { 77 throw new CruiseControlException(exceptionText); 78 } 79 80 if (in == null) { 81 in = new ByteArrayInputStream (inputText.getBytes()); 82 } 83 try { 84 parseStream(in, parser); 85 } catch (IOException e) { 86 fail("Unexpected exception " + e); 87 } finally { 88 try { 89 in.close(); 90 } catch (IOException ioe) { 91 fail("Unable to read stream: " + ioe); 92 } 93 } 94 } 95 } 96 static class MockP4ChangelistLabelIncrementer2 97 extends P4ChangelistLabelIncrementer { 98 public Iterator in; 99 public List commands = new LinkedList (); 100 public MockDelete d; 101 public MockFileSet fs; 102 103 protected void runP4Cmd(Commandline cmd, P4CmdParser parser) 104 throws CruiseControlException { 105 this.commands.add(cmd); 106 InputStream i = (InputStream ) in.next(); 107 try { 108 parseStream(i, parser); 109 } catch (IOException e) { 110 fail("Unexpected exception " + e); 111 } finally { 112 try { 113 i.close(); 114 } catch (IOException ioe) { 115 fail("Unable to read stream: " + ioe); 116 } 117 } 118 } 119 120 protected Delete createDelete(Project p) throws CruiseControlException { 121 Delete sd = super.createDelete(p); 122 assertNotNull("Created null delete object", sd); 123 d = new MockDelete(); 124 d.setProject(p); 125 return d; 126 } 127 128 protected FileSet createFileSet(Project p) throws CruiseControlException { 129 FileSet sfs = super.createFileSet(p); 130 assertNotNull("Created null fileset object", sfs); 131 fs = new MockFileSet(); 132 fs.setProject(p); 133 return fs; 134 } 135 } 136 137 138 public static class MockDelete extends Delete { 139 public FileSet fs; 140 public boolean executed = false; 141 public void addFileset(FileSet fs) { 142 assertNull("Already set the fileset", this.fs); 143 this.fs = fs; 144 super.addFileset(fs); 145 } 146 147 public void execute() { 148 executed = true; 150 } 151 } 152 153 154 public static class MockFileSet extends FileSet { 155 public List excludes = new LinkedList (); 156 public List includes = new LinkedList (); 157 158 public NameEntry createExclude() { 159 NameEntry ne = super.createExclude(); 160 excludes.add(ne); 161 return ne; 162 } 163 164 public NameEntry createInclude() { 165 NameEntry ne = super.createInclude(); 166 includes.add(ne); 167 return ne; 168 } 169 } 170 171 172 173 174 175 public void testValidate() { 176 P4ChangelistLabelIncrementer p4 = new P4ChangelistLabelIncrementer(); 177 178 try { 179 p4.validate(); 180 fail("P4 should throw exceptions when required attributes are not set."); 181 } catch (CruiseControlException expected) { 182 } 183 184 p4.setUser("user"); 185 p4.setPort("port"); 186 p4.setClient("client"); 187 p4.setView("view"); 188 189 try { 190 p4.validate(); 191 } catch (CruiseControlException e) { 192 fail("P4 should not throw exceptions when required attributes are set."); 193 } 194 } 195 196 public void testBuildBaseP4Command() throws ParseException { 197 MockP4ChangelistLabelIncrementer p4 = 198 new MockP4ChangelistLabelIncrementer(); 199 200 p4.setUser("x-user"); 201 p4.setPasswd("x-passwd"); 202 p4.setPort("x-port"); 203 p4.setClient("x-client"); 204 Commandline cmdLine = p4.buildBaseP4Command(); 205 206 assertEquals("p4 -s -c x-client -p x-port -u x-user -P x-passwd", 207 concatCommand(cmdLine)); 208 } 209 210 public void testParseChangelists1() throws Exception { 211 MockP4ChangelistLabelIncrementer p4 = 212 new MockP4ChangelistLabelIncrementer(); 213 214 p4.in = loadTestLog("p4_changes1.txt"); 215 216 try { 217 p4.getCurrentChangelist(); 218 fail("Did not throw CCE"); 219 } catch (CruiseControlException cce) { 220 if (cce.getMessage().indexOf("Could not discover the changelist") < 0) { 221 fail("Wrong exception thrown"); 222 } 223 } 224 } 225 226 public void testParseChangelists2() throws Exception { 227 MockP4ChangelistLabelIncrementer p4 = 228 new MockP4ChangelistLabelIncrementer(); 229 p4.setClient("y-client"); 230 p4.in = loadTestLog("p4_changes2.txt"); 231 232 String c = p4.getCurrentChangelist(); 233 assertEquals("Returned wrong number of changelists", 234 "1138", 235 c); 236 assertEquals("p4 -s -c y-client changes -m1 -ssubmitted", 237 concatCommand(p4.cmd)); 238 } 239 240 public void testParseChangelists3() throws Exception { 241 MockP4ChangelistLabelIncrementer p4 = 242 new MockP4ChangelistLabelIncrementer(); 243 244 p4.in = loadTestLog("p4_changes2.txt"); 245 246 String c = p4.getCurrentChangelist(); 247 assertEquals("Returned wrong number of changelists", 248 "1138", 249 c); 250 assertEquals("p4 -s changes -m1 -ssubmitted", 251 concatCommand(p4.cmd)); 252 } 253 254 public void testDeleteView1() throws Exception { 255 MockP4ChangelistLabelIncrementer2 p4 = 256 new MockP4ChangelistLabelIncrementer2(); 257 p4.setView("//..."); 258 List inp = new LinkedList (); 259 inp.add(loadTestLog("p4_where1.txt")); 260 p4.in = inp.iterator(); 261 262 p4.deleteView(); 263 264 assertNotNull("Didn't create a Delete object", p4.d); 265 assertNotNull("Didn't create a FileSet object", p4.fs); 266 assertTrue("Didn't run the delete object", p4.d.executed); 267 assertEquals("Didn't add the right number of files to fileset", 268 1, p4.fs.includes.size()); 269 assertEquals("Incorrectly added an exclude to fileset", 270 0, p4.fs.excludes.size()); 271 assertEquals("Didn't add the right item to fileset", 272 "c:\\p4\\cc\\main" + File.separator + "**", 273 ((NameEntry) (p4.fs.includes.iterator().next())).getName()); 274 } 275 276 277 public void testGetWhereView1() throws Exception { 278 MockP4ChangelistLabelIncrementer2 p4 = 279 new MockP4ChangelistLabelIncrementer2(); 280 p4.setView("//..."); 281 List inp = new LinkedList (); 282 inp.add(loadTestLog("p4_where2.txt")); 283 p4.in = inp.iterator(); 284 285 p4.getWhereView(p4.createProject()); 286 287 assertNotNull("Didn't create a FileSet object", p4.fs); 288 assertEquals("Didn't add the right number of includes to fileset", 289 3, p4.fs.includes.size()); 290 assertEquals("Didn't add the right number of excludes to fileset", 291 2, p4.fs.excludes.size()); 292 Iterator inc = p4.fs.includes.iterator(); 293 assertEquals("Didn't add item 1 to include right", 294 "c:\\p4-test\\cc" + File.separator + "**", 295 ((NameEntry) inc.next()).getName()); 296 assertEquals("Didn't add item 2 to include right", 297 "c:\\p4-test\\main" + File.separator + "**", 298 ((NameEntry) inc.next()).getName()); 299 assertEquals("Didn't add item 3 to include right", 300 "c:\\p4-test\\qa" + File.separator + "**", 301 ((NameEntry) inc.next()).getName()); 302 Iterator ex = p4.fs.excludes.iterator(); 303 assertEquals("Didn't add item 1 to exclude right", 304 "c:\\p4-test\\main\\qa" + File.separator + "**", 305 ((NameEntry) ex.next()).getName()); 306 assertEquals("Didn't add item 2 to exclude right", 307 "c:\\p4-test\\main\\core" + File.separator + "**", 308 ((NameEntry) ex.next()).getName()); 309 } 310 311 312 public void testGetWhereView2() throws Exception { 313 MockP4ChangelistLabelIncrementer2 p4 = 314 new MockP4ChangelistLabelIncrementer2(); 315 p4.setView("//..."); 316 List inp = new LinkedList (); 317 inp.add(loadTestLog("p4_where3.txt")); 318 p4.in = inp.iterator(); 319 320 p4.getWhereView(p4.createProject()); 321 322 assertNotNull("Didn't create a FileSet object", p4.fs); 323 assertEquals("Didn't add the right number of includes to fileset", 324 3, p4.fs.includes.size()); 325 assertEquals("Didn't add the right number of excludes to fileset", 326 4, p4.fs.excludes.size()); 327 Iterator inc = p4.fs.includes.iterator(); 328 assertEquals("Didn't add item 1 to include right", 329 "c:\\p4-test\\my root\\main" + File.separator + "**", 330 ((NameEntry) inc.next()).getName()); 331 assertEquals("Didn't add item 1 to include right", 332 "c:\\p4-test\\my root\\my qa" + File.separator + "**", 333 ((NameEntry) inc.next()).getName()); 334 assertEquals("Didn't add item 1 to include right", 335 "c:\\p4-test\\my root\\a b" + File.separator + "**", 336 ((NameEntry) inc.next()).getName()); 337 Iterator ex = p4.fs.excludes.iterator(); 338 assertEquals("Didn't add item 1 to exclude right", 339 "c:\\p4-test\\my root\\main\\qa" + File.separator + "**", 340 ((NameEntry) ex.next()).getName()); 341 assertEquals("Didn't add item 1 to exclude right", 342 "c:\\p4-test\\my root\\main\\a b" + File.separator + "**", 343 ((NameEntry) ex.next()).getName()); 344 assertEquals("Didn't add item 1 to exclude right", 345 "c:\\p4-test\\my root\\main\\core" + File.separator + "**", 346 ((NameEntry) ex.next()).getName()); 347 assertEquals("Didn't add item 1 to exclude right", 348 "c:\\p4-test\\my root\\main\\build" + File.separator + "**", 349 ((NameEntry) ex.next()).getName()); 350 } 351 352 353 private String concatCommand(Commandline cmdLine) { 354 String [] args = cmdLine.getCommandline(); 355 StringBuffer cmd = new StringBuffer (); 356 cmd.append(args[ 0 ]); 357 for (int i = 1; i < args.length; i++) { 358 cmd.append(" " + args[ i ]); 359 } 360 return new String (cmd); 361 } 362 363 private InputStream loadTestLog(String name) { 364 InputStream testStream = getClass().getResourceAsStream(name); 365 assertNotNull("failed to load resource " + name + " in class " + getClass().getName(), testStream); 366 return testStream; 367 } 368 369 370 public static void main(String [] args) { 371 junit.textui.TestRunner.run(P4ChangelistLabelIncrementerTest.class); 372 } 373 374 } 375 | Popular Tags |