1 37 package net.sourceforge.cruisecontrol.buildloggers; 38 39 import junit.framework.TestCase; 40 import net.sourceforge.cruisecontrol.CruiseControlException; 41 import net.sourceforge.cruisecontrol.util.Util; 42 import org.jdom.Element; 43 import org.jdom.JDOMException; 44 import org.jdom.output.XMLOutputter; 45 import org.jdom.input.SAXBuilder; 46 47 import java.io.File ; 48 import java.io.FileWriter ; 49 import java.io.IOException ; 50 import java.io.StringReader ; 51 52 public class MergeLoggerTest extends TestCase { 53 private MergeLogger logger; 54 private File tempSubdir; 55 private Element log; 56 private XMLOutputter outputter = new XMLOutputter(); 57 58 private static final String BASIC_LOG_CONTENT = "<cruisecontrol></cruisecontrol>"; 59 60 protected void setUp() throws Exception { 61 logger = new MergeLogger(); 62 log = getBasicLog(); 63 64 File tempDir = new File (System.getProperty("java.io.tmpdir")); 65 tempSubdir = new File (tempDir, "cruisecontroltest" + System.currentTimeMillis()); 66 tempSubdir.mkdir(); 67 } 68 69 protected void tearDown() throws Exception { 70 logger = null; 71 Util.deleteFile(tempSubdir); 72 tempSubdir = null; 73 log = null; 74 } 75 76 public void testFilePatternValidation() throws Exception { 77 logger.setDir("temp"); 78 logger.setPattern(null); 79 try { 80 logger.validate(); 81 fail("Expected an exception because we have not specified a pattern."); 82 } catch (CruiseControlException expected) { 83 assertEquals("no file pattern was specified", expected.getMessage()); 84 } 85 86 logger.setPattern("[a-z*"); 87 try { 88 logger.validate(); 89 fail("Expected an exception because an invalid pattern."); 90 } catch (CruiseControlException expected) { 91 String expect = "Invalid filename pattern"; 92 assertEquals(expect, expected.getMessage().substring(0, expect.length())); 93 } 94 95 logger.setPattern("*.xml"); 96 logger.validate(); 97 } 98 99 public void testMergingFile() throws Exception { 100 String content = "<name>John Doe</name>"; 101 File fileToMerge = createFile(content); 102 103 logger.setFile(fileToMerge.getAbsolutePath()); 104 logger.validate(); 105 logger.log(log); 106 107 String expected = "<cruisecontrol>" + content + "</cruisecontrol>"; 108 String actual = outputter.outputString(log); 109 assertEquals(expected, actual); 110 } 111 112 public void testMergingDirectory() throws Exception { 113 createFile("<test1>pass</test1>"); 114 createFile("<test2>pass</test2>"); 115 116 logger.setDir(tempSubdir.getAbsolutePath()); 118 logger.validate(); 119 logger.log(log); 120 121 String expected1 = "<cruisecontrol><test1>pass</test1><test2>pass</test2></cruisecontrol>"; 123 String expected2 = "<cruisecontrol><test2>pass</test2><test1>pass</test1></cruisecontrol>"; 124 125 String actual = outputter.outputString(log); 126 assertEqualsEither(expected1, expected2, actual); 127 } 128 129 public void testValidation() throws CruiseControlException { 130 try { 131 logger.validate(); 132 fail("Expected an exception because we didn't set a file or directory."); 133 } catch (CruiseControlException expected) { 134 assertEquals("one of file or dir are required attributes", expected.getMessage()); 135 } 136 137 logger.setDir("temp"); 138 logger.setFile("tempfile.xml"); 139 try { 140 logger.validate(); 141 fail("Expected an exception because we set a file and a directory."); 142 } catch (CruiseControlException expected) { 143 assertEquals("only one of file or dir may be specified", expected.getMessage()); 144 } 145 146 logger.setDir(null); 147 logger.validate(); 148 149 logger.setDir("temp"); 150 logger.setFile(null); 151 logger.validate(); 152 } 153 154 public void testGetElement() throws IOException { 155 String withProperties = "<testsuite><properties /></testsuite>"; 156 String withoutProperties = "<testsuite />"; 157 158 File with = createFile(withProperties); 159 File without = createFile(withoutProperties); 160 161 Element elementWith = logger.getElement(with); 162 Element elementWithout = logger.getElement(without); 163 164 String actualWith = outputter.outputString(elementWith); 165 String actualWithout = outputter.outputString(elementWithout); 166 167 assertEquals(withoutProperties, actualWithout); 168 assertEquals(withoutProperties, actualWith); 169 170 logger.setRemoveProperties(false); 171 172 elementWith = logger.getElement(with); 173 elementWithout = logger.getElement(without); 174 175 actualWith = outputter.outputString(elementWith); 176 actualWithout = outputter.outputString(elementWithout); 177 178 assertEquals(withoutProperties, actualWithout); 179 assertEquals(withProperties, actualWith); 180 } 181 182 private Element getBasicLog() throws JDOMException, IOException { 183 SAXBuilder saxBuilder = new SAXBuilder(); 184 return saxBuilder.build(new StringReader (BASIC_LOG_CONTENT)).getRootElement(); 185 } 186 187 private static void writeFileContents(File theFile, String contents) throws IOException { 188 FileWriter fw = new FileWriter (theFile); 189 fw.write(contents); 190 fw.close(); 191 } 192 193 private File createFile(String content) throws IOException { 194 File fileToMerge = File.createTempFile(MergeLoggerTest.class.getName(), ".xml", tempSubdir); 195 writeFileContents(fileToMerge, content); 196 return fileToMerge; 197 } 198 199 202 private void assertEqualsEither(String expected1, String expected2, String actual) { 203 if (!expected1.equals(actual) && !expected2.equals(actual)) { 204 fail("Expected either [" + expected1 + "] or [" + expected2 205 + "], but was [" + actual + "]."); 206 } 207 } 208 209 } 210 | Popular Tags |