[GEM-2191] Fixes for code review. * Rename `rng` to `randomNumberGenerator`. * Move private class def to the end of the test code.
Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/c0505b90 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/c0505b90 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/c0505b90 Branch: refs/heads/master Commit: c0505b90836c9463e573383eac714a1fc486b0b2 Parents: 1d49bc1 Author: Galen O'Sullivan <[email protected]> Authored: Thu Dec 8 14:04:55 2016 -0800 Committer: Udo Kohlmeyer <[email protected]> Committed: Tue Jan 24 10:17:11 2017 -0800 ---------------------------------------------------------------------- ...ternalDataSerializerRandomizedJUnitTest.java | 143 +++++++++---------- 1 file changed, 71 insertions(+), 72 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/c0505b90/geode-core/src/test/java/org/apache/geode/internal/InternalDataSerializerRandomizedJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/internal/InternalDataSerializerRandomizedJUnitTest.java b/geode-core/src/test/java/org/apache/geode/internal/InternalDataSerializerRandomizedJUnitTest.java index 0fe2817..cf3dc1b 100644 --- a/geode-core/src/test/java/org/apache/geode/internal/InternalDataSerializerRandomizedJUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/internal/InternalDataSerializerRandomizedJUnitTest.java @@ -39,6 +39,73 @@ import java.security.SecureRandom; public class InternalDataSerializerRandomizedJUnitTest { private static final int ITERATIONS = 1000; + private static void testStringSerializedDeserializesToSameValue(String originalString) + throws IOException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream); + + DataSerializer.writeString(originalString, dataOutputStream); + dataOutputStream.flush(); + + byte[] stringBytes = byteArrayOutputStream.toByteArray(); + DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(stringBytes)); + String returnedString = DataSerializer.readString(dataInputStream); + + assertEquals("Deserialized string matches original", originalString, returnedString); + } + + @Before + public void setUp() { + // this may be unnecessary, but who knows what tests run before us. + InternalDataSerializer.reinitialize(); + } + + @Test + public void testRandomStringsSerializeThenDeserializeToSameValues() throws Exception { + RandomStringGenerator stringGenerator = new RandomStringGenerator(); + try { + for (int i = 0; i < ITERATIONS; i++) { + String str = stringGenerator.randomString(); + testStringSerializedDeserializesToSameValue(str); + } + } catch (Throwable throwable) { + throw new Exception("Failed with seed " + stringGenerator.getSeedString(), throwable); + } + } + + @Test + public void testEdgeCaseSerializationDeserialization() throws IOException { + testStringSerializedDeserializesToSameValue("\0"); + testStringSerializedDeserializesToSameValue(""); + + RandomStringGenerator stringGenerator = new RandomStringGenerator(); + testStringSerializedDeserializesToSameValue(stringGenerator.randomString(65534)); + testStringSerializedDeserializesToSameValue(stringGenerator.randomString(65535)); + testStringSerializedDeserializesToSameValue(stringGenerator.randomString(65536)); + testStringSerializedDeserializesToSameValue(stringGenerator.randomString(65537)); + + testStringSerializedDeserializesToSameValue(stringGenerator.randomString()); + } + + @Test + public void testABigString() throws IOException { + RandomStringGenerator stringGenerator = new RandomStringGenerator(); + final int strlen = 128001; + + StringBuilder stringBuilder = new StringBuilder(strlen); + while (stringBuilder.length() < strlen) { + stringBuilder.append(stringGenerator.randomCodepoint()); + } + // the last few we have to worry about codepoinuts being too big. + while (stringBuilder.length() < strlen) { + int codepoint = stringGenerator.randomCodepoint(); + if (codepoint <= strlen) { + stringBuilder.append(codepoint); + } + } + testStringSerializedDeserializesToSameValue(stringBuilder.toString()); + } + private static class RandomStringGenerator { public static final int SEED_BYTES = 8; public static final int MAX_STRING_LENGTH = 65538; @@ -46,7 +113,7 @@ public class InternalDataSerializerRandomizedJUnitTest { public static final int MAX_UTF16_CHARS = MAX_STRING_LENGTH * 2; public static final int UNICODE_MAX = Character.MAX_CODE_POINT; private final byte[] seed; - private final SecureRandom rng; + private final SecureRandom randomNumberGenerator; public byte[] getSeed() { return seed.clone(); @@ -75,7 +142,7 @@ public class InternalDataSerializerRandomizedJUnitTest { */ public RandomStringGenerator(byte[] seed) { this.seed = seed.clone(); - rng = new SecureRandom(this.seed); + randomNumberGenerator = new SecureRandom(this.seed); } /** @@ -87,7 +154,7 @@ public class InternalDataSerializerRandomizedJUnitTest { } public int randomCodepoint() { - return rng.nextInt(UNICODE_MAX); + return randomNumberGenerator.nextInt(UNICODE_MAX); } /** @@ -96,7 +163,7 @@ public class InternalDataSerializerRandomizedJUnitTest { * unallocated in the Unicode spec. */ public String randomString() { - return randomString(rng.nextInt(MAX_UTF16_CHARS)); + return randomString(randomNumberGenerator.nextInt(MAX_UTF16_CHARS)); } public String randomString(int length) { @@ -113,72 +180,4 @@ public class InternalDataSerializerRandomizedJUnitTest { return stringBuilder.toString(); } } - - - private static void testStringSerializedDeserializesToSameValue(String originalString) - throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream); - - DataSerializer.writeString(originalString, dataOutputStream); - dataOutputStream.flush(); - - byte[] stringBytes = byteArrayOutputStream.toByteArray(); - DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(stringBytes)); - String returnedString = DataSerializer.readString(dataInputStream); - - assertEquals("Deserialized string matches original", originalString, returnedString); - } - - @Before - public void setUp() { - // this may be unnecessary, but who knows what tests run before us. - InternalDataSerializer.reinitialize(); - } - - @Test - public void testRandomStringsSerializeThenDeserializeToSameValues() throws Exception { - RandomStringGenerator stringGenerator = new RandomStringGenerator(); - try { - for (int i = 0; i < ITERATIONS; i++) { - String str = stringGenerator.randomString(); - testStringSerializedDeserializesToSameValue(str); - } - } catch (Throwable throwable) { - throw new Exception("Failed with seed " + stringGenerator.getSeedString(), throwable); - } - } - - @Test - public void testEdgeCaseSerializationDeserialization() throws IOException { - testStringSerializedDeserializesToSameValue("\0"); - testStringSerializedDeserializesToSameValue(""); - - RandomStringGenerator stringGenerator = new RandomStringGenerator(); - testStringSerializedDeserializesToSameValue(stringGenerator.randomString(65534)); - testStringSerializedDeserializesToSameValue(stringGenerator.randomString(65535)); - testStringSerializedDeserializesToSameValue(stringGenerator.randomString(65536)); - testStringSerializedDeserializesToSameValue(stringGenerator.randomString(65537)); - - testStringSerializedDeserializesToSameValue(stringGenerator.randomString()); - } - - @Test - public void testABigString() throws IOException { - RandomStringGenerator stringGenerator = new RandomStringGenerator(); - final int strlen = 128001; - - StringBuilder stringBuilder = new StringBuilder(strlen); - while (stringBuilder.length() < strlen) { - stringBuilder.append(stringGenerator.randomCodepoint()); - } - // the last few we have to worry about codepoinuts being too big. - while (stringBuilder.length() < strlen) { - int codepoint = stringGenerator.randomCodepoint(); - if (codepoint <= strlen) { - stringBuilder.append(codepoint); - } - } - testStringSerializedDeserializesToSameValue(stringBuilder.toString()); - } }
