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