diff --git a/javatests/google/registry/flows/FlowTestCase.java b/javatests/google/registry/flows/FlowTestCase.java index 7ccfca824..c419d945f 100644 --- a/javatests/google/registry/flows/FlowTestCase.java +++ b/javatests/google/registry/flows/FlowTestCase.java @@ -22,7 +22,7 @@ import static google.registry.flows.EppXmlTransformer.marshal; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.testing.DatastoreHelper.BILLING_EVENT_ID_STRIPPER; import static google.registry.testing.DatastoreHelper.getPollMessages; -import static google.registry.util.ResourceUtils.readResourceUtf8; +import static google.registry.testing.TestDataHelper.loadFile; import static google.registry.xml.XmlTestUtils.assertXmlEquals; import static java.nio.charset.StandardCharsets.UTF_8; import static org.joda.time.DateTimeZone.UTC; @@ -136,7 +136,7 @@ public abstract class FlowTestCase extends ShardableTestCase { } protected String readFile(String filename) { - return readResourceUtf8(getClass(), "testdata/" + filename); + return loadFile(getClass(), filename); } protected String readFile(String filename, Map substitutions) { diff --git a/javatests/google/registry/rde/RdeTestData.java b/javatests/google/registry/rde/RdeTestData.java index 148a7f068..f7c7bda6b 100644 --- a/javatests/google/registry/rde/RdeTestData.java +++ b/javatests/google/registry/rde/RdeTestData.java @@ -14,31 +14,21 @@ package google.registry.rde; -import static java.nio.charset.StandardCharsets.UTF_8; - import com.google.common.io.ByteSource; -import com.google.common.io.Resources; -import java.io.IOException; -import java.net.URL; +import google.registry.testing.TestDataHelper; /** Utility class providing easy access to contents of the {@code testdata/} directory. */ public final class RdeTestData { /** Returns {@link ByteSource} for file in {@code rde/testdata/} directory. */ public static ByteSource get(String filename) { - return Resources.asByteSource(getUrl(filename)); + return TestDataHelper.loadBytes(RdeTestData.class, filename); } /** * Loads data from file in {@code rde/testdata/} as a String (assuming file is UTF-8). - * - * @throws IOException if the file couldn't be loaded from the jar. */ - public static String loadUtf8(String filename) throws IOException { - return Resources.asCharSource(getUrl(filename), UTF_8).read(); - } - - private static URL getUrl(String filename) { - return Resources.getResource(RdeTestData.class, "testdata/" + filename); + public static String loadUtf8(String filename) { + return TestDataHelper.loadFile(RdeTestData.class, filename); } } diff --git a/javatests/google/registry/rde/imports/RdeImportsTestData.java b/javatests/google/registry/rde/imports/RdeImportsTestData.java index 5c06c28cb..dd609b3cf 100644 --- a/javatests/google/registry/rde/imports/RdeImportsTestData.java +++ b/javatests/google/registry/rde/imports/RdeImportsTestData.java @@ -14,31 +14,21 @@ package google.registry.rde.imports; -import static java.nio.charset.StandardCharsets.UTF_8; - import com.google.common.io.ByteSource; -import com.google.common.io.Resources; -import java.io.IOException; -import java.net.URL; +import google.registry.testing.TestDataHelper; /** Utility class providing easy access to contents of the {@code testdata/} directory. */ public final class RdeImportsTestData { /** Returns {@link ByteSource} for file in {@code rde/imports/testdata/} directory. */ public static ByteSource get(String filename) { - return Resources.asByteSource(getUrl(filename)); + return TestDataHelper.loadBytes(RdeImportsTestData.class, filename); } /** * Loads data from file in {@code rde/imports/testdata/} as a String (assuming file is UTF-8). - * - * @throws IOException if the file couldn't be loaded from the jar. */ - public static String loadUtf8(String filename) throws IOException { - return Resources.asCharSource(getUrl(filename), UTF_8).read(); - } - - private static URL getUrl(String filename) { - return Resources.getResource(RdeImportsTestData.class, "testdata/" + filename); + public static String loadUtf8(String filename) { + return TestDataHelper.loadFile(RdeImportsTestData.class, filename); } } diff --git a/javatests/google/registry/reporting/ReportingTestData.java b/javatests/google/registry/reporting/ReportingTestData.java index ea3d3cd07..c13582be9 100644 --- a/javatests/google/registry/reporting/ReportingTestData.java +++ b/javatests/google/registry/reporting/ReportingTestData.java @@ -14,27 +14,19 @@ package google.registry.reporting; -import static java.nio.charset.StandardCharsets.UTF_8; - import com.google.common.io.ByteSource; -import com.google.common.io.Resources; -import java.io.IOException; -import java.net.URL; +import google.registry.testing.TestDataHelper; /** Utility class providing easy access to contents of the {@code testdata/} directory. */ public final class ReportingTestData { /** Returns {@link ByteSource} for file in {@code reporting/testdata/} directory. */ public static ByteSource get(String filename) { - return Resources.asByteSource(getUrl(filename)); + return TestDataHelper.loadBytes(ReportingTestData.class, filename); } /** Returns a {@link String} from a file in the {@code reporting/testdata/} directory. */ - public static String getString(String filename) throws IOException { - return Resources.asCharSource(getUrl(filename), UTF_8).read(); - } - - private static URL getUrl(String filename) { - return Resources.getResource(ReportingTestData.class, "testdata/" + filename); + public static String getString(String filename) { + return TestDataHelper.loadFile(ReportingTestData.class, filename); } } diff --git a/javatests/google/registry/testing/TestDataHelper.java b/javatests/google/registry/testing/TestDataHelper.java index fef7f391e..ecfb2d166 100644 --- a/javatests/google/registry/testing/TestDataHelper.java +++ b/javatests/google/registry/testing/TestDataHelper.java @@ -15,28 +15,62 @@ package google.registry.testing; import static google.registry.util.CollectionUtils.nullToEmpty; +import static google.registry.util.ResourceUtils.readResourceBytes; import static google.registry.util.ResourceUtils.readResourceUtf8; +import com.google.auto.value.AutoValue; +import com.google.common.io.ByteSource; import java.util.Map; import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; /** Contains helper methods for dealing with test data. */ public final class TestDataHelper { + @AutoValue + abstract static class FileKey { + abstract Class context(); + + abstract String filename(); + + static FileKey create(Class context, String filename) { + return new AutoValue_TestDataHelper_FileKey(context, filename); + } + } + + private static final Map fileCache = new ConcurrentHashMap<>(); + private static final Map byteCache = new ConcurrentHashMap<>(); + + /** + * Loads a text file from the "testdata" directory relative to the location of the specified + * context class. + */ + public static String loadFile(Class context, String filename) { + return fileCache.computeIfAbsent( + FileKey.create(context, filename), + k -> readResourceUtf8(context, "testdata/" + filename)); + } + /** * Loads a text file from the "testdata" directory relative to the location of the specified * context class, and substitutes in values for placeholders of the form %tagname%. */ public static String loadFileWithSubstitutions( Class context, String filename, Map substitutions) { - return applySubstitutions(readResourceUtf8(context, "testdata/" + filename), substitutions); - } - - /** Applies the given substitutions to the given string and returns the result. */ - public static String applySubstitutions(String fileContents, Map substitutions) { + String fileContents = loadFile(context, filename); for (Entry entry : nullToEmpty(substitutions).entrySet()) { fileContents = fileContents.replaceAll("%" + entry.getKey() + "%", entry.getValue()); } return fileContents; } + + /** + * Loads a {@link ByteSource} from the "testdata" directory relative to the location of the + * specified context class. + */ + public static ByteSource loadBytes(Class context, String filename) { + return byteCache.computeIfAbsent( + FileKey.create(context, filename), + k -> readResourceBytes(context, "testdata/" + filename)); + } } diff --git a/javatests/google/registry/tmch/TmchTestData.java b/javatests/google/registry/tmch/TmchTestData.java index 706a6c7a5..a59d6451e 100644 --- a/javatests/google/registry/tmch/TmchTestData.java +++ b/javatests/google/registry/tmch/TmchTestData.java @@ -16,10 +16,9 @@ package google.registry.tmch; import static com.google.common.base.CharMatcher.whitespace; import static com.google.common.io.BaseEncoding.base64; -import static google.registry.util.ResourceUtils.readResourceBytes; -import static google.registry.util.ResourceUtils.readResourceUtf8; import com.google.common.io.ByteSource; +import google.registry.testing.TestDataHelper; /** Utility class providing easy access to contents of the {@code testdata/} directory. */ public final class TmchTestData { @@ -29,12 +28,12 @@ public final class TmchTestData { /** Returns {@link ByteSource} for file in {@code tmch/testdata/} directory. */ public static ByteSource loadBytes(String filename) { - return readResourceBytes(TmchTestData.class, "testdata/" + filename); + return TestDataHelper.loadBytes(TmchTestData.class, filename); } /** Loads data from file in {@code tmch/testdata/} as a String. */ public static String loadString(String filename) { - return readResourceUtf8(TmchTestData.class, "testdata/" + filename); + return TestDataHelper.loadFile(TmchTestData.class, filename); } /** Extracts SMD XML from an ASCII-armored file. */ diff --git a/javatests/google/registry/tools/LockDomainCommandTest.java b/javatests/google/registry/tools/LockDomainCommandTest.java index b6714c659..8d8153879 100644 --- a/javatests/google/registry/tools/LockDomainCommandTest.java +++ b/javatests/google/registry/tools/LockDomainCommandTest.java @@ -20,7 +20,6 @@ import static google.registry.testing.DatastoreHelper.newDomainResource; import static google.registry.testing.DatastoreHelper.persistActiveDomain; import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.JUnitBackports.expectThrows; -import static google.registry.testing.TestDataHelper.applySubstitutions; import static google.registry.tools.LockOrUnlockDomainCommand.REGISTRY_LOCK_STATUSES; import static google.registry.tools.server.ToolsTestData.loadUtf8; @@ -68,14 +67,13 @@ public class LockDomainCommandTest extends EppToolCommandTestCase params = new ArrayList<>(); List expectedXmls = new ArrayList<>(); params.add("--client=NewRegistrar"); - String updateDomainXml = loadUtf8("domain_lock.xml"); // Create 26 domains -- one more than the number of entity groups allowed in a transaction (in // case that was going to be the failure point). for (int n = 0; n < 26; n++) { String domain = String.format("domain%d.tld", n); persistActiveDomain(domain); params.add(domain); - expectedXmls.add(applySubstitutions(updateDomainXml, ImmutableMap.of("DOMAIN", domain))); + expectedXmls.add(loadUtf8("domain_lock.xml", ImmutableMap.of("DOMAIN", domain))); } runCommandForced(params); eppVerifier().verifySentContents(expectedXmls); diff --git a/javatests/google/registry/tools/UnlockDomainCommandTest.java b/javatests/google/registry/tools/UnlockDomainCommandTest.java index 81949dbd7..56ae2a260 100644 --- a/javatests/google/registry/tools/UnlockDomainCommandTest.java +++ b/javatests/google/registry/tools/UnlockDomainCommandTest.java @@ -22,7 +22,6 @@ import static google.registry.testing.DatastoreHelper.newDomainResource; import static google.registry.testing.DatastoreHelper.persistActiveDomain; import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.JUnitBackports.expectThrows; -import static google.registry.testing.TestDataHelper.applySubstitutions; import static google.registry.tools.server.ToolsTestData.loadUtf8; import com.google.common.collect.ImmutableList; @@ -80,14 +79,13 @@ public class UnlockDomainCommandTest extends EppToolCommandTestCase params = new ArrayList<>(); List expectedXmls = new ArrayList<>(); params.add("--client=NewRegistrar"); - String updateDomainXml = loadUtf8("domain_unlock.xml"); // Create 26 domains -- one more than the number of entity groups allowed in a transaction (in // case that was going to be the failure point). for (int n = 0; n < 26; n++) { String domain = String.format("domain%d.tld", n); persistLockedDomain(domain); params.add(domain); - expectedXmls.add(applySubstitutions(updateDomainXml, ImmutableMap.of("DOMAIN", domain))); + expectedXmls.add(loadUtf8("domain_unlock.xml", ImmutableMap.of("DOMAIN", domain))); } runCommandForced(params); eppVerifier().verifySentContents(expectedXmls); diff --git a/javatests/google/registry/tools/server/ToolsTestData.java b/javatests/google/registry/tools/server/ToolsTestData.java index a946e5c0b..ae5196944 100644 --- a/javatests/google/registry/tools/server/ToolsTestData.java +++ b/javatests/google/registry/tools/server/ToolsTestData.java @@ -14,11 +14,8 @@ package google.registry.tools.server; -import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteSource; -import com.google.common.io.Resources; import google.registry.testing.TestDataHelper; -import java.net.URL; import java.util.Map; /** Utility class providing easy access to contents of the {@code testdata/} directory. */ @@ -26,14 +23,14 @@ public final class ToolsTestData { /** Returns {@link ByteSource} for file in {@code tools/server/testdata/} directory. */ public static ByteSource get(String filename) { - return Resources.asByteSource(getUrl(filename)); + return TestDataHelper.loadBytes(ToolsTestData.class, filename); } /** * Loads data from file in {@code tools/server/testdata/} as a UTF-8 String. */ public static String loadUtf8(String filename) { - return loadUtf8(filename, ImmutableMap.of()); + return TestDataHelper.loadFile(ToolsTestData.class, filename); } /** @@ -42,8 +39,4 @@ public final class ToolsTestData { public static String loadUtf8(String filename, Map substitutions) { return TestDataHelper.loadFileWithSubstitutions(ToolsTestData.class, filename, substitutions); } - - private static URL getUrl(String filename) { - return Resources.getResource(ToolsTestData.class, "testdata/" + filename); - } } diff --git a/javatests/google/registry/whois/WhoisHelper.java b/javatests/google/registry/whois/WhoisHelper.java index a53302477..52ef14661 100644 --- a/javatests/google/registry/whois/WhoisHelper.java +++ b/javatests/google/registry/whois/WhoisHelper.java @@ -14,7 +14,7 @@ package google.registry.whois; -import static google.registry.util.ResourceUtils.readResourceUtf8; +import google.registry.testing.TestDataHelper; /** Test helper methods for the whois package. */ final class WhoisHelper { @@ -24,7 +24,6 @@ final class WhoisHelper { * that WHOIS requires. */ static String loadWhoisTestFile(String filename) { - return readResourceUtf8(WhoisHelper.class, "testdata/" + filename) - .replaceAll("\r?\n", "\r\n"); + return TestDataHelper.loadFile(WhoisHelper.class, filename).replaceAll("\r?\n", "\r\n"); } } diff --git a/javatests/google/registry/xjc/XmlTestdataTest.java b/javatests/google/registry/xjc/XmlTestdataTest.java index 8d3e12d6e..96f8d0817 100644 --- a/javatests/google/registry/xjc/XmlTestdataTest.java +++ b/javatests/google/registry/xjc/XmlTestdataTest.java @@ -15,11 +15,11 @@ package google.registry.xjc; import static com.google.common.truth.Truth.assertThat; -import static google.registry.util.ResourceUtils.readResourceUtf8; import static google.registry.xjc.XjcXmlTransformer.unmarshal; import static java.nio.charset.StandardCharsets.UTF_8; import google.registry.testing.ExceptionRule; +import google.registry.testing.TestDataHelper; import google.registry.xjc.epp.XjcEpp; import google.registry.xjc.rde.XjcRdeDeposit; import java.io.ByteArrayInputStream; @@ -44,7 +44,7 @@ public class XmlTestdataTest { private Example(String filename) { this.xmlStream = new ByteArrayInputStream( - readResourceUtf8(XmlTestdataTest.class, "testdata/" + filename).getBytes(UTF_8)); + TestDataHelper.loadFile(XmlTestdataTest.class, filename).getBytes(UTF_8)); } } diff --git a/javatests/google/registry/xml/XmlTestUtilsTest.java b/javatests/google/registry/xml/XmlTestUtilsTest.java index a44f3c5f3..cccf07a6a 100644 --- a/javatests/google/registry/xml/XmlTestUtilsTest.java +++ b/javatests/google/registry/xml/XmlTestUtilsTest.java @@ -14,7 +14,7 @@ package google.registry.xml; -import static google.registry.util.ResourceUtils.readResourceUtf8; +import static google.registry.testing.TestDataHelper.loadFile; import static google.registry.xml.XmlTestUtils.assertXmlEquals; import google.registry.testing.ExceptionRule; @@ -31,9 +31,7 @@ public class XmlTestUtilsTest { public final ExceptionRule thrown = new ExceptionRule(); void runTest(String file1, String file2) throws Exception { - String s1 = readResourceUtf8(getClass(), "testdata/" + file1); - String s2 = readResourceUtf8(getClass(), "testdata/" + file2); - assertXmlEquals(s1, s2); + assertXmlEquals(loadFile(getClass(), file1), loadFile(getClass(), file2)); } @Test