KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > labelincrementers > P4ChangelistLabelIncrementerTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.labelincrementers;
38
39 import java.io.ByteArrayInputStream JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.text.ParseException JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.LinkedList JavaDoc;
46 import java.util.List JavaDoc;
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 /**
58  * This test references several resources from the same package. It also
59  * has "p4_client3.txt" which, though not referenced here, was used to
60  * generate "p4_where3.txt".
61  *
62  * @author <a HREF="mailto:groboclown@users.sourceforge.net">Matt Albrecht</a>
63  */

64 public class P4ChangelistLabelIncrementerTest extends TestCase {
65
66     static class MockP4ChangelistLabelIncrementer
67             extends P4ChangelistLabelIncrementer {
68         public String JavaDoc inputText;
69         public String JavaDoc exceptionText;
70         public InputStream JavaDoc 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 JavaDoc(inputText.getBytes());
82             }
83             try {
84                 parseStream(in, parser);
85             } catch (IOException JavaDoc e) {
86                 fail("Unexpected exception " + e);
87             } finally {
88                 try {
89                     in.close();
90                 } catch (IOException JavaDoc ioe) {
91                     fail("Unable to read stream: " + ioe);
92                 }
93             }
94         }
95     }
96     static class MockP4ChangelistLabelIncrementer2
97             extends P4ChangelistLabelIncrementer {
98         public Iterator JavaDoc in;
99         public List JavaDoc commands = new LinkedList JavaDoc();
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 JavaDoc i = (InputStream JavaDoc) in.next();
107             try {
108                 parseStream(i, parser);
109             } catch (IOException JavaDoc e) {
110                 fail("Unexpected exception " + e);
111             } finally {
112                 try {
113                     i.close();
114                 } catch (IOException JavaDoc 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             // don't do anything for real
149
executed = true;
150         }
151     }
152     
153     
154     public static class MockFileSet extends FileSet {
155         public List JavaDoc excludes = new LinkedList JavaDoc();
156         public List JavaDoc includes = new LinkedList JavaDoc();
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
227         MockP4ChangelistLabelIncrementer p4 =
228             new MockP4ChangelistLabelIncrementer();
229         p4.setClient("y-client");
230         p4.in = loadTestLog("p4_changes2.txt");
231         
232         String JavaDoc 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 JavaDoc {
241         MockP4ChangelistLabelIncrementer p4 =
242             new MockP4ChangelistLabelIncrementer();
243         
244         p4.in = loadTestLog("p4_changes2.txt");
245         
246         String JavaDoc 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 JavaDoc {
255         MockP4ChangelistLabelIncrementer2 p4 =
256             new MockP4ChangelistLabelIncrementer2();
257         p4.setView("//...");
258         List JavaDoc inp = new LinkedList JavaDoc();
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 JavaDoc {
278         MockP4ChangelistLabelIncrementer2 p4 =
279             new MockP4ChangelistLabelIncrementer2();
280         p4.setView("//...");
281         List JavaDoc inp = new LinkedList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc {
313         MockP4ChangelistLabelIncrementer2 p4 =
314             new MockP4ChangelistLabelIncrementer2();
315         p4.setView("//...");
316         List JavaDoc inp = new LinkedList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc concatCommand(Commandline cmdLine) {
354         String JavaDoc[] args = cmdLine.getCommandline();
355         StringBuffer JavaDoc cmd = new StringBuffer JavaDoc();
356         cmd.append(args[ 0 ]);
357         for (int i = 1; i < args.length; i++) {
358             cmd.append(" " + args[ i ]);
359         }
360         return new String JavaDoc(cmd);
361     }
362
363     private InputStream JavaDoc loadTestLog(String JavaDoc name) {
364         InputStream JavaDoc 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 JavaDoc[] args) {
371         junit.textui.TestRunner.run(P4ChangelistLabelIncrementerTest.class);
372     }
373
374 }
375
Popular Tags