Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jelly.expression.xpath;

import junit.framework.TestCase;
import org.apache.commons.jelly.expression.ConstantExpression;
import org.apache.commons.jelly.impl.DefaultTagFactory;
import org.apache.commons.jelly.impl.StaticTagScript;

import java.util.HashMap;
import java.util.Map;

/**
* Unit tests for class {@link XPathExpression}.
*
* @see XPathExpression
*
*/
public class TestXPathExpression extends TestCase {

public void testCreateUriMapReturningMapWhereIsEmptyIsFalse() {
XPathExpression xPathExpression = new XPathExpression();
Map<Object, Object> hashMap = new HashMap<Object, Object>();
hashMap.put("a", "a");
Map map = xPathExpression.createUriMap(hashMap);

assertNull(xPathExpression.toString());
assertNull(xPathExpression.getExpressionText());

assertFalse(hashMap.isEmpty());
assertEquals(1, hashMap.size());

assertEquals(1, map.size());
assertFalse(map.isEmpty());
}

public void testCreatesXPathExpressionTakingNoArguments() {
Map<Object, Object> hashMap = new HashMap<Object, Object>();
hashMap.put( null, hashMap);
XPathExpression xPathExpression = new XPathExpression();
Map map = xPathExpression.createUriMap(hashMap);

assertFalse(hashMap.isEmpty());
assertEquals(1, hashMap.size());

assertNull(xPathExpression.toString());
assertNull(xPathExpression.getExpressionText());

assertTrue(map.isEmpty());
assertEquals(0, map.size());
}

public void testCreatesXPathExpressionTaking3Arguments() {
Map<Object, Object> hashMap = new HashMap<Object, Object>();
hashMap.put("a", "a");
StaticTagScript staticTagScript = new StaticTagScript(new DefaultTagFactory());
XPathExpression xPathExpression = new XPathExpression("", new ConstantExpression(""), staticTagScript);
Map map = xPathExpression.createUriMap(hashMap);

assertFalse(hashMap.isEmpty());
assertEquals(1, hashMap.size());

assertEquals((-1), staticTagScript.getLineNumber());
assertNull(staticTagScript.getFileName());

assertEquals((-1), staticTagScript.getColumnNumber());
assertNull(staticTagScript.getElementName());

assertNull(staticTagScript.getLocalName());
assertEquals("", xPathExpression.getExpressionText());

assertEquals("", xPathExpression.toString());
assertFalse(map.isEmpty());

assertEquals(1, map.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jelly.xpath;

import junit.framework.TestCase;
import org.dom4j.QName;
import org.dom4j.bean.BeanElement;
import org.dom4j.dom.DOMDocument;
import org.dom4j.tree.DefaultDocument;
import org.jaxen.BaseXPath;
import org.jaxen.JaxenException;
import org.jaxen.javabean.DocumentNavigator;
import org.jaxen.javabean.JavaBeanXPath;
import org.jaxen.saxpath.SAXPathException;

/**
* Unit tests for class {@link XPathComparator}.
*
* @see XPathComparator
*
*/
public class TestXPathComparator extends TestCase {


public void testCompareTaking2NodesWithNull() throws JaxenException {
BaseXPath baseXPath = new BaseXPath(".", new DocumentNavigator());
QName qName = QName.get("org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher", ".", ".");
BeanElement beanElement = new BeanElement(qName, null);

assertEquals(0, new XPathComparator(baseXPath, false).compare(null, beanElement));

}


public void testCreatesXPathComparatorTaking2Arguments() throws SAXPathException {
JavaBeanXPath javaBeanXPath = (JavaBeanXPath)new DocumentNavigator().parseXPath("3");
DefaultDocument defaultDocument = new DefaultDocument();

assertEquals(0, new XPathComparator(javaBeanXPath, true).compare(defaultDocument, defaultDocument));
}


public void testCreatesXPathComparatorTakingNoArgumentsAndCallsSetXpath() throws SAXPathException {
JavaBeanXPath javaBeanXPath = (JavaBeanXPath) new DocumentNavigator().parseXPath("txext()");
XPathComparator xPathComparator = new XPathComparator();
xPathComparator.setXpath(javaBeanXPath);

DOMDocument dOMDocument = new DOMDocument("txext()");

try {
xPathComparator.compare(dOMDocument, dOMDocument);
fail("Expecting exception: XPathSortException");
} catch(XPathComparator.XPathSortException e) {
assertEquals(XPathComparator.class.getName(), e.getStackTrace()[0].getClassName());
}
}


}