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