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  package org.apache.commons.compress.harmony.unpack200;
20  
21  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  
26  import java.util.stream.Stream;
27  
28  import org.apache.commons.compress.harmony.pack200.Codec;
29  import org.apache.commons.compress.harmony.pack200.Pack200Exception;
30  import org.apache.commons.compress.harmony.unpack200.bytecode.CPUTF8;
31  import org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry;
32  import org.junit.jupiter.api.Test;
33  import org.junit.jupiter.params.ParameterizedTest;
34  import org.junit.jupiter.params.provider.Arguments;
35  import org.junit.jupiter.params.provider.MethodSource;
36  
37  class AttributeLayoutTest {
38  
39      public class TestSegment extends Segment {
40  
41          private ClassFileEntry entry(final String string) {
42              return new CPUTF8(string);
43          }
44  
45          @Override
46          public SegmentConstantPool getConstantPool() {
47              final ClassFileEntry[][] data = { {}, // ALL
48                      { entry("Zero"), entry("One"), entry("Two"), entry("Three"), entry("Four"), entry("Five"), entry("Six"), entry("Seven"), entry("Eight"),
49                              entry("Nine") }, // UTF-8
50                      {}, {}, {}, {}, {}, {}, { entry("Eins"), entry("Zwei"), entry("Drei"), entry("Vier"), entry("Funf"), entry("Sechs"), entry("Sieben"),
51                              entry("Acht"), entry("Neun") }, // Signature
52              };
53              return new SegmentConstantPool(null) {
54  
55                  @Override
56                  public ClassFileEntry getValue(final int cp, final long index) {
57                      if (index == -1) {
58                          return null;
59                      }
60                      return data[cp][(int) index];
61                  }
62  
63              };
64          }
65      }
66  
67      static Stream<Arguments> badData() {
68          return Stream.of(Arguments.of(null, AttributeLayout.CONTEXT_CLASS, ""), Arguments.of("", AttributeLayout.CONTEXT_CLASS, ""),
69                  Arguments.of("name", -1, ""), Arguments.of("name", 1234, ""));
70      }
71  
72      static Stream<Arguments> codec() {
73          return Stream.of(Arguments.of("O", AttributeLayout.CONTEXT_CLASS, "HOBS", Codec.BRANCH5),
74                  Arguments.of("P", AttributeLayout.CONTEXT_METHOD, "PIN", Codec.BCI5), Arguments.of("S", AttributeLayout.CONTEXT_FIELD, "HS", Codec.SIGNED5),
75                  Arguments.of("RS", AttributeLayout.CONTEXT_CODE, "RRRS", Codec.UNSIGNED5),
76                  Arguments.of("KS", AttributeLayout.CONTEXT_CLASS, "RKS", Codec.UNSIGNED5),
77                  Arguments.of("B", AttributeLayout.CONTEXT_CLASS, "TRKSB", Codec.BYTE1));
78      }
79  
80      static Stream<Arguments> okData() {
81          return Stream.of(Arguments.of("name", AttributeLayout.CONTEXT_CLASS, ""), Arguments.of("name", AttributeLayout.CONTEXT_METHOD, ""),
82                  Arguments.of("name", AttributeLayout.CONTEXT_FIELD, ""), Arguments.of("name", AttributeLayout.CONTEXT_CODE, ""));
83      }
84  
85      @ParameterizedTest
86      @MethodSource("badData")
87      void testBadData(final String name, final int context, final String layout) {
88          assertThrows(Pack200Exception.class, () -> new AttributeLayout(name, context, layout, -1));
89      }
90  
91      @ParameterizedTest
92      @MethodSource("codec")
93      void testGetCodec(final String name, final int context, final String layout, final Codec expectedCodec) throws Pack200Exception {
94          final AttributeLayout attributeLayout = new AttributeLayout(name, context, layout, 1);
95          assertEquals(expectedCodec, attributeLayout.getCodec());
96      }
97  
98      @Test
99      void testLayoutRS() throws Pack200Exception {
100         final AttributeLayout layout = new AttributeLayout("RS", AttributeLayout.CONTEXT_CLASS, "RS", 1);
101         final Segment segment = new TestSegment();
102         assertNull(layout.getValue(-1, segment.getConstantPool()));
103         assertEquals("Eins", ((CPUTF8) layout.getValue(0, segment.getConstantPool())).underlyingString());
104         assertEquals("Zwei", ((CPUTF8) layout.getValue(1, segment.getConstantPool())).underlyingString());
105     }
106 
107     @Test
108     void testLayoutRSN() throws Pack200Exception {
109         final AttributeLayout layout = new AttributeLayout("RSN", AttributeLayout.CONTEXT_CLASS, "RSN", 1);
110         final Segment segment = new TestSegment();
111         assertNull(layout.getValue(0, segment.getConstantPool()));
112         assertEquals("Eins", ((CPUTF8) layout.getValue(1, segment.getConstantPool())).underlyingString());
113         assertEquals("Zwei", ((CPUTF8) layout.getValue(2, segment.getConstantPool())).underlyingString());
114     }
115 
116     @Test
117     void testLayoutRU() throws Pack200Exception {
118         final AttributeLayout layout = new AttributeLayout("RU", AttributeLayout.CONTEXT_CLASS, "RU", 1);
119         final Segment segment = new TestSegment();
120         assertNull(layout.getValue(-1, segment.getConstantPool()));
121         assertEquals("Zero", ((CPUTF8) layout.getValue(0, segment.getConstantPool())).underlyingString());
122         assertEquals("One", ((CPUTF8) layout.getValue(1, segment.getConstantPool())).underlyingString());
123     }
124 
125     @Test
126     void testLayoutRUN() throws Pack200Exception {
127         final AttributeLayout layout = new AttributeLayout("RUN", AttributeLayout.CONTEXT_CLASS, "RUN", 1);
128         final Segment segment = new TestSegment();
129         assertNull(layout.getValue(0, segment.getConstantPool()));
130         assertEquals("Zero", ((CPUTF8) layout.getValue(1, segment.getConstantPool())).underlyingString());
131         assertEquals("One", ((CPUTF8) layout.getValue(2, segment.getConstantPool())).underlyingString());
132     }
133 
134     @ParameterizedTest
135     @MethodSource("okData")
136     void testOkData(final String name, final int context, final String layout) {
137         assertDoesNotThrow(() -> new AttributeLayout(name, context, layout, -1));
138     }
139 }