|
1 | 1 | package com.clickhouse.client.data; |
2 | 2 |
|
3 | 3 | import org.testng.Assert; |
| 4 | +import org.testng.annotations.DataProvider; |
4 | 5 | import org.testng.annotations.Test; |
5 | 6 | import java.io.ByteArrayInputStream; |
6 | 7 | import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.EOFException; |
7 | 9 | import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.nio.charset.StandardCharsets; |
8 | 12 |
|
9 | 13 | public class ClickhouseLZ4InputStreamTest { |
| 14 | + private InputStream generateInputStream(String prefix, int samples, StringBuilder builder) throws IOException { |
| 15 | + builder.setLength(0); |
| 16 | + |
| 17 | + byte[] result = null; |
| 18 | + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 19 | + ClickHouseLZ4OutputStream lz4Out = new ClickHouseLZ4OutputStream(out, 1024 * 1024)) { |
| 20 | + for (int i = 0; i < samples; i++) { |
| 21 | + String s = prefix + i; |
| 22 | + lz4Out.write(s.getBytes(StandardCharsets.UTF_8)); |
| 23 | + builder.append(s); |
| 24 | + } |
| 25 | + lz4Out.flush(); |
| 26 | + result = out.toByteArray(); |
| 27 | + // Assert.assertTrue(result.length < builder.length() / 2); |
| 28 | + } |
| 29 | + |
| 30 | + return new ByteArrayInputStream(result); |
| 31 | + } |
| 32 | + |
| 33 | + @DataProvider(name = "prefixes") |
| 34 | + private Object[][] getPrefixes() { |
| 35 | + return new Object[][] { { "test" }, { "萌萌哒" }, |
| 36 | + { "1😂2萌🥘" } }; |
| 37 | + }; |
| 38 | + |
| 39 | + @Test(dataProvider = "prefixes", groups = { "unit" }) |
| 40 | + public void testReadByte(String prefix) throws IOException { |
| 41 | + StringBuilder builder = new StringBuilder(); |
| 42 | + boolean readAll = false; |
| 43 | + try (InputStream in = generateInputStream(prefix, 10000, builder); |
| 44 | + ClickHouseLZ4InputStream lz4In = new ClickHouseLZ4InputStream(in); |
| 45 | + ByteArrayOutputStream out = new ByteArrayOutputStream();) { |
| 46 | + try { |
| 47 | + while (true) { |
| 48 | + out.write(0xFF & lz4In.readByte()); |
| 49 | + } |
| 50 | + } catch (EOFException e) { |
| 51 | + readAll = true; |
| 52 | + } |
| 53 | + |
| 54 | + out.flush(); |
| 55 | + |
| 56 | + Assert.assertEquals(new String(out.toByteArray(), StandardCharsets.UTF_8), builder.toString()); |
| 57 | + } |
| 58 | + Assert.assertTrue(readAll, "All bytes should have read without any issue"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test(dataProvider = "prefixes", groups = { "unit" }) |
| 62 | + public void testRead(String prefix) throws IOException { |
| 63 | + StringBuilder builder = new StringBuilder(); |
| 64 | + boolean readAll = false; |
| 65 | + try (InputStream in = generateInputStream(prefix, 10000, builder); |
| 66 | + ClickHouseLZ4InputStream lz4In = new ClickHouseLZ4InputStream(in); |
| 67 | + ByteArrayOutputStream out = new ByteArrayOutputStream();) { |
| 68 | + int result = 0; |
| 69 | + while ((result = lz4In.read()) != -1) { |
| 70 | + out.write(result); |
| 71 | + } |
| 72 | + out.flush(); |
| 73 | + readAll = true; |
| 74 | + |
| 75 | + Assert.assertEquals(new String(out.toByteArray(), StandardCharsets.UTF_8), builder.toString()); |
| 76 | + } |
| 77 | + Assert.assertTrue(readAll, "All bytes should have read without any issue"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test(dataProvider = "prefixes", groups = { "unit" }) |
| 81 | + public void testReadBytes(String prefix) throws IOException { |
| 82 | + StringBuilder builder = new StringBuilder(); |
| 83 | + boolean readAll = false; |
| 84 | + for (int i = 1; i < 1025; i++) { |
| 85 | + byte[] bytes = new byte[i]; |
| 86 | + try (InputStream in = generateInputStream(prefix, 10000, builder); |
| 87 | + ClickHouseLZ4InputStream lz4In = new ClickHouseLZ4InputStream(in); |
| 88 | + ByteArrayOutputStream out = new ByteArrayOutputStream();) { |
| 89 | + int result = 0; |
| 90 | + while ((result = lz4In.read(bytes)) != -1) { |
| 91 | + out.write(bytes, 0, result); |
| 92 | + } |
| 93 | + out.flush(); |
| 94 | + readAll = true; |
| 95 | + |
| 96 | + Assert.assertEquals(new String(out.toByteArray(), StandardCharsets.UTF_8), builder.toString()); |
| 97 | + } |
| 98 | + Assert.assertTrue(readAll, "All bytes should have read without any issue"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
10 | 102 | @Test(groups = { "unit" }) |
11 | 103 | public void testLZ4Stream() throws IOException { |
12 | 104 | StringBuilder sb = new StringBuilder(); |
|
0 commit comments