dmitri 01/06/03 14:19:48
Added: jpath/src/test/org/apache/commons/jpath JPathTestCase.java
NestedTestBean.java TestBean.java
Log:
Initial contribution by PLOTNIX, INC
Revision Changes Path
1.1
jakarta-commons-sandbox/jpath/src/test/org/apache/commons/jpath/JPathTestCase.java
Index: JPathTestCase.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/jpath/src/test/org/apache/commons/jpath/JPathTestCase.java,v
1.1 2001/06/03 21:19:47 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2001/06/03 21:19:47 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jpath;
import java.lang.reflect.InvocationTargetException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.w3c.dom.Node;
import java.util.*;
/**
* <p>
* Test Case for the JPath class. The majority of these tests use
* instances of the TestBean class, so be sure to update the tests if you
* change the characteristics of that class.
* </p>
*
* <p>
* Note that the tests are dependant upon the static aspects
* (such as array sizes...) of the TestBean.java class, so ensure
* than all changes to TestBean are reflected here.
* </p>
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2001/06/03 21:19:47 $
*/
public class JPathTestCase extends TestCase
{
/**
* Exercises this test case only
*/
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
// ---------------------------------------------------- Instance Variables
/**
* The test bean for each test.
*/
protected TestBean bean = null;
// ---------------------------------------------------------- Constructors
/**
* Construct a new instance of this test case.
*
* @param name Name of the test case
*/
public JPathTestCase(String name)
{
super(name);
}
// -------------------------------------------------- Overall Test Methods
/**
* Set up instance variables required by this test case.
*/
public void setUp()
{
bean = new TestBean();
}
/**
* Return the tests included in this test suite.
*/
public static Test suite()
{
return (new TestSuite(JPathTestCase.class));
}
/**
* Tear down instance variables required by this test case.
*/
public void tearDown()
{
bean = null;
}
// ------------------------------------------------ Individual Test Methods
/**
* Test JPath.getValue() with various arguments
*/
public void testGetValue(){
JPathContext context = JPathContext.newContext(bean);
testGetValue(context, "2+2", new Double(4.0));
testGetValue(context, "boolean", Boolean.FALSE);
testGetValue(context, "substring(boolean, 1,2)", "fa"); // 'fa'lse
testGetValue(context, "int*2", new Double(2.0));
testGetValue(context, "integers[1]", new Integer(1));
testGetValue(context, "nestedBean", bean.getNestedBean());
testGetValue(context, "nestedBean/boolean", Boolean.FALSE);
testGetValue(context, "object/name", "Name 0");
testGetValue(context, "objects[1]", new Integer(1));
testGetValue(context, "map/Key1", "Value 1");
// testGetValue(context, "[1]", new Integer(1)); // Should
this work?
// testGetValue(context, "id('foo')", new Integer(1));
// testGetValue(context, "key('foo', 'bar')", new Integer(1));
}
/**
* Test JPath.getValue() with variables
*/
public void testVariables(){
JPathContext context = JPathContext.newContext(bean);
context.getVariables().declareVariable("x", new Double(7.0));
context.getVariables().declareVariable("y", null);
context.getVariables().declareVariable("z", bean);
context.getVariables().declareVariable("t", new String[]{"a", "b"});
context.getVariables().declareVariable("m", bean.getMap());
testGetValue(context, "$x + 3", new Double(10.0));
testGetValue(context, "$y", null);
testGetValue(context, "$y + 1", new Double(1.0));
boolean exception = false;
try {
testGetValue(context, "$none", null);
}
catch (Exception ex){
exception = true;
}
assert("Evaluating '$none', expected exception - did not get it", exception);
testGetValue(context, "$z/int", new Integer(1));
testGetValue(context, "$z/integers[$x - 5]", new Integer(2));
testGetValue(context, "$t[2]", "b");
testGetValue(context, "$m/Key1", "Value 1");
// testGetValue(context, "[1]", new Integer(2));
}
private void testGetValue(JPathContext context, String xpath, Object expected) {
Object actual = context.getValue(xpath);
assertEquals("Evaluating <" + xpath + ">", expected, actual);
}
/**
* Test JPath.eval() with various arguments
*/
public void testEval(){
JPathContext context = JPathContext.newContext(bean);
testEval(context, "integers[position()<3]",
Arrays.asList(new Integer[]{new Integer(1), new Integer(2)}));
}
private void testEval(JPathContext context, String xpath, Object expected) {
Object actual = context.eval(xpath);
assertEquals("Evaluating <" + xpath + ">", expected, actual);
}
/**
* Test JPath.setValue() with various arguments
*/
public void testSetValue(){
JPathContext context = JPathContext.newContext(bean);
context.getVariables().declareVariable("x", null);
context.setValue("$x", new Integer(1));
assertEquals("Modified <" + "$x" + ">", new Double(1),
context.getValue("$x"));
boolean exception = false;
try {
context.setValue("$y", new Integer(1));
}
catch (Exception ex){
exception = true;
}
assert("Setting '$y = 1', expected exception - did not get it", exception);
}
/**
* Test JPath.getValue() with nested contexts
*/
public void testNestedContext(){
JPathContext pcontext = JPathContext.newContext(null);
pcontext.getVariables().declareVariable("x", bean);
JPathContext context = JPathContext.newContext(pcontext, bean, null);
testGetValue(context, "integers[$x/int]", new Integer(1));
}
/*
* Remove the underscore from the method name if you want to see the output
*/
public void _testXSLT(){
Node node = DOMWrapper.createNode(bean, "test");
printXML(node, System.err);
}
private static void printXML(Node node, java.io.OutputStream outputStream){
try {
javax.xml.transform.dom.DOMSource source = new
javax.xml.transform.dom.DOMSource(node);
javax.xml.transform.stream.StreamResult result = new
javax.xml.transform.stream.StreamResult(outputStream);
javax.xml.transform.Transformer trans =
javax.xml.transform.TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
"2");
trans.transform(source, result);
}
catch (Exception ex){
// We don't care about this. The method is only for debugging
ex.printStackTrace();
}
}
}
1.1
jakarta-commons-sandbox/jpath/src/test/org/apache/commons/jpath/NestedTestBean.java
Index: NestedTestBean.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/jpath/src/test/org/apache/commons/jpath/NestedTestBean.java,v
1.1 2001/06/03 21:19:48 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2001/06/03 21:19:48 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jpath;
import org.w3c.dom.*;
import java.beans.PropertyDescriptor;
import java.lang.reflect.*;
/**
* A general purpose JavaBean for JUnit tests for the "jpath" component.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2001/06/03 21:19:48 $
*/
public class NestedTestBean {
private String name = "Name 0";
public NestedTestBean(){
}
public NestedTestBean(String name){
this.name = name;
}
/**
* A read-only String property
*/
public String getName(){
return name;
}
/**
* A read-only boolean property
*/
public boolean isBoolean(){
return false;
}
/**
* A read-only int property
*/
public int getInt(){
return 1;
}
}
1.1
jakarta-commons-sandbox/jpath/src/test/org/apache/commons/jpath/TestBean.java
Index: TestBean.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/jpath/src/test/org/apache/commons/jpath/TestBean.java,v
1.1 2001/06/03 21:19:48 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2001/06/03 21:19:48 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jpath;
import java.util.*;
/**
* General purpose test bean for JUnit tests for the "jpath" component.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2001/06/03 21:19:48 $
*/
public class TestBean {
// ------------------------------------------------------------- Properties
/**
* A boolean property.
*/
private boolean bool = false;
public boolean getBoolean(){
return bool;
}
public void setBoolean(boolean bool){
this.bool = bool;
}
/**
* A read-only integer property
*/
public int getInt(){
return 1;
}
/**
* A read-only array of integers
*/
private static int[] array = {1, 2, 3};
public int[] getIntegers(){
return array;
}
/**
* A nested read-only java bean
*/
private NestedTestBean nestedBean = new NestedTestBean();
public NestedTestBean getNestedBean(){
return nestedBean;
}
/**
* An array of nested java beans.
*/
private static NestedTestBean[] beans;
public NestedTestBean[] getBeans(){
if (beans == null){
beans = new NestedTestBean[2];
beans[0] = new NestedTestBean("Name 1");
beans[1] = new NestedTestBean("Name 2");
}
return beans;
}
/**
* A heterogeneous list: String, Integer, NestedTestBean
*/
private ArrayList list;
public List getList(){
if (list == null){
list = new ArrayList();
list.add("String 3");
list.add(new Integer(3));
list.add(new NestedTestBean("Name 3"));
}
return list;
}
/**
* A heterogeneous set: String, Integer, NestedTestBean
*/
private HashSet set;
public Set getSet(){
if (set == null){
set = new HashSet();
set.add("String 4");
set.add(new Integer(4));
set.add(new NestedTestBean("Name 4"));
}
return set;
}
/**
* A Map
*/
private HashMap map;
public Map getMap(){
if (map == null){
map = new HashMap();
map.put("Key1", "Value 1");
map.put("Key2", new NestedTestBean("Name 5"));
map.put("Key3", null);
}
return map;
}
/**
* Returns a NestedTestBean: testing recognition of generic objects
*/
public Object getObject(){
return getNestedBean();
}
/**
* Returns an array of ints: testing recognition of generic objects
*/
public Object getObjects(){
return getIntegers();
}
}