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.ZipLong.
30   */
31  class ZipLongTest {
32  
33      @Test
34      void testClone() {
35          final ZipLong s1 = new ZipLong(42);
36          final ZipLong s2 = (ZipLong) 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 ZipLong zl = new ZipLong(0x12345678);
48          final ZipLong zl2 = new ZipLong(0x12345678);
49          final ZipLong zl3 = new ZipLong(0x87654321);
50  
51          assertEquals(zl, zl, "reflexive");
52  
53          assertEquals(zl, zl2, "works");
54          assertNotEquals(zl, zl3, "works, part two");
55  
56          assertEquals(zl2, zl, "symmetric");
57  
58          assertNotEquals(null, zl, "null handling");
59          assertNotEquals(zl, Integer.valueOf(0x1234), "non ZipLong handling");
60      }
61  
62      /**
63       * Test conversion from bytes.
64       */
65      @Test
66      void testFromBytes() {
67          final byte[] val = { 0x78, 0x56, 0x34, 0x12 };
68          final ZipLong zl = new ZipLong(val);
69          assertEquals(0x12345678, zl.getValue(), "value from bytes");
70      }
71  
72      /**
73       * Test conversion to bytes.
74       */
75      @Test
76      void testPut() {
77          final byte[] arr = new byte[5];
78          ZipLong.putLong(0x12345678, arr, 1);
79          assertEquals(0x78, arr[1], "first byte getBytes");
80          assertEquals(0x56, arr[2], "second byte getBytes");
81          assertEquals(0x34, arr[3], "third byte getBytes");
82          assertEquals(0x12, arr[4], "fourth byte getBytes");
83      }
84  
85      /**
86       * Test sign handling.
87       */
88      @Test
89      void testSign() {
90          ZipLong zl = new ZipLong(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF });
91          assertEquals(0x00000000FFFFFFFFL, zl.getValue());
92          assertEquals(-1, zl.getIntValue());
93  
94          zl = new ZipLong(0xFFFF_FFFFL);
95          assertEquals(0x00000000FFFFFFFFL, zl.getValue());
96          zl = new ZipLong(0xFFFF_FFFF);
97          assertEquals(0xFFFF_FFFF_FFFF_FFFFL, zl.getValue());
98  
99      }
100 
101     /**
102      * Test conversion to bytes.
103      */
104     @Test
105     void testToBytes() {
106         final ZipLong zl = new ZipLong(0x12345678);
107         final byte[] result = zl.getBytes();
108         assertEquals(4, result.length, "length getBytes");
109         assertEquals(0x78, result[0], "first byte getBytes");
110         assertEquals(0x56, result[1], "second byte getBytes");
111         assertEquals(0x34, result[2], "third byte getBytes");
112         assertEquals(0x12, result[3], "fourth byte getBytes");
113     }
114 }