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   *   https://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.harmony.unpack200;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  class SegmentConstantPoolArrayCacheTest {
29  
30      @Test
31      void testMultipleArrayMultipleHit() {
32          final SegmentConstantPoolArrayCache arrayCache = new SegmentConstantPoolArrayCache();
33          final String[] arrayOne = { "Zero", "Shared", "Two", "Shared", "Shared" };
34          final String[] arrayTwo = { "Shared", "One", "Shared", "Shared", "Shared" };
35  
36          List<Integer> listOne = arrayCache.indexesForArrayKey(arrayOne, "Shared");
37          List<Integer> listTwo = arrayCache.indexesForArrayKey(arrayTwo, "Shared");
38          // Make sure we're using the cached values. First trip
39          // through builds the cache.
40          listOne = arrayCache.indexesForArrayKey(arrayOne, "Two");
41          listTwo = arrayCache.indexesForArrayKey(arrayTwo, "Shared");
42  
43          assertEquals(1, listOne.size());
44          assertEquals(2, listOne.get(0).intValue());
45  
46          // Now look for a different element in list one
47          listOne = arrayCache.indexesForArrayKey(arrayOne, "Shared");
48          assertEquals(3, listOne.size());
49          assertEquals(1, listOne.get(0).intValue());
50          assertEquals(3, listOne.get(1).intValue());
51          assertEquals(4, listOne.get(2).intValue());
52  
53          assertEquals(4, listTwo.size());
54          assertEquals(0, listTwo.get(0).intValue());
55          assertEquals(2, listTwo.get(1).intValue());
56          assertEquals(3, listTwo.get(2).intValue());
57          assertEquals(4, listTwo.get(3).intValue());
58  
59          final List<Integer> listThree = arrayCache.indexesForArrayKey(arrayOne, "Not found");
60          assertEquals(0, listThree.size());
61      }
62  
63      @Test
64      void testSingleMultipleHitArray() {
65          final SegmentConstantPoolArrayCache arrayCache = new SegmentConstantPoolArrayCache();
66          final String[] array = { "Zero", "OneThreeFour", "Two", "OneThreeFour", "OneThreeFour" };
67          final List<Integer> list = arrayCache.indexesForArrayKey(array, "OneThreeFour");
68          assertEquals(3, list.size());
69          assertEquals(1, list.get(0).intValue());
70          assertEquals(3, list.get(1).intValue());
71          assertEquals(4, list.get(2).intValue());
72      }
73  
74      @Test
75      void testSingleSimpleArray() {
76          final SegmentConstantPoolArrayCache arrayCache = new SegmentConstantPoolArrayCache();
77          final String[] array = { "Zero", "One", "Two", "Three", "Four" };
78          final List<Integer> list = arrayCache.indexesForArrayKey(array, "Three");
79          assertEquals(1, list.size());
80          assertEquals(3, list.get(0).intValue());
81      }
82  
83  }