1+ package org .asdfformat .asdf .io .compression ;
2+
3+ import org .junit .jupiter .api .BeforeEach ;
4+ import org .junit .jupiter .api .Nested ;
5+ import org .junit .jupiter .api .Test ;
6+
7+ import java .io .ByteArrayOutputStream ;
8+ import java .io .IOException ;
9+ import java .nio .ByteBuffer ;
10+ import java .nio .charset .StandardCharsets ;
11+ import java .util .zip .DeflaterOutputStream ;
12+
13+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
14+ import static org .junit .jupiter .api .Assertions .assertEquals ;
15+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
16+
17+ public class ZlibCompressorTest {
18+ private ZlibCompressor compressor ;
19+
20+ @ BeforeEach
21+ void beforeEach () {
22+ compressor = new ZlibCompressor ();
23+ }
24+
25+ @ Nested
26+ class GetIdentifier {
27+ @ Test
28+ void testGetIdentifier () {
29+ final byte [] identifier = compressor .getIdentifier ();
30+
31+ assertNotNull (identifier );
32+ assertArrayEquals ("zlib" .getBytes (StandardCharsets .UTF_8 ), identifier );
33+ }
34+ }
35+
36+ @ Nested
37+ class Decompress {
38+ @ Test
39+ void testDecompressSimpleText () throws IOException {
40+ final String originalText = "Hello World" ;
41+ final byte [] compressedData = compressWithZlib (originalText .getBytes ());
42+
43+ final ByteBuffer inputBuffer = ByteBuffer .wrap (compressedData );
44+ final ByteBuffer outputBuffer = ByteBuffer .allocate (1024 );
45+
46+ final long decompressedSize = compressor .decompress (inputBuffer , outputBuffer );
47+
48+ assertEquals (originalText .length (), decompressedSize );
49+ outputBuffer .flip ();
50+ final byte [] decompressedData = new byte [outputBuffer .remaining ()];
51+ outputBuffer .get (decompressedData );
52+ assertArrayEquals (originalText .getBytes (), decompressedData );
53+ }
54+
55+ @ Test
56+ void testDecompressEmptyData () throws IOException {
57+ final byte [] compressedData = compressWithZlib (new byte [0 ]);
58+
59+ final ByteBuffer inputBuffer = ByteBuffer .wrap (compressedData );
60+ final ByteBuffer outputBuffer = ByteBuffer .allocate (1024 );
61+
62+ final long decompressedSize = compressor .decompress (inputBuffer , outputBuffer );
63+
64+ assertEquals (0 , decompressedSize );
65+ assertEquals (0 , outputBuffer .position ());
66+ }
67+
68+ @ Test
69+ void testDecompressLargeData () throws IOException {
70+ final byte [] originalData = new byte [10000 ];
71+ for (int i = 0 ; i < originalData .length ; i ++) {
72+ originalData [i ] = (byte ) (i % 256 );
73+ }
74+ final byte [] compressedData = compressWithZlib (originalData );
75+
76+ final ByteBuffer inputBuffer = ByteBuffer .wrap (compressedData );
77+ final ByteBuffer outputBuffer = ByteBuffer .allocate (15000 );
78+
79+ final long decompressedSize = compressor .decompress (inputBuffer , outputBuffer );
80+
81+ assertEquals (originalData .length , decompressedSize );
82+ outputBuffer .flip ();
83+ final byte [] decompressedData = new byte [outputBuffer .remaining ()];
84+ outputBuffer .get (decompressedData );
85+ assertArrayEquals (originalData , decompressedData );
86+ }
87+
88+ @ Test
89+ void testMultipleDecompressions () throws IOException {
90+ final String text1 = "First text" ;
91+ final String text2 = "Second text" ;
92+ final byte [] compressed1 = compressWithZlib (text1 .getBytes ());
93+ final byte [] compressed2 = compressWithZlib (text2 .getBytes ());
94+
95+ final ByteBuffer inputBuffer1 = ByteBuffer .wrap (compressed1 );
96+ final ByteBuffer outputBuffer1 = ByteBuffer .allocate (1024 );
97+ final long size1 = compressor .decompress (inputBuffer1 , outputBuffer1 );
98+
99+ final ByteBuffer inputBuffer2 = ByteBuffer .wrap (compressed2 );
100+ final ByteBuffer outputBuffer2 = ByteBuffer .allocate (1024 );
101+ final long size2 = compressor .decompress (inputBuffer2 , outputBuffer2 );
102+
103+ assertEquals (text1 .length (), size1 );
104+ assertEquals (text2 .length (), size2 );
105+
106+ outputBuffer1 .flip ();
107+ final byte [] result1 = new byte [outputBuffer1 .remaining ()];
108+ outputBuffer1 .get (result1 );
109+ assertArrayEquals (text1 .getBytes (), result1 );
110+
111+ outputBuffer2 .flip ();
112+ final byte [] result2 = new byte [outputBuffer2 .remaining ()];
113+ outputBuffer2 .get (result2 );
114+ assertArrayEquals (text2 .getBytes (), result2 );
115+ }
116+
117+ @ Test
118+ void testDecompressWithBufferReuse () throws IOException {
119+ final String text1 = "First text" ;
120+ final String text2 = "Second longer text" ;
121+ final byte [] compressed1 = compressWithZlib (text1 .getBytes ());
122+ final byte [] compressed2 = compressWithZlib (text2 .getBytes ());
123+
124+ final ByteBuffer outputBuffer = ByteBuffer .allocate (1024 );
125+
126+ final ByteBuffer inputBuffer1 = ByteBuffer .wrap (compressed1 );
127+ final long size1 = compressor .decompress (inputBuffer1 , outputBuffer );
128+
129+ outputBuffer .flip ();
130+ final byte [] result1 = new byte [outputBuffer .remaining ()];
131+ outputBuffer .get (result1 );
132+ assertArrayEquals (text1 .getBytes (), result1 );
133+
134+ outputBuffer .clear ();
135+
136+ final ByteBuffer inputBuffer2 = ByteBuffer .wrap (compressed2 );
137+ final long size2 = compressor .decompress (inputBuffer2 , outputBuffer );
138+
139+ outputBuffer .flip ();
140+ final byte [] result2 = new byte [outputBuffer .remaining ()];
141+ outputBuffer .get (result2 );
142+ assertArrayEquals (text2 .getBytes (), result2 );
143+
144+ assertEquals (text1 .length (), size1 );
145+ assertEquals (text2 .length (), size2 );
146+ }
147+ }
148+
149+ private byte [] compressWithZlib (final byte [] data ) {
150+ try {
151+ final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
152+ try (final DeflaterOutputStream dos = new DeflaterOutputStream (baos )) {
153+ dos .write (data );
154+ }
155+ return baos .toByteArray ();
156+ } catch (IOException e ) {
157+ throw new RuntimeException ("Failed to compress test data" , e );
158+ }
159+ }
160+ }
0 commit comments