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