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 this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to You under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * 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, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package org.apache.commons.compress.harmony.unpack200.bytecode;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotEquals;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import org.junit.jupiter.api.Test;
24  
25  public class ClassFileEntryTest {
26  
27      private void checkEquality(final Object equal1, final Object equal2, final String toString, final Object unequal) {
28          assertEquals(equal1, equal2);
29          assertEquals(equal1.hashCode(), equal2.hashCode());
30          assertTrue(equal1.toString().indexOf(toString) >= 0);
31          assertNotEquals(equal1, unequal);
32          assertNotEquals(equal2, unequal);
33          assertNotEquals(unequal, equal1);
34          assertNotEquals(unequal, equal2);
35      }
36  
37      @Test
38      public void testCPDouble() {
39          final CPDouble cp1 = new CPDouble(Double.valueOf(3), 3);
40          final CPDouble cp2 = new CPDouble(Double.valueOf(3), 3);
41          final CPDouble cp3 = new CPDouble(Double.valueOf(5), 5);
42          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
43      }
44  
45      @Test
46      public void testCPField() {
47          final CPMember cp1 = new CPMember(new CPUTF8("Name", 3), new CPUTF8("I", 4), 0, null);
48          final CPMember cp2 = new CPMember(new CPUTF8("Name", 3), new CPUTF8("I", 4), 0, null);
49          final CPMember cp3 = new CPMember(new CPUTF8("Name", 3), new CPUTF8("Z", 5), 0, null);
50          final CPMember cp4 = new CPMember(new CPUTF8("GName", 6), new CPUTF8("I", 4), 0, null);
51          checkEquality(cp1, cp2, "Name", cp3); //$NON-NLS-1$
52          checkEquality(cp1, cp2, "I", cp4); //$NON-NLS-1$
53      }
54  
55      @Test
56      public void testCPFloat() {
57          final CPFloat cp1 = new CPFloat(Float.valueOf(3), 3);
58          final CPFloat cp2 = new CPFloat(Float.valueOf(3), 3);
59          final CPFloat cp3 = new CPFloat(Float.valueOf(5), 5);
60          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
61      }
62  
63      @Test
64      public void testCPInteger() {
65          final CPInteger cp1 = new CPInteger(Integer.valueOf(3), 3);
66          final CPInteger cp2 = new CPInteger(Integer.valueOf(3), 3);
67          final CPInteger cp3 = new CPInteger(Integer.valueOf(5), 5);
68          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
69      }
70  
71      @Test
72      public void testCPLong() {
73          final CPLong cp1 = new CPLong(Long.valueOf(3), 3);
74          final CPLong cp2 = new CPLong(Long.valueOf(3), 3);
75          final CPLong cp3 = new CPLong(Long.valueOf(5), 5);
76          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
77      }
78  
79      @Test
80      public void testCPString() {
81          final CPString cp1 = new CPString(new CPUTF8("3", 3), 3);
82          final CPString cp2 = new CPString(new CPUTF8("3", 3), 3);
83          final CPString cp3 = new CPString(new CPUTF8("5", 5), 5);
84          checkEquality(cp1, cp2, "3", cp3); //$NON-NLS-1$
85      }
86  
87      @Test
88      public void testSourceAttribute() {
89          final SourceFileAttribute sfa1 = new SourceFileAttribute(new CPUTF8("Thing.java", 1)); //$NON-NLS-1$
90          final SourceFileAttribute sfa2 = new SourceFileAttribute(new CPUTF8("Thing.java", 1)); //$NON-NLS-1$
91          final SourceFileAttribute sfa3 = new SourceFileAttribute(new CPUTF8("OtherThing.java", 2)); //$NON-NLS-1$
92          checkEquality(sfa1, sfa2, "Thing.java", sfa3); //$NON-NLS-1$
93      }
94  
95      @Test
96      public void testUTF8() {
97          final CPUTF8 u1 = new CPUTF8("thing", 1); //$NON-NLS-1$
98          final CPUTF8 u2 = new CPUTF8("thing", 1); //$NON-NLS-1$
99          final CPUTF8 u3 = new CPUTF8("otherthing", 2); //$NON-NLS-1$
100         checkEquality(u1, u2, "thing", u3);
101     }
102 
103 }