KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > XPathAwareChildTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, 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.util;
38
39 import junit.framework.TestCase;
40 import net.sourceforge.cruisecontrol.CruiseControlException;
41 import net.sourceforge.cruisecontrol.testutil.TestUtil;
42 import org.jdom.Element;
43
44 import java.io.ByteArrayInputStream JavaDoc;
45
46 public class XPathAwareChildTest extends TestCase {
47
48
49     public void testFailsIfCurrentLogNotSet() {
50         XPathAwareChild xpathField = new XPathAwareChild();
51         xpathField.setXPathExpression("foo");
52
53         try {
54             xpathField.validate();
55             xpathField.lookupValue(null);
56             fail("Expected an exception");
57         } catch (CruiseControlException expected) {
58             assertTrue(expected.getMessage().indexOf("current cruisecontrol log not set") >= 0);
59         }
60     }
61
62
63     public void testXPathExpression() throws CruiseControlException {
64         String JavaDoc xmlDocument = "<foo><bar>baz</bar></foo>";
65         String JavaDoc bazXPath = "/foo/bar/text()";
66
67         XPathAwareChild xpathField = new XPathAwareChild();
68         xpathField.setXPathExpression(bazXPath);
69         xpathField.setInputStream(new ByteArrayInputStream JavaDoc(xmlDocument.getBytes()));
70
71         xpathField.validate();
72         assertEquals("baz", xpathField.lookupValue(null));
73     }
74
75     public void testXPathExpressionAndValueSetOnField() {
76         XPathAwareChild xpathField = new XPathAwareChild();
77         xpathField.setValue("foo");
78         xpathField.setXPathExpression("bar");
79
80         try {
81             xpathField.validate();
82             fail("Expected a validation exception");
83         } catch (CruiseControlException expected) {
84         }
85     }
86
87     public void testXPathExpressionAndValueSetOnDescription() {
88         XPathAwareChild description = new XPathAwareChild();
89         description.setValue("foo");
90         description.setXPathExpression("bar");
91
92         try {
93             description.validate();
94             fail("Expected a validation exception");
95         } catch (CruiseControlException expected) {
96         }
97     }
98
99     public void testXMLFileShouldDefaultToLog() throws CruiseControlException {
100         String JavaDoc bazXPath = "/cruisecontrol/info/property[@name='builddate']/@value";
101
102         XPathAwareChild xpathField = new XPathAwareChild();
103         xpathField.setXPathExpression(bazXPath);
104         Element log = TestUtil.createElement(true, true);
105
106         assertNotNull(log.getDocument());
107
108         xpathField.validate();
109         assertEquals("11/30/2005 12:07:27", xpathField.lookupValue(log));
110     }
111
112     public void testMustValidateFirst() throws CruiseControlException {
113         XPathAwareChild child = new XPathAwareChild();
114         try {
115             child.lookupValue(null);
116             fail("Expected an exception");
117         } catch (IllegalStateException JavaDoc expected) {
118         }
119     }
120
121     public void testXmlFileShouldOnlyBeSetIfXPathExpression() {
122         XPathAwareChild child = new XPathAwareChild();
123         child.setXMLFile("foo");
124         try {
125             child.validate();
126             fail("Should not be able to validate.");
127         } catch (CruiseControlException expected) {
128             assertTrue("wrong exception caught",
129                     expected.getMessage().indexOf("xmlFile should only be set if xpathExpression is also set.") >= 0);
130         }
131     }
132 }
133
Popular Tags