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.assertTrue;
24  
25  import org.apache.commons.compress.harmony.unpack200.Segment;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  class ConstantPoolTest {
30  
31      private ClassConstantPool pool;
32  
33      @BeforeEach
34      public void setUp() {
35          pool = new ClassConstantPool();
36      }
37  
38      @Test
39      void testDuplicateField() {
40          final CPMember cp1 = new CPMember(new CPUTF8("name", 1), new CPUTF8("I", 2), 0, null);
41          pool.add(cp1);
42          pool.addNestedEntries();
43          assertEquals(2, pool.size());
44          final CPMember cp2 = new CPMember(new CPUTF8("name", 1), new CPUTF8("I", 2), 0, null);
45          pool.add(cp2);
46          pool.addNestedEntries();
47          assertEquals(2, pool.size());
48      }
49  
50      @Test
51      void testDuplicateUTF8() {
52          final CPUTF8 u1 = new CPUTF8("thing", 1);
53          final CPUTF8 u2 = new CPUTF8("thing", 1);
54          pool.add(u1);
55          pool.add(u2);
56          assertEquals(1, pool.size());
57      }
58  
59      @Test
60      void testEntries() {
61          pool.add(new CPClass(new CPUTF8("RandomClass", 1), 10));
62          pool.add(new CPClass(new CPUTF8("RandomClass2", 2), 20));
63          assertEquals(2, pool.entries().size());
64      }
65  
66      @Test
67      void testIndex() {
68          pool.add(new CPUTF8("OtherThing", 1));
69          final CPUTF8 u1 = new CPUTF8("thing", 2);
70          pool.add(u1);
71          pool.resolve(new Segment());
72          assertTrue(pool.indexOf(u1) > 0);
73      }
74  }