KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, 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.BufferedReader JavaDoc;
40 import java.io.InputStreamReader JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.text.ParseException JavaDoc;
44 import java.text.SimpleDateFormat JavaDoc;
45 import java.util.Date JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.ArrayList JavaDoc;
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 /**
55  * The unit test for an AlienBrain source control interface for
56  * CruiseControl
57  *
58  * @author <a HREF="mailto:scottj+cc@escherichia.net">Scott Jacobs</a>
59  */

60 public class AlienBrainTest extends TestCase {
61    
62     private static final SimpleDateFormat JavaDoc DATE_FORMAT = new SimpleDateFormat JavaDoc("M/d/yyyy z");
63     private static final Date JavaDoc NT_TIME_ZERO;
64     private static final Date JavaDoc 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 JavaDoc e) {
71             throw new RuntimeException JavaDoc(e.getMessage());
72         }
73     }
74
75     /**
76      * Just want to see if the AlienBrain class can even be found.
77      */

78     public void testConstruction() {
79         new AlienBrain();
80     }
81     
82     /**
83      */

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 JavaDoc {
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 JavaDoc {
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 JavaDoc now = new Date JavaDoc();
117         assertEquals(now,
118             AlienBrain.filetimeToDate(AlienBrain.dateToFiletime(now)));
119     }
120     
121     public void testBuildGetModificationsCommand() throws ParseException JavaDoc {
122         AlienBrain ab = new AlienBrain();
123         
124         ab.setUser("FooUser");
125         ab.setPath("FooProject");
126
127         Date JavaDoc 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 JavaDoc {
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         //The CC AlienBrain SourceControl class does not yet support changesets.
145
//therefore each modified file results in a modification containing
146
//one file.
147
assertEquals(1, m.files.size());
148         assertEquals("/a/path/to/a/file.cpp",
149             ((Modification.ModifiedFile) (m.files.get(0))).fileName);
150     }
151     
152     /**
153      * Returns a file as a List of Strings, one String per line.
154      */

155     private List JavaDoc loadTestLog(String JavaDoc name) throws IOException JavaDoc {
156         InputStream JavaDoc testStream = getClass().getResourceAsStream(name);
157         assertNotNull("failed to load resource " + name + " in class "
158             + getClass().getName(), testStream);
159
160         List JavaDoc lines = new ArrayList JavaDoc();
161         String JavaDoc line;
162         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(testStream));
163         while ((line = reader.readLine()) != null) {
164             lines.add(line);
165         }
166
167         return lines;
168     }
169     
170     public void testParseModifications() throws IOException JavaDoc, ParseException JavaDoc {
171         List JavaDoc results = loadTestLog("alienbrain_modifications.txt");
172         
173         AlienBrain ab = new AlienBrain();
174         
175         List JavaDoc modifications = ab.parseModifications(results);
176         
177         assertEquals(
178             "Returned wrong number of modifications.",
179             7,
180             modifications.size());
181
182         SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("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     /**
217      */

218     public void testParseNoModifications() throws IOException JavaDoc {
219         List JavaDoc results = loadTestLog("alienbrain_nomodifications.txt");
220         
221         AlienBrain ab = new AlienBrain();
222         
223         List JavaDoc modifications = ab.parseModifications(results);
224         assertEquals(0, modifications.size());
225     }
226     
227     //The following tests all actually use the AlienBrain executable and
228
//may need to access a server. Therefore they can only be run if you
229
//have a licensed command-line client and access to a server.
230
/*
231     //In order for some of the following tests to pass, these members must
232     //be assigned values valid for your AlienBrain server.
233     private static final String TESTING_PATH = "alienbrain://Projects/Code/Engine/Inc";
234     private static final String TESTING_BRANCH = "Root Branch/SubBranch";
235     // Set any of the following to null if you do not want to
236     // override any NXN_AB_* environment variables you may be using.
237     private static final String TESTING_USERNAME = null; //"sjacobs";
238     private static final String TESTING_PASSWORD = null; //"pass123";
239     private static final String TESTING_SERVER = null; //"abserver";
240     private static final String TESTING_DATABASE = null; //"StudioVault";
241
242     public void testGetModifications() throws Exception {
243         AlienBrain ab = new AlienBrain();
244
245         ab.setServer(TESTING_SERVER);
246         ab.setDatabase(TESTING_DATABASE);
247         ab.setUser(TESTING_USERNAME);
248         ab.setPassword(TESTING_PASSWORD);
249         ab.setPath(TESTING_PATH);
250         
251         List modifications = ab.getModifications(new Date(0), new Date());
252         assertTrue("I would have expected the AlienBrain database "
253             + "to have at least one file modified since 1970!",
254             0 != modifications.size());
255         
256         for (java.util.Iterator it = modifications.iterator(); it.hasNext(); ) {
257             Modification m = (Modification) it.next();
258             System.out.println(m);
259         }
260     }
261     
262     public static void main(String[] args) {
263         junit.textui.TestRunner.run(AlienBrainTest.class);
264     }
265 */
// End of tests that require an actual AlienBrain installation.
266
}
267
Popular Tags