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