1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
67 assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray, "NotThere", "^<init>.*", 0));
68
69
70
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
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
86 assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, "NotThere", 0));
87
88
89
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 }