View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * Check that we can copy a attribute correctly.
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       * Check that a record can be visited by our visitors
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       * Check that we can save and load the attribute correctly.
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          // Read in the new version and check it is OK
69          final SyntheticRepository repos2 = createRepos(".");
70          final JavaClass clazzFromRepo = repos2.loadClass("SimpleRecord");
71          assertNotNull(clazzFromRepo); // Use the variable to avoid a warning
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       * A record type, once compiled, should result in a class file that is
79       * marked such that we can determine from the access flags
80       * (through BCEL) that it is in fact a record.
81       *
82       * @throws IOException if an I/O error occurs.
83       * @throws ClassFormatException
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       * A simple record with two simple fields, an integer and a String field, should
95       * show its content in its string representation.
96       *
97       * @throws ClassNotFoundException
98       * @throws ClassFormatException
99       * @throws IOException if an I/O error occurs.
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 }