|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.sdk.coders; |
| 19 | + |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | +import static org.hamcrest.Matchers.equalTo; |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertFalse; |
| 24 | +import static org.junit.Assert.assertTrue; |
| 25 | + |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Optional; |
| 31 | +import org.apache.beam.sdk.testing.CoderProperties; |
| 32 | +import org.apache.beam.sdk.transforms.windowing.GlobalWindow; |
| 33 | +import org.apache.beam.sdk.values.TypeDescriptor; |
| 34 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; |
| 35 | +import org.junit.Rule; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.rules.ExpectedException; |
| 38 | +import org.junit.runner.RunWith; |
| 39 | +import org.junit.runners.JUnit4; |
| 40 | + |
| 41 | +/** Unit tests for {@link OptionalCoder}. */ |
| 42 | +@RunWith(JUnit4.class) |
| 43 | +public class OptionalCoderTest { |
| 44 | + |
| 45 | + private static final Coder<Optional<String>> TEST_CODER = OptionalCoder.of(StringUtf8Coder.of()); |
| 46 | + |
| 47 | + private static final List<Optional<String>> TEST_VALUES = |
| 48 | + Arrays.asList( |
| 49 | + Optional.of(""), |
| 50 | + Optional.of("a"), |
| 51 | + Optional.of("13"), |
| 52 | + Optional.of("hello"), |
| 53 | + Optional.empty(), |
| 54 | + Optional.of("a longer string with spaces and all that"), |
| 55 | + Optional.of("a string with a \n newline"), |
| 56 | + Optional.of("スタリング")); |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testDecodeEncodeContentsInSameOrder() throws Exception { |
| 60 | + for (Optional<String> value : TEST_VALUES) { |
| 61 | + CoderProperties.coderDecodeEncodeEqual(TEST_CODER, value); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testCoderSerializable() throws Exception { |
| 67 | + CoderProperties.coderSerializable(TEST_CODER); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testCoderIsSerializableWithWellKnownCoderType() throws Exception { |
| 72 | + CoderProperties.coderSerializable(OptionalCoder.of(GlobalWindow.Coder.INSTANCE)); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Generated data to check that the wire format has not changed. To regenerate, see {@code |
| 77 | + * PrintBase64Encodings}. |
| 78 | + * |
| 79 | + * @see PrintBase64Encodings |
| 80 | + */ |
| 81 | + private static final List<String> TEST_ENCODINGS = |
| 82 | + Arrays.asList( |
| 83 | + "AQA", |
| 84 | + "AQFh", |
| 85 | + "AQIxMw", |
| 86 | + "AQVoZWxsbw", |
| 87 | + "AA", |
| 88 | + "AShhIGxvbmdlciBzdHJpbmcgd2l0aCBzcGFjZXMgYW5kIGFsbCB0aGF0", |
| 89 | + "ARlhIHN0cmluZyB3aXRoIGEgCiBuZXdsaW5l", |
| 90 | + "AQ_jgrnjgr_jg6rjg7PjgrA"); |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testWireFormatEncode() throws Exception { |
| 94 | + CoderProperties.coderEncodesBase64(TEST_CODER, TEST_VALUES, TEST_ENCODINGS); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testEncodedSize() throws Exception { |
| 99 | + OptionalCoder<Double> coder = OptionalCoder.of(DoubleCoder.of()); |
| 100 | + assertEquals(1, coder.getEncodedElementByteSize(Optional.empty())); |
| 101 | + assertEquals(9, coder.getEncodedElementByteSize(Optional.of(5.0))); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testEncodedSizeNested() throws Exception { |
| 106 | + OptionalCoder<String> varLenCoder = OptionalCoder.of(StringUtf8Coder.of()); |
| 107 | + assertEquals(1, varLenCoder.getEncodedElementByteSize(Optional.empty())); |
| 108 | + assertEquals(6, varLenCoder.getEncodedElementByteSize(Optional.of("spam"))); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testObserverIsCheap() throws Exception { |
| 113 | + OptionalCoder<Double> coder = OptionalCoder.of(DoubleCoder.of()); |
| 114 | + assertTrue(coder.isRegisterByteSizeObserverCheap(Optional.of(5.0))); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testObserverIsNotCheap() throws Exception { |
| 119 | + OptionalCoder<List<String>> coder = OptionalCoder.of(ListCoder.of(StringUtf8Coder.of())); |
| 120 | + assertFalse(coder.isRegisterByteSizeObserverCheap(Optional.of(ImmutableList.of("hi", "test")))); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testObserverIsAlwaysCheapForEmptyValues() throws Exception { |
| 125 | + OptionalCoder<List<String>> coder = OptionalCoder.of(ListCoder.of(StringUtf8Coder.of())); |
| 126 | + assertTrue(coder.isRegisterByteSizeObserverCheap(Optional.empty())); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testStructuralValueConsistentWithEquals() throws Exception { |
| 131 | + CoderProperties.structuralValueConsistentWithEquals( |
| 132 | + TEST_CODER, Optional.empty(), Optional.empty()); |
| 133 | + } |
| 134 | + |
| 135 | + @Rule public ExpectedException thrown = ExpectedException.none(); |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testDecodingError() throws Exception { |
| 139 | + thrown.expect(CoderException.class); |
| 140 | + thrown.expectMessage( |
| 141 | + equalTo("NullableCoder expects either a byte valued 0 (null) or 1 (present), got 5")); |
| 142 | + |
| 143 | + InputStream input = new ByteArrayInputStream(new byte[] {5}); |
| 144 | + TEST_CODER.decode(input); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testEncodedTypeDescriptor() throws Exception { |
| 149 | + TypeDescriptor<Optional<String>> expectedTypeDescriptor = |
| 150 | + new TypeDescriptor<Optional<String>>() {}; |
| 151 | + assertThat(TEST_CODER.getEncodedTypeDescriptor(), equalTo(expectedTypeDescriptor)); |
| 152 | + } |
| 153 | +} |
0 commit comments