Commit 5a4d82c7 by eguven Committed by Oliver Woodman

TestUtil.assertOutput() uses "*.unklen.dump" file if only it exists and…

TestUtil.assertOutput() uses "*.unklen.dump" file if only it exists and simulateUnknownLength is true.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=123638992
parent 911d86bf
...@@ -29,6 +29,7 @@ import org.mockito.MockitoAnnotations; ...@@ -29,6 +29,7 @@ import org.mockito.MockitoAnnotations;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays;
import java.util.Random; import java.util.Random;
/** /**
...@@ -44,7 +45,7 @@ public class TestUtil { ...@@ -44,7 +45,7 @@ public class TestUtil {
} }
private static final String DUMP_EXTENSION = ".dump"; private static final String DUMP_EXTENSION = ".dump";
private static final String UNKNOWN_LENGTH_EXTENSION = ".unklen"; private static final String UNKNOWN_LENGTH_EXTENSION = ".unklen" + DUMP_EXTENSION;
private TestUtil() {} private TestUtil() {}
...@@ -158,6 +159,15 @@ public class TestUtil { ...@@ -158,6 +159,15 @@ public class TestUtil {
MockitoAnnotations.initMocks(instrumentationTestCase); MockitoAnnotations.initMocks(instrumentationTestCase);
} }
public static boolean assetExists(Instrumentation instrumentation, String fileName)
throws IOException {
int i = fileName.lastIndexOf('/');
String path = i >= 0 ? fileName.substring(0, i) : "";
String file = i >= 0 ? fileName.substring(i + 1) : fileName;
return Arrays.asList(instrumentation.getContext().getResources().getAssets().list(path))
.contains(file);
}
public static byte[] getByteArray(Instrumentation instrumentation, String fileName) public static byte[] getByteArray(Instrumentation instrumentation, String fileName)
throws IOException { throws IOException {
InputStream is = instrumentation.getContext().getResources().getAssets().open(fileName); InputStream is = instrumentation.getContext().getResources().getAssets().open(fileName);
...@@ -199,9 +209,9 @@ public class TestUtil { ...@@ -199,9 +209,9 @@ public class TestUtil {
/** /**
* Asserts that {@code extractor} consumes {@code sampleFile} successfully and its output equals * Asserts that {@code extractor} consumes {@code sampleFile} successfully and its output equals
* to a prerecorded output dump file. The prerecorded output dump file name is determined by * to a prerecorded output dump file with the name {@code sampleFile} + "{@value
* appending "{@value UNKNOWN_LENGTH_EXTENSION}" if {@code simulateUnknownLength} is true and * #DUMP_EXTENSION}". If {@code simulateUnknownLength} is true and {@code sampleFile} + "{@value
* appending "{@value DUMP_EXTENSION}" to the end of {@code sampleFile}. * #UNKNOWN_LENGTH_EXTENSION}" exists, it's preferred.
* *
* @param extractor The {@link Extractor} to be tested. * @param extractor The {@link Extractor} to be tested.
* @param sampleFile The path to the input sample. * @param sampleFile The path to the input sample.
...@@ -226,12 +236,12 @@ public class TestUtil { ...@@ -226,12 +236,12 @@ public class TestUtil {
input.resetPeekPosition(); input.resetPeekPosition();
FakeExtractorOutput extractorOutput = consumeTestData(extractor, input, true); FakeExtractorOutput extractorOutput = consumeTestData(extractor, input, true);
String dumpFile = sampleFile; if (simulateUnknownLength
if (simulateUnknownLength) { && assetExists(instrumentation, sampleFile + UNKNOWN_LENGTH_EXTENSION)) {
dumpFile += UNKNOWN_LENGTH_EXTENSION; extractorOutput.assertOutput(instrumentation, sampleFile + UNKNOWN_LENGTH_EXTENSION);
} else {
extractorOutput.assertOutput(instrumentation, sampleFile + DUMP_EXTENSION);
} }
dumpFile += DUMP_EXTENSION;
extractorOutput.assertOutput(instrumentation, dumpFile);
return extractorOutput; return extractorOutput;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment