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.assertDoesNotThrow;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import java.util.stream.Stream;
27
28 import org.apache.commons.compress.harmony.pack200.Codec;
29 import org.apache.commons.compress.harmony.pack200.Pack200Exception;
30 import org.apache.commons.compress.harmony.unpack200.bytecode.CPUTF8;
31 import org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.params.ParameterizedTest;
34 import org.junit.jupiter.params.provider.Arguments;
35 import org.junit.jupiter.params.provider.MethodSource;
36
37 class AttributeLayoutTest {
38
39 public class TestSegment extends Segment {
40
41 private ClassFileEntry entry(final String string) {
42 return new CPUTF8(string);
43 }
44
45 @Override
46 public SegmentConstantPool getConstantPool() {
47 final ClassFileEntry[][] data = { {},
48 { entry("Zero"), entry("One"), entry("Two"), entry("Three"), entry("Four"), entry("Five"), entry("Six"), entry("Seven"), entry("Eight"),
49 entry("Nine") },
50 {}, {}, {}, {}, {}, {}, { entry("Eins"), entry("Zwei"), entry("Drei"), entry("Vier"), entry("Funf"), entry("Sechs"), entry("Sieben"),
51 entry("Acht"), entry("Neun") },
52 };
53 return new SegmentConstantPool(null) {
54
55 @Override
56 public ClassFileEntry getValue(final int cp, final long index) {
57 if (index == -1) {
58 return null;
59 }
60 return data[cp][(int) index];
61 }
62
63 };
64 }
65 }
66
67 static Stream<Arguments> badData() {
68 return Stream.of(Arguments.of(null, AttributeLayout.CONTEXT_CLASS, ""), Arguments.of("", AttributeLayout.CONTEXT_CLASS, ""),
69 Arguments.of("name", -1, ""), Arguments.of("name", 1234, ""));
70 }
71
72 static Stream<Arguments> codec() {
73 return Stream.of(Arguments.of("O", AttributeLayout.CONTEXT_CLASS, "HOBS", Codec.BRANCH5),
74 Arguments.of("P", AttributeLayout.CONTEXT_METHOD, "PIN", Codec.BCI5), Arguments.of("S", AttributeLayout.CONTEXT_FIELD, "HS", Codec.SIGNED5),
75 Arguments.of("RS", AttributeLayout.CONTEXT_CODE, "RRRS", Codec.UNSIGNED5),
76 Arguments.of("KS", AttributeLayout.CONTEXT_CLASS, "RKS", Codec.UNSIGNED5),
77 Arguments.of("B", AttributeLayout.CONTEXT_CLASS, "TRKSB", Codec.BYTE1));
78 }
79
80 static Stream<Arguments> okData() {
81 return Stream.of(Arguments.of("name", AttributeLayout.CONTEXT_CLASS, ""), Arguments.of("name", AttributeLayout.CONTEXT_METHOD, ""),
82 Arguments.of("name", AttributeLayout.CONTEXT_FIELD, ""), Arguments.of("name", AttributeLayout.CONTEXT_CODE, ""));
83 }
84
85 @ParameterizedTest
86 @MethodSource("badData")
87 void testBadData(final String name, final int context, final String layout) {
88 assertThrows(Pack200Exception.class, () -> new AttributeLayout(name, context, layout, -1));
89 }
90
91 @ParameterizedTest
92 @MethodSource("codec")
93 void testGetCodec(final String name, final int context, final String layout, final Codec expectedCodec) throws Pack200Exception {
94 final AttributeLayout attributeLayout = new AttributeLayout(name, context, layout, 1);
95 assertEquals(expectedCodec, attributeLayout.getCodec());
96 }
97
98 @Test
99 void testLayoutRS() throws Pack200Exception {
100 final AttributeLayout layout = new AttributeLayout("RS", AttributeLayout.CONTEXT_CLASS, "RS", 1);
101 final Segment segment = new TestSegment();
102 assertNull(layout.getValue(-1, segment.getConstantPool()));
103 assertEquals("Eins", ((CPUTF8) layout.getValue(0, segment.getConstantPool())).underlyingString());
104 assertEquals("Zwei", ((CPUTF8) layout.getValue(1, segment.getConstantPool())).underlyingString());
105 }
106
107 @Test
108 void testLayoutRSN() throws Pack200Exception {
109 final AttributeLayout layout = new AttributeLayout("RSN", AttributeLayout.CONTEXT_CLASS, "RSN", 1);
110 final Segment segment = new TestSegment();
111 assertNull(layout.getValue(0, segment.getConstantPool()));
112 assertEquals("Eins", ((CPUTF8) layout.getValue(1, segment.getConstantPool())).underlyingString());
113 assertEquals("Zwei", ((CPUTF8) layout.getValue(2, segment.getConstantPool())).underlyingString());
114 }
115
116 @Test
117 void testLayoutRU() throws Pack200Exception {
118 final AttributeLayout layout = new AttributeLayout("RU", AttributeLayout.CONTEXT_CLASS, "RU", 1);
119 final Segment segment = new TestSegment();
120 assertNull(layout.getValue(-1, segment.getConstantPool()));
121 assertEquals("Zero", ((CPUTF8) layout.getValue(0, segment.getConstantPool())).underlyingString());
122 assertEquals("One", ((CPUTF8) layout.getValue(1, segment.getConstantPool())).underlyingString());
123 }
124
125 @Test
126 void testLayoutRUN() throws Pack200Exception {
127 final AttributeLayout layout = new AttributeLayout("RUN", AttributeLayout.CONTEXT_CLASS, "RUN", 1);
128 final Segment segment = new TestSegment();
129 assertNull(layout.getValue(0, segment.getConstantPool()));
130 assertEquals("Zero", ((CPUTF8) layout.getValue(1, segment.getConstantPool())).underlyingString());
131 assertEquals("One", ((CPUTF8) layout.getValue(2, segment.getConstantPool())).underlyingString());
132 }
133
134 @ParameterizedTest
135 @MethodSource("okData")
136 void testOkData(final String name, final int context, final String layout) {
137 assertDoesNotThrow(() -> new AttributeLayout(name, context, layout, -1));
138 }
139 }