View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress;
21  
22  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.net.URISyntaxException;
28  import java.nio.file.Files;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.stream.Stream;
32  
33  import org.apache.commons.compress.archivers.ArchiveEntry;
34  import org.junit.jupiter.api.BeforeAll;
35  import org.junit.jupiter.params.ParameterizedTest;
36  import org.junit.jupiter.params.provider.Arguments;
37  import org.junit.jupiter.params.provider.MethodSource;
38  
39  /**
40   * Test that can read various archive file examples.
41   *
42   * This is a very simple implementation.
43   *
44   * Files must be in resources/archives, and there must be a file.txt containing the list of files in the archives.
45   */
46  class ArchiveReadTest extends AbstractTest {
47  
48      private static final ClassLoader CLASS_LOADER = ArchiveReadTest.class.getClassLoader();
49      private static final File ARC_DIR;
50      private static final ArrayList<String> FILE_LIST = new ArrayList<>();
51  
52      static {
53          try {
54              ARC_DIR = new File(CLASS_LOADER.getResource("archives").toURI());
55          } catch (final URISyntaxException e) {
56              throw new AssertionError(e);
57          }
58      }
59  
60      public static Stream<Arguments> data() {
61          assertTrue(ARC_DIR.exists());
62          final Collection<Arguments> params = new ArrayList<>();
63          for (final String fileName : ARC_DIR.list((dir, name) -> !name.endsWith(".txt"))) {
64              params.add(Arguments.of(new File(ARC_DIR, fileName)));
65          }
66          return params.stream();
67      }
68  
69      @BeforeAll
70      public static void setUpFileList() throws Exception {
71          assertTrue(ARC_DIR.exists());
72          final File listing = new File(ARC_DIR, "files.txt");
73          assertTrue(listing.canRead(), "files.txt is readable");
74          try (BufferedReader br = new BufferedReader(Files.newBufferedReader(listing.toPath()))) {
75              String line;
76              while ((line = br.readLine()) != null) {
77                  if (!line.startsWith("#")) {
78                      FILE_LIST.add(line);
79                  }
80              }
81          }
82      }
83  
84      // files.txt contains size and file name
85      @Override
86      protected String getExpectedString(final ArchiveEntry entry) {
87          return entry.getSize() + " " + entry.getName();
88      }
89  
90      @ParameterizedTest
91      @MethodSource("data")
92      void testArchive(final File file) throws Exception {
93          @SuppressWarnings("unchecked") // fileList is correct type already
94          final ArrayList<String> expected = (ArrayList<String>) FILE_LIST.clone();
95          assertDoesNotThrow(() -> checkArchiveContent(file, expected), "Problem checking " + file);
96      }
97  }