11package org .java_websocket .extensions ;
22
3- import static java .util .zip .GZIPInputStream .GZIP_MAGIC ;
43import static org .junit .Assert .assertArrayEquals ;
54import static org .junit .Assert .assertEquals ;
65import static org .junit .Assert .assertFalse ;
109import java .nio .ByteBuffer ;
1110import java .util .Arrays ;
1211import java .util .zip .Deflater ;
13- import java . util . zip . Inflater ;
12+
1413import org .java_websocket .exceptions .InvalidDataException ;
1514import org .java_websocket .extensions .permessage_deflate .PerMessageDeflateExtension ;
16- import org .java_websocket .framing .BinaryFrame ;
1715import org .java_websocket .framing .ContinuousFrame ;
1816import org .java_websocket .framing .TextFrame ;
1917import org .junit .Test ;
@@ -55,8 +53,7 @@ public void testDecodeFrameIfRSVIsNotSet() throws InvalidDataException {
5553
5654 @ Test
5755 public void testDecodeFrameNoCompression () throws InvalidDataException {
58- PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension ();
59- deflateExtension .setDeflaterLevel (Deflater .NO_COMPRESSION );
56+ PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension (Deflater .NO_COMPRESSION );
6057 deflateExtension .setThreshold (0 );
6158 String str = "This is a highly compressable text"
6259 + "This is a highly compressable text"
@@ -76,8 +73,7 @@ public void testDecodeFrameNoCompression() throws InvalidDataException {
7673
7774 @ Test
7875 public void testDecodeFrameBestSpeedCompression () throws InvalidDataException {
79- PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension ();
80- deflateExtension .setDeflaterLevel (Deflater .BEST_SPEED );
76+ PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension (Deflater .BEST_SPEED );
8177 deflateExtension .setThreshold (0 );
8278 String str = "This is a highly compressable text"
8379 + "This is a highly compressable text"
@@ -103,8 +99,7 @@ public void testDecodeFrameBestSpeedCompression() throws InvalidDataException {
10399
104100 @ Test
105101 public void testDecodeFrameBestCompression () throws InvalidDataException {
106- PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension ();
107- deflateExtension .setDeflaterLevel (Deflater .BEST_COMPRESSION );
102+ PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension (Deflater .BEST_COMPRESSION );
108103 deflateExtension .setThreshold (0 );
109104 String str = "This is a highly compressable text"
110105 + "This is a highly compressable text"
@@ -128,35 +123,6 @@ public void testDecodeFrameBestCompression() throws InvalidDataException {
128123 assertArrayEquals (message , frame .getPayloadData ().array ());
129124 }
130125
131- @ Test
132- public void testDecodeFrameSwitchCompression () throws InvalidDataException {
133- PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension ();
134- deflateExtension .setDeflaterLevel (Deflater .NO_COMPRESSION );
135- deflateExtension .setThreshold (0 );
136- String str = "This is a highly compressable text"
137- + "This is a highly compressable text"
138- + "This is a highly compressable text"
139- + "This is a highly compressable text"
140- + "This is a highly compressable text" ;
141- byte [] message = str .getBytes ();
142- TextFrame frame = new TextFrame ();
143- frame .setPayload (ByteBuffer .wrap (message ));
144-
145- Deflater localDeflater = new Deflater (Deflater .BEST_COMPRESSION ,true );
146- localDeflater .setInput (ByteBuffer .wrap (message ).array ());
147- byte [] buffer = new byte [1024 ];
148- int bytesCompressed = localDeflater .deflate (buffer , 0 , buffer .length , Deflater .SYNC_FLUSH );
149-
150- // Change the deflater level after the creation and switch to a new deflater level
151- // Compression strategy should be applied instantly since we call .deflate manually
152- deflateExtension .setDeflaterLevel (Deflater .BEST_COMPRESSION );
153- deflateExtension .encodeFrame (frame );
154- byte [] payloadArray = frame .getPayloadData ().array ();
155- assertArrayEquals (Arrays .copyOfRange (buffer ,0 , bytesCompressed ), Arrays .copyOfRange (payloadArray ,0 ,payloadArray .length ));
156- assertTrue (frame .isRSV1 ());
157- deflateExtension .decodeFrame (frame );
158- assertArrayEquals (message , frame .getPayloadData ().array ());
159- }
160126
161127 @ Test
162128 public void testEncodeFrame () {
@@ -304,41 +270,31 @@ public void testCopyInstance() {
304270 assertEquals (deflateExtension .getThreshold (), newDeflateExtension .getThreshold ());
305271 assertEquals (deflateExtension .isClientNoContextTakeover (), newDeflateExtension .isClientNoContextTakeover ());
306272 assertEquals (deflateExtension .isServerNoContextTakeover (), newDeflateExtension .isServerNoContextTakeover ());
307- assertEquals (deflateExtension .getDeflaterLevel (), newDeflateExtension .getDeflaterLevel ());
273+ assertEquals (deflateExtension .getCompressionLevel (), newDeflateExtension .getCompressionLevel ());
308274
309275
310- deflateExtension = new PerMessageDeflateExtension ();
276+ deflateExtension = new PerMessageDeflateExtension (Deflater . BEST_COMPRESSION );
311277 deflateExtension .setThreshold (512 );
312278 deflateExtension .setServerNoContextTakeover (false );
313279 deflateExtension .setClientNoContextTakeover (true );
314- deflateExtension .setDeflaterLevel (Deflater .BEST_COMPRESSION );
315280 newDeflateExtension = (PerMessageDeflateExtension )deflateExtension .copyInstance ();
316281
317282 assertEquals (deflateExtension .getThreshold (), newDeflateExtension .getThreshold ());
318283 assertEquals (deflateExtension .isClientNoContextTakeover (), newDeflateExtension .isClientNoContextTakeover ());
319284 assertEquals (deflateExtension .isServerNoContextTakeover (), newDeflateExtension .isServerNoContextTakeover ());
320- assertEquals (deflateExtension .getDeflaterLevel (), newDeflateExtension .getDeflaterLevel ());
285+ assertEquals (deflateExtension .getCompressionLevel (), newDeflateExtension .getCompressionLevel ());
321286
322287
323- deflateExtension = new PerMessageDeflateExtension ();
288+ deflateExtension = new PerMessageDeflateExtension (Deflater . NO_COMPRESSION );
324289 deflateExtension .setThreshold (64 );
325290 deflateExtension .setServerNoContextTakeover (true );
326291 deflateExtension .setClientNoContextTakeover (false );
327- deflateExtension .setDeflaterLevel (Deflater .NO_COMPRESSION );
328292 newDeflateExtension = (PerMessageDeflateExtension )deflateExtension .copyInstance ();
329293
330294 assertEquals (deflateExtension .getThreshold (), newDeflateExtension .getThreshold ());
331295 assertEquals (deflateExtension .isClientNoContextTakeover (), newDeflateExtension .isClientNoContextTakeover ());
332296 assertEquals (deflateExtension .isServerNoContextTakeover (), newDeflateExtension .isServerNoContextTakeover ());
333- assertEquals (deflateExtension .getDeflaterLevel (), newDeflateExtension .getDeflaterLevel ());
334- }
335-
336- @ Test
337- public void testDeflaterLevel () {
338- PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension ();
339- assertEquals (Deflater .DEFAULT_COMPRESSION , deflateExtension .getDeflaterLevel ());
340- deflateExtension .setDeflaterLevel (Deflater .BEST_SPEED );
341- assertEquals (Deflater .BEST_SPEED , deflateExtension .getDeflaterLevel ());
297+ assertEquals (deflateExtension .getCompressionLevel (), newDeflateExtension .getCompressionLevel ());
342298 }
343299
344300 @ Test
@@ -347,6 +303,6 @@ public void testDefaults() {
347303 assertFalse (deflateExtension .isClientNoContextTakeover ());
348304 assertTrue (deflateExtension .isServerNoContextTakeover ());
349305 assertEquals (1024 , deflateExtension .getThreshold ());
350- assertEquals (Deflater .DEFAULT_COMPRESSION , deflateExtension .getDeflaterLevel ());
306+ assertEquals (Deflater .DEFAULT_COMPRESSION , deflateExtension .getCompressionLevel ());
351307 }
352308}
0 commit comments