Package org.jostraca.tree.path

Source Code of org.jostraca.tree.path.StepPathTest

package org.jostraca.tree.path;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import org.jostraca.tree.Node;
import org.jostraca.tree.NodeTest;
import org.jostraca.tree.XmlNodeParser;

import junit.framework.TestCase;

public class StepPathTest extends TestCase {

  public void testParse() throws Exception {

    assertEquals("foo",ps("foo"));
    assertEquals("foo / bar",ps("foo/bar"));
    assertEquals("foo / bar / baz",ps("foo/bar/baz"));

    assertEquals("foo",ps("child::foo"));
    assertEquals("child",ps("child"));
   
    assertEquals("and",ps("and"));
    assertEquals("and / or",ps("and/or"));
    assertEquals("and / or / not",ps("and/or/not"));
    assertEquals("and[div]",ps("and[div]"));
    assertEquals("and[div / mod]",ps("and[div/mod]"));
   
    assertEquals("*",ps("*"));
    assertEquals("* / bar",ps("* / bar"));
    assertEquals("* / bar / baz",ps("* /bar/baz"));

    assertEquals("foo",ps("foo"));
    assertEquals("foo / *",ps("foo/*"));
    assertEquals("foo / * / baz",ps("foo/* /baz"));

    assertEquals("foo",ps("foo"));
    assertEquals("foo / bar",ps("foo/bar"));
    assertEquals("foo / bar / *",ps("foo/bar/*"));

   
    assertEquals("foo / @bar",ps("foo/@bar"));
    assertEquals("foo / bar / @baz",ps("foo/bar/@baz"));
    assertEquals("foo / * / @baz",ps("foo/*/@baz"));
   
    assertEquals("@foo",ps("@foo"));
   
    assertEquals("foo / bar / .. / bar",ps("foo/bar/../bar"));
   
    assertEquals("foo / //bar / qaz",ps("foo//bar/qaz"));
    assertEquals("foo / //bar",ps("child::foo/descendant::bar"));
   
    assertEquals("foo[true]",ps("foo[true]"));
    assertEquals("foo[(true) and (false)]",ps("foo[true and false]"));
    assertEquals("foo[(true) and ((true) and (false))]",ps("foo[true and (true and false)]"));
    assertEquals("foo[((true) and (true)) and (false)]",ps("foo[(true and true) and false]"));
   
    assertEquals("foo[bar]",ps("foo[bar]"));
    assertEquals("foo / bar",ps("foo/bar"));
    assertEquals("foo / far[boo / bar]",ps("foo/far[boo/bar]"));

    assertEquals("foo[bar[baz]]",ps("foo[bar[baz]]"));
   
    assertEquals("far: foo",ps("far:foo"));
    assertEquals("far: foo / bar: boo",ps("far:foo/bar:boo"));
    assertEquals("far: foo / bar: boo / @qar: qoo",ps("far:foo/bar:boo/@qar:qoo"));
  }
 
 
  public String ps( String pPathStr ) throws Exception {
    return new Path( pPathStr ).toString();
  }
 
 
  public void testSelect() throws Exception {
    Node n = parse("<r><foo><bar/><bar/></foo></r>");
    StepPath sp = new StepPath();
    sp.add( new ChildStep("foo") );
    sp.add( new ChildStep("bar") );
    assertEquals( "[bar, bar, ]", NodeTest.toString(sp.select(n)));

    sp = new StepPath();
    sp.add( new ChildStep("*") );
    sp.add( new ChildStep("bar") );
    assertEquals( "[bar, bar, ]", NodeTest.toString(sp.select(n)));

    sp = new StepPath();
    sp.add( new DescendantStep("foo") );
    sp.add( new ChildStep("bar") );
    assertEquals( "[bar, bar, ]", NodeTest.toString(sp.select(n)));

    n = parse("<r><foo><bar/><bar><qaz/></bar></foo></r>");
    sp = new StepPath();
    sp.add( new ChildStep("foo") );
    sp.add( new ChildStep("bar") );
    sp.add( new ChildStep("qaz") );
    assertEquals( "[qaz, ]", NodeTest.toString(sp.select(n)));

    sp = new StepPath();
    sp.add( new ChildStep("foo") );
    sp.add( new ChildStep("*") );
    sp.add( new ChildStep("qaz") );
    assertEquals( "[qaz, ]", NodeTest.toString(sp.select(n)));

    sp = new StepPath();
    sp.add( new DescendantStep("bar") );
    sp.add( new ChildStep("qaz") );
    assertEquals( "[qaz, ]", NodeTest.toString(sp.select(n)));

    n = parse("<r><foo><bar><qaz/><qwe><bar><qaz/></bar></qwe></bar></foo></r>");
    sp = new StepPath();
    sp.add( new DescendantStep("bar") );
    sp.add( new ChildStep("qaz") );
    assertEquals( "[qaz, qaz, ]", NodeTest.toString(sp.select(n)));
  }
 
 
  public void testPredicate() throws Exception {
    Node n = parse("<r><foo><bar/><bar/></foo></r>");
    assertEquals( "[bar, bar, ]", NodeTest.toString(p("foo/bar[true()]").select(n)));
    assertEquals( "[]", NodeTest.toString(p("foo/bar[false()]").select(n)));
    assertEquals( "[bar, bar, ]", NodeTest.toString(p("foo/bar[1=1.0]").select(n)));
    assertEquals( "[]", NodeTest.toString(p("foo/bar[1=2]").select(n)));
  }
 
 
  public void testNamespace() throws Exception {
    Node n = parse("<r xmlns:q='http://www.example.com/q'>"
        +"<foo><q:bar/><q:bar/></foo></r>", "http://www.example.com/q", "q");
    System.out.println(n);
    assertEquals( "[q: bar, q: bar, ]", NodeTest.toString(p("foo/q:bar[true()]").select(n),true));
    assertEquals( "[]", NodeTest.toString(p("foo/q:bar[false()]").select(n),true));
    assertEquals( "[q: bar, q: bar, ]", NodeTest.toString(p("foo/q:bar[1=1.0]").select(n),true));
    assertEquals( "[]", NodeTest.toString(p("foo/q:bar[1=2]").select(n),true));

    n = parse(
        "<root xmlns:date='http://www.jostraca.org/example/namespace/for/dates'>"+
        "<wives><wife name='Catherine of Aragon'>"+
        "  <fate>divorced</fate>"+
        "  <married>"+
        "    <date:range>"+
        "      <date:date date:name='from' date:year='1509'></date:date>"+
        "      <date:date date:name='to'   date:year='1533'></date:date>"+
        "    </date:range>"+
        "  </married>"+
        "</wife></wives></root>",
        "http://www.jostraca.org/example/namespace/for/dates", "date");
    System.out.println(n);
 
    assertEquals( "1509", p("wives/wife/married/date:range/date:date[@date:name='from']").select(n).get(0).value("@date:year"));
    assertEquals( "1533", p("wives/wife/married/date:range/date:date[@date:name='to']").select(n).get(0).value("@date:year"));

    assertEquals("Catherine of Aragon", p("wives/wife/@name").value(n));
  }
 
 
  public Path p( String pPathStr ) throws Exception {
    return new Path( pPathStr );
  }
 
  public Node parse( String pXML, String... pNS ) throws Exception {
    InputStream is = new ByteArrayInputStream( pXML.getBytes() );
   
    XmlNodeParser xnp = new XmlNodeParser();
   
    for( int nsI = 0; nsI < pNS.length; nsI+=2) {
      xnp.addNamespaceType(pNS[nsI], pNS[nsI+1]);
    }
   
    Node root = xnp.parse(is, "test");
    return root;
  }
}
TOP

Related Classes of org.jostraca.tree.path.StepPathTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.