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.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.IOException;
26  import java.util.List;
27  
28  import org.apache.commons.compress.harmony.pack200.Pack200Exception;
29  import org.apache.commons.compress.harmony.unpack200.NewAttributeBands.Call;
30  import org.apache.commons.compress.harmony.unpack200.NewAttributeBands.Callable;
31  import org.apache.commons.compress.harmony.unpack200.NewAttributeBands.Integral;
32  import org.apache.commons.compress.harmony.unpack200.NewAttributeBands.Reference;
33  import org.apache.commons.compress.harmony.unpack200.NewAttributeBands.Replication;
34  import org.apache.commons.compress.harmony.unpack200.NewAttributeBands.Union;
35  import org.apache.commons.compress.harmony.unpack200.NewAttributeBands.UnionCase;
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.params.ParameterizedTest;
38  import org.junit.jupiter.params.provider.ValueSource;
39  
40  /**
41   * Tests for unpack200 support for non-predefined attributes
42   */
43  class NewAttributeBandsTest extends AbstractBandsTest {
44  
45      private final class MockNewAttributeBands extends NewAttributeBands {
46  
47          MockNewAttributeBands(final Segment segment, final AttributeLayout layout) throws IOException {
48              super(segment, layout);
49          }
50  
51          public List<?> getLayoutElements() {
52              return attributeLayoutElements;
53          }
54      }
55  
56      @Test
57      void testEmptyLayout() throws IOException, Pack200Exception {
58          final MockNewAttributeBands newAttributeBands = new MockNewAttributeBands(new MockSegment(),
59                  new AttributeLayout("test", AttributeLayout.CONTEXT_CLASS, "", 25));
60          final List<?> layoutElements = newAttributeBands.getLayoutElements();
61          assertEquals(0, layoutElements.size());
62      }
63  
64      @ParameterizedTest
65      @ValueSource(strings = { "B", "FB", "SB", "H", "FH", "SH", "I", "FI", "SI", "PB", "OB", "OSB", "POB", "PH", "OH", "OSH", "POH", "PI", "OI", "OSI", "POI" })
66      void testIntegralLayout(final String layoutStr) throws IOException, Pack200Exception {
67          final MockNewAttributeBands newAttributeBands = new MockNewAttributeBands(new MockSegment(),
68                  new AttributeLayout("test", AttributeLayout.CONTEXT_CLASS, layoutStr, 25));
69          final List layoutElements = newAttributeBands.getLayoutElements();
70          assertEquals(1, layoutElements.size());
71          final Integral element = (Integral) layoutElements.get(0);
72          assertEquals(layoutStr, element.getTag());
73      }
74  
75      @Test
76      void testLayoutWithBackwardsCall() throws IOException, Pack200Exception {
77          MockNewAttributeBands newAttributeBands = new MockNewAttributeBands(new MockSegment(),
78                  new AttributeLayout("test", AttributeLayout.CONTEXT_METHOD, "[NH[(1)]][KIH][(-1)]", 20));
79          List layoutElements = newAttributeBands.getLayoutElements();
80          assertEquals(3, layoutElements.size());
81          Callable firstCallable = (Callable) layoutElements.get(0);
82          Callable secondCallable = (Callable) layoutElements.get(1);
83          Callable thirdCallable = (Callable) layoutElements.get(2);
84          List thirdBody = thirdCallable.getBody();
85          assertEquals(1, thirdBody.size());
86          Call call = (Call) thirdBody.get(0);
87          assertEquals(secondCallable, call.getCallable());
88          assertTrue(secondCallable.isBackwardsCallable());
89          assertFalse(firstCallable.isBackwardsCallable());
90          assertFalse(thirdCallable.isBackwardsCallable());
91  
92          newAttributeBands = new MockNewAttributeBands(new MockSegment(),
93                  new AttributeLayout("test", AttributeLayout.CONTEXT_METHOD, "[NH[(1)]][KIH][(-2)]", 20));
94          layoutElements = newAttributeBands.getLayoutElements();
95          assertEquals(3, layoutElements.size());
96          firstCallable = (Callable) layoutElements.get(0);
97          secondCallable = (Callable) layoutElements.get(1);
98          thirdCallable = (Callable) layoutElements.get(2);
99          thirdBody = thirdCallable.getBody();
100         assertEquals(1, thirdBody.size());
101         call = (Call) thirdBody.get(0);
102         assertEquals(firstCallable, call.getCallable());
103         assertTrue(firstCallable.isBackwardsCallable());
104         assertFalse(secondCallable.isBackwardsCallable());
105         assertFalse(thirdCallable.isBackwardsCallable());
106 
107         newAttributeBands = new MockNewAttributeBands(new MockSegment(),
108                 new AttributeLayout("test", AttributeLayout.CONTEXT_METHOD, "[NH[(1)]][KIH][(0)]", 20));
109         layoutElements = newAttributeBands.getLayoutElements();
110         assertEquals(3, layoutElements.size());
111         firstCallable = (Callable) layoutElements.get(0);
112         secondCallable = (Callable) layoutElements.get(1);
113         thirdCallable = (Callable) layoutElements.get(2);
114         thirdBody = thirdCallable.getBody();
115         assertEquals(1, thirdBody.size());
116         call = (Call) thirdBody.get(0);
117         assertEquals(thirdCallable, call.getCallable());
118         assertTrue(thirdCallable.isBackwardsCallable());
119         assertFalse(firstCallable.isBackwardsCallable());
120         assertFalse(secondCallable.isBackwardsCallable());
121         assertFalse(firstCallable.isBackwardsCallable());
122     }
123 
124     @Test
125     void testLayoutWithCalls() throws IOException, Pack200Exception {
126         // @formatter:off
127         final MockNewAttributeBands newAttributeBands = new MockNewAttributeBands(new MockSegment(), new AttributeLayout("test", AttributeLayout.CONTEXT_FIELD,
128             "[NH[(1)]][RSH NH[RUH(1)]][TB(66,67,73,83,90)[KIH](68)[KDH](70)[KFH](74)[KJH](99)[RSH](101)[RSH RUH](115)[RUH](91)[NH[(0)]](64)[RSH[RUH(0)]]()[]]",
129             26));
130         // @formatter:on
131         final List layoutElements = newAttributeBands.getLayoutElements();
132         assertEquals(3, layoutElements.size());
133         final Callable firstCallable = (Callable) layoutElements.get(0);
134         final Callable secondCallable = (Callable) layoutElements.get(1);
135         final Callable thirdCallable = (Callable) layoutElements.get(2);
136         final List firstBody = firstCallable.getBody();
137         assertEquals(1, firstBody.size());
138         final Replication rep = (Replication) firstBody.get(0);
139         final List repBody = rep.getLayoutElements();
140         assertEquals(1, repBody.size());
141         final Call call = (Call) repBody.get(0);
142         assertEquals(1, call.getCallableIndex());
143         assertEquals(secondCallable, call.getCallable());
144         assertFalse(firstCallable.isBackwardsCallable());
145         assertFalse(secondCallable.isBackwardsCallable());
146         assertFalse(thirdCallable.isBackwardsCallable());
147     }
148 
149     @ParameterizedTest
150     @ValueSource(strings = { "KIB", "KIH", "KII", "KINH", "KJH", "KDH", "KSH", "KQH", "RCH", "RSH", "RDH", "RFH", "RMH", "RIH", "RUH", "RQH", "RQNH", "RQNI" })
151     void testReferenceLayouts(final String layout) throws IOException, Pack200Exception {
152         final MockNewAttributeBands newAttributeBands = new MockNewAttributeBands(new MockSegment(),
153                 new AttributeLayout("test", AttributeLayout.CONTEXT_CODE, layout, 26));
154         final List layoutElements = newAttributeBands.getLayoutElements();
155         assertEquals(1, layoutElements.size());
156         final Reference element = (Reference) layoutElements.get(0);
157         assertEquals(layout, element.getTag());
158     }
159 
160     @Test
161     void testReplicationLayout() throws IOException, Pack200Exception {
162         final MockNewAttributeBands newAttributeBands = new MockNewAttributeBands(new MockSegment(),
163                 new AttributeLayout("test", AttributeLayout.CONTEXT_CLASS, "NH[PHOHRUHRSHH]", 25));
164         final List layoutElements = newAttributeBands.getLayoutElements();
165         assertEquals(1, layoutElements.size());
166         final Replication element = (Replication) layoutElements.get(0);
167         final Integral countElement = element.getCountElement();
168         assertEquals("H", countElement.getTag());
169         final List replicatedElements = element.getLayoutElements();
170         assertEquals(5, replicatedElements.size());
171         final Integral firstElement = (Integral) replicatedElements.get(0);
172         assertEquals("PH", firstElement.getTag());
173         final Integral secondElement = (Integral) replicatedElements.get(1);
174         assertEquals("OH", secondElement.getTag());
175         final Reference thirdElement = (Reference) replicatedElements.get(2);
176         assertEquals("RUH", thirdElement.getTag());
177         final Reference fourthElement = (Reference) replicatedElements.get(3);
178         assertEquals("RSH", fourthElement.getTag());
179         final Integral fifthElement = (Integral) replicatedElements.get(4);
180         assertEquals("H", fifthElement.getTag());
181     }
182 
183     @Test
184     void testUnionLayout() throws IOException, Pack200Exception {
185         final MockNewAttributeBands newAttributeBands = new MockNewAttributeBands(new MockSegment(),
186                 new AttributeLayout("test", AttributeLayout.CONTEXT_CODE, "TB(55)[FH](23)[]()[RSH]", 26));
187         final List layoutElements = newAttributeBands.getLayoutElements();
188         assertEquals(1, layoutElements.size());
189         final Union element = (Union) layoutElements.get(0);
190         final Integral tag = element.getUnionTag();
191         assertEquals("B", tag.getTag());
192         final List unionCases = element.getUnionCases();
193         assertEquals(2, unionCases.size());
194         final UnionCase firstCase = (UnionCase) unionCases.get(0);
195         assertTrue(firstCase.hasTag(55));
196         assertFalse(firstCase.hasTag(23));
197         List body = firstCase.getBody();
198         assertEquals(1, body.size());
199         final Integral bodyElement = (Integral) body.get(0);
200         assertEquals("FH", bodyElement.getTag());
201         final UnionCase secondCase = (UnionCase) unionCases.get(1);
202         assertTrue(secondCase.hasTag(23));
203         assertFalse(secondCase.hasTag(55));
204         body = secondCase.getBody();
205         assertEquals(0, body.size());
206         final List defaultBody = element.getDefaultCaseBody();
207         assertEquals(1, defaultBody.size());
208         final Reference ref = (Reference) defaultBody.get(0);
209         assertEquals("RSH", ref.getTag());
210     }
211 
212 }