001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.sourcecontrols;
037:
038: import junit.framework.TestCase;
039: import net.sourceforge.cruisecontrol.CruiseControlException;
040: import org.jdom.Document;
041: import org.jdom.Element;
042: import org.jdom.JDOMException;
043: import org.jdom.input.SAXBuilder;
044:
045: import java.io.BufferedInputStream;
046: import java.io.File;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.text.SimpleDateFormat;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Vector;
053:
054: /**
055: * @author Eric Lefevre
056: */
057: public class ClearCaseTest extends TestCase {
058:
059: private static final String WINDOWS_LOG = "clearcase-history.txt";
060: private static final String UNIX_LOG = "clearcase-history-alt.txt";
061: private static final String WINDOWS_XML = "clearcase-history.xml";
062: private static final String UNIX_XML = "clearcase-history-alt.xml";
063:
064: public static final SimpleDateFormat DATE_FMT = new SimpleDateFormat(
065: "yyyy/MM/dd HH:mm:ss");
066:
067: private ClearCase clearCase;
068: private List mods;
069:
070: private InputStream loadTestLog(String name) {
071: InputStream testStream = getClass().getResourceAsStream(name);
072: assertNotNull("failed to load resource " + name + " in class "
073: + getClass().getName(), testStream);
074: return testStream;
075: }
076:
077: protected void setUp() throws JDOMException, IOException {
078: // Initialize our ClearCase element
079: clearCase = new ClearCase();
080: mods = new Vector();
081:
082: String testXML;
083: if (File.separatorChar == '\\') {
084: testXML = WINDOWS_XML;
085: } else {
086: testXML = UNIX_XML;
087: }
088:
089: // Set up the modification list to match against
090: SAXBuilder parser = new SAXBuilder();
091: Document doc = parser.build(loadTestLog(testXML));
092: List elts = doc.getRootElement().getChildren();
093:
094: Iterator it = elts.iterator();
095: while (it.hasNext()) {
096: Element elt = (Element) it.next();
097: ClearCaseModification mod = new ClearCaseModification();
098: mod.fromElement(elt, DATE_FMT);
099: mods.add(mod);
100: }
101: }
102:
103: /**
104: * Tests the streams of bytes that can be returned by the ClearCase server.
105: */
106: public void testClearCaseStream() throws IOException {
107: String testLog;
108: if (File.separatorChar == '\\') {
109: testLog = WINDOWS_LOG;
110: } else {
111: testLog = UNIX_LOG;
112: }
113: BufferedInputStream stream = new BufferedInputStream(
114: loadTestLog(testLog));
115:
116: List list = clearCase.parseStream(stream);
117: assertEquals(mods.size(), list.size());
118:
119: for (int i = 0; i < list.size(); i++) {
120: ClearCaseModification a = (ClearCaseModification) mods
121: .get(i);
122: ClearCaseModification b = (ClearCaseModification) list
123: .get(i);
124:
125: assertEquals(a.type, b.type);
126: assertEquals(a.modifiedTime, b.modifiedTime);
127: assertEquals(a.userName, b.userName);
128: assertEquals(a.emailAddress, b.emailAddress);
129: assertEquals(a.revision, b.revision);
130: assertEquals(a.labels, b.labels);
131: assertEquals(a.attributes, b.attributes);
132:
133: assertEquals(a.files.size(), b.files.size());
134: for (int j = 0; j < b.files.size(); j++) {
135: ClearCaseModification.ModifiedFile af = (ClearCaseModification.ModifiedFile) a.files
136: .get(j);
137: ClearCaseModification.ModifiedFile bf = (ClearCaseModification.ModifiedFile) b.files
138: .get(j);
139: assertEquals(af.action, bf.action);
140: assertEquals(af.fileName, bf.fileName);
141: assertEquals(af.folderName, bf.folderName);
142: assertEquals(af.revision, bf.revision);
143: }
144:
145: StringBuffer bc = new StringBuffer(b.comment);
146: for (int j = 0; j < bc.length(); j++) {
147: if (bc.charAt(j) == 13) {
148: bc.deleteCharAt(j);
149: }
150: }
151: assertEquals(a.comment, bc.toString());
152:
153: System.out.println("Record " + i + " OK");
154: }
155: }
156:
157: public void testValidate() {
158: ClearCase cc = new ClearCase();
159:
160: try {
161: cc.validate();
162: fail("ClearCase should throw exceptions when required attributes are not set.");
163: } catch (CruiseControlException e) {
164: assertTrue(true);
165: }
166:
167: cc.setViewpath("path");
168:
169: try {
170: cc.validate();
171: assertTrue(true);
172: } catch (CruiseControlException e) {
173: fail("ClearCase should not throw exceptions when required attributes are set.");
174: }
175: }
176:
177: public void testRecursiveAndAll() {
178: ClearCase cc = new ClearCase();
179: cc.setViewpath("path");
180: cc.setBranch("branch");
181:
182: // test setting just 'all'
183: cc.setAll(true);
184: try {
185: cc.validate();
186: assertTrue(true);
187: } catch (CruiseControlException e) {
188: fail("ClearCase should not throw exceptions when only the 'all' attribute is set.");
189: }
190:
191: // test 'recursive' together with 'all'
192: cc.setRecursive(true);
193: try {
194: cc.validate();
195: fail("ClearCase should throw an exception when both 'recursive' and 'all' are set.");
196: } catch (CruiseControlException e) {
197: assertTrue(true);
198: }
199:
200: // reset object to make sure we are testing with defaults
201: cc = new ClearCase();
202: cc.setViewpath("path");
203: cc.setBranch("branch");
204:
205: // test setting just 'recursive'
206: cc.setRecursive(true);
207: try {
208: cc.validate();
209: assertTrue(true);
210: } catch (CruiseControlException e) {
211: fail("ClearCase should not throw an exception when only the 'all' attribute is set.");
212: }
213: }
214:
215: }
|