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 final File file;
62  
63      public LongPathTest(final 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          final File listing= new File(ARCDIR,"files.txt");
71          assertTrue("files.txt is readable",listing.canRead());
72          final 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          final Collection<Object[]> params = new ArrayList<Object[]>();
85          for (final String f : ARCDIR.list(new FilenameFilter() {
86              @Override
87              public boolean accept(final File dir, final String name) {
88                  return !name.endsWith(".txt");
89              }
90          })) 
91          {
92              params.add(new Object[] { f });
93          }
94        return params;
95      }
96  
97      @Override
98      protected String getExpectedString(final ArchiveEntry entry) {
99          if (entry instanceof TarArchiveEntry) {
100             final TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
101             if (tarEntry.isSymbolicLink()) {
102                 return tarEntry.getName() + " -> " + tarEntry.getLinkName();
103             }
104         }
105         return entry.getName();
106     }
107 
108     @Test
109     public void testArchive() throws Exception {
110         @SuppressWarnings("unchecked") // fileList is of correct type
111         final
112         ArrayList<String> expected = (ArrayList<String>) FILELIST.clone();
113         final String name = file.getName();
114         if ("minotaur.jar".equals(name) || "minotaur-0.jar".equals(name)){
115             expected.add("META-INF/");
116             expected.add("META-INF/MANIFEST.MF");
117         }
118         final ArchiveInputStream ais = factory.createArchiveInputStream(new BufferedInputStream(new FileInputStream(file)));
119         // check if expected type recognized
120         if (name.endsWith(".tar")){
121             assertTrue(ais instanceof TarArchiveInputStream);
122         } else if (name.endsWith(".jar") || name.endsWith(".zip")){
123             assertTrue(ais instanceof ZipArchiveInputStream);
124         } else if (name.endsWith(".cpio")){
125             assertTrue(ais instanceof CpioArchiveInputStream);
126             // Hack: cpio does not add trailing "/" to directory names
127             for(int i=0; i < expected.size(); i++){
128                 final String ent = expected.get(i);
129                 if (ent.endsWith("/")){
130                     expected.set(i, ent.substring(0, ent.length()-1));
131                 }
132             }
133         } else if (name.endsWith(".ar")){
134             assertTrue(ais instanceof ArArchiveInputStream);
135             // CPIO does not store directories or directory names
136             expected.clear();
137             for (final String ent : FILELIST) {
138                 if (!ent.endsWith("/")) {// not a directory
139                     final int lastSlash = ent.lastIndexOf('/');
140                     if (lastSlash >= 0) { // extract path name
141                         expected.add(ent.substring(lastSlash + 1, ent.length()));
142                     } else {
143                         expected.add(ent);
144                     }
145                 }
146             }
147         } else {
148             fail("Unexpected file type: "+name);
149         }
150         try {
151             checkArchiveContent(ais, expected);
152         } catch (final AssertionFailedError e) {
153             fail("Error processing "+file.getName()+" "+e);
154         } finally {
155             ais.close();
156         }
157     }
158 }