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