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.tar;
20  
21  import static org.apache.commons.compress.AbstractTestCase.getFile;
22  import static org.junit.Assert.*;
23  import org.junit.Test;
24  
25  import java.io.File;
26  import java.io.FileInputStream;
27  
28  
29  public class SparseFilesTest {
30  
31      @Test
32      public void testOldGNU() throws Throwable {
33          final File file = getFile("oldgnu_sparse.tar");
34          TarArchiveInputStream tin = null;
35          try {
36              tin = new TarArchiveInputStream(new FileInputStream(file));
37              final TarArchiveEntry ae = tin.getNextTarEntry();
38              assertEquals("sparsefile", ae.getName());
39              assertTrue(ae.isOldGNUSparse());
40              assertTrue(ae.isGNUSparse());
41              assertFalse(ae.isPaxGNUSparse());
42              assertFalse(tin.canReadEntryData(ae));
43          } finally {
44              if (tin != null) {
45                  tin.close();
46              }
47          }
48      }
49  
50      @Test
51      public void testPaxGNU() throws Throwable {
52          final File file = getFile("pax_gnu_sparse.tar");
53          TarArchiveInputStream tin = null;
54          try {
55              tin = new TarArchiveInputStream(new FileInputStream(file));
56              assertPaxGNUEntry(tin, "0.0");
57              assertPaxGNUEntry(tin, "0.1");
58              assertPaxGNUEntry(tin, "1.0");
59          } finally {
60              if (tin != null) {
61                  tin.close();
62              }
63          }
64      }
65  
66      private void assertPaxGNUEntry(final TarArchiveInputStream tin, final String suffix) throws Throwable {
67          final TarArchiveEntry ae = tin.getNextTarEntry();
68          assertEquals("sparsefile-" + suffix, ae.getName());
69          assertTrue(ae.isGNUSparse());
70          assertTrue(ae.isPaxGNUSparse());
71          assertFalse(ae.isOldGNUSparse());
72          assertFalse(tin.canReadEntryData(ae));
73      }
74  }
75