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 java.math.BigInteger;
24  
25  import org.junit.Test;
26  
27  /**
28   * JUnit testcases for org.apache.commons.compress.archivers.zip.ZipEightByteInteger.
29   *
30   */
31  public class ZipEightByteIntegerTest {
32  
33      /**
34       * Test conversion to bytes.
35       */
36      @Test
37      public void testLongToBytes() {
38          ZipEightByteInteger zl = new ZipEightByteInteger(0xAB12345678l);
39          byte[] result = zl.getBytes();
40          assertEquals("length getBytes", 8, result.length);
41          assertEquals("first byte getBytes", 0x78, result[0]);
42          assertEquals("second byte getBytes", 0x56, result[1]);
43          assertEquals("third byte getBytes", 0x34, result[2]);
44          assertEquals("fourth byte getBytes", 0x12, result[3]);
45          assertEquals("fifth byte getBytes", (byte) 0xAB, result[4]);
46          assertEquals("sixth byte getBytes", 0, result[5]);
47          assertEquals("seventh byte getBytes", 0, result[6]);
48          assertEquals("eighth byte getBytes", 0, result[7]);
49      }
50  
51      /**
52       * Test conversion from bytes.
53       */
54      @Test
55      public void testLongFromBytes() {
56          byte[] val = new byte[] {0x78, 0x56, 0x34, 0x12, (byte) 0xAB, 0x00, 0x00, 0x00};
57          ZipEightByteInteger zl = new ZipEightByteInteger(val);
58          assertEquals("longValue from bytes", 0xAB12345678l, zl.getLongValue());
59      }
60  
61      /**
62       * Test conversion to bytes.
63       */
64      @Test
65      public void testBIToBytes() {
66          ZipEightByteInteger zl =
67              new ZipEightByteInteger(BigInteger.valueOf(Long.MAX_VALUE)
68                                      .shiftLeft(1));
69          byte[] result = zl.getBytes();
70          assertEquals("length getBytes", 8, result.length);
71          assertEquals("first byte getBytes", (byte) 0xFE, result[0]);
72          assertEquals("second byte getBytes", (byte) 0xFF, result[1]);
73          assertEquals("third byte getBytes", (byte) 0xFF, result[2]);
74          assertEquals("fourth byte getBytes", (byte) 0xFF, result[3]);
75          assertEquals("fifth byte getBytes", (byte) 0xFF, result[4]);
76          assertEquals("sixth byte getBytes", (byte) 0xFF, result[5]);
77          assertEquals("seventh byte getBytes", (byte) 0xFF, result[6]);
78          assertEquals("eighth byte getBytes", (byte) 0xFF, result[7]);
79      }
80  
81      /**
82       * Test conversion from bytes.
83       */
84      @Test
85      public void testBIFromBytes() {
86          byte[] val = new byte[] {(byte) 0xFE, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
87          ZipEightByteInteger zl = new ZipEightByteInteger(val);
88          assertEquals("value from bytes",
89                       BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1),
90                       zl.getValue());
91      }
92  
93      /**
94       * Test the contract of the equals method.
95       */
96      @Test
97      public void testEquals() {
98          ZipEightByteInteger zl = new ZipEightByteInteger(0x12345678);
99          ZipEightByteInteger zl2 = new ZipEightByteInteger(0x12345678);
100         ZipEightByteInteger zl3 = new ZipEightByteInteger(0x87654321);
101 
102         assertTrue("reflexive", zl.equals(zl));
103 
104         assertTrue("works", zl.equals(zl2));
105         assertTrue("works, part two", !zl.equals(zl3));
106 
107         assertTrue("symmetric", zl2.equals(zl));
108 
109         assertTrue("null handling", !zl.equals(null));
110         assertTrue("non ZipEightByteInteger handling", !zl.equals(new Integer(0x1234)));
111     }
112 
113     /**
114      * Test sign handling.
115      */
116     @Test
117     public void testSign() {
118         ZipEightByteInteger zl = new ZipEightByteInteger(new byte[] {(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF});
119         assertEquals(BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1).setBit(0),
120                      zl.getValue());
121     }
122 }