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