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