[jboss-svn-commits] JBL Code SVN: r6781 - in labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta: src/org/jboss/soa/esb/helpers tests/src/org/jboss/soa/esb/helpers
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Oct 12 23:42:24 EDT 2006
Author: estebanschifman
Date: 2006-10-12 23:42:18 -0400 (Thu, 12 Oct 2006)
New Revision: 6781
Added:
labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/helpers/ConfigTree.java
labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/helpers/ConfigTreeUnitTest.java
Log:
ConfigTree class to replace DomElement
Added: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/helpers/ConfigTree.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/helpers/ConfigTree.java 2006-10-13 01:39:15 UTC (rev 6780)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/helpers/ConfigTree.java 2006-10-13 03:42:18 UTC (rev 6781)
@@ -0,0 +1,457 @@
+package org.jboss.soa.esb.helpers;
+
+import java.util.*;
+import java.io.*;
+import org.apache.log4j.Logger;
+
+import javax.xml.parsers.*;
+import javax.xml.transform.*;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.*;
+import org.xml.sax.SAXException;
+/**
+ * Standard object intended to be used for run time configuration of ESB components
+ * <p/> This class should gradually replace the DomElement class
+ * <br/> It is a subset of Tree that accepts a Map of attributes, and a List of children
+ * <br/> children can only be String values, or objects of this class
+ * <p/> fromXml() and toXml() methods allow 'visible' representations of objects of this class
+ * and run time loading/dumping from/to standard XML documents
+ *
+ * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
+ *
+ */
+public class ConfigTree implements Serializable, Cloneable
+{
+ private static final long serialVersionUID = 1L;
+/**
+ * Constructor of a root node
+ * @param name String - the element name of 'this'
+ */
+ public ConfigTree(String name) { this(name,null); }
+/**
+ * Constructor of a ConfigTree as a child of another (second arg)
+ * @param name String - element name of 'this'
+ * @param dad ConfigTree - whom 'this' will be hanging from
+ */
+ public ConfigTree(String name, ConfigTree dad)
+ {
+ setName(name);
+ setParent(dad);
+ } // _______________________________
+
+ /**
+ *
+ * @return String - the name of this object
+ */
+ public String getName() { return _name; }
+ /**
+ * assign a name to 'this'
+ * @param name String - The name for this object
+ */
+ public void setName(String name)
+ {
+ if (null==name)
+ throw new IllegalArgumentException();
+ _name = name;
+ } // _______________________________
+
+ /**
+ * whose child is 'this' (null if root)
+ * @return ConfigTree - the parent tree of 'this'
+ */
+ public ConfigTree getParent() { return _dad; }
+ private void setParent(ConfigTree dad)
+ {
+ if (null!=_dad && null!=_dad._childs)
+ _dad._childs.remove(this);
+ _dad = dad;
+ if (null!=_dad)
+ _dad.addChild(this);
+ }
+ /**
+ * assign a value to a name
+ * @param name String - the name (key) for the new attribute
+ * @param value String - the value assigned to the key (if null - old value will be deleted)
+ * @return String - old value assigned to the name (null if there was none)
+ */
+ public String setAttribute(String name,String value)
+ {
+ if (null==name)
+ throw new IllegalArgumentException("Attribute name must be non null");
+ if (null==_attributes)
+ _attributes = new HashMap<String,String>();
+ String oldVal = _attributes.remove(name);
+ if(null!=value)
+ _attributes.put(name,value);
+ return oldVal;
+ } // _______________________________
+ /**
+ *
+ * @return int - the number of non null attributes that this node has been assigned
+ */
+ public int attributeCount()
+ {
+ return (null==_attributes) ? 0 : _attributes.size();
+ } // _______________________________
+ /**
+ * retrieve the value assigned to an attribute key
+ * @param name String - the search key
+ * @return String - the value assigned to the specified key
+ */
+ public String getAttribute(String name)
+ {
+ return (null==_attributes) ? null : _attributes.get(name);
+ } // _______________________________
+ /**
+ * obtain the list of attribute names
+ * @return Set<String> - the set of keys that have been assigned a non null value
+ */
+ public Set<String>getAttributeNames()
+ { return (null==_attributes)
+ ? new HashSet<String>()
+ : _attributes.keySet();
+ } // _______________________________
+ /**
+ * concatenated values of all child String values that have been added to 'this'
+ * <br/>"" (zero length String) if no String child nodes
+ * @return String - concatenation of all String segments (equivalent to xml text nodes)
+ */
+ public String getAllText()
+ {
+ if (null==_childs)
+ return "";
+ StringBuilder sb = null;
+ for (Child child : _childs)
+ { if (! (child._obj instanceof String))
+ continue;
+ if (null==sb)
+ sb = new StringBuilder((String)child._obj);
+ else
+ sb.append((String)child._obj);
+ }
+ return sb.toString();
+
+ } // _______________________________
+/**
+ * Obtain all String values with the same name
+ * @param name String - filter for child String nodes
+ * @return String[]
+ */
+ public String[] getTextChildren(String name)
+ {
+ if (null==name)
+ throw new IllegalArgumentException();
+ if (null==_childs)
+ return new String[0];
+ List<String>oRet = new ArrayList<String>();
+ for (Child oCurr : _childs)
+ {
+ ConfigTree tree = oCurr.getTree();
+ if (null != tree && tree.isPureText() && name.equals(tree.getName()))
+ oRet.add(tree.getAllText());
+ }
+ return oRet.toArray(new String[oRet.size()]);
+ } // _______________________________
+/**
+ * add a child element that consists only of the text in arg0
+ * @param value String - the text to assign to the added child node
+ */
+ public void addTextChild (String value) { new Child(value); }
+ private void addChild(ConfigTree child) { new Child(child); }
+/**
+ * retrieve list of child elements of 'this' that are instances of ConfigTree
+ * @return ConfigTree[] - Array containing all child elements of class ConfigTree
+ */
+ public ConfigTree[] getAllChildren()
+ {
+ if (null==_childs)
+ return new ConfigTree[]{};
+ List<ConfigTree>oRet = new ArrayList<ConfigTree>();
+ for (Child oCurr : _childs)
+ if (null != oCurr.getTree())
+ oRet.add(oCurr.getTree());
+ return oRet.toArray(new ConfigTree[oRet.size()]);
+ } // _______________________________
+
+ /**
+ * list of child elements of 'this' that are instances of ConfigTree, with name = arg0
+ * @param name String - the name of child nodes to filter
+ * @return ConfigTree[] - child elements of class ConfigTree with name provided
+ */
+ public ConfigTree[] getChildren(String name)
+ {
+ if (null==name)
+ throw new IllegalArgumentException();
+ if (null==_childs)
+ return new ConfigTree[0];
+ List<ConfigTree>oRet = new ArrayList<ConfigTree>();
+ for (Child oCurr : _childs)
+ if (name.equals(oCurr.getName()))
+ oRet.add(oCurr.getTree());
+ return oRet.toArray(new ConfigTree[oRet.size()]);
+ } // _______________________________
+/**
+ * <b>first</b> child of class ConfigTree with name=arg0
+ * @param name String - the name to filter
+ * @return first child element under that name - <null> if none
+ */
+ public ConfigTree getFirstChild(String name)
+ {
+ if (null==name)
+ throw new IllegalArgumentException();
+ if (null!=_childs)
+ for (Child oCurr : _childs)
+ if (name.equals(oCurr.getName()))
+ return oCurr.getTree();
+ return null;
+ } // _______________________________
+/**
+ * purge the list of children
+ */
+ public void removeAllChildren()
+ {
+ _childs = null;
+ } // _______________________________
+
+ /**
+ * remove by name
+ * @param name String - only children by that name will be removed
+ */
+ public void removeChildrenByName(String name)
+ {
+ if (null==name)
+ throw new IllegalArgumentException();
+ if (null!=_childs)
+ for (ListIterator<Child> II=_childs.listIterator(); II.hasNext();)
+ if (name.equals(II.next().getName()))
+ II.remove();
+ } // __________________________________
+
+ public int childCount()
+ {
+ return (null==_childs) ? 0 : _childs.size();
+ } // __________________________________
+
+ @Override
+ public Object clone() { return cloneObj(); }
+
+ /**
+ * @return ConfigTree - Deep copy of 'this'
+ */
+ public ConfigTree cloneObj() {return cloneSubtree(null); }
+
+ /**
+ * @return ConfigTree - Deep copy of 'this'
+ */
+ private ConfigTree cloneSubtree(ConfigTree dad)
+ {
+ ConfigTree oRet = new ConfigTree(_name, dad);
+ if (null!=_attributes)
+ for (Map.Entry<String,String> oAtt: _attributes.entrySet())
+ oRet.setAttribute(oAtt.getKey(),oAtt.getValue());
+ if (null!= _childs)
+ for (Child oChild : _childs)
+ {
+ ConfigTree tree = oChild.getTree();
+ if (null!=tree)
+ tree.cloneSubtree(oRet);
+ else
+ oRet.addTextChild(oChild._obj.toString());
+ }
+ return oRet;
+ } // __________________________________
+/**
+ * obtain an instance of this class, from a 'normalized' xml format, with the default encoding
+ * <p/> the 'normalized' xml format is the output of the instance method toXml()
+ * @param xml String - what to parse
+ * @return ConfigTree - an object of this class
+ * @throws SAXException - if xml format is invalid
+ */
+ public static ConfigTree fromXml(String xml) throws SAXException
+ {
+ try { return fromXml(xml,java.nio.charset.Charset.defaultCharset().toString()); }
+ catch (UnsupportedEncodingException e)
+ {
+ // This can't happen
+ _logger.fatal("This should not happen",e);
+ return null;
+ }
+ } // __________________________________
+
+ /**
+ * obtain an instance of this class, from a 'normalized' xml format, with the encoding defined in arg1
+ * <p/> the 'normalized' xml format is the output of the instance method toXml()
+ * @param xml String - what to parse
+ * @param encoding String - The encoding of arg 0
+ * @return ConfigTree - an object of this class
+ * @throws SAXException - if xml format is invalid
+ */
+ public static ConfigTree fromXml(String xml, String encoding)
+ throws UnsupportedEncodingException, SAXException
+ {
+ if (null==xml)
+ throw new IllegalArgumentException("Xml source String is null");
+ try { return fromInputStream(new ByteArrayInputStream(xml.getBytes(encoding))); }
+ catch (IOException e)
+ {
+ _logger.fatal("This should NOT happen",e);
+ return null;
+ }
+ } // __________________________________
+
+ /**
+ * obtain an instance of this class, from a 'normalized' xml format, from an input stream
+ * <p/> the 'normalized' xml format is the output of the instance method toXml()
+ * @param input InputStream - where to parse from
+ * @return ConfigTree - an object of this class
+ * @throws SAXException - if xml format is invalid
+ * @throws IOException - if an input/output error occurs
+ */
+ public static ConfigTree fromInputStream(InputStream input)
+ throws SAXException, IOException
+ {
+ if (null==input)
+ throw new IllegalArgumentException();
+ DocumentBuilder builder = null;
+ try { builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); }
+ catch (ParserConfigurationException e2)
+ { _logger.error("Problems with default Parser Configuration",e2);
+ return null;
+ }
+ Document oDoc = builder.parse(input);
+ oDoc.normalize();
+ return fromElement(oDoc.getDocumentElement(),null);
+ } // __________________________________
+/**
+ * Obtain a ConfigTree from a org.w3c.dom.Element
+ * @param elem Element - where to obtain the data from
+ * @param dad - where should the resulting object be added as a child
+ * @return a valid ConfigTree
+ */
+ public static ConfigTree fromElement(Element elem, ConfigTree dad)
+ {
+ ConfigTree oRet = new ConfigTree(elem.getNodeName(),dad);
+ for (Node nCurr = elem.getFirstChild();null!=nCurr;nCurr = nCurr.getNextSibling())
+ switch (nCurr.getNodeType())
+ {
+ case Node.ELEMENT_NODE:
+ fromElement((Element)nCurr,oRet);
+ break;
+ case Node.TEXT_NODE:
+ oRet.addTextChild(nCurr.getNodeValue());
+ break;
+ }
+ NamedNodeMap named = elem.getAttributes();
+ for (int i1=0; i1<named.getLength(); i1++)
+ {
+ Node nCurr = named.item(i1);
+ oRet.setAttribute(nCurr.getNodeName(), nCurr.getNodeValue());
+ }
+ return oRet;
+ } // __________________________________
+
+ private Element toElement(Document doc)
+ {
+ Element elem = doc.createElement(_name);
+ if (null!=_attributes)
+ for (Map.Entry<String,String> oAtt : _attributes.entrySet())
+ elem.setAttribute(oAtt.getKey(),oAtt.getValue());
+ if (null!=_childs)
+ for (Child child : _childs)
+ {
+ ConfigTree tree = child.getTree();
+ if (null!=tree)
+ elem.appendChild(tree.toElement(doc));
+ else
+ elem.appendChild(doc.createTextNode(child._obj.toString()));
+ }
+ return elem;
+ } // __________________________________
+ /**
+ * @return String - a String with the 'standard' xml representation of 'this',
+ * using the default encoding
+ */
+ public String toString() { return toXml(); }
+ /**
+ * @return String - a String with the 'standard' xml representation of 'this',
+ * using the default encoding
+ */
+ public String toXml()
+ {
+ return toXml(java.nio.charset.Charset.defaultCharset().toString());
+ } // __________________________________
+
+ /**
+ * @param encoding String - String
+ * @return String - a String with the 'standard' xml representation of 'this',
+ * using encoding specified in arg0
+ */
+ public String toXml(String encoding)
+ {
+ Transformer transf = null;
+ try { transf = TransformerFactory.newInstance().newTransformer(); }
+ catch (TransformerConfigurationException e1)
+ { _logger.error("Cannot obtain transformer to render XML output",e1);
+ return null;
+ }
+ transf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
+ transf.setOutputProperty(OutputKeys.INDENT, "no");
+ transf.setOutputProperty(OutputKeys.ENCODING, encoding);
+
+ DocumentBuilder builder = null;
+ try { builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); }
+ catch (ParserConfigurationException e2)
+ { _logger.error("Problems with default Parser Configuration",e2);
+ return null;
+ }
+ Document oDoc = builder.newDocument();
+ oDoc.appendChild(toElement(oDoc));
+ oDoc.normalize();
+ DOMSource src = new DOMSource(oDoc);
+
+ ByteArrayOutputStream oStrm = new ByteArrayOutputStream(5000);
+ StreamResult res = new StreamResult(oStrm);
+
+ try { transf.transform(src, res); }
+ catch (TransformerException e3)
+ { _logger.error("Problems with XML transformation",e3);
+ return null;
+ }
+
+ return oStrm.toString();
+ } // __________________________________
+ /**
+ *
+ * @return boolean - indicating if 'this' element has ONLY text children (and no ConfigTree children)
+ */
+ public boolean isPureText() { return _pureText; }
+
+ private class Child
+ {
+ Object _obj;
+ ConfigTree getTree()
+ { return (_obj instanceof ConfigTree)? (ConfigTree) _obj : null;
+ }
+ String getName()
+ { return (_obj instanceof ConfigTree)? ((ConfigTree) _obj)._name : null;
+ }
+ private Child(ConfigTree obj) {addToDad(obj); _pureText = false;}
+ private Child(String obj) {addToDad(obj); }
+ private void addToDad(Object obj)
+ { if (null==_childs)
+ _childs = new ArrayList<Child>();
+ _obj = obj;
+ _childs.add(this);
+ }
+
+ }
+
+ private boolean _pureText=true;
+ private ConfigTree _dad;
+ private String _name;
+ private Map<String,String> _attributes;
+ private List<Child> _childs;
+ private static Logger _logger = Logger.getLogger(ConfigTree.class);
+} //____________________________________________________________________________
Added: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/helpers/ConfigTreeUnitTest.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/helpers/ConfigTreeUnitTest.java 2006-10-13 01:39:15 UTC (rev 6780)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/helpers/ConfigTreeUnitTest.java 2006-10-13 03:42:18 UTC (rev 6781)
@@ -0,0 +1,260 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.helpers;
+
+import java.io.*;
+import java.util.*;
+
+import org.jboss.internal.soa.esb.util.StreamUtils;
+import org.jboss.soa.esb.StringUtils;
+import org.jboss.soa.esb.common.tests.BaseTest;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+/**
+ * Unit tests for the DomElement class.
+ * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
+ */
+public class ConfigTreeUnitTest extends BaseTest {
+
+ public void test_fromInputStream_args() throws SAXException, IOException {
+ try {
+ ConfigTree.fromInputStream(null);
+ fail("Expected IllegalArgumentException on null stream.");
+ } catch(IllegalArgumentException e) {
+ // Expected
+ }
+ }
+
+ private InputStream getStream(String sName)
+ {
+ InputStream oRet =getClass().getResourceAsStream(sName);
+ if (null==oRet)
+ try { oRet = new FileInputStream(sName); }
+ catch(IOException e) { /* OK Just fall through */}
+ return oRet;
+ }
+
+ public void test_fromInputStream_and_toXML() throws SAXException, IOException
+ {
+ ConfigTree confTree = ConfigTree.fromInputStream(getStream("testfile1.xml"));
+ byte[] expected = StreamUtils.readStream(getStream("expected_01.xml"));
+
+ // Create the ConfigTree from a Stream. Then...
+ // Dump the XML to a buffer and compare it with the expected - checking that the
+ // Created ConfigTree DOM is the same as what was supplied in the stream. Also
+ // tests the toXML method. If this fails, either fromInputStream is not working correctly
+ // or the toXML method is not serialising correctly.
+ String output = null;
+ try { output = confTree.toXml(); }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ assertTrue("ConfigTree creation from a stream failed, or, ConfigTree toXML failed - failed to produce the same XML.",
+ StringUtils.equalsIgnoreLinebreaks(new String(expected), output, false));
+ }
+
+ public void test_fromXML_args() throws SAXException, IOException
+ {
+ try {
+ ConfigTree.fromXml(null);
+ fail("Expected IllegalArgumentException on null String.");
+ } catch(IllegalArgumentException e) {
+ // Expected
+ }
+ try {
+ ConfigTree.fromXml(" ");
+ fail("Expected SAXParseException on null String.");
+ } catch(SAXParseException e) {
+ // Expected
+ }
+ }
+
+ public void test_fromXML_and_toXML() throws SAXException, IOException
+ {
+ byte[] source = StreamUtils.readStream(getStream("expected_01.xml"));
+ ConfigTree confTree = ConfigTree.fromXml(new String(source));
+ byte[] expected = source;
+
+ // Create the ConfigTree from a String. Then...
+ // Dump the XML to a buffer and compare it with the expected - checking that the
+ // Created ConfigTree DOM is the same as what was supplied in the stream. Also
+ // tests the toXML method. If this fails, either fromInputStream is not working correctly
+ // or the toXML method is not serialising correctly.
+ String output = confTree.toXml();
+ assertTrue("ConfigTree creation from a String failed, or, ConfigTree toXML failed - failed to produce the same XML.",
+ StringUtils.equalsIgnoreLinebreaks(new String(expected), output, false));
+ }
+
+ public void test_Constructor_Clone() throws SAXException, IOException {
+ ConfigTree conf1 = ConfigTree.fromInputStream(getStream("testfile1.xml"));
+// ByteArrayOutputStream output;
+ byte[] expected = StreamUtils.readStream(getStream("expected_01.xml"));
+ // Create a new ConfigTree from an existing one and compare it with the expected.
+ ConfigTree conf2 = conf1.cloneObj();
+ assertTrue("ConfigTree creation from a stream failed, or, cloneObj failed - failed to produce the same XML.",
+ StringUtils.equalsIgnoreLinebreaks(new String(expected), conf2.toXml(), false));
+ }
+
+ public void test_Constructor_String_withoutparent() throws SAXException, IOException {
+ // Create a new ConfigTree from without a parent ConfigTree.
+ ConfigTree confTree = new ConfigTree("newConfigTree",null);
+
+ assertEquals("New ConfigTree invalid.", "<newConfigTree/>" , confTree.toXml());
+ }
+
+ public void test_getName() {
+ ConfigTree confTree = new ConfigTree("newConfigTree",null);
+ assertEquals("getName returned invalid data.", "newConfigTree", confTree.getName());
+ }
+
+ public void test_getAttrKeys_hasnokeys() throws SAXException, IOException {
+ ConfigTree confTree = new ConfigTree("newConfigTree",null);
+ assertTrue("invalid attribute name list", confTree.attributeCount()== 0);
+ }
+
+ public void test_getAttrKeys_haskeys() throws SAXException, IOException {
+ ConfigTree confTree = ConfigTree.fromInputStream(getStream("testfile2.xml"));
+ String[] expected = new String[] {"attrib1", "attrib2", "attrib3"};
+ Arrays.sort(expected);
+ String[] actual = confTree.getAttributeNames().toArray(new String[confTree.attributeCount()]);
+ Arrays.sort(actual);
+
+ assertTrue("invalid attribute name list", Arrays.equals(expected, actual));
+ }
+
+ public void test_getAttr() throws SAXException, IOException {
+ ConfigTree confTree = ConfigTree.fromInputStream(getStream("testfile2.xml"));
+
+ assertEquals("invalid attribute value", "value2", confTree.getAttribute("attrib2"));
+ assertEquals("invalid attribute value", null, confTree.getAttribute("attribXX"));
+ }
+
+ public void test_getTextChildren() throws SAXException, IOException {
+ // This test also tests the static method getTextValue.
+ ConfigTree confTree = ConfigTree.fromInputStream(getStream("testfile3.xml"));
+
+ // REVIEW: These tests capture the current behavior of the getTextChildren method. I'm not convinced this behavior is correct!!
+ // I think perhaps this method should not be depending on getTextValue because getTextValue
+ // concatentates the child text nodes.
+ assertTrue("invalid Text Children list",
+ "Some root text... and some more root text...".equals(confTree.getAllText()));
+ assertTrue("invalid Text Children list",
+ Arrays.equals(new String[] {"", "Some nested text..."}, confTree.getTextChildren("el")));
+ }
+
+ public void test_getElementChildren() throws SAXException, IOException {
+ ConfigTree confTree = ConfigTree.fromInputStream(getStream("testfile3.xml"));
+ ConfigTree[] confTrees;
+
+ String expected = new String(StreamUtils.readStream(getStream("expected_03.xml"))).trim();
+ assertEquals("Wrong ConfigTree ElementChild value.", expected, confTree.toString().trim());
+
+ // Test the nested <el> elements - of which there are 2...
+ confTrees = confTree.getChildren("el");
+ assertEquals("Wrong number of ConfigTree ElementChildren.", 2, confTrees.length);
+ assertEquals("Wrong ConfigTree ElementChild value.", "<el index=\"1\"/>", confTrees[0].toString().trim());
+ assertEquals("Wrong ConfigTree ElementChild value.", "<el index=\"2\">Some nested text...</el>", confTrees[1].toString().trim());
+
+ // Test for a non-existant element...
+ confTrees = confTree.getChildren("xxxx");
+ assertEquals("Wrong number of ConfigTree ElementChildren.", 0, confTrees.length);
+ }
+
+ public void test_getFirstElementChild() throws SAXException, IOException {
+ ConfigTree confTree = ConfigTree.fromInputStream(getStream("testfile3.xml"));
+
+ String expected = new String(StreamUtils.readStream(getStream("expected_03.xml"))).trim();
+ assertEquals("Wrong ConfigTree ElementChild value.", expected, confTree.toString().trim());
+
+ // Test the nested <el> elements - of which there are 2...
+ confTree = confTree.getFirstChild("el");
+ assertEquals("Wrong ConfigTree ElementChild value.", "<el index=\"1\"/>", confTree.toString().trim());
+
+ // Test for a non-existant element...
+ confTree = confTree.getFirstChild("xxxx");
+ assertEquals("Expected null ConfigTree ElementChildren for non-existant element.", null, confTree);
+ }
+
+ public void test_getAllElemChildren() throws SAXException, IOException {
+ ConfigTree confTree = ConfigTree.fromInputStream(getStream("testfile3.xml"));
+ ConfigTree[] confTrees;
+
+ confTrees = confTree.getAllChildren();
+ assertEquals("Wrong number of ConfigTree ElementChildren.", 2, confTrees.length);
+ assertEquals("Wrong ConfigTree ElementChild value.", "<el index=\"1\"/>", confTrees[0].toString().trim());
+ assertEquals("Wrong ConfigTree ElementChild value.", "<el index=\"2\">Some nested text...</el>", confTrees[1].toString().trim());
+
+ // REVIEW: Note, this method only returns ConfigTree instances for the child elements
+ // of the document root node. Comparing this to the behaviour of getFirstElementChild
+ // would seem to demonstrate an inconsitency??? To be consistent, I would expect this method
+ // to return a list of 3 ConfigTree instances - the <root> and 2 <el> elements.
+ }
+
+ public void test_setAttr() throws SAXException, IOException {
+ ConfigTree confTree = new ConfigTree("newConfigTree");
+
+ confTree.setAttribute("attrib1", "value1");
+ assertEquals("value1", confTree.getAttribute("attrib1"));
+
+ // A null value removes the attribute.
+ confTree.setAttribute("attrib1", null);
+ assertEquals(null, confTree.getAttribute("attrib1"));
+
+ try { confTree.setAttribute(null, null); }
+ catch (IllegalArgumentException e) {/* OK we expect this */}
+ catch (Exception e2) {fail(e2.getMessage());}
+ }
+
+ public void test_addElemChild() throws SAXException, IOException {
+ ConfigTree ConfigTree1 = new ConfigTree("newConfigTree");
+ ConfigTree ConfigTreeX = new ConfigTree("X",ConfigTree1);
+ new ConfigTree("Y1",ConfigTree1);
+ new ConfigTree("Y2",ConfigTreeX);
+
+ assertEquals("<newConfigTree><X><Y2/></X><Y1/></newConfigTree>", ConfigTree1.toString().trim());
+ }
+
+ public void test_rmvChildsByName() throws SAXException, IOException {
+ ConfigTree ConfigTree1 = new ConfigTree("newConfigTree");
+ ConfigTree ConfigTreeX = new ConfigTree("X",ConfigTree1);
+ new ConfigTree("Y1",ConfigTree1);
+ new ConfigTree("Y2",ConfigTreeX);
+
+ assertEquals("<newConfigTree><X><Y2/></X><Y1/></newConfigTree>", ConfigTree1.toString().trim());
+ ConfigTree1.removeChildrenByName("Y1");
+ assertEquals("<newConfigTree><X><Y2/></X></newConfigTree>", ConfigTree1.toString().trim());
+ }
+
+ public void test_cloneObj() throws SAXException, IOException {
+ ConfigTree ConfigTree1 = new ConfigTree("newConfigTree");
+ new ConfigTree("X",ConfigTree1);
+ new ConfigTree("Y",ConfigTree1);
+
+ ConfigTree clone = ConfigTree1.cloneObj();
+
+ assertTrue("Clone should produce a completely different object", (ConfigTree1 != clone));
+ assertEquals("Clone should produce the same XML", ConfigTree1.toString(), clone.toString());
+ }
+}
More information about the jboss-svn-commits
mailing list