From 9ab68613a0e820a7d94d0922de0708c1dd42ea15 Mon Sep 17 00:00:00 2001 From: jianglai Date: Fri, 17 Nov 2017 08:18:43 -0800 Subject: [PATCH] Replace FailureStrategy with FailureMetadata in gtld's Subjects Also changed truth.SubjectFactory to truth.Subject.Factory (plain renaming) and use method reference instead of anonymous class to create the factory when applicable. FailureMetadata, an opaque object to its users, is introduced to replace FailureStrategy in in custom Subject in order to resolve some existing flaws of FailureStrategy as well as enable new features to be added to Truth. New API is available in Truth-0.36, if there is a build/pom.xml, it's also updated to use this version. More information: See [] ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=176110788 --- .../testing/AbstractDomainBaseSubject.java | 6 ++-- .../testing/AbstractEppResourceSubject.java | 6 ++-- .../testing/ContactResourceSubject.java | 12 +++---- .../testing/DomainApplicationSubject.java | 12 +++---- .../testing/DomainResourceSubject.java | 12 +++---- .../registry/testing/EppExceptionSubject.java | 13 +++---- .../registry/testing/EppMetricSubject.java | 20 ++++------- .../testing/GenericEppResourceSubject.java | 18 +++------- .../registry/testing/HistoryEntrySubject.java | 12 +++---- .../registry/testing/HostResourceSubject.java | 12 +++---- .../google/registry/testing/LogsSubject.java | 12 +++---- .../testing/ReflectiveSubjectFactory.java | 36 ------------------- 12 files changed, 44 insertions(+), 127 deletions(-) delete mode 100644 javatests/google/registry/testing/ReflectiveSubjectFactory.java diff --git a/javatests/google/registry/testing/AbstractDomainBaseSubject.java b/javatests/google/registry/testing/AbstractDomainBaseSubject.java index 68a4253ed..f4a1784de 100644 --- a/javatests/google/registry/testing/AbstractDomainBaseSubject.java +++ b/javatests/google/registry/testing/AbstractDomainBaseSubject.java @@ -15,7 +15,7 @@ package google.registry.testing; import com.google.common.collect.ImmutableSet; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import google.registry.model.domain.DomainBase; import google.registry.model.domain.DomainResource; import google.registry.model.domain.launch.LaunchNotice; @@ -29,8 +29,8 @@ public abstract class AbstractDomainBaseSubject > extends AbstractEppResourceSubject { - public AbstractDomainBaseSubject(FailureStrategy strategy, T subject) { - super(strategy, subject); + public AbstractDomainBaseSubject(FailureMetadata failureMetadata, T subject) { + super(failureMetadata, subject); } public And hasFullyQualifiedDomainName(String fullyQualifiedDomainName) { diff --git a/javatests/google/registry/testing/AbstractEppResourceSubject.java b/javatests/google/registry/testing/AbstractEppResourceSubject.java index 93c9477a9..b5d8d9866 100644 --- a/javatests/google/registry/testing/AbstractEppResourceSubject.java +++ b/javatests/google/registry/testing/AbstractEppResourceSubject.java @@ -22,7 +22,7 @@ import static google.registry.testing.HistoryEntrySubject.assertAboutHistoryEntr import static google.registry.util.DiffUtils.prettyPrintEntityDeepDiff; import com.google.common.collect.ImmutableSet; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.Subject; import google.registry.model.EppResource; import google.registry.model.ImmutableObject; @@ -39,8 +39,8 @@ import org.joda.time.DateTime; abstract class AbstractEppResourceSubject > extends Subject { - public AbstractEppResourceSubject(FailureStrategy strategy, T subject) { - super(strategy, checkNotNull(subject)); + public AbstractEppResourceSubject(FailureMetadata failureMetadata, T subject) { + super(failureMetadata, checkNotNull(subject)); } private List getHistoryEntries() { diff --git a/javatests/google/registry/testing/ContactResourceSubject.java b/javatests/google/registry/testing/ContactResourceSubject.java index 5e9acfec6..adb37d2af 100644 --- a/javatests/google/registry/testing/ContactResourceSubject.java +++ b/javatests/google/registry/testing/ContactResourceSubject.java @@ -17,7 +17,7 @@ package google.registry.testing; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.truth.Truth.assertAbout; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.SimpleSubjectBuilder; import google.registry.model.contact.ContactResource; import google.registry.model.contact.PostalInfo; @@ -29,12 +29,8 @@ import org.joda.time.DateTime; public final class ContactResourceSubject extends AbstractEppResourceSubject { - /** A factory for instances of this subject. */ - private static class SubjectFactory - extends ReflectiveSubjectFactory{} - - public ContactResourceSubject(FailureStrategy strategy, ContactResource subject) { - super(strategy, checkNotNull(subject)); + public ContactResourceSubject(FailureMetadata failureMetadata, ContactResource subject) { + super(failureMetadata, checkNotNull(subject)); } public And hasLocalizedPostalInfo(PostalInfo postalInfo) { @@ -148,6 +144,6 @@ public final class ContactResourceSubject public static SimpleSubjectBuilder assertAboutContacts() { - return assertAbout(new SubjectFactory()); + return assertAbout(ContactResourceSubject::new); } } diff --git a/javatests/google/registry/testing/DomainApplicationSubject.java b/javatests/google/registry/testing/DomainApplicationSubject.java index d81321d1d..0e9a80ecd 100644 --- a/javatests/google/registry/testing/DomainApplicationSubject.java +++ b/javatests/google/registry/testing/DomainApplicationSubject.java @@ -19,7 +19,7 @@ import static com.google.common.truth.Truth.assertAbout; import static com.google.common.truth.Truth.assertThat; import com.google.common.collect.ImmutableSet; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.SimpleSubjectBuilder; import google.registry.model.domain.DomainApplication; import google.registry.model.domain.Period; @@ -76,16 +76,12 @@ public final class DomainApplicationSubject return andChainer(); } - /** A factory for instances of this subject. */ - private static class SubjectFactory - extends ReflectiveSubjectFactory{} - - public DomainApplicationSubject(FailureStrategy strategy, DomainApplication subject) { - super(strategy, checkNotNull(subject)); + public DomainApplicationSubject(FailureMetadata failureMetadata, DomainApplication subject) { + super(failureMetadata, checkNotNull(subject)); } public static SimpleSubjectBuilder assertAboutApplications() { - return assertAbout(new SubjectFactory()); + return assertAbout(DomainApplicationSubject::new); } } diff --git a/javatests/google/registry/testing/DomainResourceSubject.java b/javatests/google/registry/testing/DomainResourceSubject.java index e2fca5afd..26164d19c 100644 --- a/javatests/google/registry/testing/DomainResourceSubject.java +++ b/javatests/google/registry/testing/DomainResourceSubject.java @@ -17,7 +17,7 @@ package google.registry.testing; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.truth.Truth.assertAbout; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.SimpleSubjectBuilder; import google.registry.model.domain.DomainResource; import google.registry.testing.TruthChainer.And; @@ -28,10 +28,6 @@ import org.joda.time.DateTime; public final class DomainResourceSubject extends AbstractDomainBaseSubject { - /** A factory for instances of this subject. */ - private static class SubjectFactory - extends ReflectiveSubjectFactory{} - public And hasRegistrationExpirationTime(DateTime expiration) { if (!Objects.equals(actual().getRegistrationExpirationTime(), expiration)) { failWithBadResults( @@ -74,11 +70,11 @@ public final class DomainResourceSubject return hasValue(smdId, actual().getSmdId(), "has smdId"); } - public DomainResourceSubject(FailureStrategy strategy, DomainResource subject) { - super(strategy, checkNotNull(subject)); + public DomainResourceSubject(FailureMetadata failureMetadata, DomainResource subject) { + super(failureMetadata, checkNotNull(subject)); } public static SimpleSubjectBuilder assertAboutDomains() { - return assertAbout(new SubjectFactory()); + return assertAbout(DomainResourceSubject::new); } } diff --git a/javatests/google/registry/testing/EppExceptionSubject.java b/javatests/google/registry/testing/EppExceptionSubject.java index 905456685..4d3b12760 100644 --- a/javatests/google/registry/testing/EppExceptionSubject.java +++ b/javatests/google/registry/testing/EppExceptionSubject.java @@ -18,10 +18,9 @@ import static com.google.common.truth.Truth.assertAbout; import static com.google.common.truth.Truth.assertThat; import static google.registry.flows.EppXmlTransformer.marshal; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.SimpleSubjectBuilder; import com.google.common.truth.Subject; -import com.google.common.truth.SubjectFactory; import google.registry.flows.EppException; import google.registry.model.eppcommon.Trid; import google.registry.model.eppoutput.EppOutput; @@ -33,8 +32,8 @@ import google.registry.xml.XmlException; /** Utility methods for asserting things about {@link EppException} instances. */ public class EppExceptionSubject extends Subject { - public EppExceptionSubject(FailureStrategy strategy, EppException subject) { - super(strategy, subject); + public EppExceptionSubject(FailureMetadata failureMetadata, EppException subject) { + super(failureMetadata, subject); } public And hasMessage(String expected) { @@ -58,10 +57,6 @@ public class EppExceptionSubject extends Subject assertAboutEppExceptions() { - return assertAbout(new SubjectFactory() { - @Override - public EppExceptionSubject getSubject(FailureStrategy strategy, EppException subject) { - return new EppExceptionSubject(strategy, subject); - }}); + return assertAbout(EppExceptionSubject::new); } } diff --git a/javatests/google/registry/testing/EppMetricSubject.java b/javatests/google/registry/testing/EppMetricSubject.java index 24052ae6c..b80ba0e2e 100644 --- a/javatests/google/registry/testing/EppMetricSubject.java +++ b/javatests/google/registry/testing/EppMetricSubject.java @@ -17,21 +17,19 @@ package google.registry.testing; import static com.google.common.truth.Truth.assertAbout; import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.Subject; -import com.google.common.truth.SubjectFactory; import google.registry.model.eppoutput.Result.Code; import google.registry.monitoring.whitebox.EppMetric; import google.registry.testing.TruthChainer.And; import java.util.Objects; import java.util.Optional; -import javax.annotation.Nullable; /** Utility methods for asserting things about {@link EppMetric} instances. */ public class EppMetricSubject extends Subject { - public EppMetricSubject(FailureStrategy strategy, EppMetric subject) { - super(strategy, subject); + public EppMetricSubject(FailureMetadata failureMetadata, EppMetric subject) { + super(failureMetadata, subject); } public static EppMetricSubject assertThat(EppMetric subject) { @@ -84,13 +82,7 @@ public class EppMetricSubject extends Subject { return new And<>(this); } - /** {@link SubjectFactory} for assertions about {@link EppMetric} objects. */ - private static final SubjectFactory SUBJECT_FACTORY = - new SubjectFactory() { - @Override - public EppMetricSubject getSubject( - FailureStrategy failureStrategy, @Nullable EppMetric target) { - return new EppMetricSubject(failureStrategy, target); - } - }; + /** {@link Subject.Factory} for assertions about {@link EppMetric} objects. */ + private static final Subject.Factory SUBJECT_FACTORY = + EppMetricSubject::new; } diff --git a/javatests/google/registry/testing/GenericEppResourceSubject.java b/javatests/google/registry/testing/GenericEppResourceSubject.java index d8f966b5c..4e9162a04 100644 --- a/javatests/google/registry/testing/GenericEppResourceSubject.java +++ b/javatests/google/registry/testing/GenericEppResourceSubject.java @@ -17,30 +17,20 @@ package google.registry.testing; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.truth.Truth.assertAbout; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.SimpleSubjectBuilder; -import com.google.common.truth.SubjectFactory; import google.registry.model.EppResource; /** Truth subject for asserting things about {@link EppResource} instances. */ public final class GenericEppResourceSubject extends AbstractEppResourceSubject { - /** A factory for instances of this subject. */ - private static class GenericEppResourceSubjectFactory - extends SubjectFactory { - @Override - public GenericEppResourceSubject getSubject(FailureStrategy strategy, EppResource subject) { - return new GenericEppResourceSubject(strategy, subject); - } - } - - public GenericEppResourceSubject(FailureStrategy strategy, EppResource subject) { - super(strategy, checkNotNull(subject)); + public GenericEppResourceSubject(FailureMetadata failureMetadata, EppResource subject) { + super(failureMetadata, checkNotNull(subject)); } public static SimpleSubjectBuilder assertAboutEppResources() { - return assertAbout(new GenericEppResourceSubjectFactory()); + return assertAbout(GenericEppResourceSubject::new); } } diff --git a/javatests/google/registry/testing/HistoryEntrySubject.java b/javatests/google/registry/testing/HistoryEntrySubject.java index 07703e67c..41b0a17f8 100644 --- a/javatests/google/registry/testing/HistoryEntrySubject.java +++ b/javatests/google/registry/testing/HistoryEntrySubject.java @@ -16,7 +16,7 @@ package google.registry.testing; import static com.google.common.truth.Truth.assertAbout; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.SimpleSubjectBuilder; import com.google.common.truth.Subject; import google.registry.model.domain.Period; @@ -30,12 +30,8 @@ public class HistoryEntrySubject extends Subject{} - - public HistoryEntrySubject(FailureStrategy strategy, HistoryEntry subject) { - super(strategy, subject); + public HistoryEntrySubject(FailureMetadata failureMetadata, HistoryEntry subject) { + super(failureMetadata, subject); } @Override @@ -105,6 +101,6 @@ public class HistoryEntrySubject extends Subject assertAboutHistoryEntries() { - return assertAbout(new SubjectFactory()); + return assertAbout(HistoryEntrySubject::new); } } diff --git a/javatests/google/registry/testing/HostResourceSubject.java b/javatests/google/registry/testing/HostResourceSubject.java index b2cc1f9e1..ba22718e5 100644 --- a/javatests/google/registry/testing/HostResourceSubject.java +++ b/javatests/google/registry/testing/HostResourceSubject.java @@ -17,7 +17,7 @@ package google.registry.testing; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.truth.Truth.assertAbout; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.SimpleSubjectBuilder; import com.googlecode.objectify.Key; import google.registry.model.domain.DomainResource; @@ -29,16 +29,12 @@ import org.joda.time.DateTime; public final class HostResourceSubject extends AbstractEppResourceSubject { - /** A factory for instances of this subject. */ - private static class SubjectFactory - extends ReflectiveSubjectFactory{} - - public HostResourceSubject(FailureStrategy strategy, HostResource subject) { - super(strategy, checkNotNull(subject)); + public HostResourceSubject(FailureMetadata failureMetadata, HostResource subject) { + super(failureMetadata, checkNotNull(subject)); } public static SimpleSubjectBuilder assertAboutHosts() { - return assertAbout(new SubjectFactory()); + return assertAbout(HostResourceSubject::new); } public And hasLastTransferTime(DateTime lastTransferTime) { diff --git a/javatests/google/registry/testing/LogsSubject.java b/javatests/google/registry/testing/LogsSubject.java index 4c3acddb2..8b8211776 100644 --- a/javatests/google/registry/testing/LogsSubject.java +++ b/javatests/google/registry/testing/LogsSubject.java @@ -20,7 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.collect.ImmutableList; import com.google.common.testing.TestLogHandler; import com.google.common.truth.Correspondence; -import com.google.common.truth.FailureStrategy; +import com.google.common.truth.FailureMetadata; import com.google.common.truth.SimpleSubjectBuilder; import com.google.common.truth.StringSubject; import com.google.common.truth.Subject; @@ -33,12 +33,8 @@ import java.util.logging.LogRecord; /** Utility methods for asserting things about logging {@link Handler} instances. */ public class LogsSubject extends Subject { - /** A factory for instances of this subject. */ - private static class SubjectFactory - extends ReflectiveSubjectFactory {} - - public LogsSubject(FailureStrategy strategy, TestLogHandler subject) { - super(strategy, subject); + public LogsSubject(FailureMetadata failureMetadata, TestLogHandler subject) { + super(failureMetadata, subject); } private static final Correspondence CONTAINS_CORRESPONDENCE = @@ -88,6 +84,6 @@ public class LogsSubject extends Subject { } public static SimpleSubjectBuilder assertAboutLogs() { - return assertAbout(new SubjectFactory()); + return assertAbout(LogsSubject::new); } } diff --git a/javatests/google/registry/testing/ReflectiveSubjectFactory.java b/javatests/google/registry/testing/ReflectiveSubjectFactory.java deleted file mode 100644 index 8a8a4b7b6..000000000 --- a/javatests/google/registry/testing/ReflectiveSubjectFactory.java +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2017 The Nomulus 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.testing; - -import com.google.common.truth.FailureStrategy; -import com.google.common.truth.Subject; -import com.google.common.truth.SubjectFactory; -import google.registry.util.TypeUtils.TypeInstantiator; - -/** Helper to reduce boilerplate in making new Truth subject classes. */ -public class ReflectiveSubjectFactory> extends SubjectFactory { - @Override - public S getSubject(FailureStrategy strategy, T subject) { - try { - Class sType = new TypeInstantiator(getClass()){}.getExactType(); - return sType - .getConstructor(FailureStrategy.class, subject.getClass()) - .newInstance(strategy, subject); - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } -} -