camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bvah...@apache.org
Subject git commit: CAMEL-7096: The ObjectHelper#createIterator utility should properly adhere the java.util.Iterator contract.
Date Thu, 26 Dec 2013 15:08:08 GMT
Updated Branches:
  refs/heads/camel-2.11.x 1d1ffaafd -> 36583fc45


CAMEL-7096: The ObjectHelper#createIterator utility should properly adhere the java.util.Iterator
contract.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/36583fc4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/36583fc4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/36583fc4

Branch: refs/heads/camel-2.11.x
Commit: 36583fc450f931b7325df546083d2b4fe404985e
Parents: 1d1ffaa
Author: Babak Vahdat <bvahdat@apache.org>
Authored: Thu Dec 26 16:03:11 2013 +0100
Committer: Babak Vahdat <bvahdat@apache.org>
Committed: Thu Dec 26 16:08:00 2013 +0100

----------------------------------------------------------------------
 .../org/apache/camel/util/ObjectHelper.java     |  31 ++-
 .../org/apache/camel/util/ObjectHelperTest.java | 200 +++++++++++++++++++
 2 files changed, 221 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/36583fc4/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
index df7fb7d..5d2184b 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -40,6 +40,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Properties;
 import java.util.Scanner;
 
@@ -547,15 +548,18 @@ public final class ObjectHelper {
             if (isPrimitiveArrayType(value.getClass())) {
                 final Object array = value;
                 return new Iterator<Object>() {
-                    int idx = -1;
+                    private int idx;
 
                     public boolean hasNext() {
-                        return (idx + 1) < Array.getLength(array);
+                        return idx < Array.getLength(array);
                     }
 
                     public Object next() {
-                        idx++;
-                        return Array.get(array, idx);
+                        if (!hasNext()) {
+                            throw new NoSuchElementException("no more element available for
'" + array + "' at the index " + idx);
+                        }
+
+                        return Array.get(array, idx++);
                     }
 
                     public void remove() {
@@ -571,15 +575,18 @@ public final class ObjectHelper {
             // lets iterate through DOM results after performing XPaths
             final NodeList nodeList = (NodeList) value;
             return new Iterator<Object>() {
-                int idx = -1;
+                private int idx;
 
                 public boolean hasNext() {
-                    return (idx + 1) < nodeList.getLength();
+                    return idx < nodeList.getLength();
                 }
 
                 public Object next() {
-                    idx++;
-                    return nodeList.item(idx);
+                    if (!hasNext()) {
+                        throw new NoSuchElementException("no more element available for '"
+ nodeList + "' at the index " + idx);
+                    }
+
+                    return nodeList.item(idx++);
                 }
 
                 public void remove() {
@@ -614,13 +621,17 @@ public final class ObjectHelper {
             } else {
                 // use a plain iterator that returns the value as is as there are only a
single value
                 return new Iterator<Object>() {
-                    int idx = -1;
+                    private int idx;
 
                     public boolean hasNext() {
-                        return idx + 1 == 0 && (allowEmptyValues || ObjectHelper.isNotEmpty(s));
+                        return idx == 0 && (allowEmptyValues || ObjectHelper.isNotEmpty(s));
                     }
 
                     public Object next() {
+                        if (!hasNext()) {
+                            throw new NoSuchElementException("no more element available for
'" + s + "' at the index " + idx);
+                        }
+
                         idx++;
                         return s;
                     }

http://git-wip-us.apache.org/repos/asf/camel/blob/36583fc4/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
index 315f866..0b21752 100644
--- a/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
@@ -26,6 +26,7 @@ import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Properties;
 
 import org.w3c.dom.Node;
@@ -52,6 +53,8 @@ public class ObjectHelperTest extends TestCase {
 
         assertNotNull("Cannot load resource without leading \"/\"", res1);
         assertNotNull("Cannot load resource with leading \"/\"", res2);
+
+        IOHelper.close(res1, res2);
     }
 
     public void testLoadResource() {
@@ -214,6 +217,13 @@ public class ObjectHelperTest extends TestCase {
         Iterator<?> it = ObjectHelper.createIterator(s, ",", true);
         assertEquals("", it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertEquals("no more element available for '' at the index 1", nsee.getMessage());
+        }
     }
 
     public void testCreateIteratorWithStringAndSemiColonSeparator() {
@@ -237,6 +247,7 @@ public class ObjectHelperTest extends TestCase {
         assertEquals("bean:bar?method=cool('A','Hello,World')", it.next());
     }
 
+    // CHECKSTYLE:OFF
     public void testCreateIteratorWithPrimitiveArrayTypes() {
         Iterator<?> it = ObjectHelper.createIterator(new byte[] {13, Byte.MAX_VALUE,
7, Byte.MIN_VALUE}, null);
         assertTrue(it.hasNext());
@@ -248,9 +259,25 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         assertEquals(Byte.MIN_VALUE, it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[B@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 4"));
+        }
 
         it = ObjectHelper.createIterator(new byte[] {}, null);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[B@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 0"));
+        }
 
         it = ObjectHelper.createIterator(new short[] {13, Short.MAX_VALUE, 7, Short.MIN_VALUE},
null);
         assertTrue(it.hasNext());
@@ -262,9 +289,25 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         assertEquals(Short.MIN_VALUE, it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[S@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 4"));
+        }
 
         it = ObjectHelper.createIterator(new short[] {}, null);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[S@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 0"));
+        }
 
         it = ObjectHelper.createIterator(new int[] {13, Integer.MAX_VALUE, 7, Integer.MIN_VALUE},
null);
         assertTrue(it.hasNext());
@@ -276,9 +319,25 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         assertEquals(Integer.MIN_VALUE, it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[I@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 4"));
+        }
 
         it = ObjectHelper.createIterator(new int[] {}, null);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[I@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 0"));
+        }
 
         it = ObjectHelper.createIterator(new long[] {13L, Long.MAX_VALUE, 7L, Long.MIN_VALUE},
null);
         assertTrue(it.hasNext());
@@ -290,9 +349,25 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         assertEquals(Long.MIN_VALUE, it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[J@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 4"));
+        }
 
         it = ObjectHelper.createIterator(new long[] {}, null);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[J@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 0"));
+        }
 
         it = ObjectHelper.createIterator(new float[] {13.7F, Float.MAX_VALUE, 7.13F, Float.MIN_VALUE},
null);
         assertTrue(it.hasNext());
@@ -304,9 +379,25 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         assertEquals(Float.MIN_VALUE, it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[F@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 4"));
+        }
 
         it = ObjectHelper.createIterator(new float[] {}, null);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[F@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 0"));
+        }
 
         it = ObjectHelper.createIterator(new double[] {13.7D, Double.MAX_VALUE, 7.13D, Double.MIN_VALUE},
null);
         assertTrue(it.hasNext());
@@ -318,9 +409,25 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         assertEquals(Double.MIN_VALUE, it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[D@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 4"));
+        }
 
         it = ObjectHelper.createIterator(new double[] {}, null);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[D@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 0"));
+        }
 
         it = ObjectHelper.createIterator(new char[] {'C', 'a', 'm', 'e', 'l'}, null);
         assertTrue(it.hasNext());
@@ -334,9 +441,25 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         assertEquals(Character.valueOf('l'), it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[C@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 5"));
+        }
 
         it = ObjectHelper.createIterator(new char[] {}, null);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[C@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 0"));
+        }
 
         it = ObjectHelper.createIterator(new boolean[] {false, true, false, true, true},
null);
         assertTrue(it.hasNext());
@@ -350,10 +473,27 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         assertEquals(Boolean.TRUE, it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[Z@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 5"));
+        }
 
         it = ObjectHelper.createIterator(new boolean[] {}, null);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for '[Z@"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 0"));
+        }
     }
+    // CHECKSTYLE:ON
 
     public void testArrayAsIterator() throws Exception {
         String[] data = {"a", "b"};
@@ -405,12 +545,32 @@ public class ObjectHelperTest extends TestCase {
     public void testIteratorEmpty() {
         Iterator<?> it = ObjectHelper.createIterator("");
         assertEquals(false, it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertEquals("no more element available for '' at the index 0", nsee.getMessage());
+        }
 
         it = ObjectHelper.createIterator("    ");
         assertEquals(false, it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertEquals("no more element available for '    ' at the index 0", nsee.getMessage());
+        }
 
         it = ObjectHelper.createIterator(null);
         assertEquals(false, it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+        }
     }
 
     public void testIteratorIdempotentNext() {
@@ -419,6 +579,13 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         it.next();
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertEquals("no more element available for 'a' at the index 1", nsee.getMessage());
+        }
     }
 
     public void testIteratorIdempotentNextWithNodeList() {
@@ -438,6 +605,14 @@ public class ObjectHelperTest extends TestCase {
         assertTrue(it.hasNext());
         it.next();
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertTrue(nsee.getMessage(), nsee.getMessage().startsWith("no more element available
for 'org.apache.camel.util.ObjectHelperTest$"));
+            assertTrue(nsee.getMessage(), nsee.getMessage().endsWith("at the index 1"));
+        }
     }
 
     public void testGetCamelContextPropertiesWithPrefix() {
@@ -593,6 +768,12 @@ public class ObjectHelperTest extends TestCase {
         assertEquals("b", it.next());
         assertEquals("c", it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+        }
     }
 
     public void testIteratorWithEmptyMessage() {
@@ -601,6 +782,13 @@ public class ObjectHelperTest extends TestCase {
 
         Iterator<Object> it = ObjectHelper.createIterator(msg);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+            assertEquals("no more element available for '' at the index 0", nsee.getMessage());
+        }
     }
 
     public void testIteratorWithNullMessage() {
@@ -609,6 +797,12 @@ public class ObjectHelperTest extends TestCase {
 
         Iterator<Object> it = ObjectHelper.createIterator(msg);
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+        }
     }
 
     public void testIterable() {
@@ -626,6 +820,12 @@ public class ObjectHelperTest extends TestCase {
         assertEquals("B", it.next());
         assertEquals("C", it.next());
         assertFalse(it.hasNext());
+        try {
+            it.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+        }
     }
 
     public void testNormalizeClassName() {


Mime
View raw message