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;
20  
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.io.BufferedInputStream;
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.FileReader;
29  import java.io.FilenameFilter;
30  import java.net.URISyntaxException;
31  import java.util.ArrayList;
32  import java.util.Collection;
33  
34  import junit.framework.AssertionFailedError;
35  import org.apache.commons.compress.AbstractTestCase;
36  import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
37  import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
38  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
39  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
40  import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.junit.runner.RunWith;
44  import org.junit.runners.Parameterized;
45  import org.junit.runners.Parameterized.Parameters;
46  
47  /**
48   * Test that can read various tar file examples.
49   * 
50    * Files must be in resources/longpath, and there must be a file.txt containing
51   * the list of files in the archives.
52  */
53  @RunWith(Parameterized.class)
54  public class LongPathTest extends AbstractTestCase {
55  
56      private static final ClassLoader CLASSLOADER = LongPathTest.class.getClassLoader();
57      private static final File ARCDIR;
58      private static final ArrayList<String> FILELIST = new ArrayList<>();
59  
60      static {
61          try {
62              ARCDIR = new File(CLASSLOADER.getResource("longpath").toURI());
63          } catch (URISyntaxException e) {
64              throw new RuntimeException(e);
65          }
66      }
67  
68      private final File file;
69  
70  
71  
72      public LongPathTest(final String file){
73          this.file = new File(ARCDIR, file);
74      }
75  
76      @BeforeClass
77      public static void setUpFileList() throws Exception {
78          assertTrue(ARCDIR.exists());
79          final File listing= new File(ARCDIR,"files.txt");
80          assertTrue("files.txt is readable",listing.canRead());
81          final BufferedReader br = new BufferedReader(new FileReader(listing));
82          String line;
83          while ((line=br.readLine())!=null){
84              if (!line.startsWith("#")){
85                  FILELIST.add(line);
86              }
87          }
88          br.close();
89      }
90  
91      @Parameters(name = "file={0}")
92      public static Collection<Object[]> data() {
93          final Collection<Object[]> params = new ArrayList<>();
94          for (final String f : ARCDIR.list(new FilenameFilter() {
95              @Override
96              public boolean accept(final File dir, final String name) {
97                  return !name.endsWith(".txt");
98              }
99          })) 
100         {
101             params.add(new Object[] { f });
102         }
103       return params;
104     }
105 
106     @Override
107     protected String getExpectedString(final ArchiveEntry entry) {
108         if (entry instanceof TarArchiveEntry) {
109             final TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
110             if (tarEntry.isSymbolicLink()) {
111                 return tarEntry.getName() + " -> " + tarEntry.getLinkName();
112             }
113         }
114         return entry.getName();
115     }
116 
117     @Test
118     public void testArchive() throws Exception {
119         @SuppressWarnings("unchecked") // fileList is of correct type
120         final
121         ArrayList<String> expected = (ArrayList<String>) FILELIST.clone();
122         final String name = file.getName();
123         if ("minotaur.jar".equals(name) || "minotaur-0.jar".equals(name)){
124             expected.add("META-INF/");
125             expected.add("META-INF/MANIFEST.MF");
126         }
127         final ArchiveInputStream ais = factory.createArchiveInputStream(new BufferedInputStream(new FileInputStream(file)));
128         // check if expected type recognized
129         if (name.endsWith(".tar")){
130             assertTrue(ais instanceof TarArchiveInputStream);
131         } else if (name.endsWith(".jar") || name.endsWith(".zip")){
132             assertTrue(ais instanceof ZipArchiveInputStream);
133         } else if (name.endsWith(".cpio")){
134             assertTrue(ais instanceof CpioArchiveInputStream);
135             // Hack: cpio does not add trailing "/" to directory names
136             for(int i=0; i < expected.size(); i++){
137                 final String ent = expected.get(i);
138                 if (ent.endsWith("/")){
139                     expected.set(i, ent.substring(0, ent.length()-1));
140                 }
141             }
142         } else if (name.endsWith(".ar")){
143             assertTrue(ais instanceof ArArchiveInputStream);
144             // CPIO does not store directories or directory names
145             expected.clear();
146             for (final String ent : FILELIST) {
147                 if (!ent.endsWith("/")) {// not a directory
148                     final int lastSlash = ent.lastIndexOf('/');
149                     if (lastSlash >= 0) { // extract path name
150                         expected.add(ent.substring(lastSlash + 1, ent.length()));
151                     } else {
152                         expected.add(ent);
153                     }
154                 }
155             }
156         } else {
157             fail("Unexpected file type: "+name);
158         }
159         try {
160             checkArchiveContent(ais, expected);
161         } catch (final AssertionFailedError e) {
162             fail("Error processing "+file.getName()+" "+e);
163         } finally {
164             ais.close();
165         }
166     }
167 }