1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.classfile;
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.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import java.io.File;
28 import java.io.IOException;
29
30 import org.apache.bcel.AbstractTest;
31 import org.apache.bcel.Const;
32 import org.apache.bcel.util.SyntheticRepository;
33 import org.apache.bcel.visitors.CountingVisitor;
34 import org.junit.jupiter.api.Test;
35
36 class RecordTest extends AbstractTest {
37
38
39
40
41 @Test
42 void recordsCanBeCopied() throws ClassNotFoundException, IOException {
43 final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
44 final JavaClass copyClazz = clazz.copy();
45 assertEquals(clazz.toString(), copyClazz.toString(), "both records should have the same value");
46 }
47
48
49
50
51 @Test
52 void recordsCanBeVisited() throws ClassNotFoundException, IOException {
53 final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
54 final CountingVisitor countVisitor = new CountingVisitor();
55 final DescendingVisitor desendingVisitor = new DescendingVisitor(clazz, countVisitor);
56 desendingVisitor.visit();
57 assertEquals(1, countVisitor.recordCount, "should count one record");
58 assertEquals(2, countVisitor.recordComponentCount, "should count two record components");
59 }
60
61
62
63
64 @Test
65 void testAttributeSerializtion() throws ClassNotFoundException, IOException {
66 final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
67 final File tfile = createTestdataFile("SimpleRecord.class");
68 final Record recordAttribute = (Record) findAttribute("Record", clazz)[0];
69 clazz.dump(tfile);
70
71 final SyntheticRepository repos2 = createRepos(".");
72 final JavaClass clazzFromRepo = repos2.loadClass("SimpleRecord");
73 assertNotNull(clazzFromRepo);
74 final Record recordAttributeFromRepo = (Record) findAttribute("Record", clazzFromRepo)[0];
75 assertEquals(recordAttribute.toString(), recordAttributeFromRepo.toString(), "Both attributes need to be equal");
76 tfile.deleteOnExit();
77 }
78
79
80
81
82
83
84
85
86
87 @Test
88 void testRecordClassSaysItIs() throws ClassNotFoundException, ClassFormatException, IOException {
89 final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
90 assertTrue(clazz.isRecord(), "Expected SimpleRecord class to say it was a record - but it didn't!");
91 final JavaClass simpleClazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.SimpleClass");
92 assertFalse(simpleClazz.isRecord(), "Expected SimpleClass class to say it was not a record - but it didn't!");
93 }
94
95 @Test
96 void testRecordComponentGetAttribute() throws ClassFormatException, IOException {
97 final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
98 final Record recordAttribute = (Record) findAttribute("Record", clazz)[0];
99 final RecordComponentInfo secondComponent = recordAttribute.getComponents()[1];
100 final RuntimeVisibleAnnotations ann = secondComponent.getAttribute(Const.ATTR_RUNTIME_VISIBLE_ANNOTATIONS);
101 assertEquals("RuntimeVisibleAnnotations:\n"
102 + " @Ljavax/annotation/Nonnull;", ann.toString());
103 assertNull(secondComponent.getAttribute(Const.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS));
104 }
105
106
107
108
109
110
111
112
113
114 @Test
115 void testRecordToString() throws ClassNotFoundException, ClassFormatException, IOException {
116 final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
117 final Attribute[] attributes = clazz.getAttributes();
118 final Record recordAttribute = (Record) findAttribute("Record", clazz)[0];
119 assertEquals(4, attributes.length);
120 assertEquals("SourceFile: SimpleRecord.java", attributes[0].toString());
121 assertEquals("Record(2):\n"
122 + " RecordComponentInfo(aNumber,I,0):\n"
123 + " RecordComponentInfo(aString,Ljava/lang/String;,1):\n"
124 + " RuntimeVisibleAnnotations:\n"
125 + " @Ljavax/annotation/Nonnull;", recordAttribute.toString());
126 final RecordComponentInfo firstComponent = recordAttribute.getComponents()[0];
127 assertEquals(5, firstComponent.getIndex());
128 assertEquals(6, firstComponent.getDescriptorIndex());
129 assertEquals(0, firstComponent.getAttributes().length);
130 assertEquals(recordAttribute.getConstantPool(), firstComponent.getConstantPool());
131 assertEquals("RecordComponentInfo(aNumber,I,0):", firstComponent.toString());
132 }
133 }