Author: cutting
Date: Thu Mar 13 18:35:31 2014
New Revision: 1577275

URL: http://svn.apache.org/r1577275
Log:
AVRO-1473. Java: Fix references to names in the empty namespace.  Contributed 
by Gabriel Reid.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java
    avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java

Modified: avro/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1577275&r1=1577274&r2=1577275&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Thu Mar 13 18:35:31 2014
@@ -43,6 +43,9 @@ Trunk (not yet released)
     AVRO-1454. Java: Fix GenericData#toString and AvroAsTextRecordReader
     to generate valid Json for NaN and infinities.  (cutting)
 
+    AVRO-1473. Java: Fix references to names in the empty namespace.
+    (Gabriel Reid via cutting)
+
 Avro 1.7.6 (15 January 2014)
 
   NEW FEATURES

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=1577275&r1=1577274&r2=1577275&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 Thu Mar 
13 18:35:31 2014
@@ -422,7 +422,7 @@ public abstract class Schema extends Jso
     }
   }
 
-  private static class Name {
+  static class Name {
     private final String name;
     private final String space;
     private final String full;
@@ -1041,6 +1041,8 @@ public abstract class Schema extends Jso
         Type primitive = PRIMITIVES.get((String)o);
         if (primitive != null) return Schema.create(primitive);
         name = new Name((String)o, space);
+        if (!containsKey(name))                   // if not in default
+          name = new Name((String)o, "");         // try anonymous
       } else {
         name = (Name)o;
       }

Modified: avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java
URL: 
http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java?rev=1577275&r1=1577274&r2=1577275&view=diff
==============================================================================
--- avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java 
(original)
+++ avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestSchema.java Thu 
Mar 13 18:35:31 2014
@@ -17,10 +17,10 @@
  */
 package org.apache.avro;
 
-import org.junit.Test;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
@@ -29,28 +29,28 @@ import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Collection;
-import java.util.Collections;
-
-import org.codehaus.jackson.JsonNode;
 
-import org.apache.avro.Schema.Type;
 import org.apache.avro.Schema.Field;
+import org.apache.avro.Schema.Type;
+import org.apache.avro.compiler.specific.TestSpecificCompiler;
+import org.apache.avro.data.Json;
 import org.apache.avro.generic.GenericData;
 import org.apache.avro.generic.GenericDatumReader;
 import org.apache.avro.generic.GenericDatumWriter;
-import org.apache.avro.io.DecoderFactory;
 import org.apache.avro.io.DatumReader;
 import org.apache.avro.io.DatumWriter;
 import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
 import org.apache.avro.io.Encoder;
 import org.apache.avro.io.EncoderFactory;
-import org.apache.avro.data.Json;
-import org.apache.avro.compiler.specific.TestSpecificCompiler;
 import org.apache.avro.util.Utf8;
+import org.codehaus.jackson.JsonNode;
+import org.junit.Test;
 
 public class TestSchema {
 
@@ -483,6 +483,27 @@ public class TestSchema {
   }
 
   @Test
+  public void testNestedNullNamespaceReferencing() {
+    Schema inner =
+        Schema.parse("{\"type\":\"record\",\"name\":\"Inner\",\"fields\":[]}");
+    Schema outer = Schema.createRecord("Outer", null, "space", false);
+    outer.setFields(Arrays.asList(new Field("f1", inner, null, null),
+                                  new Field("f2", inner, null, null)));
+    assertEquals(outer, Schema.parse(outer.toString()));
+  }
+
+  @Test
+  public void testNestedNullNamespaceReferencingWithUnion() {
+    Schema inner =
+        Schema.parse("{\"type\":\"record\",\"name\":\"Inner\",\"fields\":[]}");
+    Schema innerUnion = Schema.createUnion(Arrays.asList(inner, 
Schema.create(Type.NULL)));
+    Schema outer = Schema.createRecord("Outer", null, "space", false);
+    outer.setFields(Arrays.asList(new Field("f1", innerUnion, null, null),
+                                  new Field("f2", innerUnion, null, null)));
+    assertEquals(outer, Schema.parse(outer.toString()));
+  }
+
+  @Test
   public void testNestedNonNullNamespace1() throws Exception {
     Schema inner1 = Schema.createEnum("InnerEnum", null, "space", 
Arrays.asList("x"));
     Schema inner2 = 
Schema.parse("{\"type\":\"record\",\"namespace\":\"space\",\"name\":"
@@ -961,4 +982,32 @@ public class TestSchema {
   public void testLockedArrayList10() {
     lockedArrayList().remove(1);
   }
+
+  @Test
+  public void testNames_GetWithInheritedNamespace() {
+    Schema schema = Schema.create(Type.STRING);
+    Schema.Names names = new Schema.Names("space");
+    names.put(new Schema.Name("Name", "space"), schema);
+
+    assertEquals(schema, names.get(new Schema.Name("Name", "space")));
+    assertEquals(schema, names.get("Name"));
+  }
+
+  @Test
+  public void testNames_GetWithNullNamespace() {
+    Schema schema = Schema.create(Type.STRING);
+    Schema.Names names = new Schema.Names("space");
+    names.put(new Schema.Name("Name", ""), schema);
+
+    assertEquals(schema, names.get(new Schema.Name("Name", "")));
+    assertEquals(schema, names.get("Name"));
+  }
+
+  @Test
+  public void testNames_GetNotFound() {
+    Schema.Names names = new Schema.Names("space");
+    names.put(new Schema.Name("Name", "otherspace"), 
Schema.create(Type.STRING));
+
+    assertNull(names.get("Name"));
+  }
 }


Reply via email to