KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > buildloggers > MergeLoggerTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

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 JavaDoc;
48 import java.io.FileWriter JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.io.StringReader JavaDoc;
51
52 public class MergeLoggerTest extends TestCase {
53     private MergeLogger logger;
54     private File JavaDoc tempSubdir;
55     private Element log;
56     private XMLOutputter outputter = new XMLOutputter();
57
58     private static final String JavaDoc BASIC_LOG_CONTENT = "<cruisecontrol></cruisecontrol>";
59
60     protected void setUp() throws Exception JavaDoc {
61         logger = new MergeLogger();
62         log = getBasicLog();
63
64         File JavaDoc tempDir = new File JavaDoc(System.getProperty("java.io.tmpdir"));
65         tempSubdir = new File JavaDoc(tempDir, "cruisecontroltest" + System.currentTimeMillis());
66         tempSubdir.mkdir();
67     }
68
69     protected void tearDown() throws Exception JavaDoc {
70         logger = null;
71         Util.deleteFile(tempSubdir);
72         tempSubdir = null;
73         log = null;
74     }
75
76     public void testFilePatternValidation() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
100         String JavaDoc content = "<name>John Doe</name>";
101         File JavaDoc fileToMerge = createFile(content);
102
103         logger.setFile(fileToMerge.getAbsolutePath());
104         logger.validate();
105         logger.log(log);
106
107         String JavaDoc expected = "<cruisecontrol>" + content + "</cruisecontrol>";
108         String JavaDoc actual = outputter.outputString(log);
109         assertEquals(expected, actual);
110     }
111
112     public void testMergingDirectory() throws Exception JavaDoc {
113         createFile("<test1>pass</test1>");
114         createFile("<test2>pass</test2>");
115
116         //Merge the xml files from the subdirectory.
117
logger.setDir(tempSubdir.getAbsolutePath());
118         logger.validate();
119         logger.log(log);
120
121         //Since the order isn't guaranteed, the expected value is one of two things
122
String JavaDoc expected1 = "<cruisecontrol><test1>pass</test1><test2>pass</test2></cruisecontrol>";
123         String JavaDoc expected2 = "<cruisecontrol><test2>pass</test2><test1>pass</test1></cruisecontrol>";
124
125         String JavaDoc 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 JavaDoc {
155         String JavaDoc withProperties = "<testsuite><properties /></testsuite>";
156         String JavaDoc withoutProperties = "<testsuite />";
157
158         File JavaDoc with = createFile(withProperties);
159         File JavaDoc without = createFile(withoutProperties);
160
161         Element elementWith = logger.getElement(with);
162         Element elementWithout = logger.getElement(without);
163
164         String JavaDoc actualWith = outputter.outputString(elementWith);
165         String JavaDoc 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 JavaDoc {
183         SAXBuilder saxBuilder = new SAXBuilder();
184         return saxBuilder.build(new StringReader JavaDoc(BASIC_LOG_CONTENT)).getRootElement();
185     }
186
187     private static void writeFileContents(File JavaDoc theFile, String JavaDoc contents) throws IOException JavaDoc {
188         FileWriter JavaDoc fw = new FileWriter JavaDoc(theFile);
189         fw.write(contents);
190         fw.close();
191     }
192
193     private File JavaDoc createFile(String JavaDoc content) throws IOException JavaDoc {
194         File JavaDoc fileToMerge = File.createTempFile(MergeLoggerTest.class.getName(), ".xml", tempSubdir);
195         writeFileContents(fileToMerge, content);
196         return fileToMerge;
197     }
198
199     /**
200      * Asserts that the actual equals either expected1 or expected2.
201      */

202     private void assertEqualsEither(String JavaDoc expected1, String JavaDoc expected2, String JavaDoc 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