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  
20  package org.apache.bcel.generic;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertNotNull;
25  import static org.junit.jupiter.api.Assertions.assertNull;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  
28  import org.apache.bcel.Const;
29  import org.apache.bcel.classfile.ConstantLong;
30  import org.apache.bcel.classfile.ConstantPool;
31  import org.apache.bcel.classfile.Field;
32  import org.junit.jupiter.api.Disabled;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.params.ParameterizedTest;
35  import org.junit.jupiter.params.provider.ValueSource;
36  
37  /**
38   * Tests {@link FieldGen}.
39   */
40  class FieldGenTest {
41  
42      @Test
43      @Disabled
44      void testBCELComparator() throws Exception {
45          final ConstantLong[] constantPool = { new ConstantLong(0), new ConstantLong(0) };
46          final FieldGen obj = new FieldGen(new Field(0, 0, 0, null, new ConstantPool(constantPool)), new ConstantPoolGen(constantPool));
47          assertTrue(FieldGen.getComparator().equals(null, null));
48          assertTrue(FieldGen.getComparator().equals(obj, obj));
49          assertFalse(FieldGen.getComparator().equals(obj, null));
50          assertFalse(FieldGen.getComparator().equals(null, obj));
51      }
52  
53      @ParameterizedTest
54      @ValueSource(booleans = { false, true })
55      void testBooleanInitValue(final boolean value) {
56          final FieldGen fg = new FieldGen(Const.ACC_PUBLIC | Const.ACC_STATIC | Const.ACC_FINAL, Type.BOOLEAN, "VALUE", new ConstantPoolGen());
57          fg.setInitValue(value);
58          final Field field = fg.getField();
59          assertNotNull(field.getConstantValue());
60          assertEquals(value ? 1 : 0, field.getConstantPool().getConstantInteger(field.getConstantValue().getConstantValueIndex()).getBytes());
61      }
62  
63      @ParameterizedTest
64      @ValueSource(booleans = { false, true })
65      void testBooleanInitValueReplacement(final boolean value) {
66          final FieldGen fg = new FieldGen(Const.ACC_PUBLIC | Const.ACC_STATIC | Const.ACC_FINAL, Type.BOOLEAN, "VALUE", new ConstantPoolGen());
67          fg.setInitValue(!value);
68          fg.setInitValue(value);
69          final Field field = fg.getField();
70          assertNotNull(field.getConstantValue());
71          assertEquals(value ? 1 : 0, field.getConstantPool().getConstantInteger(field.getConstantValue().getConstantValueIndex()).getBytes());
72      }
73  
74      @ParameterizedTest
75      @ValueSource(booleans = { false, true })
76      void testCancelBooleanInitValue(final boolean value) {
77          final FieldGen fg = new FieldGen(Const.ACC_PUBLIC | Const.ACC_STATIC | Const.ACC_FINAL, Type.BOOLEAN, "VALUE", new ConstantPoolGen());
78          fg.setInitValue(value);
79          fg.cancelInitValue();
80          assertNull(fg.getField().getConstantValue());
81      }
82  }