[ https://issues.apache.org/jira/browse/JXPATH-71?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12462496 ]
Matt Benson commented on JXPATH-71: ----------------------------------- I think this is a duplicate of JXPATH-71 > Ponter.asPath() return values not always correct > ------------------------------------------------ > > Key: JXPATH-71 > URL: https://issues.apache.org/jira/browse/JXPATH-71 > Project: Commons JXPath > Issue Type: Bug > Affects Versions: 1.2 Final > Environment: WInXP, Java 1.5, Eclipse 3.2 > Reporter: John Attwood > > String returned by Pointer.asPath() is incorrect when path starts with '//' > and target is a collection. The path returned always has a final subscript > equal to the size of the collection, although Pointer.getValue() still > returns the correct element in each case. Below are two classes and a JUnit > testcase which reproduce the bug and isolate it to the case where the path > starts with '//' and the target is a collection. > I found this problem whilst trying to write the equivalent of XPathExplorer > for my JXPath-based object trees. It does't affect the main app, as > getValue() always returns the correct node, but in my explorer it only ever > highlights the last element in any collection (the objects in my trees aren't > always unique so the path is only way to identify them individually and allow > the matching nodes to be highlighted). > Otherwise an excellent, easy-to-use and really useful package. > /////////////////////////////////////// Parent.java > ////////////////////////////////////////////////////////////////////////////////// > package test; > import java.util.ArrayList; > public class Parent { > private int id; > private ArrayList<Child> kids; > public Parent(int id) { > this.id = id; > this.kids = new ArrayList<Child>(); > } > public int getId() { > return id; > } > public ArrayList<Child> getKids() { > return kids; > } > public void addKid(Child kid) { > kids.add(kid); > } > public void setId(int id) { > this.id = id; > } > public void setKids(ArrayList<Child> kids) { > this.kids = kids; > } > } > /////////////////////////////////////////////////////////// Child.java > ///////////////////////////////////////////////////////////////////////////////////////////////////////////// > package test; > public class Child { > private int id; > public Child(int id) { > this.id = id; > } > public int getId() { > return id; > } > public void setId(int id) { > this.id = id; > } > } > /////////////////////////////////////////////////////////// > TestPointerToPath.java > /////////////////////////////////////////////////////////////////////////// > package test; > import java.util.HashSet; > import java.util.Iterator; > import java.util.Set; > import junit.framework.TestCase; > import org.apache.commons.jxpath.JXPathContext; > import org.apache.commons.jxpath.Pointer; > public class TestPonterToPath extends TestCase { > private Parent parent; > private Set<String> expectedPaths, actualPaths; > private Set<Object> actualObjects, expectedObjects; > private JXPathContext ctx; > > private static final int SIZE = 4; > > public void setUp() { > parent = new Parent(1); > for (int i = 1; i <= SIZE; i++) { > parent.addKid(new Child(i)); > } > expectedPaths = new HashSet<String>(); > expectedObjects = new HashSet<Object>(); > actualPaths = new HashSet<String>(); > actualObjects = new HashSet<Object>(); > ctx = JXPathContext.newContext(parent); > } > > private void doExpected(String path1, String path2) { > for (int i = 1; i <= SIZE; i++) { > Pointer p = ctx.getPointer(path1 + i + path2); > expectedPaths.add(p.asPath()); > expectedObjects.add(p.getValue()); > } > assertEquals(SIZE, expectedPaths.size()); > assertEquals(SIZE, expectedObjects.size()); > } > > private void doActual(String path) { > Iterator it = ctx.iteratePointers(path); > while (it.hasNext()) { > Pointer p = (Pointer) it.next(); > actualPaths.add(p.asPath()); > actualObjects.add(p.getValue()); > } > assertEquals(SIZE, actualObjects.size()); > } > public void testToPathLeafAbs() { > doExpected("/kids[", "]/id"); > doActual("/kids/id"); > assertEquals(expectedObjects, actualObjects); > assertEquals(expectedPaths, actualPaths); > } > public void testToPathLeafRel() { > doExpected("//kids[", "]/id"); > doActual("//kids/id"); > assertEquals(expectedObjects, actualObjects); > assertEquals(expectedPaths, actualPaths); > } > public void testToPathCollectionAbs() { > doExpected("/kids[", "]"); > doActual("/kids"); > assertEquals(expectedObjects, actualObjects); > assertEquals(expectedPaths, actualPaths); > } > > public void testToPathCollectionRel() { > doExpected("//kids[", "]"); > doActual("//kids"); > assertEquals(expectedObjects, actualObjects); > /* next test fails as all actualPaths are /kids[SIZE] */ > assertEquals(expectedPaths, actualPaths); > } > > } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]