1 16 package scriptella.configuration; 17 18 import org.w3c.dom.Element ; 19 import org.xml.sax.InputSource ; 20 import scriptella.AbstractTestCase; 21 import scriptella.expression.PropertiesSubstitutor; 22 import scriptella.spi.MockParametersCallbacks; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import java.io.StringReader ; 28 import java.net.URL ; 29 30 36 public class XmlElementTest extends AbstractTestCase { 37 private static final DocumentBuilder BUILDER; 38 39 static { 40 try { 41 BUILDER = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 42 } catch (ParserConfigurationException e) { 43 throw new IllegalStateException (e.getMessage(), e); 44 } 45 } 46 47 public void testGetXPath() { 48 String xml = "<etl>\n" + 49 " <query connection-id=\"db1\">\n" + 50 " query1" + 51 " <script connection-id=\"db2\">\n" + 52 " SCRIPT1" + 53 " </script>\n" + 54 " </query>\n" + 55 " <query connection-id=\"db3\">\n" + 56 " query2" + 57 " <script connection-id=\"db4\">\n" + 58 " script2" + 59 " </script>\n" + 60 " </query>\n" + 61 "\n" + 62 "</etl>"; 63 XmlElement root = asElement(xml); 64 XmlElement el = root.getChildren("query").get(1).getChild("script"); 66 assertEquals("/etl/query[2]/script[1]", el.getXPath()); 67 68 } 69 70 static XmlElement asElement(String xml) { 71 try { 72 Element el = BUILDER.parse(new InputSource (new StringReader (xml))).getDocumentElement(); 73 return new XmlElement(el, new URL ("file:/test"), new PropertiesSubstitutor(MockParametersCallbacks.NULL)); 74 } catch (Exception e) { 75 throw new IllegalStateException ("Unable to create XML element", e); 76 } 77 } 78 79 } 80 | Popular Tags |