diff --git a/javatests/google/registry/model/BUILD b/javatests/google/registry/model/BUILD index 6fe3e5e1b..45e20ddbe 100644 --- a/javatests/google/registry/model/BUILD +++ b/javatests/google/registry/model/BUILD @@ -16,8 +16,7 @@ java_library( "**/*.java", ]), resources = [ - "schema.txt", - ] + glob(["**/testdata/*.xml"]), + ] + glob(["**/testdata/*"]), deps = [ "//java/google/registry/config", "//java/google/registry/dns/writer", diff --git a/javatests/google/registry/model/SchemaVersionTest.java b/javatests/google/registry/model/SchemaVersionTest.java index d57ede042..cfe420477 100644 --- a/javatests/google/registry/model/SchemaVersionTest.java +++ b/javatests/google/registry/model/SchemaVersionTest.java @@ -14,8 +14,6 @@ package google.registry.model; -import static com.google.common.io.Resources.getResource; - import google.registry.testing.AppEngineRule; import google.registry.testing.GoldenFileTestHelper; import org.junit.Rule; @@ -36,10 +34,9 @@ public class SchemaVersionTest { @Test public void testGoldenSchemaFile() throws Exception { - GoldenFileTestHelper.testGoldenFile( - SchemaVersion.getSchema(), - getResource(SchemaVersionTest.class, "schema.txt"), - "Datastore schema", - "get_schema"); + GoldenFileTestHelper.assertThat(SchemaVersion.getSchema()) + .describedAs("Datastore schema") + .createdByNomulusCommand("get_schema") + .isEqualToGolden(SchemaVersionTest.class, "schema.txt"); } } diff --git a/javatests/google/registry/model/schema.txt b/javatests/google/registry/model/testdata/schema.txt similarity index 100% rename from javatests/google/registry/model/schema.txt rename to javatests/google/registry/model/testdata/schema.txt diff --git a/javatests/google/registry/module/backend/BackendRequestComponentTest.java b/javatests/google/registry/module/backend/BackendRequestComponentTest.java index 4fb7a29c3..5958a90aa 100644 --- a/javatests/google/registry/module/backend/BackendRequestComponentTest.java +++ b/javatests/google/registry/module/backend/BackendRequestComponentTest.java @@ -14,9 +14,6 @@ package google.registry.module.backend; -import static com.google.common.io.Resources.getResource; - -import google.registry.request.RouterDisplayHelper; import google.registry.testing.GoldenFileTestHelper; import org.junit.Test; import org.junit.runner.RunWith; @@ -28,10 +25,8 @@ public class BackendRequestComponentTest { @Test public void testRoutingMap() throws Exception { - GoldenFileTestHelper.testGoldenFile( - RouterDisplayHelper.extractHumanReadableRoutesFromComponent(BackendRequestComponent.class), - getResource(BackendRequestComponentTest.class, "testdata/backend_routing.txt"), - "backend routing map", - "get_routing_map -c " + BackendRequestComponent.class.getName()); + GoldenFileTestHelper.assertThatRoutesFromComponent(BackendRequestComponent.class) + .describedAs("backend routing map") + .isEqualToGolden(BackendRequestComponentTest.class, "backend_routing.txt"); } } diff --git a/javatests/google/registry/module/frontend/FrontendRequestComponentTest.java b/javatests/google/registry/module/frontend/FrontendRequestComponentTest.java index 58070c2a7..2850f345c 100644 --- a/javatests/google/registry/module/frontend/FrontendRequestComponentTest.java +++ b/javatests/google/registry/module/frontend/FrontendRequestComponentTest.java @@ -14,9 +14,7 @@ package google.registry.module.frontend; -import static com.google.common.io.Resources.getResource; -import google.registry.request.RouterDisplayHelper; import google.registry.testing.GoldenFileTestHelper; import org.junit.Test; import org.junit.runner.RunWith; @@ -28,10 +26,8 @@ public class FrontendRequestComponentTest { @Test public void testRoutingMap() throws Exception { - GoldenFileTestHelper.testGoldenFile( - RouterDisplayHelper.extractHumanReadableRoutesFromComponent(FrontendRequestComponent.class), - getResource(FrontendRequestComponentTest.class, "testdata/frontend_routing.txt"), - "frontend routing map", - "get_routing_map -c " + FrontendRequestComponent.class.getName()); + GoldenFileTestHelper.assertThatRoutesFromComponent(FrontendRequestComponent.class) + .describedAs("frontend routing map") + .isEqualToGolden(FrontendRequestComponentTest.class, "frontend_routing.txt"); } } diff --git a/javatests/google/registry/module/tools/ToolsRequestComponentTest.java b/javatests/google/registry/module/tools/ToolsRequestComponentTest.java index 12c6ce63a..1ebd9806a 100644 --- a/javatests/google/registry/module/tools/ToolsRequestComponentTest.java +++ b/javatests/google/registry/module/tools/ToolsRequestComponentTest.java @@ -14,9 +14,6 @@ package google.registry.module.tools; -import static com.google.common.io.Resources.getResource; - -import google.registry.request.RouterDisplayHelper; import google.registry.testing.GoldenFileTestHelper; import org.junit.Test; import org.junit.runner.RunWith; @@ -28,10 +25,8 @@ public class ToolsRequestComponentTest { @Test public void testRoutingMap() throws Exception { - GoldenFileTestHelper.testGoldenFile( - RouterDisplayHelper.extractHumanReadableRoutesFromComponent(ToolsRequestComponent.class), - getResource(ToolsRequestComponentTest.class, "testdata/tools_routing.txt"), - "tools routing map", - "get_routing_map -c " + ToolsRequestComponent.class.getName()); + GoldenFileTestHelper.assertThatRoutesFromComponent(ToolsRequestComponent.class) + .describedAs("tools routing map") + .isEqualToGolden(ToolsRequestComponentTest.class, "tools_routing.txt"); } } diff --git a/javatests/google/registry/testing/GoldenFileTestHelper.java b/javatests/google/registry/testing/GoldenFileTestHelper.java index 60c1e67c4..f35131390 100644 --- a/javatests/google/registry/testing/GoldenFileTestHelper.java +++ b/javatests/google/registry/testing/GoldenFileTestHelper.java @@ -14,12 +14,14 @@ package google.registry.testing; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; import static com.google.common.truth.Truth.assert_; -import static google.registry.util.ResourceUtils.readResourceUtf8; +import static google.registry.testing.TestDataHelper.filePath; +import static google.registry.testing.TestDataHelper.loadFile; import com.google.common.base.Joiner; -import java.net.MalformedURLException; -import java.net.URL; +import google.registry.request.RouterDisplayHelper; /** * Helper class to compare a string against a golden file and print out update instructions if @@ -27,8 +29,12 @@ import java.net.URL; */ public class GoldenFileTestHelper { + String actualValue = null; + String nomulusCommand = null; + String goldenFileDescription = null; + private static final String UPDATE_COMMAND = - "google.registry.tools.RegistryTool -e localhost %1$s >javatests%2$s"; + "google.registry.tools.RegistryTool -e localhost %1$s > %2$s"; private static final String UPDATE_INSTRUCTIONS = Joiner.on('\n') @@ -39,36 +45,46 @@ public class GoldenFileTestHelper { UPDATE_COMMAND, ""); - private static String getPathProper(URL url) throws MalformedURLException { - String protocol = url.getProtocol(); - if (protocol.equals("jar")) { - url = new URL(url.getPath()); - protocol = url.getProtocol(); - } - if (protocol.equals("file")) { - String[] components = url.getPath().split("!"); - if (components.length >= 2) { - return components[1]; - } - } - return url.getPath(); + + public static GoldenFileTestHelper assertThat(String actualValue) { + return new GoldenFileTestHelper().setActualValue(actualValue); } - public static void testGoldenFile( - String actualValue, - URL goldenFileUrl, - String goldenFileDescription, - String nomulusCommand) - throws Exception { - // Don't use Truth's isEqualTo() because the output is huge and unreadable for large files. - if (!actualValue.equals(readResourceUtf8(goldenFileUrl).trim())) { + public static GoldenFileTestHelper assertThatRoutesFromComponent(Class component) { + return assertThat(RouterDisplayHelper.extractHumanReadableRoutesFromComponent(component)) + .createdByNomulusCommand("get_routing_map -c " + component.getName()); + } + + public GoldenFileTestHelper createdByNomulusCommand(String nomulusCommand) { + checkState(this.nomulusCommand == null, "Trying to set nomulus command twice"); + this.nomulusCommand = checkNotNull(nomulusCommand); + return this; + } + + public GoldenFileTestHelper describedAs(String goldenFileDescription) { + checkState(this.goldenFileDescription == null, "Trying to set description twice"); + this.goldenFileDescription = checkNotNull(goldenFileDescription); + return this; + } + + public void isEqualToGolden(Class context, String filename) { + checkNotNull(nomulusCommand, "Didn't set nomulus command"); + checkNotNull(goldenFileDescription, "Didn't set description"); + checkNotNull(context); + checkNotNull(filename); + if (!actualValue.equals(loadFile(context, filename).trim())) { assert_() .fail( String.format( UPDATE_INSTRUCTIONS, nomulusCommand, - getPathProper(goldenFileUrl), + filePath(context, filename), goldenFileDescription)); } } + + private GoldenFileTestHelper setActualValue(String actualValue) { + this.actualValue = checkNotNull(actualValue); + return this; + } }