Skip to content

Commit

Permalink
Fix Coder.Context equality and hashCode
Browse files Browse the repository at this point in the history
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
lukecwik authored and davorbonaci committed Feb 2, 2016
1 parent 8ca76b6 commit 5f31b8b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.cloud.dataflow.sdk.util.CloudObject;
import com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;

import java.io.IOException;
Expand All @@ -30,7 +32,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -92,6 +93,25 @@ public Context(boolean isWholeStream) {
public Context nested() {
return NESTED;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Context)) {
return false;
}
return Objects.equal(isWholeStream, ((Context) obj).isWholeStream);
}

@Override
public int hashCode() {
return Objects.hashCode(isWholeStream);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(Context.class)
.addValue(isWholeStream ? "OUTER" : "NESTED").toString();
}
}

/**
Expand Down
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"));
}
}

0 comments on commit 5f31b8b

Please sign in to comment.