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