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.ZipShort.
27   *
28   */
29  public class ZipShortTest {
30  
31      /**
32       * Test conversion to bytes.
33       */
34      @Test
35      public void testToBytes() {
36          ZipShort zs = new ZipShort(0x1234);
37          byte[] result = zs.getBytes();
38          assertEquals("length getBytes", 2, result.length);
39          assertEquals("first byte getBytes", 0x34, result[0]);
40          assertEquals("second byte getBytes", 0x12, result[1]);
41      }
42  
43  
44      /**
45       * Test conversion to bytes.
46       */
47      @Test
48      public void testPut() {
49          byte[] arr = new byte[3];
50          ZipShort.putShort(0x1234, arr, 1);
51          assertEquals("first byte getBytes", 0x34, arr[1]);
52          assertEquals("second byte getBytes", 0x12, arr[2]);
53      }
54  
55  
56      /**
57       * Test conversion from bytes.
58       */
59      @Test
60      public void testFromBytes() {
61          byte[] val = new byte[] {0x34, 0x12};
62          ZipShort zs = new ZipShort(val);
63          assertEquals("value from bytes", 0x1234, zs.getValue());
64      }
65  
66      /**
67       * Test the contract of the equals method.
68       */
69      @Test
70      public void testEquals() {
71          ZipShort zs = new ZipShort(0x1234);
72          ZipShort zs2 = new ZipShort(0x1234);
73          ZipShort zs3 = new ZipShort(0x5678);
74  
75          assertTrue("reflexive", zs.equals(zs));
76  
77          assertTrue("works", zs.equals(zs2));
78          assertTrue("works, part two", !zs.equals(zs3));
79  
80          assertTrue("symmetric", zs2.equals(zs));
81  
82          assertTrue("null handling", !zs.equals(null));
83          assertTrue("non ZipShort handling", !zs.equals(new Integer(0x1234)));
84      }
85  
86      /**
87       * Test sign handling.
88       */
89      @Test
90      public void testSign() {
91          ZipShort zs = new ZipShort(new byte[] {(byte)0xFF, (byte)0xFF});
92          assertEquals(0x0000FFFF, zs.getValue());
93      }
94  
95      @Test
96      public void testClone() {
97          ZipShort s1 = new ZipShort(42);
98          ZipShort s2 = (ZipShort) s1.clone();
99          assertNotSame(s1, s2);
100         assertEquals(s1, s2);
101         assertEquals(s1.getValue(), s2.getValue());
102     }
103 }