KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > P4Test


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.sourcecontrols;
38
39 import java.io.BufferedInputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.text.DateFormat JavaDoc;
43 import java.text.ParseException JavaDoc;
44 import java.text.SimpleDateFormat JavaDoc;
45 import java.util.Calendar JavaDoc;
46 import java.util.Date JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
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 /**
60  * @author Robert Watkins
61  * @author Jason Yip, jcyip@thoughtworks.com
62  * @author Patrick Conant Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
63  */

64 public class P4Test extends TestCase {
65
66     /**
67      * Mocks a P4 class by returning a specific P4 server-time offset
68      */

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 JavaDoc 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 JavaDoc {
111         P4 p4 = new MockP4(0);
112         p4.setView("foo");
113
114         DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("MM/dd/yyyy");
115         Date JavaDoc date = dateFormat.parse("12/30/2004");
116         Commandline cmdLine = p4.buildChangesCommand(date, date, true);
117
118         String JavaDoc[] args = cmdLine.getCommandline();
119         StringBuffer JavaDoc cmd = new StringBuffer JavaDoc();
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 JavaDoc {
129         P4 p4 = new MockP4(0);
130         p4.setView("foo");
131
132         DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("MM/dd/yyyy");
133         Date JavaDoc date = dateFormat.parse("12/30/2004");
134         Commandline cmdLine = p4.buildChangesCommand(date, date, false);
135
136         String JavaDoc[] args = cmdLine.getCommandline();
137         StringBuffer JavaDoc cmd = new StringBuffer JavaDoc();
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 JavaDoc loadTestLog(String JavaDoc name) {
147         InputStream JavaDoc 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 JavaDoc {
153         BufferedInputStream JavaDoc input =
154                 new BufferedInputStream JavaDoc(loadTestLog("p4_changes.txt"));
155
156         P4 p4 = new P4();
157         String JavaDoc[] 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 JavaDoc[] expectedChangelists = new String JavaDoc[]{"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 JavaDoc, MalformedPatternException {
172         BufferedInputStream JavaDoc input =
173                 new BufferedInputStream JavaDoc(loadTestLog("p4_describe.txt"));
174
175         P4 p4 = new P4();
176         List JavaDoc 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         // XMLOutputter outputter = new XMLOutputter();
206
// for (Iterator iterator = changelistElements.iterator(); iterator.hasNext();) {
207
// Element element = (Element) iterator.next();
208
// Use next lines if you want to see the output of the run. This is what is inserted into the logs.
209
// outputter.setNewlines(true);
210
// outputter.setIndent(true);
211
// System.out.println(outputter.outputString(element));
212
// }
213
}
214
215     /**
216      * Check that all modifications match expected values.
217      *
218      * @param modification The modification to be checked
219      * @param depotPrefix The prefix all filenames are expected
220      * to start with.
221      * @param modCount The expected number of files changes in
222      * this modification.
223      */

224     public void checkModifications(Modification modification,
225             String JavaDoc depotPrefix, int modCount) throws MalformedPatternException {
226         List JavaDoc changeList = modification.files;
227         assertEquals("Wrong number of entries", modCount, changeList.size());
228         for (Iterator JavaDoc 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 JavaDoc {
243         BufferedInputStream JavaDoc input = new BufferedInputStream JavaDoc(loadTestLog("p4_info.txt"));
244
245         P4 p4 = new P4();
246         p4.setCorrectForServerTime(true);
247
248         Calendar JavaDoc cal = Calendar.getInstance();
249         //this date is encoded in p4_info.txt. Note month is indexed from 0
250
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         //Need to accept some difference in the expected offset and the actual
259
//offset, because the test takes some time to run. To be safe, we'll
260
//allow up to 1 minute of variability in the offset value.
261
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 JavaDoc str, String JavaDoc pattern) throws MalformedPatternException {
272         return new Perl5Matcher().matches(str, new Perl5Compiler().compile(pattern));
273     }
274
275     // public void testGetModifications() throws Exception {
276
//
277
// // REAL TEST IF YOU NEED IT
278
// P4 p4 = new P4();
279
// p4.setView("//depot/...");
280
// List changelists = p4.getModifications(new Date(0), new Date(), 0);
281
// assertEquals("Returned wrong number of changelists", 3, changelists.size());
282
//
283
// }
284

285     public static void main(String JavaDoc[] args) {
286         junit.textui.TestRunner.run(P4Test.class);
287     }
288
289 }
290
Popular Tags