avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1045347 - in /avro/trunk: CHANGES.txt lang/java/src/java/org/apache/avro/generic/GenericData.java lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java
Date Mon, 13 Dec 2010 20:01:25 GMT
Author: cutting
Date: Mon Dec 13 20:01:24 2010
New Revision: 1045347

URL: http://svn.apache.org/viewvc?rev=1045347&view=rev
Log:
AVRO-710. Java: Add bounds checking to GenericData.Array#get(int).  Contributed by Bo Shi.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java
    avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1045347&r1=1045346&r2=1045347&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Mon Dec 13 20:01:24 2010
@@ -70,6 +70,9 @@ Avro 1.5.0 (unreleased)
     AVRO-705. Java: Fix DirectBinaryDecoder to correctly reinitialize.
     (thiru via cutting)
 
+    AVRO-710. Java: Add bounds checking to GenericData.Array#get(int).
+    (Bo Shi via cutting)
+
 Avro 1.4.1 (13 October 2010)
 
   NEW FEATURES

Modified: avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java?rev=1045347&r1=1045346&r2=1045347&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java Mon Dec 13 20:01:24
2010
@@ -108,7 +108,11 @@ public class GenericData {
         public void remove() { throw new UnsupportedOperationException(); }
       };
     }
-    @Override public T get(int i) { return (T)elements[i]; }
+    @Override public T get(int i) {
+      if (i >= size)
+        throw new IndexOutOfBoundsException("Index " + i + " out of bounds.");
+      return (T)elements[i];
+    }
     @Override public boolean add(T o) {
       if (size == elements.length) {
         Object[] newElements = new Object[(size * 3)/2 + 1];

Modified: avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java?rev=1045347&r1=1045346&r2=1045347&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java (original)
+++ avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java Mon Dec
13 20:01:24 2010
@@ -123,5 +123,22 @@ public class TestGenericData {
       forward.reverse();
       assertTrue(forward.equals(backward));
   }
-    
+
+  @Test
+  public void testArrayListInterface() {
+    Schema schema = Schema.createArray(Schema.create(Schema.Type.INT));
+    GenericArray<Integer> array = new GenericData.Array<Integer>(1, schema);
+    array.add(99);
+    assertEquals(new Integer(99), array.get(0));
+    try {
+      array.get(2);
+      fail("Expected IndexOutOfBoundsException getting index 2");
+    } catch (IndexOutOfBoundsException e) {}
+    array.clear();
+    assertEquals(0, array.size());
+    try {
+      array.get(0);
+      fail("Expected IndexOutOfBoundsException getting index 0 after clear()");
+    } catch (IndexOutOfBoundsException e) {}
+  }
 }



Mime
View raw message