You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by jb...@apache.org on 2012/02/07 08:23:56 UTC
svn commit: r1241366 - in /avro/trunk: CHANGES.txt
lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java
share/test/schemas/contexts.avdl
Author: jbaldassari
Date: Tue Feb 7 07:23:55 2012
New Revision: 1241366
URL: http://svn.apache.org/viewvc?rev=1241366&view=rev
Log:
AVRO-996. Java: SpecificRecord builder pattern object copy fails with unions in some cases.
Added:
avro/trunk/share/test/schemas/contexts.avdl
Modified:
avro/trunk/CHANGES.txt
avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java
Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1241366&r1=1241365&r2=1241366&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Feb 7 07:23:55 2012
@@ -97,6 +97,9 @@ Avro 1.6.2 (unreleased)
AVRO-1014. C: Check for errors producing JSON output in avrocat.
(Lucas Martin-King via dcreager)
+ AVRO-996. Java: SpecificRecord builder pattern object copy fails
+ with unions in some cases. (scottcarey and jbaldassari)
+
Avro 1.6.1 (8 November 2011)
INCOMPATIBLE CHANGES
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=1241366&r1=1241365&r2=1241366&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 Tue Feb 7 07:23:55 2012
@@ -830,14 +830,8 @@ public class GenericData {
}
return new Utf8(value.toString());
case UNION:
- for (Schema type : schema.getTypes()) {
- if (GenericData.get().validate(type, value)) {
- return deepCopy(type, value);
- }
- }
- throw new AvroRuntimeException(
- "Deep copy failed for schema \"" + schema + "\" and value \"" +
- value + "\"");
+ return deepCopy(
+ schema.getTypes().get(resolveUnion(schema, value)), value);
default:
throw new AvroRuntimeException(
"Deep copy failed for schema \"" + schema + "\" and value \"" +
Modified: avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java?rev=1241366&r1=1241365&r2=1241366&view=diff
==============================================================================
--- avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java (original)
+++ avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java Tue Feb 7 07:23:55 2012
@@ -17,13 +17,22 @@
*/
package org.apache.avro.specific;
+import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import junit.framework.Assert;
+import org.apache.avro.Foo;
+import org.apache.avro.Interop;
+import org.apache.avro.Kind;
+import org.apache.avro.MD5;
+import org.apache.avro.Node;
+import org.apache.avro.ipc.specific.PageView;
import org.apache.avro.ipc.specific.Person;
+import org.apache.avro.ipc.specific.ProductPage;
import org.junit.Ignore;
import org.junit.Test;
@@ -83,6 +92,67 @@ public class TestSpecificRecordBuilder {
Assert.assertTrue(person2.getFriends().isEmpty());
}
+ @Test
+ public void testUnions() {
+ long datetime = 1234L;
+ String product = "widget";
+ PageView p = PageView.newBuilder()
+ .setDatetime(1234L)
+ .setPageContext(ProductPage.newBuilder()
+ .setProduct(product)
+ .build())
+ .build();
+ Assert.assertEquals(datetime, p.getDatetime().longValue());
+ Assert.assertEquals(ProductPage.class, p.getPageContext().getClass());
+ Assert.assertEquals(product, ((ProductPage)p.getPageContext()).getProduct());
+
+ PageView p2 = PageView.newBuilder(p).build();
+
+ Assert.assertEquals(datetime, p2.getDatetime().longValue());
+ Assert.assertEquals(ProductPage.class, p2.getPageContext().getClass());
+ Assert.assertEquals(product, ((ProductPage)p2.getPageContext()).getProduct());
+
+ Assert.assertEquals(p, p2);
+
+ }
+
+ @Test
+ public void testInterop() {
+ Interop interop = Interop.newBuilder()
+ .setArrayField(Arrays.asList(new Double[] { 3.14159265, 6.022 }))
+ .setBoolField(true)
+ .setBytesField(ByteBuffer.allocate(4).put(new byte[] { 3, 2, 1, 0 }))
+ .setDoubleField(1.41421)
+ .setEnumField(Kind.C)
+ .setFixedField(new MD5(
+ new byte[] { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 }))
+ .setFloatField(1.61803f)
+ .setIntField(64)
+ .setLongField(1024)
+ .setMapField(Collections.singletonMap("Foo1", new Foo()))
+ .setRecordField(new Node())
+ .setStringField("MyInterop")
+ .setUnionField(2.71828)
+ .build();
+
+ Interop copy = Interop.newBuilder(interop).build();
+ Assert.assertEquals(interop.getArrayField().size(), copy.getArrayField().size());
+ Assert.assertEquals(interop.getArrayField(), copy.getArrayField());
+ Assert.assertEquals(interop.getBoolField(), copy.getBoolField());
+ Assert.assertEquals(interop.getBytesField().capacity(), copy.getBytesField().capacity());
+ Assert.assertEquals(interop.getBytesField(), copy.getBytesField());
+ Assert.assertEquals(interop.getDoubleField(), copy.getDoubleField());
+ Assert.assertEquals(interop.getEnumField(), copy.getEnumField());
+ Assert.assertEquals(interop.getFixedField(), copy.getFixedField());
+ Assert.assertEquals(interop.getFloatField(), copy.getFloatField());
+ Assert.assertEquals(interop.getIntField(), copy.getIntField());
+ Assert.assertEquals(interop.getLongField(), copy.getLongField());
+ Assert.assertEquals(interop.getMapField(), copy.getMapField());
+ Assert.assertEquals(interop.getRecordField(), copy.getRecordField());
+ Assert.assertEquals(interop.getStringField(), copy.getStringField());
+ Assert.assertEquals(interop.getUnionField(), copy.getUnionField());
+ }
+
@Test(expected=org.apache.avro.AvroRuntimeException.class)
public void attemptToSetNonNullableFieldToNull() {
Person.newBuilder().setName(null);
Added: avro/trunk/share/test/schemas/contexts.avdl
URL: http://svn.apache.org/viewvc/avro/trunk/share/test/schemas/contexts.avdl?rev=1241366&view=auto
==============================================================================
--- avro/trunk/share/test/schemas/contexts.avdl (added)
+++ avro/trunk/share/test/schemas/contexts.avdl Tue Feb 7 07:23:55 2012
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+@version("1.0.5")
+@namespace("org.apache.avro.ipc.specific")
+protocol Contexts {
+ record HomePage {
+ }
+
+ record ProductPage {
+ string product;
+ }
+
+ record CartPage {
+ array<string> productsInCart;
+ }
+
+ record UnknownPage {
+ }
+
+ record PageView {
+ long datetime;
+ union {UnknownPage, HomePage, ProductPage, CartPage} pageContext;
+ }
+
+}