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.archivers.zip;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotEquals;
24  import static org.junit.jupiter.api.Assertions.assertNotSame;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * JUnit tests for org.apache.commons.compress.archivers.zip.ZipShort.
30   */
31  class ZipShortTest {
32  
33      @Test
34      void testClone() {
35          final ZipShort s1 = new ZipShort(42);
36          final ZipShort s2 = (ZipShort) s1.clone();
37          assertNotSame(s1, s2);
38          assertEquals(s1, s2);
39          assertEquals(s1.getValue(), s2.getValue());
40      }
41  
42      /**
43       * Test the contract of the equals method.
44       */
45      @Test
46      void testEquals() {
47          final ZipShort zs = new ZipShort(0x1234);
48          final ZipShort zs2 = new ZipShort(0x1234);
49          final ZipShort zs3 = new ZipShort(0x5678);
50  
51          assertEquals(zs, zs, "reflexive");
52  
53          assertEquals(zs, zs2, "works");
54          assertNotEquals(zs, zs3, "works, part two");
55  
56          assertEquals(zs2, zs, "symmetric");
57  
58          assertNotEquals(null, zs, "null handling");
59          assertNotEquals(zs, Integer.valueOf(0x1234), "non ZipShort handling");
60      }
61  
62      /**
63       * Test conversion from bytes.
64       */
65      @Test
66      void testFromBytes() {
67          final byte[] val = { 0x34, 0x12 };
68          final ZipShort zs = new ZipShort(val);
69          assertEquals(0x1234, zs.getValue(), "value from bytes");
70      }
71  
72      /**
73       * Test conversion to bytes.
74       */
75      @Test
76      void testPut() {
77          final byte[] arr = new byte[3];
78          ZipShort.putShort(0x1234, arr, 1);
79          assertEquals(0x34, arr[1], "first byte getBytes");
80          assertEquals(0x12, arr[2], "second byte getBytes");
81      }
82  
83      /**
84       * Test sign handling.
85       */
86      @Test
87      void testSign() {
88          final ZipShort zs = new ZipShort(new byte[] { (byte) 0xFF, (byte) 0xFF });
89          assertEquals(0x0000FFFF, zs.getValue());
90      }
91  
92      /**
93       * Test conversion to bytes.
94       */
95      @Test
96      void testToBytes() {
97          final ZipShort zs = new ZipShort(0x1234);
98          final byte[] result = zs.getBytes();
99          assertEquals(2, result.length, "length getBytes");
100         assertEquals(0x34, result[0], "first byte getBytes");
101         assertEquals(0x12, result[1], "second byte getBytes");
102     }
103 }