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  package org.apache.commons.compress;
19  
20  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.BufferedReader;
24  import java.io.File;
25  import java.net.URISyntaxException;
26  import java.nio.file.Files;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.stream.Stream;
30  
31  import org.apache.commons.compress.archivers.ArchiveEntry;
32  import org.junit.jupiter.api.BeforeAll;
33  import org.junit.jupiter.params.ParameterizedTest;
34  import org.junit.jupiter.params.provider.Arguments;
35  import org.junit.jupiter.params.provider.MethodSource;
36  
37  /**
38   * Test that can read various archive file examples.
39   *
40   * This is a very simple implementation.
41   *
42   * Files must be in resources/archives, and there must be a file.txt containing the list of files in the archives.
43   */
44  public class ArchiveReadTest extends AbstractTest {
45  
46      private static final ClassLoader CLASSLOADER = ArchiveReadTest.class.getClassLoader();
47      private static final File ARCDIR;
48      private static final ArrayList<String> FILELIST = new ArrayList<>();
49  
50      static {
51          try {
52              ARCDIR = new File(CLASSLOADER.getResource("archives").toURI());
53          } catch (final URISyntaxException e) {
54              throw new AssertionError(e);
55          }
56      }
57  
58      public static Stream<Arguments> data() {
59          assertTrue(ARCDIR.exists());
60          final Collection<Arguments> params = new ArrayList<>();
61          for (final String fileName : ARCDIR.list((dir, name) -> !name.endsWith(".txt"))) {
62              params.add(Arguments.of(new File(ARCDIR, fileName)));
63          }
64          return params.stream();
65      }
66  
67      @BeforeAll
68      public static void setUpFileList() throws Exception {
69          assertTrue(ARCDIR.exists());
70          final File listing = new File(ARCDIR, "files.txt");
71          assertTrue(listing.canRead(), "files.txt is readable");
72          try (BufferedReader br = new BufferedReader(Files.newBufferedReader(listing.toPath()))) {
73              String line;
74              while ((line = br.readLine()) != null) {
75                  if (!line.startsWith("#")) {
76                      FILELIST.add(line);
77                  }
78              }
79          }
80      }
81  
82      // files.txt contains size and file name
83      @Override
84      protected String getExpectedString(final ArchiveEntry entry) {
85          return entry.getSize() + " " + entry.getName();
86      }
87  
88      @ParameterizedTest
89      @MethodSource("data")
90      public void testArchive(final File file) throws Exception {
91          @SuppressWarnings("unchecked") // fileList is correct type already
92          final ArrayList<String> expected = (ArrayList<String>) FILELIST.clone();
93          assertDoesNotThrow(() -> checkArchiveContent(file, expected), "Problem checking " + file);
94      }
95  }