KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
40 import net.sourceforge.cruisecontrol.CruiseControlException;
41 import org.jdom.Document;
42 import org.jdom.Element;
43 import org.jdom.JDOMException;
44 import org.jdom.input.SAXBuilder;
45
46 import java.io.BufferedInputStream JavaDoc;
47 import java.io.File JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.text.SimpleDateFormat JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Vector JavaDoc;
54
55 /**
56  * @author Eric Lefevre
57  */

58 public class ClearCaseTest extends TestCase {
59
60     private static final String JavaDoc WINDOWS_LOG = "clearcase-history.txt";
61     private static final String JavaDoc UNIX_LOG = "clearcase-history-alt.txt";
62     private static final String JavaDoc WINDOWS_XML = "clearcase-history.xml";
63     private static final String JavaDoc UNIX_XML = "clearcase-history-alt.xml";
64
65     public static final SimpleDateFormat JavaDoc DATE_FMT = new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss");
66
67     private ClearCase clearCase;
68     private List JavaDoc mods;
69
70     private InputStream JavaDoc loadTestLog(String JavaDoc name) {
71         InputStream JavaDoc testStream = getClass().getResourceAsStream(name);
72         assertNotNull("failed to load resource " + name + " in class " + getClass().getName(), testStream);
73         return testStream;
74     }
75
76     protected void setUp() throws JDOMException, IOException JavaDoc {
77         // Initialize our ClearCase element
78
clearCase = new ClearCase();
79         mods = new Vector JavaDoc();
80
81         String JavaDoc testXML;
82         if (File.separatorChar == '\\') {
83             testXML = WINDOWS_XML;
84         } else {
85             testXML = UNIX_XML;
86         }
87
88         // Set up the modification list to match against
89
SAXBuilder parser = new SAXBuilder();
90         Document doc = parser.build(loadTestLog(testXML));
91         List JavaDoc elts = doc.getRootElement().getChildren();
92
93         Iterator JavaDoc it = elts.iterator();
94         while (it.hasNext()) {
95             Element elt = (Element) it.next();
96             ClearCaseModification mod = new ClearCaseModification();
97             mod.fromElement(elt, DATE_FMT);
98             mods.add(mod);
99         }
100     }
101
102     /**
103      * Tests the streams of bytes that can be returned by the ClearCase server.
104      */

105     public void testClearCaseStream() throws IOException JavaDoc {
106         String JavaDoc testLog;
107         if (File.separatorChar == '\\') {
108             testLog = WINDOWS_LOG;
109         } else {
110             testLog = UNIX_LOG;
111         }
112         BufferedInputStream JavaDoc stream =
113                 new BufferedInputStream JavaDoc(loadTestLog(testLog));
114
115         List JavaDoc list = clearCase.parseStream(stream);
116         assertEquals(mods.size(), list.size());
117
118         for (int i = 0; i < list.size(); i++) {
119             ClearCaseModification a = (ClearCaseModification) mods.get(i);
120             ClearCaseModification b = (ClearCaseModification) list.get(i);
121
122             assertEquals(a.type, b.type);
123             assertEquals(a.modifiedTime, b.modifiedTime);
124             assertEquals(a.userName, b.userName);
125             assertEquals(a.emailAddress, b.emailAddress);
126             assertEquals(a.revision, b.revision);
127             assertEquals(a.labels, b.labels);
128             assertEquals(a.attributes, b.attributes);
129
130             assertEquals(a.files.size(), b.files.size());
131             for (int j = 0; j < b.files.size(); j++) {
132                 ClearCaseModification.ModifiedFile af =
133                         (ClearCaseModification.ModifiedFile) a.files.get(j);
134                 ClearCaseModification.ModifiedFile bf =
135                         (ClearCaseModification.ModifiedFile) b.files.get(j);
136                 assertEquals(af.action, bf.action);
137                 assertEquals(af.fileName, bf.fileName);
138                 assertEquals(af.folderName, bf.folderName);
139                 assertEquals(af.revision, bf.revision);
140             }
141
142
143             StringBuffer JavaDoc bc = new StringBuffer JavaDoc(b.comment);
144             for (int j = 0; j < bc.length(); j++) {
145                 if (bc.charAt(j) == 13) {
146                     bc.deleteCharAt(j);
147                 }
148             }
149             assertEquals(a.comment, bc.toString());
150
151             System.out.println("Record " + i + " OK");
152         }
153     }
154
155     public void testValidate() {
156         ClearCase cc = new ClearCase();
157
158         try {
159             cc.validate();
160             fail("ClearCase should throw exceptions when required attributes are not set.");
161         } catch (CruiseControlException e) {
162             assertTrue(true);
163         }
164
165         cc.setViewpath("path");
166         cc.setBranch("branch");
167
168         try {
169             cc.validate();
170             assertTrue(true);
171         } catch (CruiseControlException e) {
172             fail("ClearCase should not throw exceptions when required attributes are set.");
173         }
174     }
175
176     public void testRecursiveAndAll() {
177         ClearCase cc = new ClearCase();
178         cc.setViewpath("path");
179         cc.setBranch("branch");
180
181         // test setting just 'all'
182
cc.setAll(true);
183         try {
184             cc.validate();
185             assertTrue(true);
186         } catch (CruiseControlException e) {
187             fail("ClearCase should not throw exceptions when only the 'all' attribute is set.");
188         }
189
190         // test 'recursive' together with 'all'
191
cc.setRecursive(true);
192         try {
193             cc.validate();
194             fail("ClearCase should throw an exception when both 'recursive' and 'all' are set.");
195         } catch (CruiseControlException e) {
196             assertTrue(true);
197         }
198
199         // reset object to make sure we are testing with defaults
200
cc = new ClearCase();
201         cc.setViewpath("path");
202         cc.setBranch("branch");
203
204         // test setting just 'recursive'
205
cc.setRecursive(true);
206         try {
207             cc.validate();
208             assertTrue(true);
209         } catch (CruiseControlException e) {
210             fail("ClearCase should not throw an exception when only the 'all' attribute is set.");
211         }
212     }
213
214
215     public void testOutput() throws IOException JavaDoc {
216
217     }
218
219     public static void main(java.lang.String JavaDoc[] args) {
220         junit.textui.TestRunner.run(ClearCaseTest.class);
221     }
222
223 }
224
Popular Tags