View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress.archivers.zip;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.IOException;
28  import java.io.InputStream;
29  
30  import org.junit.jupiter.api.Test;
31  
32  public class BinaryTreeTest {
33  
34      @Test
35      public void testDecode() throws IOException {
36          final InputStream in = new ByteArrayInputStream(new byte[] { 0x02, 0x42, 0x01, 0x13 });
37  
38          final BinaryTree tree = BinaryTree.decode(in, 8);
39  
40          assertNotNull(tree);
41  
42          try (BitStream stream = new BitStream(new ByteArrayInputStream(new byte[] { (byte) 0x8D, (byte) 0xC5, (byte) 0x11, 0x00 }))) {
43              assertEquals(0, tree.read(stream));
44              assertEquals(1, tree.read(stream));
45              assertEquals(2, tree.read(stream));
46              assertEquals(3, tree.read(stream));
47              assertEquals(4, tree.read(stream));
48              assertEquals(5, tree.read(stream));
49              assertEquals(6, tree.read(stream));
50              assertEquals(7, tree.read(stream));
51          }
52      }
53  
54      @Test
55      public void testExceptions() {
56          final BinaryTree binaryFinary = new BinaryTree(4);
57          binaryFinary.addLeaf(0, 0, 0, 1);
58          assertThrows(IllegalArgumentException.class, () -> binaryFinary.addLeaf(0, 0, 0, 1));
59  
60          final InputStream is = new ByteArrayInputStream(new byte[] {});
61          assertThrows(IOException.class, () -> BinaryTree.decode(is, 0));
62          assertThrows(IOException.class, () -> new BinaryTree(4).read(new BitStream(new ByteArrayInputStream(new byte[] { 0 }))));
63      }
64  }