1 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 ; 47 import java.io.File ; 48 import java.io.IOException ; 49 import java.io.InputStream ; 50 import java.text.SimpleDateFormat ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 import java.util.Vector ; 54 55 58 public class ClearCaseTest extends TestCase { 59 60 private static final String WINDOWS_LOG = "clearcase-history.txt"; 61 private static final String UNIX_LOG = "clearcase-history-alt.txt"; 62 private static final String WINDOWS_XML = "clearcase-history.xml"; 63 private static final String UNIX_XML = "clearcase-history-alt.xml"; 64 65 public static final SimpleDateFormat DATE_FMT = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); 66 67 private ClearCase clearCase; 68 private List mods; 69 70 private InputStream loadTestLog(String name) { 71 InputStream 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 { 77 clearCase = new ClearCase(); 79 mods = new Vector (); 80 81 String testXML; 82 if (File.separatorChar == '\\') { 83 testXML = WINDOWS_XML; 84 } else { 85 testXML = UNIX_XML; 86 } 87 88 SAXBuilder parser = new SAXBuilder(); 90 Document doc = parser.build(loadTestLog(testXML)); 91 List elts = doc.getRootElement().getChildren(); 92 93 Iterator 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 105 public void testClearCaseStream() throws IOException { 106 String testLog; 107 if (File.separatorChar == '\\') { 108 testLog = WINDOWS_LOG; 109 } else { 110 testLog = UNIX_LOG; 111 } 112 BufferedInputStream stream = 113 new BufferedInputStream (loadTestLog(testLog)); 114 115 List 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 bc = new StringBuffer (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 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 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 cc = new ClearCase(); 201 cc.setViewpath("path"); 202 cc.setBranch("branch"); 203 204 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 { 216 217 } 218 219 public static void main(java.lang.String [] args) { 220 junit.textui.TestRunner.run(ClearCaseTest.class); 221 } 222 223 } 224 | Popular Tags |