Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tests of BrotliDecoderChannel #147

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (c) 2020-2023, Aayush Atharva
*
* Brotli4j licenses this file to you under the
* Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aayushatharva.brotli4j.decoder;

import com.aayushatharva.brotli4j.Brotli4jLoader;
import com.aayushatharva.brotli4j.encoder.BrotliOutputStream;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class BrotliDecoderChannelTest {
private static final byte[] compressedData = new byte[]{-117, 1, -128, 77, 101, 111, 119, 3};

@BeforeAll
static void load() {
Brotli4jLoader.ensureAvailability();
}

@Test
public void decompress() throws IOException {
BrotliDecoderChannel channel = new BrotliDecoderChannel(new EntireBufferByteChannel(ByteBuffer.wrap(compressedData)));
ByteBuffer output = ByteBuffer.allocate(2048);
channel.read(output);
output.flip();
String result = StandardCharsets.UTF_8.decode(output).toString();
assertEquals("Meow", result);
}

@Test
public void decompressLonger() throws IOException {
String data = "In ancient times cats were worshipped as gods; they have not forgotten this.";
ByteBuffer compressed;

try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
BrotliOutputStream output = new BrotliOutputStream(baos)) {
output.write(data.getBytes(StandardCharsets.UTF_8));
output.close();
compressed = ByteBuffer.wrap(baos.toByteArray());
}

// Quick verification that it compressed as expected
assertEquals(59, compressed.remaining());

BrotliDecoderChannel channel = new BrotliDecoderChannel(new EntireBufferByteChannel(compressed));
ByteBuffer output = ByteBuffer.allocate(2048);
channel.read(output);
output.flip();
String result = StandardCharsets.UTF_8.decode(output).toString();
assertEquals(data, result);
}

@Test
public void decompressOneByteAtATime() throws IOException {
BrotliDecoderChannel channel = new BrotliDecoderChannel(new OneByteAtATimeByteChannel(ByteBuffer.wrap(compressedData)));
ByteBuffer output = ByteBuffer.allocate(2048);
channel.read(output);
output.flip();
String result = StandardCharsets.UTF_8.decode(output).toString();
assertEquals("Meow", result);
}

@Test
public void decompressOneByteAtATimeLonger() throws IOException {
String data = "In ancient times cats were worshipped as gods; they have not forgotten this.";
ByteBuffer compressed;

try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
BrotliOutputStream output = new BrotliOutputStream(baos)) {
output.write(data.getBytes(StandardCharsets.UTF_8));
output.close();
compressed = ByteBuffer.wrap(baos.toByteArray());
}

// Quick verification that it compressed as expected
assertEquals(59, compressed.remaining());

BrotliDecoderChannel channel = new BrotliDecoderChannel(new OneByteAtATimeByteChannel(compressed));
ByteBuffer output = ByteBuffer.allocate(2048);
channel.read(output);
output.flip();
String result = StandardCharsets.UTF_8.decode(output).toString();
assertEquals(data, result);
}

private static class EntireBufferByteChannel implements ReadableByteChannel {
private final ByteBuffer buffer;

public EntireBufferByteChannel(ByteBuffer buffer) {
this.buffer = buffer.slice();
}

@Override
public int read(ByteBuffer dst) {
if (!buffer.hasRemaining())
return -1;
int pos = dst.position();
dst.put(buffer);
return dst.position() - pos;
}

@Override
public boolean isOpen() {
return true;
}

@Override
public void close() {
}
}

private static class OneByteAtATimeByteChannel implements ReadableByteChannel {
private final ByteBuffer buffer;

public OneByteAtATimeByteChannel(ByteBuffer buffer) {
this.buffer = buffer.slice();
}

@Override
public boolean isOpen() {
return true;
}

@Override
public void close() {
}

@Override
public int read(ByteBuffer dst) {
if (!buffer.hasRemaining())
return -1;
dst.put(buffer.get());
return 1;
}
}
}
Loading