avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1142048 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/Schema.java lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
Date Fri, 01 Jul 2011 19:52:49 GMT
Author: cutting
Date: Fri Jul  1 19:52:48 2011
New Revision: 1142048

URL: http://svn.apache.org/viewvc?rev=1142048&view=rev
Log:
AVRO-844. Java: Provide better errors for null or missing record fields.  Contributed by Bill
Graham.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
    avro/trunk/lang/java/avro/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=1142048&r1=1142047&r2=1142048&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Jul  1 19:52:48 2011
@@ -19,6 +19,9 @@ Avro 1.6.0 (unreleased)
 
     AVRO-847. Java: Add a unit test for Java MapReduce tether. (Jeremy Lewi)
 
+    AVRO-844. Java: Provide better errors for null or missing record fields.
+    (Bill Graham via cutting)
+
   BUG FIXES
 
     AVRO-845. setup.py uses Python2.7+ specific code

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java?rev=1142048&r1=1142047&r2=1142048&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java Fri Jul  1 19:52:48
2011
@@ -558,11 +558,15 @@ public abstract class Schema {
 
     @Override
     public Field getField(String fieldname) {
+      if (fieldMap == null)
+        throw new AvroRuntimeException("Schema fields not set yet");
       return fieldMap.get(fieldname);
     }
 
     @Override
     public List<Field> getFields() {
+      if (fields == null)
+        throw new AvroRuntimeException("Schema fields not set yet");
       return fields;
     }
 

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java?rev=1142048&r1=1142047&r2=1142048&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java Fri Jul
 1 19:52:48 2011
@@ -55,7 +55,11 @@ public class GenericData {
     }
     @Override public Schema getSchema() { return schema; }
     @Override public void put(String key, Object value) {
-      values[schema.getField(key).pos()] = value;
+      Schema.Field field = schema.getField(key);
+      if (field == null)
+        throw new AvroRuntimeException("Not a valid schema field: "+key);
+
+      values[field.pos()] = value;
     }
     @Override public void put(int i, Object v) { values[i] = v; }
     @Override public Object get(String key) {

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java?rev=1142048&r1=1142047&r2=1142048&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java Fri
Jul  1 19:52:48 2011
@@ -65,6 +65,34 @@ public class TestGenericData {
     public void testArrayConstructorWrongSchema() throws Exception {
     new GenericData.Array<Object>(1, Schema.create(Schema.Type.INT));
   }
+
+  @Test(expected=AvroRuntimeException.class)
+  public void testRecordCreateEmptySchema() throws Exception {
+    Schema s = Schema.createRecord("schemaName", "schemaDoc", "namespace", false);
+    Record r = new GenericData.Record(s);
+  }
+
+  @Test(expected=AvroRuntimeException.class)
+  public void testGetEmptySchemaFields() throws Exception {
+    Schema s = Schema.createRecord("schemaName", "schemaDoc", "namespace", false);
+    s.getFields();
+  }
+
+  @Test(expected=AvroRuntimeException.class)
+  public void testGetEmptySchemaField() throws Exception {
+    Schema s = Schema.createRecord("schemaName", "schemaDoc", "namespace", false);
+    s.getField("foo");
+  }
+
+  @Test(expected=AvroRuntimeException.class)
+  public void testRecordPutInvalidField() throws Exception {
+    Schema s = Schema.createRecord("schemaName", "schemaDoc", "namespace", false);
+    List<Schema.Field> fields = new ArrayList<Schema.Field>();
+    fields.add(new Schema.Field("someFieldName", s, "docs", null));
+    s.setFields(fields);
+    Record r = new GenericData.Record(s);
+    r.put("invalidFieldName", "someValue");
+  }
   
   @Test
   /** Make sure that even with nulls, hashCode() doesn't throw NPE. */



Mime
View raw message