1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.classreader.impl; |
34 | |
|
35 | |
import java.io.*; |
36 | |
|
37 | 14 | public class TestElementValueFactory extends TestAnnotationsBase { |
38 | |
private static final int CONST_VALUE_INDEX = 2; |
39 | |
private static final int CONST_VALUE = 3; |
40 | |
private static final int TYPE_NAME_INDEX = 3; |
41 | |
private static final int CONST_NAME_INDEX = 4; |
42 | |
private static final int CLASS_INFO_INDEX = 5; |
43 | |
private static final int TYPE_INDEX = 6; |
44 | |
private static final String TYPE = "Labc;"; |
45 | |
|
46 | |
private ElementValueFactory sut; |
47 | |
|
48 | |
protected void setUp() throws Exception { |
49 | 14 | super.setUp(); |
50 | |
|
51 | 14 | sut = new ElementValueFactory(); |
52 | 14 | } |
53 | |
|
54 | |
public void testCreateByteConstantElementValue() throws Exception { |
55 | 1 | expectLookupInteger(CONST_VALUE_INDEX, CONST_VALUE); |
56 | 1 | doTestCreateConstantElementValue('B', ByteConstantElementValue.class); |
57 | 1 | } |
58 | |
|
59 | |
public void testCreateCharConstantElementValue() throws Exception { |
60 | 1 | expectLookupInteger(CONST_VALUE_INDEX, CONST_VALUE); |
61 | 1 | doTestCreateConstantElementValue('C', CharConstantElementValue.class); |
62 | 1 | } |
63 | |
|
64 | |
public void testCreateDoubleConstantElementValue() throws Exception { |
65 | 1 | expectLookupDouble(CONST_VALUE_INDEX, CONST_VALUE); |
66 | 1 | doTestCreateConstantElementValue('D', DoubleConstantElementValue.class); |
67 | 1 | } |
68 | |
|
69 | |
public void testCreateFloatConstantElementValue() throws Exception { |
70 | 1 | expectLookupFloat(CONST_VALUE_INDEX, CONST_VALUE); |
71 | 1 | doTestCreateConstantElementValue('F', FloatConstantElementValue.class); |
72 | 1 | } |
73 | |
|
74 | |
public void testCreateIntegerConstantElementValue() throws Exception { |
75 | 1 | expectLookupInteger(CONST_VALUE_INDEX, CONST_VALUE); |
76 | 1 | doTestCreateConstantElementValue('I', IntegerConstantElementValue.class); |
77 | 1 | } |
78 | |
|
79 | |
public void testCreateLongConstantElementValue() throws Exception { |
80 | 1 | expectLookupLong(CONST_VALUE_INDEX, CONST_VALUE); |
81 | 1 | doTestCreateConstantElementValue('J', LongConstantElementValue.class); |
82 | 1 | } |
83 | |
|
84 | |
public void testCreateShortConstantElementValue() throws Exception { |
85 | 1 | expectLookupInteger(CONST_VALUE_INDEX, CONST_VALUE); |
86 | 1 | doTestCreateConstantElementValue('S', ShortConstantElementValue.class); |
87 | 1 | } |
88 | |
|
89 | |
public void testCreateBooleanConstantElementValue() throws Exception { |
90 | 1 | expectLookupInteger(CONST_VALUE_INDEX, CONST_VALUE); |
91 | 1 | doTestCreateConstantElementValue('Z', BooleanConstantElementValue.class); |
92 | 1 | } |
93 | |
|
94 | |
public void testCreateStringConstantElementValue() throws Exception { |
95 | 1 | expectLookupUtf8(CONST_VALUE_INDEX, "abc"); |
96 | 1 | doTestCreateConstantElementValue('s', StringConstantElementValue.class); |
97 | 1 | } |
98 | |
|
99 | |
public void testCreateEnumElementValue() throws Exception { |
100 | 1 | expectReadTag('e'); |
101 | 1 | expectReadU2(TYPE_NAME_INDEX); |
102 | 1 | expectReadU2(CONST_NAME_INDEX); |
103 | |
|
104 | 1 | ElementValue elementValue = sut.create(mockConstantPool, mockIn); |
105 | 1 | assertNotNull("ElementValueFactory returned null", elementValue); |
106 | 1 | assertTrue("Not a " + EnumElementValue.class.getSimpleName(), EnumElementValue.class.isInstance(elementValue)); |
107 | 1 | assertEquals("Type name index", TYPE_NAME_INDEX, ((EnumElementValue) elementValue).getTypeNameIndex()); |
108 | 1 | assertEquals("Const name index", CONST_NAME_INDEX, ((EnumElementValue) elementValue).getConstNameIndex()); |
109 | 1 | } |
110 | |
|
111 | |
public void testCreateClassElementValue() throws Exception { |
112 | 1 | expectReadTag('c'); |
113 | 1 | expectReadClassInfoIndex(CLASS_INFO_INDEX); |
114 | |
|
115 | 1 | ElementValue elementValue = sut.create(mockConstantPool, mockIn); |
116 | 1 | assertNotNull("ElementValueFactory returned null", elementValue); |
117 | 1 | assertTrue("Not a " + ClassElementValue.class.getSimpleName(), ClassElementValue.class.isInstance(elementValue)); |
118 | 1 | assertEquals("Class info index", CLASS_INFO_INDEX, ((ClassElementValue) elementValue).getClassInfoIndex()); |
119 | 1 | } |
120 | |
|
121 | |
public void testCreateAnnotationElementValue() throws Exception { |
122 | 1 | expectReadTag('@'); |
123 | 1 | expectReadTypeIndex(TYPE_INDEX); |
124 | 1 | expectLookupUtf8(TYPE_INDEX, TYPE); |
125 | 1 | expectReadNumElementValuePairs(0); |
126 | |
|
127 | 1 | ElementValue elementValue = sut.create(mockConstantPool, mockIn); |
128 | 1 | assertNotNull("ElementValueFactory returned null", elementValue); |
129 | 1 | assertTrue("Not a " + AnnotationElementValue.class.getSimpleName(), AnnotationElementValue.class.isInstance(elementValue)); |
130 | 1 | assertNotNull("Annotation value", ((AnnotationElementValue) elementValue).getAnnotation()); |
131 | 1 | assertEquals("Type index", TYPE_INDEX, ((AnnotationElementValue) elementValue).getAnnotation().getTypeIndex()); |
132 | 1 | assertEquals("Number of element value pairs", 0, ((AnnotationElementValue) elementValue).getAnnotation().getElementValuePairs().size()); |
133 | 1 | } |
134 | |
|
135 | |
public void testCreateArrayElementValue() throws Exception { |
136 | 1 | expectReadTag('['); |
137 | 1 | expectReadNumValues(0); |
138 | |
|
139 | 1 | ElementValue elementValue = sut.create(mockConstantPool, mockIn); |
140 | 1 | assertNotNull("ElementValueFactory returned null", elementValue); |
141 | 1 | assertTrue("Not a " + ArrayElementValue.class.getSimpleName(), ArrayElementValue.class.isInstance(elementValue)); |
142 | 1 | assertNotNull("Number of element values", ((ArrayElementValue) elementValue).getValues().size()); |
143 | 1 | } |
144 | |
|
145 | |
public void testCreateWithUnknownTag() throws Exception { |
146 | 1 | expectReadTag('A'); |
147 | |
|
148 | |
try { |
149 | 1 | sut.create(mockConstantPool, mockIn); |
150 | 0 | fail("Did not fail on illegal tag value"); |
151 | 1 | } catch (IOException ex) { |
152 | |
|
153 | 0 | } |
154 | 1 | } |
155 | |
|
156 | |
private void doTestCreateConstantElementValue(char tag, Class<? extends ElementValue> elementValueClass) throws IOException { |
157 | 9 | expectReadTag(tag); |
158 | 9 | expectReadU2(CONST_VALUE_INDEX); |
159 | |
|
160 | 9 | ElementValue elementValue = sut.create(mockConstantPool, mockIn); |
161 | 9 | assertNotNull("ElementValueFactory returned null", elementValue); |
162 | 9 | assertTrue("Not a " + elementValueClass.getSimpleName(), elementValueClass.isInstance(elementValue)); |
163 | 9 | assertEquals("Const value index", CONST_VALUE_INDEX, ((ConstantElementValue) elementValue).getConstValueIndex()); |
164 | 9 | } |
165 | |
} |