Uploaded image for project: 'Pivot'
  1. Pivot
  2. PIVOT-533

org.apache.pivot.collections.Sequence.Tree - add, get, insert, remove & update methods do not handle empty Paths

    XMLWordPrintableJSON

    Details

      Description

      The javadocs for
      org.apache.pivot.collections.Sequence.Tree#get(Sequence<T> sequence, Path path)
      state that it will return null if supplied with an 'empty' path.

      The method only actually checks for a null Path, and not for an empty one (assuming empty means path.getLength() == 0)
      resulting in
      java.lang.IndexOutOfBoundsException: index 0 out of bounds.

      This has a knock on effect with
      add, insert, remove & update methods in the same class.

      public class SequenceTest {

      public static void main(String[] args) {

      final Path emptyPath = new Path();
      final TreeBranch branch = new TreeBranch("xxx");
      final List<TreeNode> data = new ArrayList<TreeNode>(new TreeNode("aaa"), new TreeBranch("bbb"), new TreeNode("ccc"), branch);

      // Succeeds
      Sequence.Tree.get(data, new Path(0));
      try

      { // Fails with java.lang.IllegalArgumentException: path is null. (as expected) Sequence.Tree.get(data, null); }

      catch (java.lang.IllegalArgumentException e)

      { // Expected }

      // Fails with java.lang.IndexOutOfBoundsException: index 0 out of bounds.
      Sequence.Tree.get(data, emptyPath);

      // Fails with java.lang.IndexOutOfBoundsException: index 0 out of bounds.
      Sequence.Tree.add(data, new TreeBranch("eee"), emptyPath);

      // Fails with java.lang.IndexOutOfBoundsException: index 0 out of bounds.
      Sequence.Tree.insert(data, new TreeBranch("fff"), emptyPath, 0);

      // Succeeds
      Sequence.Tree.remove(data, branch);

      // Fails with java.lang.IndexOutOfBoundsException: index 0 out of bounds.
      Sequence.Tree.remove(data, emptyPath, 1);

      // Fails with java.lang.IndexOutOfBoundsException: index 0 out of bounds.
      Sequence.Tree.update(data, emptyPath, new TreeBranch("ggg"));
      }
      }

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              cbartlett Christopher James Bartlett
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: