forked from GoogleCloudPlatform/DataflowJavaSDK
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Coder.Context equality and hashCode
This only worked if you depended on the instances directly and didn't construct your own. Also add Coder.Context#toString() ----Release Notes---- [] ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=112736780
- Loading branch information
1 parent
8ca76b6
commit 5f31b8b
Showing
2 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
sdk/src/test/java/com/google/cloud/dataflow/sdk/coders/CoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (C) 2016 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.contains; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import com.google.cloud.dataflow.sdk.coders.Coder.Context; | ||
import com.google.cloud.dataflow.sdk.coders.Coder.NonDeterministicException; | ||
|
||
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.util.Collections; | ||
|
||
/** Tests for constructs defined within {@link Coder}. */ | ||
@RunWith(JUnit4.class) | ||
public class CoderTest { | ||
@Rule public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
@Test | ||
public void testContextEqualsAndHashCode() { | ||
assertEquals(Context.NESTED, new Context(false)); | ||
assertEquals(Context.OUTER, new Context(true)); | ||
assertNotEquals(Context.NESTED, Context.OUTER); | ||
|
||
assertEquals(Context.NESTED.hashCode(), new Context(false).hashCode()); | ||
assertEquals(Context.OUTER.hashCode(), new Context(true).hashCode()); | ||
// Even though this isn't strictly required by the hashCode contract, | ||
// we still want this to be true. | ||
assertNotEquals(Context.NESTED.hashCode(), Context.OUTER.hashCode()); | ||
} | ||
|
||
@Test | ||
public void testContextToString() { | ||
assertEquals("Context{NESTED}", Context.NESTED.toString()); | ||
assertEquals("Context{OUTER}", Context.OUTER.toString()); | ||
} | ||
|
||
@Test | ||
public void testNonDeterministicExcpetionRequiresReason() { | ||
expectedException.expect(IllegalArgumentException.class); | ||
expectedException.expectMessage("Reasons must not be empty"); | ||
new NonDeterministicException(VoidCoder.of(), Collections.<String>emptyList()); | ||
} | ||
|
||
@Test | ||
public void testNonDeterministicException() { | ||
NonDeterministicException rootCause = | ||
new NonDeterministicException(VoidCoder.of(), "Root Cause"); | ||
NonDeterministicException exception = | ||
new NonDeterministicException(StringUtf8Coder.of(), "Problem", rootCause); | ||
assertEquals(rootCause, exception.getCause()); | ||
assertThat(exception.getReasons(), contains("Problem")); | ||
assertThat(exception.toString(), containsString("Problem")); | ||
assertThat(exception.toString(), containsString("is not deterministic")); | ||
} | ||
} | ||
|