1 37 46 package net.sourceforge.cruisecontrol.sourcecontrols; 47 48 import java.io.BufferedReader ; 49 import java.io.IOException ; 50 import java.io.InputStream ; 51 import java.io.InputStreamReader ; 52 import java.text.ParseException ; 53 import java.text.SimpleDateFormat ; 54 import java.util.ArrayList ; 55 import java.util.Calendar ; 56 import java.util.Date ; 57 import java.util.GregorianCalendar ; 58 import java.util.List ; 59 import java.util.StringTokenizer ; 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 lines; 73 public boolean parseStream(InputStream iStream) throws CruiseControlException { 74 BufferedReader reader = new BufferedReader (new InputStreamReader (iStream)); 75 lines = new ArrayList (); 76 String line; 77 try { 78 while ((line = reader.readLine()) != null) { 79 lines.add(line); 80 } 81 } catch (IOException e) { 82 throw new CruiseControlException("Error reading Accurev output"); 83 } 84 return true; 85 } 86 } 87 88 private static final SimpleDateFormat DTFM = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); 89 90 private Date parseLogDateFormat(String dateString) throws ParseException { 91 return DTFM.parse(dateString); 92 } 93 94 Modification createModification(String userName, String emailAddress, String comment, 95 String revision, String modifiedTime, String type) throws ParseException { 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 { 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 ago = new GregorianCalendar (); 115 ago.add(Calendar.MONTH, -1); 116 List modifications = accurev.getModifications(ago.getTime(), new GregorianCalendar ().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 146 private String 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 lastLine = collector.lines.get(collector.lines.size() - 1).toString(); 155 return getNameFromLine(lastLine); 156 } 157 158 private String getNameFromLine(String lastLine) { 159 return new StringTokenizer (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 |