commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r603052 - in /commons/proper/jxpath/trunk/src: java/org/apache/commons/jxpath/BasicNodeSet.java test/org/apache/commons/jxpath/BasicNodeSetTest.java
Date Mon, 10 Dec 2007 21:15:28 GMT
Author: mbenson
Date: Mon Dec 10 13:15:27 2007
New Revision: 603052

URL: http://svn.apache.org/viewvc?rev=603052&view=rev
Log:
add BasicNodeSetTest; fix caching issues detected thereby.

Added:
    commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/BasicNodeSetTest.java 
 (with props)
Modified:
    commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/BasicNodeSet.java

Modified: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/BasicNodeSet.java
URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/BasicNodeSet.java?rev=603052&r1=603051&r2=603052&view=diff
==============================================================================
--- commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/BasicNodeSet.java (original)
+++ commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/BasicNodeSet.java Mon Dec
10 13:15:27 2007
@@ -33,27 +33,27 @@
 
     public void add(Pointer pointer) {
         pointers.add(pointer);
-        readOnlyPointers = null;
+        clearCacheLists();
     }
 
     public void add(NodeSet nodeSet) {
         pointers.addAll(nodeSet.getPointers());
-        readOnlyPointers = null;
+        clearCacheLists();
     }
 
     public void remove(Pointer pointer) {
         pointers.remove(pointer);
-        readOnlyPointers = null;
+        clearCacheLists();
     }
     
-    public List getPointers() {
+    public synchronized List getPointers() {
         if (readOnlyPointers == null) {
             readOnlyPointers = Collections.unmodifiableList(pointers);
         }
         return readOnlyPointers;
     }
 
-    public List getNodes() {
+    public synchronized List getNodes() {
         if (nodes == null) {
             nodes = new ArrayList();
             for (int i = 0; i < pointers.size(); i++) {
@@ -65,7 +65,7 @@
         return nodes;
     }
 
-    public List getValues() {
+    public synchronized List getValues() {
         if (values == null) {
             values = new ArrayList();
             for (int i = 0; i < pointers.size(); i++) {
@@ -79,5 +79,14 @@
     
     public String toString() {
         return pointers.toString();
+    }
+
+    /**
+     * Clear cache list members.
+     */
+    private synchronized void clearCacheLists() {
+        readOnlyPointers = null;
+        nodes = null;
+        values = null;
     }
 }

Added: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/BasicNodeSetTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/BasicNodeSetTest.java?rev=603052&view=auto
==============================================================================
--- commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/BasicNodeSetTest.java (added)
+++ commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/BasicNodeSetTest.java Mon
Dec 10 13:15:27 2007
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.jxpath;
+
+import java.util.Iterator;
+
+/**
+ * Test BasicNodeSet
+ * 
+ * @author Matt Benson
+ * @version $Revision$ $Date$
+ */
+public class BasicNodeSetTest extends JXPathTestCase {
+    /** JXPathContext */
+    protected JXPathContext context;
+
+    /** BasicNodeSet */
+    protected BasicNodeSet nodeSet;
+
+    /**
+     * Create a new BasicNodeSetTest.
+     * 
+     * @param name testcase name
+     */
+    public BasicNodeSetTest(String name) {
+        super(name);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+        context = JXPathContext.newContext(new TestBean());
+        nodeSet = new BasicNodeSet();
+    }
+
+    /**
+     * Add the pointers for the specified path to <code>nodeSet</code>.
+     * 
+     * @param xpath
+     */
+    protected void addPointers(String xpath) {
+        for (Iterator iter = context.iteratePointers(xpath); iter.hasNext();) {
+            nodeSet.add((Pointer) iter.next());
+        }
+        nudge();
+    }
+
+    /**
+     * Remove the pointers for the specified path from <code>nodeSet</code>.
+     * 
+     * @param xpath
+     */
+    protected void removePointers(String xpath) {
+        for (Iterator iter = context.iteratePointers(xpath); iter.hasNext();) {
+            nodeSet.remove((Pointer) iter.next());
+        }
+        nudge();
+    }
+
+    /**
+     * "Nudge" the nodeSet.
+     */
+    protected void nudge() {
+        nodeSet.getPointers();
+        nodeSet.getValues();
+        nodeSet.getNodes();
+    }
+
+    /**
+     * Test adding pointers.
+     */
+    public void testAdd() {
+        addPointers("/integers");
+        assertEquals(nodeSet.getPointers().toString(), list("/integers[1]",
+                "/integers[2]", "/integers[3]", "/integers[4]").toString());
+    }
+
+    /**
+     * Test removing a pointer.
+     */
+    public void testRemove() {
+        addPointers("/integers");
+        removePointers("/integers[4]");
+        assertEquals(list("/integers[1]", "/integers[2]", "/integers[3]")
+                .toString(), nodeSet.getPointers().toString());
+        assertEquals(list(new Integer(1), new Integer(2), new Integer(3)),
+                nodeSet.getValues());
+    }
+}

Propchange: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/BasicNodeSetTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/BasicNodeSetTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Mon Dec 10 13:15:27 2007
@@ -0,0 +1,5 @@
+Date
+Author
+Id
+Revision
+HeadURL



Mime
View raw message