View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.commons.compress.harmony.unpack200;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.util.List;
23  
24  import org.junit.jupiter.api.Test;
25  
26  public class SegmentConstantPoolArrayCacheTest {
27  
28      @Test
29      public void testMultipleArrayMultipleHit() {
30          final SegmentConstantPoolArrayCache arrayCache = new SegmentConstantPoolArrayCache();
31          final String[] arrayOne = { "Zero", "Shared", "Two", "Shared", "Shared" };
32          final String[] arrayTwo = { "Shared", "One", "Shared", "Shared", "Shared" };
33  
34          List<Integer> listOne = arrayCache.indexesForArrayKey(arrayOne, "Shared");
35          List<Integer> listTwo = arrayCache.indexesForArrayKey(arrayTwo, "Shared");
36          // Make sure we're using the cached values. First trip
37          // through builds the cache.
38          listOne = arrayCache.indexesForArrayKey(arrayOne, "Two");
39          listTwo = arrayCache.indexesForArrayKey(arrayTwo, "Shared");
40  
41          assertEquals(1, listOne.size());
42          assertEquals(2, listOne.get(0).intValue());
43  
44          // Now look for a different element in list one
45          listOne = arrayCache.indexesForArrayKey(arrayOne, "Shared");
46          assertEquals(3, listOne.size());
47          assertEquals(1, listOne.get(0).intValue());
48          assertEquals(3, listOne.get(1).intValue());
49          assertEquals(4, listOne.get(2).intValue());
50  
51          assertEquals(4, listTwo.size());
52          assertEquals(0, listTwo.get(0).intValue());
53          assertEquals(2, listTwo.get(1).intValue());
54          assertEquals(3, listTwo.get(2).intValue());
55          assertEquals(4, listTwo.get(3).intValue());
56  
57          final List<Integer> listThree = arrayCache.indexesForArrayKey(arrayOne, "Not found");
58          assertEquals(0, listThree.size());
59      }
60  
61      @Test
62      public void testSingleMultipleHitArray() {
63          final SegmentConstantPoolArrayCache arrayCache = new SegmentConstantPoolArrayCache();
64          final String[] array = { "Zero", "OneThreeFour", "Two", "OneThreeFour", "OneThreeFour" };
65          final List<Integer> list = arrayCache.indexesForArrayKey(array, "OneThreeFour");
66          assertEquals(3, list.size());
67          assertEquals(1, list.get(0).intValue());
68          assertEquals(3, list.get(1).intValue());
69          assertEquals(4, list.get(2).intValue());
70      }
71  
72      @Test
73      public void testSingleSimpleArray() {
74          final SegmentConstantPoolArrayCache arrayCache = new SegmentConstantPoolArrayCache();
75          final String[] array = { "Zero", "One", "Two", "Three", "Four" };
76          final List<Integer> list = arrayCache.indexesForArrayKey(array, "Three");
77          assertEquals(1, list.size());
78          assertEquals(3, list.get(0).intValue());
79      }
80  
81  }