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;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.commons.compress.archivers.ArchiveEntry;
31  import org.apache.commons.compress.archivers.ArchiveException;
32  
33  /**
34   * Test that can read various archive file examples.
35   * 
36   * This is a very simple implementation.
37   * 
38   * Files must be in resources/archives, and there must be a file.txt containing
39   * the list of files in the archives.
40   * 
41   * The class uses nested suites in order to be able to name the test after the file name,
42   * as JUnit does not allow one to change the display name of a test.
43   */
44  public class ArchiveReadTests extends AbstractTestCase {
45  
46      final static ClassLoader classLoader = ArchiveReadTests.class.getClassLoader();
47  
48      private File file;
49      private static final ArrayList<String> fileList = new ArrayList<String>();
50  
51      public ArchiveReadTests(String name) {
52          super(name);
53      }
54  
55      private ArchiveReadTests(String name, File file){
56          super(name);
57          this.file = file;
58      }
59  
60      public static TestSuite suite() throws IOException{
61          TestSuite suite = new TestSuite("ArchiveReadTests");
62          File arcdir =new File(classLoader.getResource("archives").getFile());
63          assertTrue(arcdir.exists());
64          File listing= new File(arcdir,"files.txt");
65          assertTrue("files.txt is readable",listing.canRead());
66          BufferedReader br = new BufferedReader(new FileReader(listing));
67          String line;
68          while ((line=br.readLine())!=null){
69              if (line.startsWith("#")){
70                  continue;
71              }
72              fileList.add(line);
73          }
74          br.close();
75          File[]files=arcdir.listFiles();
76          for (final File file : files) {
77              if (file.getName().endsWith(".txt")){
78                  continue;
79              }
80              // Appears to be the only way to give the test a variable name
81              TestSuite namedSuite = new TestSuite(file.getName());
82              Test test = new ArchiveReadTests("testArchive", file);
83              namedSuite.addTest(test);
84              suite.addTest(namedSuite);
85          }
86          return suite;
87      }
88  
89      // files.txt contains size and filename
90      @Override
91      protected String getExpectedString(ArchiveEntry entry) {
92          return entry.getSize() + " " + entry.getName();
93      }
94  
95      public void testArchive() throws Exception{
96          @SuppressWarnings("unchecked") // fileList is correct type already
97          ArrayList<String> expected= (ArrayList<String>) fileList.clone();
98          try {
99             checkArchiveContent(file, expected);
100         } catch (ArchiveException e) {
101             fail("Problem checking "+file);
102         }
103     }
104 }