String desc,
long numBytes ) throws Exception {
InputStream content = getClass().getClassLoader().getResourceAsStream(resourcePath);
assertThat(content, is(notNullValue()));
Stopwatch sw = new Stopwatch();
sw.start();
Binary binary = store.storeValue(content, false);
sw.stop();
if (print) System.out.println("Time to store " + desc + ": " + sw.getTotalDuration());
if (numBytes == 0) {
assertThat(binary, is(instanceOf(EmptyBinaryValue.class)));
} else if (numBytes < MIN_BINARY_SIZE) {
assertThat(binary, is(instanceOf(InMemoryBinaryValue.class)));
} else {
assertThat(binary, is(instanceOf(StoredBinaryValue.class)));
}
assertThat(binary.getHexHash(), is(expectedSha1));
assertThat(binary.getSize(), is(numBytes));
// Now try reading and comparing the two streams ...
InputStream expected = getClass().getClassLoader().getResourceAsStream(resourcePath);
InputStream actual = binary.getStream();
byte[] buffer1 = new byte[1024];
byte[] buffer2 = new byte[1024];
int numRead = 0;
while ((numRead = expected.read(buffer1)) == actual.read(buffer2)) {
if (numRead == -1) break;
for (int i = 0; i != numRead; ++i) {
assertThat(buffer1[i], is(buffer2[i]));
}
}
if (print) {
// And try measuring how fast we can read the file ...
sw = new Stopwatch();
sw.start();
while (-1 != actual.read(buffer2)) {
}
sw.stop();
System.out.println("Time to read " + desc + ": " + sw.getTotalDuration());
}
}