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