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