Author: maksimkaszynski
Date: 2008-04-11 11:16:12 -0400 (Fri, 11 Apr 2008)
New Revision: 7769
Added:
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodyMerge.java
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodySerializer.java
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XPathComparator.java
trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodyMergeTest.java
trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodySerializerTest.java
trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XPathComparatorTest.java
Modified:
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBody.java
trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodyTest.java
Log:
http://jira.jboss.com/jira/browse/RF-2526
Modified: trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBody.java
===================================================================
--- trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBody.java 2008-04-11
13:21:11 UTC (rev 7768)
+++ trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBody.java 2008-04-11
15:16:12 UTC (rev 7769)
@@ -24,19 +24,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
-import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.transform.ErrorListener;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
@@ -44,7 +37,6 @@
import org.ajax4jsf.builder.config.ParsingException;
import org.w3c.dom.Document;
-import org.w3c.dom.DocumentFragment;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -87,14 +79,14 @@
// Create Document Builder Factory
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
-
+ docFactory.setIgnoringElementContentWhitespace(true);
docFactory.setValidating(false);
docFactory.setNamespaceAware(namespaceAware);
// Create Document Builder
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
+ //docBuilder.
+ //docBuilder.isValidating();
- docBuilder.isValidating();
-
// Disable loading of external Entityes
docBuilder.setEntityResolver(new EntityResolver() {
// Dummi resolver - alvays do nothing
@@ -152,66 +144,36 @@
}
private String serializeNodes(NodeList childNodes) throws ParsingException {
- DocumentFragment fragment = xmlDocument.createDocumentFragment();
- for (int i = 0; i < childNodes.getLength(); i++) {
- fragment.appendChild(childNodes.item(i).cloneNode(true));
- }
try {
- TransformerFactory transformerFactory = TransformerFactory
- .newInstance();
- Transformer transformer = transformerFactory.newTransformer();
- transformer.setErrorListener(new ErrorListener(){
-
- public void error(TransformerException exception)
- throws TransformerException {
- // TODO Auto-generated method stub
-
- }
-
- public void fatalError(TransformerException exception)
- throws TransformerException {
- // TODO Auto-generated method stub
-
- }
-
- public void warning(TransformerException exception)
- throws TransformerException {
- // TODO Auto-generated method stub
-
- }
-
- });
- transformer.setOutputProperty("omit-xml-declaration", "yes");
- StringWriter out = new StringWriter();
- StreamResult result = new StreamResult(out);
- transformer.transform(new DOMSource(fragment), result);
- return out.toString();
-
+ return new XMLBodySerializer().serialize(childNodes, xmlDocument);
} catch (Exception e) {
throw new ParsingException(e);
}
}
public String getContent(String xpath) throws ParsingException{
+ return serializeNodes(getByXpath(xpath));
+ }
+
+ public NodeList getByXpath(String xpath) throws ParsingException {
XPath path = XPathFactory.newInstance().newXPath();
NodeList childNodes;
try {
- childNodes = (NodeList) path.evaluate(xpath, xmlDocument,XPathConstants.NODESET);
+ childNodes = (NodeList) path.evaluate(xpath, xmlDocument, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
throw new ParsingException("Error evaluate xpath",e);
}
- return serializeNodes(childNodes);
+ return childNodes;
}
-
- public String getContentUnique(String xpath, String keyXPath, Set<String> keySet)
throws ParsingException{
+
+ public NodeList getByXpathUnique(String xpath, String keyXPath, Set<String>
keySet) throws ParsingException {
if (keyXPath == null) {
- return getContent(xpath);
+ return getByXpath(xpath);
} else {
-
XPath path = XPathFactory.newInstance().newXPath();
NodeList childNodes;
try {
- childNodes = (NodeList) path.evaluate(xpath, xmlDocument,XPathConstants.NODESET);
+ childNodes = getByXpath(xpath);
List<Node> nodeSet = new ArrayList<Node>();
@@ -224,14 +186,16 @@
nodeSet.add(node);
}
}
-
- return serializeNodes(new ArrayNodeList(nodeSet.toArray(new Node[nodeSet.size()])));
-
+ return new ArrayNodeList(nodeSet.toArray(new Node[nodeSet.size()]));
} catch (XPathExpressionException e) {
throw new ParsingException("Error evaluate xpath",e);
}
+
}
}
+ public String getContentUnique(String xpath, String keyXPath, Set<String> keySet)
throws ParsingException{
+ return serializeNodes(getByXpathUnique(xpath, keyXPath, keySet));
+ }
}
class ArrayNodeList implements NodeList {
Copied: trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodyMerge.java (from
rev 7758,
branches/3.1.x/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodyMerge.java)
===================================================================
--- trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodyMerge.java
(rev 0)
+++
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodyMerge.java 2008-04-11
15:16:12 UTC (rev 7769)
@@ -0,0 +1,130 @@
+/**
+ *
+ */
+package org.ajax4jsf.builder.xml;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.ajax4jsf.builder.config.ParsingException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class XMLBodyMerge implements NodeList{
+
+ private String xpath;
+
+ private List<Node> nodes = new ArrayList<Node>();
+
+ private Document document = null;
+
+ private XPathExpression keyXpath = null;
+
+ private StringBuffer content = new StringBuffer();
+
+ private Set<String> keys = new HashSet<String>();
+
+
+
+ public XMLBodyMerge(String xpath) {
+ super();
+ this.xpath = xpath;
+ }
+
+ public XMLBodyMerge(String xpath, String keyXpath) {
+ this(xpath);
+ if (keyXpath != null) {
+ try {
+ XPath newXPath = XPathFactory.newInstance().newXPath();
+ this.keyXpath = newXPath.compile(keyXpath);
+ } catch (XPathExpressionException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void add(Node node) {
+
+ if (keyXpath != null) {
+ String key = getKey(node);
+ if (key == null || keys.contains(key)) {
+ return;
+ }
+ }
+
+ if (document == null) {
+ document = node.getOwnerDocument();
+ } else {
+ node = document.importNode(node, true);
+ }
+ nodes.add(node);
+ }
+
+ public void add(XMLBody xmlBody) throws ParsingException {
+
+ if (xpath != null) {
+ NodeList nodeList = xmlBody.getByXpath(xpath);
+ if (nodeList != null) {
+ for(int i = 0; i < nodeList.getLength(); i++) {
+ add(nodeList.item(i));
+ }
+ }
+ } else {
+ content.append(xmlBody.getContent());
+ }
+
+ }
+
+ public int getLength() {
+ return nodes.size();
+ }
+
+ public Node item(int index) {
+ if (index < nodes.size()) {
+ return nodes.get(index);
+ }
+ return null;
+ }
+
+
+ public void sort(Comparator<Node> comparator) {
+ Collections.sort(nodes, comparator);
+ }
+
+ public String getContent() throws Exception{
+ StringBuilder buf = new StringBuilder();
+ if (content != null) {
+ buf.append(content);
+ }
+ if (document != null) {
+ buf.append(new XMLBodySerializer().serialize(this, document));
+ }
+
+ return buf.toString();
+ }
+
+ private String getKey(Node node) {
+ try {
+ NodeList list = (NodeList) keyXpath.evaluate(node, XPathConstants.NODESET);
+ return new XMLBodySerializer().serialize(list, node.getOwnerDocument());
+ } catch (Exception e) {
+ }
+ return null;
+ }
+}
Copied: trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodySerializer.java
(from rev 7758,
branches/3.1.x/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodySerializer.java)
===================================================================
--- trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodySerializer.java
(rev 0)
+++
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XMLBodySerializer.java 2008-04-11
15:16:12 UTC (rev 7769)
@@ -0,0 +1,60 @@
+/**
+ *
+ */
+package org.ajax4jsf.builder.xml;
+
+import java.io.StringWriter;
+
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.ajax4jsf.builder.config.ParsingException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.NodeList;
+
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class XMLBodySerializer {
+ public String serialize(NodeList childNodes, Document xmlDocument) throws
ParsingException {
+ try {
+ StringWriter out;
+ DocumentFragment fragment = xmlDocument.createDocumentFragment();
+ for (int i = 0; i < childNodes.getLength(); i++) {
+ fragment.appendChild(childNodes.item(i).cloneNode(true));
+ }
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ Transformer transformer = transformerFactory.newTransformer();
+ transformer.setErrorListener(new ErrorListener(){
+
+ public void error(TransformerException exception)
+ throws TransformerException {
+ }
+
+ public void fatalError(TransformerException exception)
+ throws TransformerException {
+ }
+
+ public void warning(TransformerException exception)
+ throws TransformerException {
+ }
+
+ });
+ transformer.setOutputProperty("indent", "yes");
+ transformer.setOutputProperty("omit-xml-declaration", "yes");
+ out = new StringWriter();
+ StreamResult result = new StreamResult(out);
+ transformer.transform(new DOMSource(fragment), result);
+ return out.toString();
+ } catch (Exception e) {
+ throw new ParsingException(e);
+ }
+ }
+}
Copied: trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XPathComparator.java
(from rev 7758,
branches/3.1.x/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XPathComparator.java)
===================================================================
--- trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XPathComparator.java
(rev 0)
+++
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/xml/XPathComparator.java 2008-04-11
15:16:12 UTC (rev 7769)
@@ -0,0 +1,79 @@
+/**
+ *
+ */
+package org.ajax4jsf.builder.xml;
+
+import java.util.Comparator;
+
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Node;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class XPathComparator implements Comparator<Node> {
+
+ class XPathCompatorCriterion {
+ private XPathExpression expression = null;
+
+ public XPathCompatorCriterion(String xPath){
+ try {
+ expression = XPathFactory.newInstance().newXPath().compile(xPath);
+ } catch (XPathExpressionException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public String getValue(Object node) throws XPathExpressionException {
+ return expression == null ? null : expression.evaluate(node);
+ }
+ }
+
+ private XPathCompatorCriterion [] criteria;
+
+ public XPathComparator(String ... criteria) {
+ this.criteria = new XPathCompatorCriterion[criteria.length];
+ for(int i = 0; i < criteria.length; i++) {
+ this.criteria[i] = new XPathCompatorCriterion(criteria[i]);
+ }
+ }
+
+ public int compare(Node o1, Node o2) {
+ int result = 0;
+
+ for(int i = 0; i < criteria.length && result == 0; i++) {
+ String s1 = null;
+ String s2 = null;
+ try {
+ s1 = this.criteria[i].getValue(o1);
+ } catch (XPathExpressionException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ try {
+ s2 = this.criteria[i].getValue(o2);
+ } catch (XPathExpressionException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ if (s1 != null) {
+ if (s2 != null) {
+ result = s1.compareTo(s2);
+ } else {
+ result = 1;
+ }
+ } else if (s2 != null) {
+ result = -1;
+ }
+ }
+
+ return result;
+ }
+
+}
Copied: trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodyMergeTest.java
(from rev 7758,
branches/3.1.x/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodyMergeTest.java)
===================================================================
--- trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodyMergeTest.java
(rev 0)
+++
trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodyMergeTest.java 2008-04-11
15:16:12 UTC (rev 7769)
@@ -0,0 +1,64 @@
+/**
+ *
+ */
+package org.ajax4jsf.builder.xml;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class XMLBodyMergeTest extends TestCase {
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Test method for {@link org.ajax4jsf.builder.xml.XMLBodyMerge#add(org.w3c.dom.Node)}.
+ */
+ public void testAddNode() {
+ XMLBodyMerge merge = new XMLBodyMerge("//node()");
+
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.builder.xml.XMLBodyMerge#add(org.ajax4jsf.builder.xml.XMLBody)}.
+ */
+ public void testAddXMLBody() {
+ //fail("Not yet implemented");
+ }
+
+ /**
+ * Test method for {@link org.ajax4jsf.builder.xml.XMLBodyMerge#getLength()}.
+ */
+ public void testGetLength() {
+ //fail("Not yet implemented");
+ }
+
+ /**
+ * Test method for {@link org.ajax4jsf.builder.xml.XMLBodyMerge#item(int)}.
+ */
+ public void testItem() {
+ //fail("Not yet implemented");
+ }
+
+ /**
+ * Test method for {@link org.ajax4jsf.builder.xml.XMLBodyMerge#getContent()}.
+ */
+ public void testGetContent() {
+ //fail("Not yet implemented");
+ }
+
+}
Copied:
trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodySerializerTest.java
(from rev 7758,
branches/3.1.x/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodySerializerTest.java)
===================================================================
--- trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodySerializerTest.java
(rev 0)
+++
trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodySerializerTest.java 2008-04-11
15:16:12 UTC (rev 7769)
@@ -0,0 +1,73 @@
+/**
+ *
+ */
+package org.ajax4jsf.builder.xml;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.ajax4jsf.builder.config.ParsingException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class XMLBodySerializerTest extends TestCase {
+
+ private XMLBodySerializer serializer;
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ serializer = new XMLBodySerializer();
+ }
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ serializer = null;
+ super.tearDown();
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.builder.xml.XMLBodySerializer#serialize(org.w3c.dom.NodeList,
org.w3c.dom.Document)}.
+ */
+ public void testSerialize() throws ParsingException{
+ String xml = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n"
+ + "<!DOCTYPE faces-config PUBLIC \"-//Sun Microsystems, Inc.//DTD
JavaServer Faces Config 1.1//EN\"\n"
+ + "
\"http://java.sun.com/dtd/web-facesconfig_1_1.dtd\">\n"
+ + "<faces-config>\n" +
" <component><test>blabla</test></component><component><test>blabla</test></component><component><test>blabla2</test></component>\n"
+ + "</faces-config>";
+ InputStream in = new ByteArrayInputStream(xml.getBytes());
+ XMLBody body = new XMLBody();
+ body.loadXML(in);
+
+ NodeList singleElementList = body.getByXpath("/faces-config");
+ assertEquals(1, singleElementList.getLength());
+ Node node = singleElementList.item(0);
+ assertNotNull(node);
+ assertEquals("faces-config", node.getNodeName());
+ String actual = serializer.serialize(singleElementList,
node.getOwnerDocument()).replaceAll("\\s", "");
+ String expected =
"<faces-config><component><test>blabla</test></component><component><test>blabla</test></component><component><test>blabla2</test></component></faces-config>";
+ assertEquals(expected, actual);
+
+ NodeList children = node.getChildNodes();
+ actual = serializer.serialize(children,
node.getOwnerDocument()).replaceAll("\\s", "");
+ expected =
"<component><test>blabla</test></component><component><test>blabla</test></component><component><test>blabla2</test></component>";
+ assertEquals(expected, actual);
+
+
+ }
+
+}
Modified: trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodyTest.java
===================================================================
--- trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodyTest.java 2008-04-11
13:21:11 UTC (rev 7768)
+++ trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XMLBodyTest.java 2008-04-11
15:16:12 UTC (rev 7769)
@@ -25,12 +25,10 @@
import java.io.InputStream;
import java.util.HashSet;
-import javax.xml.xpath.XPathExpressionException;
+import junit.framework.TestCase;
import org.ajax4jsf.builder.config.ParsingException;
-import junit.framework.TestCase;
-
/**
* @author shura
*
@@ -145,7 +143,7 @@
InputStream in = new ByteArrayInputStream(xml.getBytes());
XMLBody body = new XMLBody();
body.loadXML(in);
- assertEquals("<modelVersion>4.0.0</modelVersion>",
body.getContent());
+ assertEquals("<modelVersion>4.0.0</modelVersion>",
body.getContent().trim());
}
public void testGetContentXpath() throws ParsingException {
@@ -160,7 +158,7 @@
try {
assertEquals(
"<component>blabla</component>",
- body.getContent("/faces-config/component"));
+ body.getContent("/faces-config/component").trim());
} catch (ParsingException e) {
e.printStackTrace();
assertTrue(e.getMessage(),false);
@@ -177,9 +175,12 @@
XMLBody body = new XMLBody();
body.loadXML(in);
try {
+ String expected =
"<component><test>blabla</test></component><component><test>blabla2</test></component>";
+ String actual = body.getContentUnique("/faces-config/component",
"test/text()", new HashSet<String>()).replaceAll("\\s",
"");
+
assertEquals(
- "<component><test>blabla</test></component><component><test>blabla2</test></component>",
- body.getContentUnique("/faces-config/component", "test/text()",
new HashSet<String>()));
+ expected,
+ actual);
} catch (ParsingException e) {
e.printStackTrace();
assertTrue(e.getMessage(),false);
@@ -197,7 +198,7 @@
try {
assertEquals(
"<f:component xmlns:f=\"http://foo.baz\"><test
f:foo=\"xxx\"
xmlns=\"http://foo.bar\">blabla</test></f:component>",
- body.getContent());
+ body.getContent().replace("\r", "\n").replace("\n",
""));
} catch (ParsingException e) {
e.printStackTrace();
assertTrue(e.getMessage(),false);
Copied:
trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XPathComparatorTest.java (from
rev 7758,
branches/3.1.x/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XPathComparatorTest.java)
===================================================================
--- trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XPathComparatorTest.java
(rev 0)
+++
trunk/cdk/generator/src/test/java/org/ajax4jsf/builder/xml/XPathComparatorTest.java 2008-04-11
15:16:12 UTC (rev 7769)
@@ -0,0 +1,75 @@
+/**
+ *
+ */
+package org.ajax4jsf.builder.xml;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class XPathComparatorTest extends TestCase {
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testCompare() throws Exception{
+ String xml = "<faces-config>" +
+ "<component><test>blabla1</test></component>" +
+ "<component><test>blabla</test></component>" +
+ "<renderer><foo>blabla2</foo></renderer>" +
+ "<component><test>blabla2</test></component>"
+ + "</faces-config>";
+ InputStream in = new ByteArrayInputStream(xml.getBytes());
+ XMLBody body = new XMLBody();
+ body.loadXML(in);
+ NodeList list = body.getByXpath("//component|//renderer");
+ assertEquals(4, list.getLength());
+ Node node0 = list.item(0);
+ Node node1 = list.item(1);
+ Node node2 = list.item(2);
+ Node node3 = list.item(3);
+
+ XPathComparator dummyComparator = new XPathComparator();
+ assertEquals(0, dummyComparator.compare(node0, node1));
+ assertEquals(0, dummyComparator.compare(node1, node2));
+ assertEquals(0, dummyComparator.compare(node0, node2));
+ assertEquals(0, dummyComparator.compare(node0, node3));
+ assertEquals(0, dummyComparator.compare(node2, node3));
+ assertEquals(0, dummyComparator.compare(node1, node0));
+ assertEquals(0, dummyComparator.compare(node2, node1));
+ assertEquals(0, dummyComparator.compare(node2, node0));
+
+ XPathComparator simpleComparator = new XPathComparator("local-name()");
+ assertEquals(0, simpleComparator.compare(node0, node1));
+ assertEquals(0, simpleComparator.compare(node0, node3));
+ assertTrue(simpleComparator.compare(node0, node2) < 0);
+ assertTrue(simpleComparator.compare(node2, node1) > 0);
+
+ XPathComparator advancedComparator = new XPathComparator("local-name()",
"test/text()", "foo/text()");
+ assertTrue(advancedComparator.compare(node0, node2) < 0);
+ assertTrue(advancedComparator.compare(node2, node1) > 0);
+ assertTrue(advancedComparator.compare(node0, node1) > 0);
+ assertTrue(advancedComparator.compare(node1, node0) < 0);
+
+
+ }
+}