1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28
29 public class ZipEightByteIntegerTest extends TestCase {
30
31 public ZipEightByteIntegerTest(String name) {
32 super(name);
33 }
34
35
36
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
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
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
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
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
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 }