google-nomulus/javatests/google/registry/model/ModelUtilsTest.java
Michael Muller c458c05801 Rename Java packages to use the .google TLD
The dark lord Gosling designed the Java package naming system so that
ownership flows from the DNS system. Since we own the domain name
registry.google, it seems only appropriate that we should use
google.registry as our package name.
2016-05-13 20:04:42 -04:00

141 lines
4.3 KiB
Java

// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// 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 google.registry.model;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.annotation.Id;
import google.registry.testing.AppEngineRule;
import google.registry.testing.ExceptionRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.lang.reflect.Field;
import java.util.Map;
/** Unit tests for {@link ModelUtils}. */
@RunWith(JUnit4.class)
public class ModelUtilsTest {
@Rule
public final ExceptionRule thrown = new ExceptionRule();
@Rule
public AppEngineRule appEngineRule = new AppEngineRule.Builder().build();
/** Test class for reflection methods. */
public static class TestClass extends ImmutableObject implements Buildable {
@Id
String id;
String a;
String b;
/** Note that there is no getter for {@link #b}.*/
public String getId() {
return id;
}
public String getA() {
return a;
}
/** Builder for {@link TestClass}. Note that there is no setter for {@link #a}.*/
public static class Builder extends Buildable.Builder<TestClass> {
protected Builder() {}
protected Builder(TestClass instance) {
super(instance);
}
public Builder setId(String id) {
getInstance().id = id;
return this;
}
public Builder setB(String b) {
getInstance().b = b;
return this;
}
}
@Override
public Builder asBuilder() {
return new Builder(clone(this));
}
}
@Before
public void resetCaches() {
ModelUtils.resetCaches();
}
@Test
public void testGetAllFields() throws Exception {
Map<String, Field> expected = ImmutableMap.<String, Field>of(
"id", TestClass.class.getDeclaredField("id"),
"a", TestClass.class.getDeclaredField("a"),
"b", TestClass.class.getDeclaredField("b"));
// More complicated version of isEqualTo() so that we check for ordering.
assertThat(ModelUtils.getAllFields(TestClass.class).entrySet())
.containsExactlyElementsIn(expected.entrySet())
.inOrder();
// Test again, to make sure we hit the cache.
assertThat(ModelUtils.getAllFields(TestClass.class).entrySet())
.containsExactlyElementsIn(expected.entrySet())
.inOrder();
}
@Test
public void testGetFieldValues() throws Exception {
TestClass testInstance = new TestClass();
testInstance.id = "foo";
testInstance.a = "a";
testInstance.b = "b";
// More complicated version of isEqualTo() so that we check for ordering.
assertThat(ModelUtils.getFieldValues(testInstance).entrySet())
.containsExactlyElementsIn(ImmutableMap.of("id", "foo", "a", "a", "b", "b").entrySet())
.inOrder();
// Test again, to make sure we aren't caching values.
testInstance.id = "bar";
testInstance.a = "1";
testInstance.b = "2";
// More complicated version of isEqualTo() so that we check for ordering.
assertThat(ModelUtils.getFieldValues(testInstance).entrySet())
.containsExactlyElementsIn(ImmutableMap.of("id", "bar", "a", "1", "b", "2").entrySet())
.inOrder();
}
@Test
public void testBuildingResetsHashCode() {
TestClass original = new TestClass();
original.id = "foo";
TestClass cloned = original.asBuilder().setId("bar").build();
assertThat(cloned.hashCode()).isNotEqualTo(original.hashCode());
cloned.id = "foo"; // Violates immutability contract.
// The hashCode is now cached and is stale (but that's the expected behavior).
assertThat(cloned.hashCode()).isNotEqualTo(original.hashCode());
}
}