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