ObjectCheckerTest: Factor duplicate instances out to constants
The tests: - testCheckBlobNotCorrupt - testCheckBlobCorrupt create instances of ObjectChecker that are the same. The tests: - testCheckBlobWithBlobObjectCheckerNotCorrupt - testCheckBlobWithBlobObjectCheckerCorrupt also create instances of ObjectChecker that are the same. Factor these instances out to constants instead of creating them in the tests. The `checker` member is still created anew in each test, since some of the tests change its state. Change-Id: I2d90263829d01d208632185b1ec2f678ae1a3f4c Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
This commit is contained in:
parent
17e849145c
commit
d6aec5da31
|
@ -79,6 +79,42 @@
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
public class ObjectCheckerTest {
|
public class ObjectCheckerTest {
|
||||||
|
private static final ObjectChecker SECRET_KEY_CHECKER = new ObjectChecker() {
|
||||||
|
@Override
|
||||||
|
public void checkBlob(byte[] raw) throws CorruptObjectException {
|
||||||
|
String in = decode(raw);
|
||||||
|
if (in.contains("secret_key")) {
|
||||||
|
throw new CorruptObjectException("don't add a secret key");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final ObjectChecker SECRET_KEY_BLOB_CHECKER = new ObjectChecker() {
|
||||||
|
@Override
|
||||||
|
public BlobObjectChecker newBlobObjectChecker() {
|
||||||
|
return new BlobObjectChecker() {
|
||||||
|
private boolean containSecretKey;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(byte[] in, int offset, int len) {
|
||||||
|
String str = decode(in, offset, offset + len);
|
||||||
|
if (str.contains("secret_key")) {
|
||||||
|
containSecretKey = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void endBlob(AnyObjectId id)
|
||||||
|
throws CorruptObjectException {
|
||||||
|
if (containSecretKey) {
|
||||||
|
throw new CorruptObjectException(
|
||||||
|
"don't add a secret key");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private ObjectChecker checker;
|
private ObjectChecker checker;
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
|
@ -109,94 +145,28 @@ public void testCheckBlob() throws CorruptObjectException {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCheckBlobNotCorrupt() throws CorruptObjectException {
|
public void testCheckBlobNotCorrupt() throws CorruptObjectException {
|
||||||
checker = new ObjectChecker() {
|
SECRET_KEY_CHECKER.check(OBJ_BLOB, encodeASCII("key = \"public_key\""));
|
||||||
@Override
|
|
||||||
public void checkBlob(byte[] raw) throws CorruptObjectException {
|
|
||||||
String in = decode(raw);
|
|
||||||
if (in.contains("secret_key")) {
|
|
||||||
throw new CorruptObjectException("don't add a secret key");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
checker.check(OBJ_BLOB, encodeASCII("key = \"public_key\""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCheckBlobCorrupt() throws CorruptObjectException {
|
public void testCheckBlobCorrupt() throws CorruptObjectException {
|
||||||
checker = new ObjectChecker() {
|
|
||||||
@Override
|
|
||||||
public void checkBlob(byte[] raw) throws CorruptObjectException {
|
|
||||||
String in = decode(raw);
|
|
||||||
if (in.contains("secret_key")) {
|
|
||||||
throw new CorruptObjectException("don't add a secret key");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
thrown.expect(CorruptObjectException.class);
|
thrown.expect(CorruptObjectException.class);
|
||||||
checker.check(OBJ_BLOB, encodeASCII("key = \"secret_key\""));
|
SECRET_KEY_CHECKER.check(OBJ_BLOB, encodeASCII("key = \"secret_key\""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCheckBlobWithBlobObjectCheckerNotCorrupt()
|
public void testCheckBlobWithBlobObjectCheckerNotCorrupt()
|
||||||
throws CorruptObjectException {
|
throws CorruptObjectException {
|
||||||
checker = new ObjectChecker() {
|
SECRET_KEY_BLOB_CHECKER.check(OBJ_BLOB,
|
||||||
@Override
|
encodeASCII("key = \"public_key\""));
|
||||||
public BlobObjectChecker newBlobObjectChecker() {
|
|
||||||
return new BlobObjectChecker() {
|
|
||||||
private boolean containSecretKey;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update(byte[] in, int offset, int len) {
|
|
||||||
String str = decode(in, offset, offset + len);
|
|
||||||
if (str.contains("secret_key")) {
|
|
||||||
containSecretKey = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void endBlob(AnyObjectId id)
|
|
||||||
throws CorruptObjectException {
|
|
||||||
if (containSecretKey) {
|
|
||||||
throw new CorruptObjectException(
|
|
||||||
"don't add a secret key");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
checker.check(OBJ_BLOB, encodeASCII("key = \"public_key\""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCheckBlobWithBlobObjectCheckerCorrupt()
|
public void testCheckBlobWithBlobObjectCheckerCorrupt()
|
||||||
throws CorruptObjectException {
|
throws CorruptObjectException {
|
||||||
checker = new ObjectChecker() {
|
|
||||||
@Override
|
|
||||||
public BlobObjectChecker newBlobObjectChecker() {
|
|
||||||
return new BlobObjectChecker() {
|
|
||||||
private boolean containSecretKey;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update(byte[] in, int offset, int len) {
|
|
||||||
String str = decode(in, offset, offset + len);
|
|
||||||
if (str.contains("secret_key")) {
|
|
||||||
containSecretKey = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void endBlob(AnyObjectId id)
|
|
||||||
throws CorruptObjectException {
|
|
||||||
if (containSecretKey) {
|
|
||||||
throw new CorruptObjectException(
|
|
||||||
"don't add a secret key");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
thrown.expect(CorruptObjectException.class);
|
thrown.expect(CorruptObjectException.class);
|
||||||
checker.check(OBJ_BLOB, encodeASCII("key = \"secret_key\""));
|
SECRET_KEY_BLOB_CHECKER.check(OBJ_BLOB,
|
||||||
|
encodeASCII("key = \"secret_key\""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue