Java Code Examples for org.apache.commons.jxpath.JXPathContext#iteratePointers()

The following examples show how to use org.apache.commons.jxpath.JXPathContext#iteratePointers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MixedModelTest.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
public void testIteratePointersArray() {
    Map map = new HashMap();
    map.put("foo", new String[] { "a", "b", "c" });

    JXPathContext context = JXPathContext.newContext(map);

    Iterator it = context.iteratePointers("foo");
    List actual = new ArrayList();
    while (it.hasNext()) {
        Pointer ptr = (Pointer) it.next();
        actual.add(context.getValue(ptr.asPath()));
    }
    assertEquals(
        "Iterating pointers <" + "foo" + ">",
        list("a", "b", "c"),
        actual);
}
 
Example 2
Source File: BeanModelTestCase.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
public void testIteratePropertyArrayWithHasNext() {
    JXPathContext context = JXPathContext.newContext(createContextBean());
    Iterator it = context.iteratePointers("/integers");
    List actual = new ArrayList();
    while (it.hasNext()) {
        actual.add(((Pointer) it.next()).asPath());
    }
    assertEquals(
        "Iterating 'hasNext'/'next'<" + "/integers" + ">",
        list(
            "/integers[1]",
            "/integers[2]",
            "/integers[3]",
            "/integers[4]"),
        actual);
}
 
Example 3
Source File: BeanModelTestCase.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
public void testIteratePropertyArrayWithoutHasNext() {
    JXPathContext context = JXPathContext.newContext(createContextBean());
    Iterator it = context.iteratePointers("/integers");
    List actual = new ArrayList();
    for (int i = 0; i < 4; i++) {
        actual.add(it.next().toString());
    }
    assertEquals(
        "Iterating 'next'<" + "/integers" + ">",
        list(
            "/integers[1]",
            "/integers[2]",
            "/integers[3]",
            "/integers[4]"),
        actual);
}
 
Example 4
Source File: BeanModelTestCase.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
public void testIterateAndSet() {
    JXPathContext context = JXPathContext.newContext(createContextBean());

    Iterator it = context.iteratePointers("beans/int");
    int i = 5;
    while (it.hasNext()) {
        NodePointer pointer = (NodePointer) it.next();
        pointer.setValue(new Integer(i++));
    }

    it = context.iteratePointers("beans/int");
    List actual = new ArrayList();
    while (it.hasNext()) {
        actual.add(((Pointer) it.next()).getValue());
    }
    assertEquals(
        "Iterating <" + "beans/int" + ">",
        list(new Integer(5), new Integer(6)),
        actual);
}
 
Example 5
Source File: MixedModelTest.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public void testIteratePointersArrayElementWithVariable() {
    Map map = new HashMap();
    map.put("foo", new String[] { "a", "b", "c" });

    JXPathContext context = JXPathContext.newContext(map);
    context.getVariables().declareVariable("x", new Integer(2));
    Iterator it = context.iteratePointers("foo[$x]");
    List actual = new ArrayList();
    while (it.hasNext()) {
        Pointer ptr = (Pointer) it.next();
        actual.add(context.getValue(ptr.asPath()));
    }
    assertEquals("Iterating pointers <" + "foo" + ">", list("b"), actual);
}
 
Example 6
Source File: BeanModelTestCase.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Test contributed by Kate Dvortsova
 */
public void testIteratePointerSetValue() {
    JXPathContext context = JXPathContext.newContext(createContextBean());

    assertXPathValue(context, "/beans[1]/name", "Name 1");
    assertXPathValue(context, "/beans[2]/name", "Name 2");

    // Test setting via context
    context.setValue("/beans[2]/name", "Name 2 set");
    assertXPathValue(context, "/beans[2]/name", "Name 2 set");

    // Restore original value
    context.setValue("/beans[2]/name", "Name 2");
    assertXPathValue(context, "/beans[2]/name", "Name 2");

    int iterCount = 0;
    Iterator iter = context.iteratePointers("/beans/name");
    while (iter.hasNext()) {
        iterCount++;
        Pointer pointer = (Pointer) iter.next();
        String s = (String) pointer.getValue();
        s = s + "suffix";
        pointer.setValue(s);
        assertEquals("pointer.getValue", s, pointer.getValue());
        // fails right here, the value isn't getting set in the bean.
        assertEquals(
            "context.getValue",
            s,
            context.getValue(pointer.asPath()));
    }
    assertEquals("Iteration count", 2, iterCount);

    assertXPathValue(context, "/beans[1]/name", "Name 1suffix");
    assertXPathValue(context, "/beans[2]/name", "Name 2suffix");
}
 
Example 7
Source File: JXPathServletContextTest.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
private void checkPointerIterator(JXPathContext context) {
    Iterator it = context.iteratePointers("/*");
    assertTrue("Empty context", it.hasNext());
    while (it.hasNext())
    {
        Pointer pointer = (Pointer) it.next();
        assertNotNull("null pointer", pointer);
        assertNotNull("null path", pointer.asPath());
    }
}
 
Example 8
Source File: JXPath118Test.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public void testJXPATH118IssueWithAsPath() throws Exception
{
    Object contextBean = new SomeChildClass();
    JXPathContext context = JXPathContext.newContext(contextBean);
    Iterator iteratePointers = context.iteratePointers("//*");
    Assert.assertEquals("/bar", ((Pointer) iteratePointers.next()).asPath());
    Assert.assertEquals("/baz", ((Pointer) iteratePointers.next()).asPath());
    Assert.assertEquals("/foo", ((Pointer) iteratePointers.next()).asPath());
}
 
Example 9
Source File: JXPathServletContextTest.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
public void testPageContext() {
    MockServletContext servletContext = new MockServletContext();
    servletContext.setAttribute("app", "app");

    MockServletConfig servletConfig = new MockServletConfig();
    servletConfig.setServletContext(servletContext);

    MockHttpSession session = new MockHttpSession();
    session.setupServletContext(servletContext);
    session.setAttribute("session", "session");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute("request", "request");
    request.setSession(session);

    MockPageContext pageContext = new MockPageContext();
    pageContext.setServletConfig(servletConfig);
    pageContext.setServletRequest(request);
    pageContext.setAttribute("page", "page");

    assertSame("Request session", session, request.getSession());


    JXPathContext context = JXPathServletContexts.getPageContext(pageContext);
    context.setLenient(true);
    
    checkPointerIterator(context);

    assertEquals("Page Scope", "page", context.getValue("page"));
    assertEquals("Request Scope", "request", context.getValue("request"));
    assertEquals("Session Scope", "session", context.getValue("session"));
    assertEquals("Application Scope", "app", context.getValue("app"));

    assertEquals("Explicit Page Scope", "page", context.getValue("$page/page"));
    assertEquals("Explicit Request Scope", "request", context.getValue("$request/request"));
    assertEquals("Explicit Session Scope", "session", context.getValue("$session/session"));
    assertEquals("Explicit Application Scope", "app", context.getValue("$application/app"));

    // iterate through the elements of page context only (two elements expected, 'page' and the context)
    Iterator it = context.iteratePointers("$page/*");
    assertTrue("element not found", it.hasNext());
    it.next();
    it.next();
    assertFalse("too many elements", it.hasNext());

    // test setting a value in the context
    context.setValue("/foo1", "bar1");
    assertEquals("Context property", "bar1", context.getValue("/foo1"));

    context.setValue("$page/foo2", "bar2");
    assertEquals("Context property", "bar2", context.getValue("$page/foo2"));
}