Skip to content
This repository was archived by the owner on Nov 11, 2022. It is now read-only.

Commit 169e340

Browse files
lukecwikdavorbonaci
authored andcommitted
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
1 parent c41d154 commit 169e340

File tree

2 files changed

+99
-1
lines changed
  • sdk/src

2 files changed

+99
-1
lines changed

sdk/src/main/java/com/google/cloud/dataflow/sdk/coders/Coder.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.google.cloud.dataflow.sdk.util.CloudObject;
2222
import com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver;
2323
import com.google.common.base.Joiner;
24+
import com.google.common.base.MoreObjects;
25+
import com.google.common.base.Objects;
2426
import com.google.common.base.Preconditions;
2527

2628
import java.io.IOException;
@@ -30,7 +32,6 @@
3032
import java.util.Arrays;
3133
import java.util.Collection;
3234
import java.util.List;
33-
import java.util.Objects;
3435

3536
import javax.annotation.Nullable;
3637

@@ -92,6 +93,25 @@ public Context(boolean isWholeStream) {
9293
public Context nested() {
9394
return NESTED;
9495
}
96+
97+
@Override
98+
public boolean equals(Object obj) {
99+
if (!(obj instanceof Context)) {
100+
return false;
101+
}
102+
return Objects.equal(isWholeStream, ((Context) obj).isWholeStream);
103+
}
104+
105+
@Override
106+
public int hashCode() {
107+
return Objects.hashCode(isWholeStream);
108+
}
109+
110+
@Override
111+
public String toString() {
112+
return MoreObjects.toStringHelper(Context.class)
113+
.addValue(isWholeStream ? "OUTER" : "NESTED").toString();
114+
}
95115
}
96116

97117
/**
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)