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  package org.apache.commons.compress.harmony.unpack200;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Tests for org.apache.commons.compress.harmony.unpack200.SegmentConstantPool.
29   */
30  class SegmentConstantPoolTest {
31  
32      public class MockSegmentConstantPool extends SegmentConstantPool {
33  
34          MockSegmentConstantPool() {
35              super(new CpBands(new Segment()));
36          }
37  
38          @Override
39          public int matchSpecificPoolEntryIndex(final String[] classNameArray, final String desiredClassName, final int desiredIndex) {
40              return super.matchSpecificPoolEntryIndex(classNameArray, desiredClassName, desiredIndex);
41          }
42  
43          @Override
44          public int matchSpecificPoolEntryIndex(final String[] classNameArray, final String[] methodNameArray, final String desiredClassName,
45                  final String desiredMethodRegex, final int desiredIndex) {
46              return super.matchSpecificPoolEntryIndex(classNameArray, methodNameArray, desiredClassName, desiredMethodRegex, desiredIndex);
47          }
48  
49          public boolean regexMatchesVisible(final String regexString, final String compareString) {
50              return SegmentConstantPool.regexMatches(regexString, compareString);
51          }
52      }
53  
54      String[] testClassArray = { "Object", "Object", "java/lang/String", "java/lang/String", "Object", "Other" };
55      String[] testMethodArray = { "<init>()", "clone()", "equals()", "<init>", "isNull()", "Other" };
56  
57      @Test
58      void testMatchSpecificPoolEntryIndex_DoubleArray() {
59          final MockSegmentConstantPool mockInstance = new MockSegmentConstantPool();
60          // Elements should be found at the proper position.
61          assertEquals(0, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray, "Object", "^<init>.*", 0));
62          assertEquals(2, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray, "java/lang/String", ".*", 0));
63          assertEquals(3, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray, "java/lang/String", "^<init>.*", 0));
64          assertEquals(5, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray, "Other", ".*", 0));
65  
66          // Elements that don't exist shouldn't be found
67          assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray, "NotThere", "^<init>.*", 0));
68  
69          // Elements that exist but don't have the requisite number
70          // of hits shouldn't be found.
71          assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray, "java/lang/String", "^<init>.*", 1));
72      }
73  
74      @Test
75      void testMatchSpecificPoolEntryIndex_SingleArray() {
76          final MockSegmentConstantPool mockInstance = new MockSegmentConstantPool();
77          // Elements should be found at the proper position.
78          assertEquals(0, mockInstance.matchSpecificPoolEntryIndex(testClassArray, "Object", 0));
79          assertEquals(1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, "Object", 1));
80          assertEquals(2, mockInstance.matchSpecificPoolEntryIndex(testClassArray, "java/lang/String", 0));
81          assertEquals(3, mockInstance.matchSpecificPoolEntryIndex(testClassArray, "java/lang/String", 1));
82          assertEquals(4, mockInstance.matchSpecificPoolEntryIndex(testClassArray, "Object", 2));
83          assertEquals(5, mockInstance.matchSpecificPoolEntryIndex(testClassArray, "Other", 0));
84  
85          // Elements that don't exist shouldn't be found
86          assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, "NotThere", 0));
87  
88          // Elements that exist but don't have the requisite number
89          // of hits shouldn't be found.
90          assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, "java/lang/String", 2));
91      }
92  
93      @Test
94      void testRegexReplacement() {
95          final MockSegmentConstantPool mockPool = new MockSegmentConstantPool();
96          assertTrue(mockPool.regexMatchesVisible(".*", "anything"));
97          assertTrue(mockPool.regexMatchesVisible(".*", ""));
98          assertTrue(mockPool.regexMatchesVisible("^<init>.*", "<init>"));
99          assertTrue(mockPool.regexMatchesVisible("^<init>.*", "<init>stuff"));
100         assertFalse(mockPool.regexMatchesVisible("^<init>.*", "init>stuff"));
101         assertFalse(mockPool.regexMatchesVisible("^<init>.*", "<init"));
102         assertFalse(mockPool.regexMatchesVisible("^<init>.*", ""));
103     }
104 }