1 37 package net.sourceforge.cruisecontrol; 38 39 import java.io.File ; 40 import java.io.FilenameFilter ; 41 import java.io.IOException ; 42 import java.io.StringReader ; 43 import java.text.ParseException ; 44 import java.util.ArrayList ; 45 import java.util.Calendar ; 46 import java.util.Date ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import junit.framework.TestCase; 50 import net.sourceforge.cruisecontrol.logmanipulators.DeleteManipulator; 51 import net.sourceforge.cruisecontrol.logmanipulators.GZIPManipulator; 52 53 import org.jdom.Element; 54 import org.jdom.JDOMException; 55 import org.jdom.input.SAXBuilder; 56 import org.jdom.output.Format; 57 import org.jdom.output.XMLOutputter; 58 59 60 public class LogTest extends TestCase { 61 private final List filesToClear = new ArrayList (); 62 63 public void tearDown() { 64 for (Iterator iterator = filesToClear.iterator(); iterator.hasNext();) { 65 File file = (File ) iterator.next(); 66 if (file.exists()) { 67 file.delete(); 68 } 69 } 70 } 71 72 public void testCreatingLog() { 73 try { 75 Log log = new Log(); 76 log.setProjectName(null); 77 fail("Expected an exception when creating a Log instance with " 78 + "a null Project name."); 79 } catch (NullPointerException npe) { 80 } 82 83 try { 85 Log log = new Log(); 86 log.validate(); 87 fail("Expected an exception when creating a Log instance with " 88 + "a null Project name."); 89 } catch (IllegalStateException npe) { 90 } catch (CruiseControlException cce) { 92 fail("unepected: " + cce.getMessage()); 93 } 94 } 95 96 public void testDefaultLogLocation() { 97 Log log = new Log(); 98 log.setProjectName("foo"); 99 assertEquals("logs" + File.separatorChar + "foo", log.getLogDir()); 100 } 101 102 public void testFormatLogFileName() { 103 Calendar augTweleveCalendar = Calendar.getInstance(); 104 augTweleveCalendar.set(2004, 7, 12, 1, 1, 1); 105 Date augTweleve = augTweleveCalendar.getTime(); 106 107 String expected = "log20040812010101.xml"; 108 String actual = Log.formatLogFileName(augTweleve); 109 assertEquals( 110 expected + "--" + actual, 111 expected, actual); 112 assertEquals("log20040812010101Lbuild.1.xml", Log.formatLogFileName(augTweleve, "build.1")); 113 } 114 115 public void testWasSuccessfulBuild() { 116 assertTrue(Log.wasSuccessfulBuild("log20040812010101Lbuild.1.xml")); 117 assertFalse(Log.wasSuccessfulBuild("log20040812010101.xml")); 118 assertFalse(Log.wasSuccessfulBuild(null)); 119 } 120 121 public void testParseDateFromLogFileName() throws ParseException { 122 Calendar augTweleveCalendar = Calendar.getInstance(); 123 augTweleveCalendar.set(2004, 7, 12, 1, 1, 1); 124 Date augTweleve = augTweleveCalendar.getTime(); 125 126 assertEquals(augTweleve.toString(), Log.parseDateFromLogFileName("log20040812010101Lbuild.1.xml").toString()); 127 assertEquals(augTweleve.toString(), Log.parseDateFromLogFileName("log20040812010101.xml").toString()); 128 } 129 130 public void testParseLabelFromLogFileName() { 131 assertEquals("build.1", Log.parseLabelFromLogFileName("log20040812010101Lbuild.1.xml")); 132 assertEquals("", Log.parseLabelFromLogFileName("log20040812010101.xml")); 133 } 134 135 public void testXMLEncoding() 136 throws CruiseControlException, IOException , JDOMException { 137 String [] encodings = { "UTF-8", "ISO-8859-1", null }; 138 139 SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); 140 XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); 141 for (int i = 0; i < encodings.length; i++) { 142 Log log = new Log(); 143 log.setProjectName("testXMLEncoding"); 144 log.setDir("target"); 145 if (encodings[i] != null) { 146 log.setEncoding(encodings[i]); 147 } 148 log.validate(); 149 150 log.addContent(getBuildLogInfo()); 152 Element build = new Element("build"); 153 log.addContent(build); 154 log.addContent(new Element("modifications")); 155 156 build.setText("Something with special characters: \u00c6\u00d8\u00c5"); 158 159 log.writeLogFile(new Date ()); 161 File logFile = log.getLastLogFile(); 162 filesToClear.add(logFile); 163 Element actualContent = builder.build(logFile).getRootElement(); 164 165 String expected = outputter.outputString(log.getContent()); 169 String actual = outputter.outputString(actualContent); 170 assertEquals(expected, actual); 171 } 172 } 173 174 public void testManipulateLog() throws Exception { 175 String testProjectName = "testBackupLog"; 176 String testLogDir = "target"; 177 178 Calendar date = Calendar.getInstance(); 180 date.set(Calendar.YEAR, date.get(Calendar.YEAR) - 1); 181 date.set(Calendar.MONTH, date.get(Calendar.MONTH) - 13); 182 GZIPManipulator gzip = new GZIPManipulator(); 183 gzip.setEvery(12); 184 gzip.setUnit("month"); 185 Log log = getWrittenTestLog(testProjectName, testLogDir, date.getTime()); 186 log = getWrittenTestLog(testProjectName, testLogDir, new Date ()); 187 log.add(gzip); 188 log.validate(); 189 assertBackupsHelper(log, 2, 1, 1); 190 191 date = Calendar.getInstance(); 193 date.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH) - 2); 194 gzip = new GZIPManipulator(); 195 gzip.setEvery(2); 196 gzip.setUnit("day"); 197 log = getWrittenTestLog(testProjectName, testLogDir, date.getTime()); 198 log.add(gzip); 199 log.validate(); 200 assertBackupsHelper(log, 3, 1, 2); 201 202 date = Calendar.getInstance(); 204 date.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH) - 2); 205 DeleteManipulator deleteManipulator = new DeleteManipulator(); 206 deleteManipulator.setEvery(2); 207 deleteManipulator.setUnit("day"); 208 log = getWrittenTestLog(testProjectName, testLogDir, date.getTime()); 209 log.add(deleteManipulator); 210 log.validate(); 211 assertBackupsHelper(log, 3, 1, 2); 212 213 date = Calendar.getInstance(); 214 date.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH) - 2); 215 deleteManipulator = new DeleteManipulator(); 216 deleteManipulator.setEvery(2); 217 deleteManipulator.setUnit("day"); 218 deleteManipulator.setIgnoreSuffix(true); 220 log = getWrittenTestLog(testProjectName, testLogDir, date.getTime()); 221 log.add(deleteManipulator); 222 log.validate(); 223 assertBackupsHelper(log, 1, 1, 0); 224 225 gzip = new GZIPManipulator(); 227 gzip.setUnit("day"); 228 log = getWrittenTestLog(testProjectName, testLogDir, date.getTime()); 229 log.add(gzip); 230 try { 231 log.validate(); 232 fail("Validation should fail!"); 233 } catch (CruiseControlException e) { 234 assertTrue(true); 235 } 236 237 238 } 239 240 private void assertBackupsHelper(Log log, int expectedLength, int expectedXML, int expectedGZIP) { 241 log.callManipulators(); 242 File [] logfiles = new File (log.getLogDir()).listFiles(new FilenameFilter () { 243 244 public boolean accept(File file, String fileName) { 245 return fileName.startsWith("log20") 246 && (fileName.endsWith(".xml") || fileName 247 .endsWith(".gz")); 248 } 249 250 }); 251 int countGzip = 0; 252 int countXML = 0; 253 for (int i = 0; i < logfiles.length; i++) { 254 File file = logfiles[i]; 255 if (file.getName().endsWith(".gz")) { 256 filesToClear.add(file); 257 countGzip++; 258 } else if (file.getName().endsWith(".xml")) { 259 countXML++; 260 } else { 261 fail("Other log files exists"); 262 } 263 } 264 assertEquals(expectedLength, logfiles.length); 265 assertEquals(expectedXML, countXML); 266 assertEquals(expectedGZIP, countGzip); 267 } 268 269 private Log getWrittenTestLog(String projectName, String testLogDir, 270 Date date) throws CruiseControlException, JDOMException, 271 IOException { 272 Log log; 273 Element build; 274 log = new Log(); 275 log.setProjectName(projectName); 276 log.setDir(testLogDir); 277 log.addContent(getBuildLogInfo()); 278 build = new Element("build"); 279 log.addContent(build); 280 log.addContent(new Element("modifications")); 281 log.writeLogFile(date); 282 filesToClear.add(log.getLastLogFile()); 283 return log; 284 } 285 286 private Element getBuildLogInfo() throws JDOMException, IOException { 288 SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); 289 String infoXML = "<info><property name=\"label\" value=\"\"/>" 290 + "<property name=\"lastbuildtime\" value=\"\"/>" 291 + "<property name=\"lastgoodbuildtime\" value=\"\"/>" 292 + "<property name=\"lastbuildsuccessful\" value=\"\"/>" 293 + "<property name=\"buildfile\" value=\"\"/>" 294 + "<property name=\"buildtarget\" value=\"\"/>" 295 + "</info>"; 296 Element info = builder.build(new StringReader (infoXML)).getRootElement(); 297 return (Element) info.clone(); 298 } 299 } 300 | Popular Tags |