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;
20  
21  import org.apache.commons.compress.utils.ArchiveUtils;
22  
23  public class ArchiveUtilsTest extends AbstractTestCase {
24  
25      private static final int bytesToTest = 50;
26      private static final byte[] byteTest = new byte[bytesToTest];
27      static {
28          for(int i=0; i < byteTest.length ;) {
29              byteTest[i]=(byte) i;
30              byteTest[i+1]=(byte) -i;
31              i += 2;
32          }
33      }
34      public void testCompareBA(){
35          byte[] buffer1 = {1,2,3};
36          byte[] buffer2 = {1,2,3,0};
37          byte[] buffer3 = {1,2,3};
38          assertTrue(ArchiveUtils.isEqual(buffer1, buffer2, true));
39          assertFalse(ArchiveUtils.isEqual(buffer1, buffer2, false));
40          assertFalse(ArchiveUtils.isEqual(buffer1, buffer2));
41          assertTrue(ArchiveUtils.isEqual(buffer2, buffer1, true));
42          assertFalse(ArchiveUtils.isEqual(buffer2, buffer1, false));
43          assertFalse(ArchiveUtils.isEqual(buffer2, buffer1));
44          assertTrue(ArchiveUtils.isEqual(buffer1, buffer3));
45          assertTrue(ArchiveUtils.isEqual(buffer3, buffer1));
46      }
47  
48      public void testCompareAscii(){
49          byte[] buffer1 = {'a','b','c'};
50          byte[] buffer2 = {'d','e','f',0};
51          assertTrue(ArchiveUtils.matchAsciiBuffer("abc", buffer1));
52          assertFalse(ArchiveUtils.matchAsciiBuffer("abc\0", buffer1));
53          assertTrue(ArchiveUtils.matchAsciiBuffer("def\0", buffer2));
54          assertFalse(ArchiveUtils.matchAsciiBuffer("def", buffer2));
55      }
56  
57      public void testAsciiConversions() {
58          asciiToByteAndBackOK("");
59          asciiToByteAndBackOK("abcd");
60          asciiToByteAndBackFail("\u8025");
61      }
62  
63      private void asciiToByteAndBackOK(String inputString) {
64          assertEquals(inputString, ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString)));
65      }
66  
67      private void asciiToByteAndBackFail(String inputString) {
68          assertFalse(inputString.equals(ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString))));
69      }
70  }