Return-Path: X-Original-To: apmail-beam-commits-archive@minotaur.apache.org Delivered-To: apmail-beam-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DE094195D8 for ; Thu, 24 Mar 2016 02:47:30 +0000 (UTC) Received: (qmail 4169 invoked by uid 500); 24 Mar 2016 02:47:30 -0000 Delivered-To: apmail-beam-commits-archive@beam.apache.org Received: (qmail 4116 invoked by uid 500); 24 Mar 2016 02:47:30 -0000 Mailing-List: contact commits-help@beam.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@beam.incubator.apache.org Delivered-To: mailing list commits@beam.incubator.apache.org Received: (qmail 4107 invoked by uid 99); 24 Mar 2016 02:47:30 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Mar 2016 02:47:30 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 41C9B1A49DD for ; Thu, 24 Mar 2016 02:47:30 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.021 X-Spam-Level: X-Spam-Status: No, score=-4.021 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.001] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id zCwNE07Vsv0T for ; Thu, 24 Mar 2016 02:47:26 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with SMTP id 1973C5FACB for ; Thu, 24 Mar 2016 02:47:26 +0000 (UTC) Received: (qmail 3827 invoked by uid 99); 24 Mar 2016 02:47:25 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Mar 2016 02:47:25 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2FE77E38DC; Thu, 24 Mar 2016 02:47:25 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: dhalperi@apache.org To: commits@beam.incubator.apache.org Date: Thu, 24 Mar 2016 02:47:25 -0000 Message-Id: <0c1667d8252646c1acedf8c54c1ba4ca@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [01/67] [partial] incubator-beam git commit: Directory reorganization Repository: incubator-beam Updated Branches: refs/heads/master 9f8dd182c -> 257a7a6be http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/7bef2b7e/sdk/src/test/java/com/google/cloud/dataflow/sdk/coders/CoderRegistryTest.java ---------------------------------------------------------------------- diff --git a/sdk/src/test/java/com/google/cloud/dataflow/sdk/coders/CoderRegistryTest.java b/sdk/src/test/java/com/google/cloud/dataflow/sdk/coders/CoderRegistryTest.java deleted file mode 100644 index 2f350b2..0000000 --- a/sdk/src/test/java/com/google/cloud/dataflow/sdk/coders/CoderRegistryTest.java +++ /dev/null @@ -1,521 +0,0 @@ -/* - * Copyright (C) 2015 Google Inc. - * - * Licensed 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. - */ - -package com.google.cloud.dataflow.sdk.coders; - -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertEquals; - -import com.google.cloud.dataflow.sdk.Pipeline; -import com.google.cloud.dataflow.sdk.coders.CoderRegistry.IncompatibleCoderException; -import com.google.cloud.dataflow.sdk.coders.Proto2CoderTestMessages.MessageA; -import com.google.cloud.dataflow.sdk.coders.protobuf.ProtoCoder; -import com.google.cloud.dataflow.sdk.testing.TestPipeline; -import com.google.cloud.dataflow.sdk.transforms.Create; -import com.google.cloud.dataflow.sdk.transforms.DoFn; -import com.google.cloud.dataflow.sdk.transforms.PTransform; -import com.google.cloud.dataflow.sdk.transforms.ParDo; -import com.google.cloud.dataflow.sdk.util.CloudObject; -import com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver; -import com.google.cloud.dataflow.sdk.values.KV; -import com.google.cloud.dataflow.sdk.values.PCollection; -import com.google.cloud.dataflow.sdk.values.TypeDescriptor; -import com.google.common.collect.ImmutableList; -import com.google.protobuf.Duration; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Serializable; -import java.lang.reflect.Type; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Tests for CoderRegistry. - */ -@RunWith(JUnit4.class) -public class CoderRegistryTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - public static CoderRegistry getStandardRegistry() { - CoderRegistry registry = new CoderRegistry(); - registry.registerStandardCoders(); - return registry; - } - - private static class SerializableClass implements Serializable { - } - - private static class NotSerializableClass { } - - @Test - public void testSerializableFallbackCoderProvider() throws Exception { - CoderRegistry registry = getStandardRegistry(); - registry.setFallbackCoderProvider(SerializableCoder.PROVIDER); - Coder serializableCoder = registry.getDefaultCoder(SerializableClass.class); - - assertEquals(serializableCoder, SerializableCoder.of(SerializableClass.class)); - } - - @Test - public void testProtoCoderFallbackCoderProvider() throws Exception { - CoderRegistry registry = getStandardRegistry(); - - // MessageA is a Protocol Buffers test message with syntax 2 - assertEquals(registry.getDefaultCoder(MessageA.class), ProtoCoder.of(MessageA.class)); - - // Duration is a Protocol Buffers default type with syntax 3 - assertEquals(registry.getDefaultCoder(Duration.class), ProtoCoder.of(Duration.class)); - } - - @Test - public void testAvroFallbackCoderProvider() throws Exception { - CoderRegistry registry = getStandardRegistry(); - registry.setFallbackCoderProvider(AvroCoder.PROVIDER); - Coder avroCoder = registry.getDefaultCoder(NotSerializableClass.class); - - assertEquals(avroCoder, AvroCoder.of(NotSerializableClass.class)); - } - - @Test - public void testRegisterInstantiatedCoder() throws Exception { - CoderRegistry registry = new CoderRegistry(); - registry.registerCoder(MyValue.class, MyValueCoder.of()); - assertEquals(registry.getDefaultCoder(MyValue.class), MyValueCoder.of()); - } - - @SuppressWarnings("rawtypes") // this class exists to fail a test because of its rawtypes - private class MyListCoder extends DeterministicStandardCoder { - @Override - public void encode(List value, OutputStream outStream, Context context) - throws CoderException, IOException { - } - - @Override - public List decode(InputStream inStream, Context context) - throws CoderException, IOException { - return Collections.emptyList(); - } - - @Override - public List> getCoderArguments() { - return Collections.emptyList(); - } - } - - @Test - public void testRegisterInstantiatedCoderInvalidRawtype() throws Exception { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("may not be used with unspecialized generic classes"); - CoderRegistry registry = new CoderRegistry(); - registry.registerCoder(List.class, new MyListCoder()); - } - - @Test - public void testSimpleDefaultCoder() throws Exception { - CoderRegistry registry = getStandardRegistry(); - assertEquals(StringUtf8Coder.of(), registry.getDefaultCoder(String.class)); - } - - @Test - public void testSimpleUnknownDefaultCoder() throws Exception { - CoderRegistry registry = getStandardRegistry(); - thrown.expect(CannotProvideCoderException.class); - thrown.expectMessage(allOf( - containsString(UnknownType.class.getCanonicalName()), - containsString("No CoderFactory has been registered"), - containsString("does not have a @DefaultCoder annotation"), - containsString("does not implement Serializable"))); - registry.getDefaultCoder(UnknownType.class); - } - - @Test - public void testParameterizedDefaultListCoder() throws Exception { - CoderRegistry registry = getStandardRegistry(); - TypeDescriptor> listToken = new TypeDescriptor>() {}; - assertEquals(ListCoder.of(VarIntCoder.of()), - registry.getDefaultCoder(listToken)); - - registry.registerCoder(MyValue.class, MyValueCoder.class); - TypeDescriptor>> kvToken = - new TypeDescriptor>>() {}; - assertEquals(KvCoder.of(StringUtf8Coder.of(), - ListCoder.of(MyValueCoder.of())), - registry.getDefaultCoder(kvToken)); - - } - - @Test - public void testParameterizedDefaultMapCoder() throws Exception { - CoderRegistry registry = getStandardRegistry(); - TypeDescriptor> mapToken = new TypeDescriptor>() {}; - assertEquals(MapCoder.of(VarIntCoder.of(), StringUtf8Coder.of()), - registry.getDefaultCoder(mapToken)); - } - - @Test - public void testParameterizedDefaultNestedMapCoder() throws Exception { - CoderRegistry registry = getStandardRegistry(); - TypeDescriptor>> mapToken = - new TypeDescriptor>>() {}; - assertEquals( - MapCoder.of(VarIntCoder.of(), MapCoder.of(StringUtf8Coder.of(), DoubleCoder.of())), - registry.getDefaultCoder(mapToken)); - } - - @Test - public void testParameterizedDefaultSetCoder() throws Exception { - CoderRegistry registry = getStandardRegistry(); - TypeDescriptor> setToken = new TypeDescriptor>() {}; - assertEquals(SetCoder.of(VarIntCoder.of()), registry.getDefaultCoder(setToken)); - } - - @Test - public void testParameterizedDefaultNestedSetCoder() throws Exception { - CoderRegistry registry = getStandardRegistry(); - TypeDescriptor>> setToken = new TypeDescriptor>>() {}; - assertEquals(SetCoder.of(SetCoder.of(VarIntCoder.of())), registry.getDefaultCoder(setToken)); - } - - @Test - public void testParameterizedDefaultCoderUnknown() throws Exception { - CoderRegistry registry = getStandardRegistry(); - TypeDescriptor> listUnknownToken = new TypeDescriptor>() {}; - - thrown.expect(CannotProvideCoderException.class); - thrown.expectMessage(String.format( - "Cannot provide coder for parameterized type %s: Unable to provide a default Coder for %s", - listUnknownToken, - UnknownType.class.getCanonicalName())); - - registry.getDefaultCoder(listUnknownToken); - } - - @Test - public void testTypeParameterInferenceForward() throws Exception { - CoderRegistry registry = getStandardRegistry(); - MyGenericClass> instance = - new MyGenericClass>() {}; - - Coder bazCoder = registry.getDefaultCoder( - instance.getClass(), - MyGenericClass.class, - Collections.>singletonMap( - TypeDescriptor.of(MyGenericClass.class).getTypeParameter("FooT"), MyValueCoder.of()), - TypeDescriptor.of(MyGenericClass.class).getTypeParameter("BazT")); - - assertEquals(ListCoder.of(MyValueCoder.of()), bazCoder); - } - - @Test - public void testTypeParameterInferenceBackward() throws Exception { - CoderRegistry registry = getStandardRegistry(); - MyGenericClass> instance = - new MyGenericClass>() {}; - - Coder fooCoder = registry.getDefaultCoder( - instance.getClass(), - MyGenericClass.class, - Collections.>singletonMap( - TypeDescriptor.of(MyGenericClass.class).getTypeParameter("BazT"), - ListCoder.of(MyValueCoder.of())), - TypeDescriptor.of(MyGenericClass.class).getTypeParameter("FooT")); - - assertEquals(MyValueCoder.of(), fooCoder); - } - - @Test - public void testGetDefaultCoderFromIntegerValue() throws Exception { - CoderRegistry registry = getStandardRegistry(); - Integer i = 13; - Coder coder = registry.getDefaultCoder(i); - assertEquals(VarIntCoder.of(), coder); - } - - @Test - public void testGetDefaultCoderFromNullValue() throws Exception { - CoderRegistry registry = getStandardRegistry(); - assertEquals(VoidCoder.of(), registry.getDefaultCoder((Void) null)); - } - - @Test - public void testGetDefaultCoderFromKvValue() throws Exception { - CoderRegistry registry = getStandardRegistry(); - KV kv = KV.of(13, "hello"); - Coder> coder = registry.getDefaultCoder(kv); - assertEquals(KvCoder.of(VarIntCoder.of(), StringUtf8Coder.of()), - coder); - } - - @Test - public void testGetDefaultCoderFromKvNullValue() throws Exception { - CoderRegistry registry = getStandardRegistry(); - KV kv = KV.of((Void) null, (Void) null); - assertEquals(KvCoder.of(VoidCoder.of(), VoidCoder.of()), - registry.getDefaultCoder(kv)); - } - - @Test - public void testGetDefaultCoderFromNestedKvValue() throws Exception { - CoderRegistry registry = getStandardRegistry(); - KV>> kv = KV.of(13, KV.of(17L, KV.of("hello", "goodbye"))); - Coder>>> coder = registry.getDefaultCoder(kv); - assertEquals( - KvCoder.of(VarIntCoder.of(), - KvCoder.of(VarLongCoder.of(), - KvCoder.of(StringUtf8Coder.of(), StringUtf8Coder.of()))), - coder); - } - - @Test - public void testTypeCompatibility() throws Exception { - CoderRegistry.verifyCompatible(BigEndianIntegerCoder.of(), Integer.class); - CoderRegistry.verifyCompatible( - ListCoder.of(BigEndianIntegerCoder.of()), - new TypeDescriptor>() {}.getType()); - } - - @Test - public void testIntVersusStringIncompatibility() throws Exception { - thrown.expect(IncompatibleCoderException.class); - thrown.expectMessage("not assignable"); - CoderRegistry.verifyCompatible(BigEndianIntegerCoder.of(), String.class); - } - - private static class TooManyComponentCoders extends ListCoder { - public TooManyComponentCoders(Coder actualComponentCoder) { - super(actualComponentCoder); - } - - @Override - public List> getCoderArguments() { - return ImmutableList.>builder() - .addAll(super.getCoderArguments()) - .add(BigEndianLongCoder.of()) - .build(); - } - } - - @Test - public void testTooManyCoderArguments() throws Exception { - thrown.expect(IncompatibleCoderException.class); - thrown.expectMessage("type parameters"); - thrown.expectMessage("less than the number of coder arguments"); - CoderRegistry.verifyCompatible( - new TooManyComponentCoders<>(BigEndianIntegerCoder.of()), List.class); - } - - @Test - public void testComponentIncompatibility() throws Exception { - thrown.expect(IncompatibleCoderException.class); - thrown.expectMessage("component coder is incompatible"); - CoderRegistry.verifyCompatible( - ListCoder.of(BigEndianIntegerCoder.of()), - new TypeDescriptor>() {}.getType()); - } - - @Test - public void testDefaultCoderAnnotationGenericRawtype() throws Exception { - CoderRegistry registry = new CoderRegistry(); - registry.registerStandardCoders(); - assertEquals( - registry.getDefaultCoder(MySerializableGeneric.class), - SerializableCoder.of(MySerializableGeneric.class)); - } - - @Test - public void testDefaultCoderAnnotationGeneric() throws Exception { - CoderRegistry registry = new CoderRegistry(); - registry.registerStandardCoders(); - assertEquals( - registry.getDefaultCoder(new TypeDescriptor>() {}), - SerializableCoder.of(MySerializableGeneric.class)); - } - - private static class PTransformOutputingMySerializableGeneric - extends PTransform, PCollection>>> { - - private class OutputDoFn extends DoFn>> { - @Override - public void processElement(ProcessContext c) { } - } - - @Override - public PCollection>> - apply(PCollection input) { - return input.apply(ParDo.of(new OutputDoFn())); - } - } - - /** - * Tests that the error message for a type variable includes a mention of where the - * type variable was declared. - */ - @Test - public void testTypeVariableErrorMessage() throws Exception { - CoderRegistry registry = new CoderRegistry(); - - thrown.expect(CannotProvideCoderException.class); - thrown.expectMessage(allOf( - containsString("TestGenericT"), - containsString("erasure"), - containsString("com.google.cloud.dataflow.sdk.coders.CoderRegistryTest$TestGenericClass"))); - registry.getDefaultCoder(TypeDescriptor.of( - TestGenericClass.class.getTypeParameters()[0])); - } - - private static class TestGenericClass { } - - /** - * In-context test that assures the functionality tested in - * {@link #testDefaultCoderAnnotationGeneric} is invoked in the right ways. - */ - @Test - public void testSpecializedButIgnoredGenericInPipeline() throws Exception { - Pipeline pipeline = TestPipeline.create(); - - pipeline - .apply(Create.of("hello", "goodbye")) - .apply(new PTransformOutputingMySerializableGeneric()); - - pipeline.run(); - } - - private static class GenericOutputMySerializedGeneric - extends PTransform< - PCollection, - PCollection>>> { - - private class OutputDoFn extends DoFn>> { - @Override - public void processElement(ProcessContext c) { } - } - - @Override - public PCollection>> - apply(PCollection input) { - return input.apply(ParDo.of(new OutputDoFn())); - } - } - - @Test - public void testIgnoredGenericInPipeline() throws Exception { - Pipeline pipeline = TestPipeline.create(); - - pipeline - .apply(Create.of("hello", "goodbye")) - .apply(new GenericOutputMySerializedGeneric()); - - pipeline.run(); - } - - private static class MyGenericClass { } - - private static class MyValue { } - - private static class MyValueCoder implements Coder { - - private static final MyValueCoder INSTANCE = new MyValueCoder(); - - public static MyValueCoder of() { - return INSTANCE; - } - - @SuppressWarnings("unused") - public static List getInstanceComponents( - @SuppressWarnings("unused") MyValue exampleValue) { - return Arrays.asList(); - } - - @Override - public void encode(MyValue value, OutputStream outStream, Context context) - throws CoderException, IOException { - } - - @Override - public MyValue decode(InputStream inStream, Context context) - throws CoderException, IOException { - return new MyValue(); - } - - @Override - public List> getCoderArguments() { - return null; - } - - @Override - public CloudObject asCloudObject() { - return null; - } - - @Override - public void verifyDeterministic() { } - - @Override - public boolean consistentWithEquals() { - return true; - } - - @Override - public Object structuralValue(MyValue value) { - return value; - } - - @Override - public boolean isRegisterByteSizeObserverCheap(MyValue value, Context context) { - return true; - } - - @Override - public void registerByteSizeObserver( - MyValue value, ElementByteSizeObserver observer, Context context) - throws Exception { - observer.update(0L); - } - - @Override - public String getEncodingId() { - return getClass().getName(); - } - - @Override - public Collection getAllowedEncodings() { - return Collections.singletonList(getEncodingId()); - } - } - - private static class UnknownType { } - - @DefaultCoder(SerializableCoder.class) - private static class MySerializableGeneric implements Serializable { - @SuppressWarnings("unused") - private T foo; - } -}