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