DeterministicKeyChainTest: refactor checkSerialization
Move file/string reading/serializing logic to dedicated methods.
@@ -747,6 +747,14 @@ public class DeterministicKeyChainTest {
private String protoToString(List<Protos.Key> keys) {
StringBuilder sb = new StringBuilder();
for (Protos.Key key : keys) {
+ String keyString = serializeKey(key);
+ sb.append(keyString);
+ }
+ return sb.toString().trim();
+ }
+
+ private String serializeKey(Protos.Key key) {
+ StringBuilder sb = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new StringReader(key.toString()));
String line;
@@ -759,17 +767,20 @@ public class DeterministicKeyChainTest {
} catch (IOException e) {
throw new RuntimeException(e); // cannot happen
}
- }
- return sb.toString().trim();
+ return sb.toString();
}
private String checkSerialization(List<Protos.Key> keys, String filename) {
- try {
String sb = protoToString(keys);
- List<String> lines = Resources.readLines(getClass().getResource(filename), StandardCharsets.UTF_8);
- String expected = Joiner.on('\n').join(lines);
+ String expected = readResourceFile(filename);
assertEquals(expected, sb);
return expected;
+ }
+
+ private String readResourceFile(String filename) {
+ try {
+ List<String> lines = Resources.readLines(getClass().getResource(filename), StandardCharsets.UTF_8);
+ return Joiner.on('\n').join(lines);
} catch (IOException e) {
throw new RuntimeException(e);
}
