Search in sources :

Example 1 with RandomStringUtils

use of org.apache.commons.lang3.RandomStringUtils in project pravega by pravega.

the class RevisionDataOutputStreamTests method testWritePrimitives.

/**
 * Tests all the primitive data types that had to be implemented because of {@link DataOutput} which are not otherwise
 * tested by other means.
 */
@Test
public void testWritePrimitives() throws IOException {
    // We write to 2 streams in parallel. We verify our RevisionDataOutputStream vs Java's DataOutputStream - if the
    // outputs match, then a DataInputStream (and implicitly RevisionDataInputStream) should be able to read them back.
    @Cleanup val expectedStream = new ByteArrayOutputStream();
    @Cleanup val expectedDataStream = new DataOutputStream(expectedStream);
    @Cleanup val actualStream = new ByteArrayOutputStream();
    @Cleanup val actualDataStream = RevisionDataOutputStream.wrap(actualStream);
    // Generate a random set of strings using all available characters.
    val strings = IntStream.range(0, 20).mapToObj(RandomStringUtils::random).collect(Collectors.toList());
    // .. and another set using ASCII alphanumeric characters.
    IntStream.range(1, 20).mapToObj(i -> RandomStringUtils.random(i, true, true)).forEach(strings::add);
    // Calculate the total length expected.
    int stringLengths = strings.stream().mapToInt(s -> s.length() + 2 * s.length() + actualDataStream.getUTFLength(s)).sum();
    actualDataStream.length(1 + 1 + 1 + Short.BYTES + Short.BYTES + Integer.BYTES + Long.BYTES + Float.BYTES + Double.BYTES + stringLengths);
    // Write some data.
    WritePrimitive.apply(true, actualDataStream::writeBoolean, expectedDataStream::writeBoolean);
    WritePrimitive.apply(false, actualDataStream::writeBoolean, expectedDataStream::writeBoolean);
    WritePrimitive.apply(1, actualDataStream::writeByte, expectedDataStream::writeByte);
    WritePrimitive.apply(Short.MAX_VALUE - 2, actualDataStream::writeShort, expectedDataStream::writeShort);
    WritePrimitive.apply((int) 'a', actualDataStream::writeChar, expectedDataStream::writeChar);
    WritePrimitive.apply(Integer.MAX_VALUE - 4, actualDataStream::writeInt, expectedDataStream::writeInt);
    WritePrimitive.apply(Long.MAX_VALUE - 7, actualDataStream::writeLong, expectedDataStream::writeLong);
    WritePrimitive.apply(4.0f, actualDataStream::writeFloat, expectedDataStream::writeFloat);
    WritePrimitive.apply(8.0d, actualDataStream::writeDouble, expectedDataStream::writeDouble);
    for (val s : strings) {
        WritePrimitive.apply(s, actualDataStream::writeBytes, expectedDataStream::writeBytes);
        WritePrimitive.apply(s, actualDataStream::writeChars, expectedDataStream::writeChars);
        WritePrimitive.apply(s, actualDataStream::writeUTF, expectedDataStream::writeUTF);
    }
    actualDataStream.flush();
    expectedDataStream.flush();
    val expected = expectedStream.toByteArray();
    val actual = actualStream.toByteArray();
    Assert.assertEquals(expected.length, actualDataStream.getSize());
    Assert.assertEquals(expected.length + Integer.BYTES, actual.length);
    AssertExtensions.assertArrayEquals("", expected, 0, actual, Integer.BYTES, expected.length);
}
Also used : lombok.val(lombok.val) OutputStream(java.io.OutputStream) IntStream(java.util.stream.IntStream) Charsets(com.google.common.base.Charsets) DataOutput(java.io.DataOutput) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssertExtensions(io.pravega.test.common.AssertExtensions) lombok.val(lombok.val) Cleanup(lombok.Cleanup) IOException(java.io.IOException) Test(org.junit.Test) EOFException(java.io.EOFException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) DirectDataOutput(io.pravega.common.io.DirectDataOutput) SerializationException(io.pravega.common.io.SerializationException) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Assert(org.junit.Assert) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

Charsets (com.google.common.base.Charsets)1 ByteBufferOutputStream (io.pravega.common.io.ByteBufferOutputStream)1 DirectDataOutput (io.pravega.common.io.DirectDataOutput)1 SerializationException (io.pravega.common.io.SerializationException)1 ByteArraySegment (io.pravega.common.util.ByteArraySegment)1 AssertExtensions (io.pravega.test.common.AssertExtensions)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutput (java.io.DataOutput)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Arrays (java.util.Arrays)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Cleanup (lombok.Cleanup)1 lombok.val (lombok.val)1