|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.dataflow.sdk.coders; |
| 17 | + |
| 18 | +import static org.hamcrest.Matchers.contains; |
| 19 | +import static org.hamcrest.Matchers.containsString; |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertNotEquals; |
| 22 | +import static org.junit.Assert.assertThat; |
| 23 | + |
| 24 | +import com.google.cloud.dataflow.sdk.coders.Coder.Context; |
| 25 | +import com.google.cloud.dataflow.sdk.coders.Coder.NonDeterministicException; |
| 26 | + |
| 27 | +import org.junit.Rule; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.rules.ExpectedException; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.junit.runners.JUnit4; |
| 32 | + |
| 33 | +import java.util.Collections; |
| 34 | + |
| 35 | +/** Tests for constructs defined within {@link Coder}. */ |
| 36 | +@RunWith(JUnit4.class) |
| 37 | +public class CoderTest { |
| 38 | + @Rule public ExpectedException expectedException = ExpectedException.none(); |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testContextEqualsAndHashCode() { |
| 42 | + assertEquals(Context.NESTED, new Context(false)); |
| 43 | + assertEquals(Context.OUTER, new Context(true)); |
| 44 | + assertNotEquals(Context.NESTED, Context.OUTER); |
| 45 | + |
| 46 | + assertEquals(Context.NESTED.hashCode(), new Context(false).hashCode()); |
| 47 | + assertEquals(Context.OUTER.hashCode(), new Context(true).hashCode()); |
| 48 | + // Even though this isn't strictly required by the hashCode contract, |
| 49 | + // we still want this to be true. |
| 50 | + assertNotEquals(Context.NESTED.hashCode(), Context.OUTER.hashCode()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testContextToString() { |
| 55 | + assertEquals("Context{NESTED}", Context.NESTED.toString()); |
| 56 | + assertEquals("Context{OUTER}", Context.OUTER.toString()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testNonDeterministicExcpetionRequiresReason() { |
| 61 | + expectedException.expect(IllegalArgumentException.class); |
| 62 | + expectedException.expectMessage("Reasons must not be empty"); |
| 63 | + new NonDeterministicException(VoidCoder.of(), Collections.<String>emptyList()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testNonDeterministicException() { |
| 68 | + NonDeterministicException rootCause = |
| 69 | + new NonDeterministicException(VoidCoder.of(), "Root Cause"); |
| 70 | + NonDeterministicException exception = |
| 71 | + new NonDeterministicException(StringUtf8Coder.of(), "Problem", rootCause); |
| 72 | + assertEquals(rootCause, exception.getCause()); |
| 73 | + assertThat(exception.getReasons(), contains("Problem")); |
| 74 | + assertThat(exception.toString(), containsString("Problem")); |
| 75 | + assertThat(exception.toString(), containsString("is not deterministic")); |
| 76 | + } |
| 77 | +} |
| 78 | + |
0 commit comments