KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
38  * Created on 04-Jul-2005 by norru
39  *
40  * Copyright (C) Sony Computer Entertainment Europe
41  * Studio Liverpool Server Group
42  *
43  * Authors:
44  * Nicola Orru' <Nicola_Orru@scee.net>
45  */

46 package net.sourceforge.cruisecontrol.sourcecontrols;
47
48 import java.io.BufferedReader JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.io.InputStream JavaDoc;
51 import java.io.InputStreamReader JavaDoc;
52 import java.text.ParseException JavaDoc;
53 import java.text.SimpleDateFormat JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.Calendar JavaDoc;
56 import java.util.Date JavaDoc;
57 import java.util.GregorianCalendar JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.StringTokenizer JavaDoc;
60
61 import net.sourceforge.cruisecontrol.CruiseControlException;
62 import net.sourceforge.cruisecontrol.Modification;
63 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommand;
64 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommandline;
65 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevInputParser;
66
67 public class AccurevSourcecontrolTest extends AccurevTest {
68   public AccurevSourcecontrolTest() {
69     super();
70   }
71   public class LineCollector implements AccurevInputParser {
72     public List JavaDoc lines;
73     public boolean parseStream(InputStream JavaDoc iStream) throws CruiseControlException {
74       BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(iStream));
75       lines = new ArrayList JavaDoc();
76       String JavaDoc line;
77       try {
78         while ((line = reader.readLine()) != null) {
79           lines.add(line);
80         }
81       } catch (IOException JavaDoc e) {
82         throw new CruiseControlException("Error reading Accurev output");
83       }
84       return true;
85     }
86   }
87
88   private static final SimpleDateFormat JavaDoc DTFM = new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss");
89
90   private Date JavaDoc parseLogDateFormat(String JavaDoc dateString) throws ParseException JavaDoc {
91     return DTFM.parse(dateString);
92   }
93
94   Modification createModification(String JavaDoc userName, String JavaDoc emailAddress, String JavaDoc comment,
95                                   String JavaDoc revision, String JavaDoc modifiedTime, String JavaDoc type) throws ParseException JavaDoc {
96     Modification modification = new Modification();
97     modification.userName = userName;
98     modification.emailAddress = emailAddress;
99     modification.comment = comment;
100     modification.revision = revision;
101     modification.modifiedTime = parseLogDateFormat(modifiedTime);
102     modification.type = type;
103     return modification;
104   }
105
106   public void testAccurevSourcecontrol() throws Exception JavaDoc {
107     fake("accurev_show_wspaces.txt", 0);
108     fake("accurev_hist_lastbuild_now.txt", 0);
109     Accurev accurev = new Accurev();
110     accurev.setRunner(getMockRunner());
111     accurev.setVerbose(true);
112     accurev.setStream(getTestStreamName());
113     accurev.validate();
114     Calendar JavaDoc ago = new GregorianCalendar JavaDoc();
115     ago.add(Calendar.MONTH, -1);
116     List JavaDoc modifications = accurev.getModifications(ago.getTime(), new GregorianCalendar JavaDoc().getTime());
117     accurev.getProperties();
118
119     assertNotNull(modifications);
120     assertEquals(6, modifications.size());
121
122     Modification modification;
123
124     modification = createModification("norru", null, " Comment\n", "120208", "2005/06/22 10:53:10", "keep");
125     assertEquals(modification, modifications.get(0));
126
127     modification = createModification("norru", null, "", "120209", "2005/06/22 10:54:44", "defcomp");
128     assertEquals(modification, modifications.get(1));
129
130     modification = createModification("norru", null, "", "120210", "2005/06/22 10:57:23", "defcomp");
131     assertEquals(modification, modifications.get(2));
132
133     modification = createModification("norru", null, "", "120214", "2005/06/22 11:01:07", "defcomp");
134     assertEquals(modification, modifications.get(3));
135
136     modification = createModification("norru", null, "", "120216", "2005/06/22 11:06:55", "defcomp");
137     assertEquals(modification, modifications.get(4));
138
139     modification = createModification("norru", null, "", "120217", "2005/06/22 11:07:27", "defcomp");
140     assertEquals(modification, modifications.get(5));
141   }
142   
143   /**
144    * Picks the last stream name from a list of streams
145    */

146   private String JavaDoc getTestStreamName() {
147     LineCollector collector = new LineCollector();
148     AccurevCommandline show = AccurevCommand.SHOW.create(getMockRunner());
149     show.addArgument("wspaces");
150     show.setInputParser(collector);
151     show.run();
152     assertNotNull(collector.lines);
153     assertTrue(collector.lines.size() > 1);
154     String JavaDoc lastLine = collector.lines.get(collector.lines.size() - 1).toString();
155     return getNameFromLine(lastLine);
156   }
157
158   private String JavaDoc getNameFromLine(String JavaDoc lastLine) {
159     return new StringTokenizer JavaDoc(lastLine, " \t").nextToken();
160   }
161   
162   public void testValidate() {
163     Accurev accurev = new Accurev();
164     try {
165       accurev.validate();
166       fail("Validate should throw an exception when the stream is not set");
167     } catch (CruiseControlException e) {
168     }
169   }
170 }
171
Popular Tags