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.commons.compress.harmony.unpack200.bytecode;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotEquals;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import org.junit.jupiter.api.Test;
27  
28  class ClassFileEntryTest {
29  
30      private void checkEquality(final Object equal1, final Object equal2, final String toString, final Object unequal) {
31          assertEquals(equal1, equal2);
32          assertEquals(equal1.hashCode(), equal2.hashCode());
33          assertTrue(equal1.toString().contains(toString));
34          assertNotEquals(equal1, unequal);
35          assertNotEquals(equal2, unequal);
36          assertNotEquals(unequal, equal1);
37          assertNotEquals(unequal, equal2);
38      }
39  
40      @Test
41      void testCPDouble() {
42          final CPDouble cp1 = new CPDouble(Double.valueOf(3), 3);
43          final CPDouble cp2 = new CPDouble(Double.valueOf(3), 3);
44          final CPDouble cp3 = new CPDouble(Double.valueOf(5), 5);
45          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
46      }
47  
48      @Test
49      void testCPField() {
50          final CPMember cp1 = new CPMember(new CPUTF8("Name", 3), new CPUTF8("I", 4), 0, null);
51          final CPMember cp2 = new CPMember(new CPUTF8("Name", 3), new CPUTF8("I", 4), 0, null);
52          final CPMember cp3 = new CPMember(new CPUTF8("Name", 3), new CPUTF8("Z", 5), 0, null);
53          final CPMember cp4 = new CPMember(new CPUTF8("GName", 6), new CPUTF8("I", 4), 0, null);
54          checkEquality(cp1, cp2, "Name", cp3); //$NON-NLS-1$
55          checkEquality(cp1, cp2, "I", cp4); //$NON-NLS-1$
56      }
57  
58      @Test
59      void testCPFloat() {
60          final CPFloat cp1 = new CPFloat(Float.valueOf(3), 3);
61          final CPFloat cp2 = new CPFloat(Float.valueOf(3), 3);
62          final CPFloat cp3 = new CPFloat(Float.valueOf(5), 5);
63          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
64      }
65  
66      @Test
67      void testCPInteger() {
68          final CPInteger cp1 = new CPInteger(Integer.valueOf(3), 3);
69          final CPInteger cp2 = new CPInteger(Integer.valueOf(3), 3);
70          final CPInteger cp3 = new CPInteger(Integer.valueOf(5), 5);
71          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
72      }
73  
74      @Test
75      void testCPLong() {
76          final CPLong cp1 = new CPLong(Long.valueOf(3), 3);
77          final CPLong cp2 = new CPLong(Long.valueOf(3), 3);
78          final CPLong cp3 = new CPLong(Long.valueOf(5), 5);
79          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
80      }
81  
82      @Test
83      void testCPString() {
84          final CPString cp1 = new CPString(new CPUTF8("3", 3), 3);
85          final CPString cp2 = new CPString(new CPUTF8("3", 3), 3);
86          final CPString cp3 = new CPString(new CPUTF8("5", 5), 5);
87          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
88      }
89  
90      @Test
91      void testSourceAttribute() {
92          final SourceFileAttribute sfa1 = new SourceFileAttribute(new CPUTF8("Thing.java", 1)); //$NON-NLS-1$
93          final SourceFileAttribute sfa2 = new SourceFileAttribute(new CPUTF8("Thing.java", 1)); //$NON-NLS-1$
94          final SourceFileAttribute sfa3 = new SourceFileAttribute(new CPUTF8("OtherThing.java", 2)); //$NON-NLS-1$
95          checkEquality(sfa1, sfa2, "Thing.java", sfa3); //$NON-NLS-1$
96      }
97  
98      @Test
99      void testUTF8() {
100         final CPUTF8 u1 = new CPUTF8("thing", 1); //$NON-NLS-1$
101         final CPUTF8 u2 = new CPUTF8("thing", 1); //$NON-NLS-1$
102         final CPUTF8 u3 = new CPUTF8("otherthing", 2); //$NON-NLS-1$
103         checkEquality(u1, u2, "thing", u3);
104     }
105 
106 }