1// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.archivepatcher.shared;
16
17import org.junit.Assert;
18import org.junit.Test;
19import org.junit.runner.RunWith;
20import org.junit.runners.JUnit4;
21
22/**
23 * Tests for {@link DefaultDeflateCompatibilityWindow}.
24 */
25@RunWith(JUnit4.class)
26@SuppressWarnings("javadoc")
27public class JreDeflateParametersTest {
28
29  @Test
30  public void testOf_AllValidValues() {
31    for (int level = 1; level <= 9; level++) {
32      for (int strategy = 0; strategy <= 2; strategy++) {
33        for (boolean nowrap : new boolean[] {true, false}) {
34          JreDeflateParameters.of(level, strategy, nowrap);
35        }
36      }
37    }
38  }
39
40  private void assertIllegalArgumentException(int level, int strategy, boolean nowrap) {
41    try {
42      JreDeflateParameters.of(level, strategy, nowrap);
43      Assert.fail("Invalid configuration allowed");
44    } catch (IllegalArgumentException expected) {
45      // Pass
46    }
47  }
48
49  @Test
50  public void testOf_InvalidValues() {
51    // All of these should fail.
52    assertIllegalArgumentException(0, 0, true); // Bad compression level (store)
53    assertIllegalArgumentException(-1, 0, true); // Bad compression level (insane value < 0)
54    assertIllegalArgumentException(10, 0, true); // Bad compression level (insane value > 9)
55    assertIllegalArgumentException(1, -1, true); // Bad strategy (insane value < 0)
56    assertIllegalArgumentException(1, 3, true); // Bad strategy (valid in zlib, unsupported in JRE)
57  }
58
59  @Test
60  public void testToString() {
61    // Ensure that toString() doesn't crash and produces a non-empty string.
62    Assert.assertTrue(JreDeflateParameters.of(1, 0, true).toString().length() > 0);
63  }
64
65  @Test
66  public void testParseString() {
67    for (int level = 1; level <= 9; level++) {
68      for (int strategy = 0; strategy <= 2; strategy++) {
69        for (boolean nowrap : new boolean[] {true, false}) {
70          JreDeflateParameters params = JreDeflateParameters.of(level, strategy, nowrap);
71          String asString = params.toString();
72          JreDeflateParameters fromString = JreDeflateParameters.parseString(asString);
73          Assert.assertEquals(params, fromString);
74        }
75      }
76    }
77  }
78}
79