1 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 ; 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 xmlDocument = "<foo><bar>baz</bar></foo>"; 65 String bazXPath = "/foo/bar/text()"; 66 67 XPathAwareChild xpathField = new XPathAwareChild(); 68 xpathField.setXPathExpression(bazXPath); 69 xpathField.setInputStream(new ByteArrayInputStream (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 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 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 |