mirror of
https://github.com/google/nomulus.git
synced 2025-05-14 00:17:20 +02:00
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:
parent
bbe2584da4
commit
95647528b8
12 changed files with 66 additions and 75 deletions
|
@ -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<F extends Flow> extends ShardableTestCase {
|
|||
}
|
||||
|
||||
protected String readFile(String filename) {
|
||||
return readResourceUtf8(getClass(), "testdata/" + filename);
|
||||
return loadFile(getClass(), filename);
|
||||
}
|
||||
|
||||
protected String readFile(String filename, Map<String, String> substitutions) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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
|
||||
* context class, and substitutes in values for placeholders of the form <code>%tagname%</code>.
|
||||
*/
|
||||
public static String loadFileWithSubstitutions(
|
||||
Class<?> context, String filename, Map<String, String> 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<String, String> substitutions) {
|
||||
String fileContents = loadFile(context, filename);
|
||||
for (Entry<String, String> 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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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. */
|
||||
|
|
|
@ -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<LockDomainComm
|
|||
List<String> params = new ArrayList<>();
|
||||
List<String> 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);
|
||||
|
|
|
@ -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<UnlockDomain
|
|||
List<String> params = new ArrayList<>();
|
||||
List<String> 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);
|
||||
|
|
|
@ -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<String, String> substitutions) {
|
||||
return TestDataHelper.loadFileWithSubstitutions(ToolsTestData.class, filename, substitutions);
|
||||
}
|
||||
|
||||
private static URL getUrl(String filename) {
|
||||
return Resources.getResource(ToolsTestData.class, "testdata/" + filename);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue