Repository: netty/netty
File: codec-base/src/test/java/io/netty/handler/codec/base64/Base64Test.java (Java)

Code before:
        }
    }

    @Test
    public void testEncodeDecodeBE() {
        testEncodeDecode(ByteOrder.BIG_ENDIAN);
    }

    @Test
    public void testEncodeDecodeLE() {
        testEncodeDecode(ByteOrder.LITTLE_ENDIAN);
    }

    private static void testEncodeDecode(ByteOrder order) {
        testEncodeDecode(64, order);
        testEncodeDecode(128, order);
        testEncodeDecode(512, order);
        testEncodeDecode(1024, order);
        testEncodeDecode(4096, order);
        testEncodeDecode(8192, order);
        testEncodeDecode(16384, order);
    }

    private static void testEncodeDecode(int size, ByteOrder order) {
        byte[] bytes = new byte[size];
        PlatformDependent.threadLocalRandom().nextBytes(bytes);

        ByteBuf src = Unpooled.wrappedBuffer(bytes).order(order);
        ByteBuf encoded = Base64.encode(src);
        ByteBuf decoded = Base64.decode(encoded);
        ByteBuf expectedBuf = Unpooled.wrappedBuffer(bytes);
        try {
            assertEquals(expectedBuf, decoded,
                        StringUtil.NEWLINE + "expected: " + ByteBufUtil.hexDump(expectedBuf) +
                         StringUtil.NEWLINE + "actual--: " + ByteBufUtil.hexDump(decoded));
        } finally {
            src.release();
            encoded.release();
            decoded.release();
            expectedBuf.release();
        }
    }

    @Test
    public void testOverflowEncodedBufferSize() {
        assertEquals(Integer.MAX_VALUE, Base64.encodedBufferSize(Integer.MAX_VALUE, true));
        assertEquals(Integer.MAX_VALUE, Base64.encodedBufferSize(Integer.MAX_VALUE, false));
    }

    @Test
    public void testOverflowDecodedBufferSize() {

Review comment (suggestion):
Since Netty 4.2 is based on Java 8, we can now extend this test to also validate it's results against `java.util.Base64`

Code after:
            src.release();
            expectedEncoded.release();
            encoded.release();
        }
    }

    @Test
    public void testEncodeDecodeBE() throws IOException {
        testEncodeDecode(ByteOrder.BIG_ENDIAN);
    }

    @Test
    public void testEncodeDecodeLE() throws IOException {
        testEncodeDecode(ByteOrder.LITTLE_ENDIAN);
    }

    private static void testEncodeDecode(ByteOrder order) throws IOException {
        testEncodeDecode(order, Base64Dialect.STANDARD);
        testEncodeDecode(order, Base64Dialect.URL_SAFE);
    }

    private static void testEncodeDecode(ByteOrder order, Base64Dialect dialect) throws IOException {
        testEncodeDecode(order, dialect, true, true);
        testEncodeDecode(order, dialect, true, false);
        testEncodeDecode(order, dialect, false, true);
        testEncodeDecode(order, dialect, false, false);
    }

    private static void testEncodeDecode(ByteOrder order, Base64Dialect dialect, boolean breakLines, boolean padded)
            throws IOException {
        testEncodeDecode(64, order, dialect, breakLines, padded);
        testEncodeDecode(128, order, dialect, breakLines, padded);
        testEncodeDecode(512, order, dialect, breakLines, padded);
        testEncodeDecode(1024, order, dialect, breakLines, padded);
        testEncodeDecode(4096, order, dialect, breakLines, padded);
        testEncodeDecode(8192, order, dialect, breakLines, padded);
        testEncodeDecode(16384, order, dialect, breakLines, padded);
    }

    private static void testEncodeDecode(
            int size, ByteOrder order, Base64Dialect dialect, boolean breakLines, boolean padded) throws IOException {
        byte[] bytes = new byte[size];
        PlatformDependent.threadLocalRandom().nextBytes(bytes);

        // JDK encoder / decoder
        java.util.Base64.Encoder jdkEncoder =
                dialect == Base64Dialect.STANDARD ? java.util.Base64.getEncoder() : java.util.Base64.getUrlEncoder();
        jdkEncoder = padded ? jdkEncoder : jdkEncoder.withoutPadding();
        java.util.Base64.Decoder jdkDecoder =
                dialect == Base64Dialect.STANDARD ? java.util.Base64.getDecoder() : java.util.Base64.getUrlDecoder();