Author: davsclaus
Date: Wed Apr  7 10:40:37 2010
New Revision: 931494

URL: http://svn.apache.org/viewvc?rev=931494&view=rev
Log:
CAMEL-2622: Fixed createIterator and hasNext. Thanks to Sergey Zolotaryov for 
the patch.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=931494&r1=931493&r2=931494&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java 
Wed Apr  7 10:40:37 2010
@@ -467,15 +467,16 @@ public final class ObjectHelper {
             return list.iterator();
         } else if (value instanceof NodeList) {
             // lets iterate through DOM results after performing XPaths
-            final NodeList nodeList = (NodeList)value;
+            final NodeList nodeList = (NodeList) value;
             return CastUtils.cast(new Iterator<Node>() {
                 int idx = -1;
 
                 public boolean hasNext() {
-                    return ++idx < nodeList.getLength();
+                    return (idx + 1) < nodeList.getLength();
                 }
 
                 public Node next() {
+                    idx++;
                     return nodeList.item(idx);
                 }
 
@@ -500,10 +501,11 @@ public final class ObjectHelper {
 
                     public boolean hasNext() {
                         // empty string should not be regarded as having next
-                        return ++idx == 0 && ObjectHelper.isNotEmpty(s);
+                        return idx + 1 == 0 && ObjectHelper.isNotEmpty(s);
                     }
 
                     public String next() {
+                        idx++;
                         return s;
                     }
 

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java?rev=931494&r1=931493&r2=931494&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java
 Wed Apr  7 10:40:37 2010
@@ -18,7 +18,11 @@ package org.apache.camel.converter;
 
 import java.util.Iterator;
 
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
 import junit.framework.TestCase;
+
 import org.apache.camel.util.ObjectHelper;
 
 /**
@@ -84,4 +88,31 @@ public class ObjectHelperTest extends Te
         assertEquals(false, it.hasNext());
     }
 
+    public void testIteratorIdempotentNext() {
+        Iterator<Object> it = ObjectHelper.createIterator("a");
+        assertTrue(it.hasNext());
+        assertTrue(it.hasNext());
+        it.next();
+        assertFalse(it.hasNext());
+    }
+
+    public void testIteratorIdempotentNextWithNodeList() {
+        NodeList nodeList = new NodeList() {
+
+            public Node item(int index) {
+                return null;
+            }
+
+            public int getLength() {
+                return 1;
+            }
+        };
+
+        Iterator<Object> it = ObjectHelper.createIterator(nodeList);
+        assertTrue(it.hasNext());
+        assertTrue(it.hasNext());
+        it.next();
+        assertFalse(it.hasNext());
+    }
+
 }

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java?rev=931494&r1=931493&r2=931494&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java
 Wed Apr  7 10:40:37 2010
@@ -37,7 +37,7 @@ public class CustomProducerServicePoolTe
 
     private static int counter;
 
-    private class MyEndpoint extends DefaultEndpoint {
+    private final class MyEndpoint extends DefaultEndpoint {
 
         private MyEndpoint(String endpointUri, CamelContext camelContext) {
             super(endpointUri, camelContext);
@@ -61,7 +61,7 @@ public class CustomProducerServicePoolTe
         }
     }
 
-    private class MyProducer extends DefaultProducer implements 
ServicePoolAware {
+    private final class MyProducer extends DefaultProducer implements 
ServicePoolAware {
 
         public MyProducer(Endpoint endpoint) {
             super(endpoint);


Reply via email to