avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r927167 - in /hadoop/avro/trunk: ./ lang/java/src/java/org/apache/avro/reflect/ lang/java/src/test/java/org/apache/avro/
Date Wed, 24 Mar 2010 19:49:37 GMT
Author: cutting
Date: Wed Mar 24 19:49:36 2010
New Revision: 927167

URL: http://svn.apache.org/viewvc?rev=927167&view=rev
Log:
AVRO-487. Fix Java reflect protocols to transmit error messages.

Added:
    hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleException.java
      - copied, changed from r926257, hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleError.java
Removed:
    hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleError.java
Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java
    hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestProtocolReflect.java
    hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java

Modified: hadoop/avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=927167&r1=927166&r2=927167&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Wed Mar 24 19:49:36 2010
@@ -25,6 +25,8 @@ Avro 1.3.2 (unreleased)
     AVRO-479. Fix 'sign' target in top-level build.sh to generate md5
     checksums. (cutting)
 
+    AVRO-487. Fix Java reflect protocols to transmit error messages. (cutting)
+
 Avro 1.3.1 (16 March 2010)
 
   NEW FEATURES

Modified: hadoop/avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java?rev=927167&r1=927166&r2=927167&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java (original)
+++ hadoop/avro/trunk/lang/java/src/java/org/apache/avro/reflect/ReflectData.java Wed Mar
24 19:49:36 2010
@@ -271,8 +271,8 @@ public class ReflectData extends Specifi
           schema = Schema.createFixed(name, null /* doc */, space, size);
         } else {                                             // record
           List<Schema.Field> fields = new ArrayList<Schema.Field>();
-          schema = Schema.createRecord(name, null /* doc */, space, 
-                                       Throwable.class.isAssignableFrom(c));
+          boolean error = Throwable.class.isAssignableFrom(c);
+          schema = Schema.createRecord(name, null /* doc */, space, error);
           names.put(c.getName(), schema);
           for (Field field : getFields(c))
             if ((field.getModifiers()&(Modifier.TRANSIENT|Modifier.STATIC))==0){
@@ -280,6 +280,9 @@ public class ReflectData extends Specifi
               fields.add(new Schema.Field(field.getName(),
                   fieldSchema, null /* doc */, null));
             }
+          if (error)                              // add Throwable message
+            fields.add(new Schema.Field("detailMessage", THROWABLE_MESSAGE,
+                                        null, null));
           schema.setFields(fields);
         }
         names.put(fullName, schema);
@@ -289,6 +292,9 @@ public class ReflectData extends Specifi
     return super.createSchema(type, names);
   }
 
+  private static final Schema THROWABLE_MESSAGE =
+    makeNullable(Schema.create(Schema.Type.STRING));
+
   // if array element type is a class with a union annotation, note it
   // this is required because we cannot set a property on the union itself 
   @SuppressWarnings(value="unchecked")

Copied: hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleException.java (from
r926257, hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleError.java)
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleException.java?p2=hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleException.java&p1=hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleError.java&r1=926257&r2=927167&rev=927167&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleError.java (original)
+++ hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/SimpleException.java Wed Mar
24 19:49:36 2010
@@ -19,4 +19,7 @@ package org.apache.avro;
 
 /** This should be a static nested class in TestProtocolReflect, but that
  * breaks CheckStyle (http://jira.codehaus.org/browse/MPCHECKSTYLE-20). */
-public class SimpleError extends Exception {}
+public class SimpleException extends Exception {
+  SimpleException() {}
+  SimpleException(String message) { super(message) ; }
+}

Modified: hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestProtocolReflect.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestProtocolReflect.java?rev=927167&r1=927166&r2=927167&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestProtocolReflect.java (original)
+++ hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestProtocolReflect.java Wed
Mar 24 19:49:36 2010
@@ -50,7 +50,7 @@ public class TestProtocolReflect {
     TestRecord echo(TestRecord record);
     int add(int arg1, int arg2);
     byte[] echoBytes(byte[] data);
-    void error() throws SimpleError;
+    void error() throws SimpleException;
   }
   
   public static class TestImpl implements Simple {
@@ -58,7 +58,9 @@ public class TestProtocolReflect {
     public int add(int arg1, int arg2) { return arg1 + arg2; }
     public TestRecord echo(TestRecord record) { return record; }
     public byte[] echoBytes(byte[] data) { return data; }
-    public void error() throws SimpleError { throw new SimpleError(); }
+    public void error() throws SimpleException {
+      throw new SimpleException("foo");
+    }
   }
 
   protected static Server server;
@@ -105,13 +107,14 @@ public class TestProtocolReflect {
 
   @Test
   public void testError() throws IOException {
-    SimpleError error = null;
+    SimpleException error = null;
     try {
       proxy.error();
-    } catch (SimpleError e) {
+    } catch (SimpleException e) {
       error = e;
     }
     assertNotNull(error);
+    assertEquals("foo", error.getMessage());
   }
 
   @After

Modified: hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java?rev=927167&r1=927166&r2=927167&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java (original)
+++ hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/TestReflect.java Wed Mar 24
19:49:36 2010
@@ -19,6 +19,7 @@ package org.apache.avro;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -320,6 +321,32 @@ public class TestReflect {
     assertEquals(String.class, ReflectData.get().getClass(param));
   }
 
+  // test error
+  public static class E1 extends Exception {}
+  public static interface P2 {
+    void error() throws E1;
+  }
+
+  @Test public void testP2() throws Exception {
+    Schema e1 = ReflectData.get().getSchema(E1.class);
+    assertEquals(Schema.Type.RECORD, e1.getType());
+    assertTrue(e1.isError());
+    Field message = e1.getField("detailMessage");
+    assertNotNull("field 'detailMessage' should not be null", message);
+    Schema messageSchema = message.schema();
+    assertEquals(Schema.Type.UNION, messageSchema.getType());
+    assertEquals(Schema.Type.NULL, messageSchema.getTypes().get(0).getType());
+    assertEquals(Schema.Type.STRING, messageSchema.getTypes().get(1).getType());
+
+    Protocol p2 = ReflectData.get().getProtocol(P2.class);
+    Protocol.Message m = p2.getMessages().get("error");
+    // check error schema is union
+    Schema response = m.getErrors();
+    assertEquals(Schema.Type.UNION, response.getType());
+    assertEquals(Schema.Type.STRING, response.getTypes().get(0).getType());
+    assertEquals(e1, response.getTypes().get(1));
+  }
+
   void checkReadWrite(Object object) throws Exception {
     checkReadWrite(object, ReflectData.get().getSchema(object.getClass()));
   }



Mime
View raw message