Clean up test "load resources" methods and classes

There is a big mix of different "load Resources" from different libraries
depending on where you were and what type of resource you want. Now there is a
clear hirarchy:

ResourceUtils:
  for use in actual (non-test) code, reads a file from a context directory

TestDataHelper (uses ResourceUtils):
  for use in tests, reads a file from a context directory + "/testdata". Also
  caches the resource so calling it multiple times with the same file will
  not read the file multiple times.

Library specific helpers (e.g. ToolsTestData) (uses TestDataHelper):
  for use in that library's tests only, reads from a specific testdata directory.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=177027533
This commit is contained in:
guyben 2017-11-27 09:30:46 -08:00 committed by jianglai
parent bbe2584da4
commit 95647528b8
12 changed files with 66 additions and 75 deletions

View file

@ -22,7 +22,7 @@ import static google.registry.flows.EppXmlTransformer.marshal;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.BILLING_EVENT_ID_STRIPPER; import static google.registry.testing.DatastoreHelper.BILLING_EVENT_ID_STRIPPER;
import static google.registry.testing.DatastoreHelper.getPollMessages; 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 google.registry.xml.XmlTestUtils.assertXmlEquals;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.joda.time.DateTimeZone.UTC; import static org.joda.time.DateTimeZone.UTC;
@ -136,7 +136,7 @@ public abstract class FlowTestCase<F extends Flow> extends ShardableTestCase {
} }
protected String readFile(String filename) { protected String readFile(String filename) {
return readResourceUtf8(getClass(), "testdata/" + filename); return loadFile(getClass(), filename);
} }
protected String readFile(String filename, Map<String, String> substitutions) { protected String readFile(String filename, Map<String, String> substitutions) {

View file

@ -14,31 +14,21 @@
package google.registry.rde; package google.registry.rde;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.io.ByteSource; import com.google.common.io.ByteSource;
import com.google.common.io.Resources; import google.registry.testing.TestDataHelper;
import java.io.IOException;
import java.net.URL;
/** Utility class providing easy access to contents of the {@code testdata/} directory. */ /** Utility class providing easy access to contents of the {@code testdata/} directory. */
public final class RdeTestData { public final class RdeTestData {
/** Returns {@link ByteSource} for file in {@code rde/testdata/} directory. */ /** Returns {@link ByteSource} for file in {@code rde/testdata/} directory. */
public static ByteSource get(String filename) { 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). * 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 { public static String loadUtf8(String filename) {
return Resources.asCharSource(getUrl(filename), UTF_8).read(); return TestDataHelper.loadFile(RdeTestData.class, filename);
}
private static URL getUrl(String filename) {
return Resources.getResource(RdeTestData.class, "testdata/" + filename);
} }
} }

View file

@ -14,31 +14,21 @@
package google.registry.rde.imports; package google.registry.rde.imports;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.io.ByteSource; import com.google.common.io.ByteSource;
import com.google.common.io.Resources; import google.registry.testing.TestDataHelper;
import java.io.IOException;
import java.net.URL;
/** Utility class providing easy access to contents of the {@code testdata/} directory. */ /** Utility class providing easy access to contents of the {@code testdata/} directory. */
public final class RdeImportsTestData { public final class RdeImportsTestData {
/** Returns {@link ByteSource} for file in {@code rde/imports/testdata/} directory. */ /** Returns {@link ByteSource} for file in {@code rde/imports/testdata/} directory. */
public static ByteSource get(String filename) { 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). * 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 { public static String loadUtf8(String filename) {
return Resources.asCharSource(getUrl(filename), UTF_8).read(); return TestDataHelper.loadFile(RdeImportsTestData.class, filename);
}
private static URL getUrl(String filename) {
return Resources.getResource(RdeImportsTestData.class, "testdata/" + filename);
} }
} }

View file

@ -14,27 +14,19 @@
package google.registry.reporting; package google.registry.reporting;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.io.ByteSource; import com.google.common.io.ByteSource;
import com.google.common.io.Resources; import google.registry.testing.TestDataHelper;
import java.io.IOException;
import java.net.URL;
/** Utility class providing easy access to contents of the {@code testdata/} directory. */ /** Utility class providing easy access to contents of the {@code testdata/} directory. */
public final class ReportingTestData { public final class ReportingTestData {
/** Returns {@link ByteSource} for file in {@code reporting/testdata/} directory. */ /** Returns {@link ByteSource} for file in {@code reporting/testdata/} directory. */
public static ByteSource get(String filename) { 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. */ /** Returns a {@link String} from a file in the {@code reporting/testdata/} directory. */
public static String getString(String filename) throws IOException { public static String getString(String filename) {
return Resources.asCharSource(getUrl(filename), UTF_8).read(); return TestDataHelper.loadFile(ReportingTestData.class, filename);
}
private static URL getUrl(String filename) {
return Resources.getResource(ReportingTestData.class, "testdata/" + filename);
} }
} }

View file

@ -15,28 +15,62 @@
package google.registry.testing; package google.registry.testing;
import static google.registry.util.CollectionUtils.nullToEmpty; import static google.registry.util.CollectionUtils.nullToEmpty;
import static google.registry.util.ResourceUtils.readResourceBytes;
import static google.registry.util.ResourceUtils.readResourceUtf8; 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;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
/** Contains helper methods for dealing with test data. */ /** Contains helper methods for dealing with test data. */
public final class TestDataHelper { 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<FileKey, String> fileCache = new ConcurrentHashMap<>();
private static final Map<FileKey, ByteSource> 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 * 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 <code>%tagname%</code>. * context class, and substitutes in values for placeholders of the form <code>%tagname%</code>.
*/ */
public static String loadFileWithSubstitutions( public static String loadFileWithSubstitutions(
Class<?> context, String filename, Map<String, String> substitutions) { Class<?> context, String filename, Map<String, String> substitutions) {
return applySubstitutions(readResourceUtf8(context, "testdata/" + filename), substitutions); String fileContents = loadFile(context, filename);
}
/** Applies the given substitutions to the given string and returns the result. */
public static String applySubstitutions(String fileContents, Map<String, String> substitutions) {
for (Entry<String, String> entry : nullToEmpty(substitutions).entrySet()) { for (Entry<String, String> entry : nullToEmpty(substitutions).entrySet()) {
fileContents = fileContents.replaceAll("%" + entry.getKey() + "%", entry.getValue()); fileContents = fileContents.replaceAll("%" + entry.getKey() + "%", entry.getValue());
} }
return fileContents; 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));
}
} }

View file

@ -16,10 +16,9 @@ package google.registry.tmch;
import static com.google.common.base.CharMatcher.whitespace; import static com.google.common.base.CharMatcher.whitespace;
import static com.google.common.io.BaseEncoding.base64; 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 com.google.common.io.ByteSource;
import google.registry.testing.TestDataHelper;
/** Utility class providing easy access to contents of the {@code testdata/} directory. */ /** Utility class providing easy access to contents of the {@code testdata/} directory. */
public final class TmchTestData { public final class TmchTestData {
@ -29,12 +28,12 @@ public final class TmchTestData {
/** Returns {@link ByteSource} for file in {@code tmch/testdata/} directory. */ /** Returns {@link ByteSource} for file in {@code tmch/testdata/} directory. */
public static ByteSource loadBytes(String filename) { 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. */ /** Loads data from file in {@code tmch/testdata/} as a String. */
public static String loadString(String filename) { 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. */ /** Extracts SMD XML from an ASCII-armored file. */

View file

@ -20,7 +20,6 @@ import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.persistActiveDomain; import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows; 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.LockOrUnlockDomainCommand.REGISTRY_LOCK_STATUSES;
import static google.registry.tools.server.ToolsTestData.loadUtf8; import static google.registry.tools.server.ToolsTestData.loadUtf8;
@ -68,14 +67,13 @@ public class LockDomainCommandTest extends EppToolCommandTestCase<LockDomainComm
List<String> params = new ArrayList<>(); List<String> params = new ArrayList<>();
List<String> expectedXmls = new ArrayList<>(); List<String> expectedXmls = new ArrayList<>();
params.add("--client=NewRegistrar"); 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 // 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). // case that was going to be the failure point).
for (int n = 0; n < 26; n++) { for (int n = 0; n < 26; n++) {
String domain = String.format("domain%d.tld", n); String domain = String.format("domain%d.tld", n);
persistActiveDomain(domain); persistActiveDomain(domain);
params.add(domain); params.add(domain);
expectedXmls.add(applySubstitutions(updateDomainXml, ImmutableMap.of("DOMAIN", domain))); expectedXmls.add(loadUtf8("domain_lock.xml", ImmutableMap.of("DOMAIN", domain)));
} }
runCommandForced(params); runCommandForced(params);
eppVerifier().verifySentContents(expectedXmls); eppVerifier().verifySentContents(expectedXmls);

View file

@ -22,7 +22,6 @@ import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.persistActiveDomain; import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows; import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.testing.TestDataHelper.applySubstitutions;
import static google.registry.tools.server.ToolsTestData.loadUtf8; import static google.registry.tools.server.ToolsTestData.loadUtf8;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -80,14 +79,13 @@ public class UnlockDomainCommandTest extends EppToolCommandTestCase<UnlockDomain
List<String> params = new ArrayList<>(); List<String> params = new ArrayList<>();
List<String> expectedXmls = new ArrayList<>(); List<String> expectedXmls = new ArrayList<>();
params.add("--client=NewRegistrar"); 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 // 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). // case that was going to be the failure point).
for (int n = 0; n < 26; n++) { for (int n = 0; n < 26; n++) {
String domain = String.format("domain%d.tld", n); String domain = String.format("domain%d.tld", n);
persistLockedDomain(domain); persistLockedDomain(domain);
params.add(domain); params.add(domain);
expectedXmls.add(applySubstitutions(updateDomainXml, ImmutableMap.of("DOMAIN", domain))); expectedXmls.add(loadUtf8("domain_unlock.xml", ImmutableMap.of("DOMAIN", domain)));
} }
runCommandForced(params); runCommandForced(params);
eppVerifier().verifySentContents(expectedXmls); eppVerifier().verifySentContents(expectedXmls);

View file

@ -14,11 +14,8 @@
package google.registry.tools.server; package google.registry.tools.server;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteSource; import com.google.common.io.ByteSource;
import com.google.common.io.Resources;
import google.registry.testing.TestDataHelper; import google.registry.testing.TestDataHelper;
import java.net.URL;
import java.util.Map; import java.util.Map;
/** Utility class providing easy access to contents of the {@code testdata/} directory. */ /** 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. */ /** Returns {@link ByteSource} for file in {@code tools/server/testdata/} directory. */
public static ByteSource get(String filename) { 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. * Loads data from file in {@code tools/server/testdata/} as a UTF-8 String.
*/ */
public static String loadUtf8(String filename) { 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<String, String> substitutions) { public static String loadUtf8(String filename, Map<String, String> substitutions) {
return TestDataHelper.loadFileWithSubstitutions(ToolsTestData.class, filename, substitutions); return TestDataHelper.loadFileWithSubstitutions(ToolsTestData.class, filename, substitutions);
} }
private static URL getUrl(String filename) {
return Resources.getResource(ToolsTestData.class, "testdata/" + filename);
}
} }

View file

@ -14,7 +14,7 @@
package google.registry.whois; package google.registry.whois;
import static google.registry.util.ResourceUtils.readResourceUtf8; import google.registry.testing.TestDataHelper;
/** Test helper methods for the whois package. */ /** Test helper methods for the whois package. */
final class WhoisHelper { final class WhoisHelper {
@ -24,7 +24,6 @@ final class WhoisHelper {
* that WHOIS requires. * that WHOIS requires.
*/ */
static String loadWhoisTestFile(String filename) { static String loadWhoisTestFile(String filename) {
return readResourceUtf8(WhoisHelper.class, "testdata/" + filename) return TestDataHelper.loadFile(WhoisHelper.class, filename).replaceAll("\r?\n", "\r\n");
.replaceAll("\r?\n", "\r\n");
} }
} }

View file

@ -15,11 +15,11 @@
package google.registry.xjc; package google.registry.xjc;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.util.ResourceUtils.readResourceUtf8;
import static google.registry.xjc.XjcXmlTransformer.unmarshal; import static google.registry.xjc.XjcXmlTransformer.unmarshal;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import google.registry.testing.ExceptionRule; import google.registry.testing.ExceptionRule;
import google.registry.testing.TestDataHelper;
import google.registry.xjc.epp.XjcEpp; import google.registry.xjc.epp.XjcEpp;
import google.registry.xjc.rde.XjcRdeDeposit; import google.registry.xjc.rde.XjcRdeDeposit;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -44,7 +44,7 @@ public class XmlTestdataTest {
private Example(String filename) { private Example(String filename) {
this.xmlStream = new ByteArrayInputStream( this.xmlStream = new ByteArrayInputStream(
readResourceUtf8(XmlTestdataTest.class, "testdata/" + filename).getBytes(UTF_8)); TestDataHelper.loadFile(XmlTestdataTest.class, filename).getBytes(UTF_8));
} }
} }

View file

@ -14,7 +14,7 @@
package google.registry.xml; 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 static google.registry.xml.XmlTestUtils.assertXmlEquals;
import google.registry.testing.ExceptionRule; import google.registry.testing.ExceptionRule;
@ -31,9 +31,7 @@ public class XmlTestUtilsTest {
public final ExceptionRule thrown = new ExceptionRule(); public final ExceptionRule thrown = new ExceptionRule();
void runTest(String file1, String file2) throws Exception { void runTest(String file1, String file2) throws Exception {
String s1 = readResourceUtf8(getClass(), "testdata/" + file1); assertXmlEquals(loadFile(getClass(), file1), loadFile(getClass(), file2));
String s2 = readResourceUtf8(getClass(), "testdata/" + file2);
assertXmlEquals(s1, s2);
} }
@Test @Test