• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.assertTrue;
22 import static org.junit.Assert.fail;
23 
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.FilenameFilter;
28 import java.net.URISyntaxException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 
32 import org.apache.commons.compress.archivers.ArchiveEntry;
33 import org.apache.commons.compress.archivers.ArchiveException;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.Parameterized;
38 import org.junit.runners.Parameterized.Parameters;
39 
40 /**
41  * Test that can read various archive file examples.
42  *
43  * This is a very simple implementation.
44  *
45  * Files must be in resources/archives, and there must be a file.txt containing
46  * the list of files in the archives.
47  */
48 @RunWith(Parameterized.class)
49 public class ArchiveReadTest extends AbstractTestCase {
50 
51     private static final ClassLoader CLASSLOADER = ArchiveReadTest.class.getClassLoader();
52     private static final File ARCDIR;
53     private static final ArrayList<String> FILELIST = new ArrayList<>();
54 
55     static {
56         try {
57             ARCDIR = new File(CLASSLOADER.getResource("archives").toURI());
58         } catch (URISyntaxException e) {
59             throw new RuntimeException(e);
60         }
61     }
62 
63     private final File file;
64 
ArchiveReadTest(final String file)65     public ArchiveReadTest(final String file){
66         this.file = new File(ARCDIR, file);
67     }
68 
69     @BeforeClass
setUpFileList()70     public static void setUpFileList() throws Exception {
71         assertTrue(ARCDIR.exists());
72         final File listing= new File(ARCDIR,"files.txt");
73         assertTrue("files.txt is readable",listing.canRead());
74         final BufferedReader br = new BufferedReader(new FileReader(listing));
75         String line;
76         while ((line=br.readLine())!=null){
77             if (!line.startsWith("#")){
78                 FILELIST.add(line);
79             }
80         }
81         br.close();
82     }
83 
84     @Parameters(name = "file={0}")
data()85     public static Collection<Object[]> data() {
86         assertTrue(ARCDIR.exists());
87         final Collection<Object[]> params = new ArrayList<>();
88         for (final String f : ARCDIR.list(new FilenameFilter() {
89             @Override
90             public boolean accept(final File dir, final String name) {
91                 return !name.endsWith(".txt");
92             }
93         }))
94         {
95             params.add(new Object[] { f });
96         }
97       return params;
98     }
99 
100     // files.txt contains size and filename
101     @Override
getExpectedString(final ArchiveEntry entry)102     protected String getExpectedString(final ArchiveEntry entry) {
103         return entry.getSize() + " " + entry.getName();
104     }
105 
106     @Test
testArchive()107     public void testArchive() throws Exception{
108         @SuppressWarnings("unchecked") // fileList is correct type already
109         final
110         ArrayList<String> expected= (ArrayList<String>) FILELIST.clone();
111         try {
112            checkArchiveContent(file, expected);
113         } catch (final ArchiveException e) {
114             fail("Problem checking "+file);
115         } catch (final AssertionError e) { // show error in context
116             fail("Problem checking " + file + " " +e);
117         }
118     }
119 }
120