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