1 37 38 package net.sourceforge.cruisecontrol.testutil; 39 40 import java.util.Arrays ; 41 import java.util.Hashtable ; 42 import java.util.Iterator ; 43 import java.io.File ; 44 import java.io.PrintWriter ; 45 import java.io.BufferedWriter ; 46 import java.io.FileWriter ; 47 import java.io.IOException ; 48 49 import junit.framework.Assert; 50 51 import org.jdom.Element; 52 import org.jdom.Document; 53 54 public final class TestUtil { 55 56 private TestUtil() { 57 58 } 59 60 public static Element createElement(boolean success, boolean lastBuildSuccess) { 61 return createElement(success, lastBuildSuccess, "2 minutes 20 seconds", 4, null); 62 } 63 64 public static Element createModsElement(int numMods) { 65 Element modificationsElement = new Element("modifications"); 66 for (int i = 1; i <= numMods; i++) { 67 Element modificationElement = new Element("modification"); 68 69 Element dateElement = new Element("date"); 70 dateElement.addContent("10/30/2004 13:00:03"); modificationElement.addContent(dateElement); 72 73 Element commentElement = new Element("comment"); 74 commentElement.addContent("The comment"); 75 modificationElement.addContent(commentElement); 76 77 Element revisionElement = new Element("revision"); 78 revisionElement.addContent("1." + i); 79 modificationElement.addContent(revisionElement); 80 81 Element fileElement = new Element("file"); 82 fileElement.setAttribute("action", "modified"); 83 84 Element revision2 = new Element("revision"); 85 revision2.addContent("1." + i); 86 fileElement.addContent(revision2); 87 88 Element fileName = new Element("filename"); 89 fileName.addContent("filename" + i); 90 fileElement.addContent(fileName); 91 modificationElement.addContent(fileElement); 92 93 if (i != 1) { 94 Element projectElement = new Element("project"); 95 projectElement.addContent("basedir/subdirectory" + i); 96 fileElement.addContent(projectElement); 97 } 98 99 Element userElement = new Element("user"); 100 int userNumber = (i > 2) ? i - 1 : i; 101 userElement.addContent("user" + userNumber); 102 modificationElement.addContent(userElement); 103 104 modificationsElement.addContent(modificationElement); 105 } 106 return modificationsElement; 107 } 108 109 110 public static Element createElement( 111 boolean success, 112 boolean lastBuildSuccess, 113 String time, 114 int modCount, 115 String failureReason) { 116 Element cruisecontrolElement = new Element("cruisecontrol"); 117 Element buildElement = new Element("build"); 118 buildElement.setAttribute("time", time); 119 120 if (!success) { 121 buildElement.setAttribute( 122 "error", 123 (failureReason == null) ? "Compile failed" : failureReason); 124 } 125 126 cruisecontrolElement.addContent(createModsElement(modCount)); 127 cruisecontrolElement.addContent(buildElement); 128 cruisecontrolElement.addContent( 129 createInfoElement("somelabel", lastBuildSuccess)); 130 Document doc = new Document(); 131 doc.setRootElement(cruisecontrolElement); 132 return cruisecontrolElement; 133 } 134 135 public static Element createInfoElement(String label, boolean lastSuccessful) { 136 Element infoElement = new Element("info"); 137 138 Hashtable properties = new Hashtable (); 139 properties.put("projectname", "someproject"); 140 properties.put("label", label); 141 properties.put("lastbuildtime", ""); 142 properties.put("lastgoodbuildtime", ""); 143 properties.put("lastbuildsuccessful", lastSuccessful + ""); 144 properties.put("buildfile", ""); 145 properties.put("builddate", "11/30/2005 12:07:27"); 146 properties.put("target", ""); 147 properties.put("logfile", "log20020313120000.xml"); 148 properties.put("cctimestamp", "20020313120000"); 149 150 Iterator propertyIterator = properties.keySet().iterator(); 151 while (propertyIterator.hasNext()) { 152 String propertyName = (String ) propertyIterator.next(); 153 Element propertyElement = new Element("property"); 154 propertyElement.setAttribute("name", propertyName); 155 propertyElement.setAttribute("value", (String ) properties.get(propertyName)); 156 infoElement.addContent(propertyElement); 157 } 158 159 return infoElement; 160 } 161 162 165 public static void assertArray(String msg, String [] refarr, String [] testarr) { 166 Assert.assertNotNull(refarr); 167 Assert.assertNotNull(testarr); 168 169 Assert.assertNotNull(msg + " Reference array is null and test not", refarr); 170 Assert.assertNotNull(msg + " Test array is null and reference not", testarr); 171 int shorterLength = Math.min(refarr.length, testarr.length); 172 173 for (int i = 0; i < shorterLength; i++) { 174 Assert.assertEquals(msg + " Element " + i + " mismatch.", refarr[i], testarr[i]); 175 } 176 Assert.assertEquals(msg + " Arrays have different lengths", refarr.length, testarr.length); 177 Assert.assertEquals(msg, Arrays.asList(refarr), Arrays.asList(testarr)); 178 } 179 180 public static void write(String text, File file) throws IOException { 181 PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter (file))); 182 out.print(text); 183 out.flush(); 184 out.close(); 185 } 186 } 187 | Popular Tags |