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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
19
package org.apache.commons.compress.archivers;
21
import static org.junit.Assert.assertTrue;
22
import static org.junit.Assert.fail;
24
import java.io.BufferedInputStream;
25
import java.io.BufferedReader;
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;
33
import junit.framework.AssertionFailedError;
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;
49
* Test that can read various tar file examples.
51
* Files must be in resources/longsymlink, and there must be a file.txt containing
52
* the list of files in the archives.
54
@RunWith(Parameterized.class)
55
public class LongSymLinkTest extends AbstractTestCase {
57
private static final ClassLoader CLASSLOADER = LongSymLinkTest.class.getClassLoader();
58
private static final File ARCDIR = new File(CLASSLOADER.getResource("longsymlink").getFile());
59
private static final ArrayList<String> FILELIST = new ArrayList<String>();
63
public LongSymLinkTest(String file){
64
this.file = new File(ARCDIR, file);
68
public static void setUpFileList() throws Exception {
69
assertTrue(ARCDIR.exists());
70
File listing= new File(ARCDIR,"files.txt");
71
assertTrue("files.txt is readable",listing.canRead());
72
BufferedReader br = new BufferedReader(new FileReader(listing));
74
while ((line=br.readLine())!=null){
75
if (!line.startsWith("#")){
82
@Parameters(name = "file={0}")
83
public static Collection<Object[]> data() {
84
Collection<Object[]> params = new ArrayList<Object[]>();
85
for (String f : ARCDIR.list(new FilenameFilter() {
86
public boolean accept(File dir, String name) {
87
return !name.endsWith(".txt");
91
params.add(new Object[] { f });
98
protected String getExpectedString(ArchiveEntry entry) {
99
if (entry instanceof TarArchiveEntry) {
100
TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
101
if (tarEntry.isSymbolicLink()) {
102
return tarEntry.getName() + " -> " + tarEntry.getLinkName();
105
return entry.getName();
109
public void testArchive() throws Exception {
110
@SuppressWarnings("unchecked") // fileList is of correct type
111
ArrayList<String> expected = (ArrayList<String>) FILELIST.clone();
112
String name = file.getName();
113
if ("minotaur.jar".equals(name) || "minotaur-0.jar".equals(name)){
114
expected.add("META-INF/");
115
expected.add("META-INF/MANIFEST.MF");
117
ArchiveInputStream ais = factory.createArchiveInputStream(new BufferedInputStream(new FileInputStream(file)));
118
// check if expected type recognised
119
if (name.endsWith(".tar")){
120
assertTrue(ais instanceof TarArchiveInputStream);
121
} else if (name.endsWith(".jar") || name.endsWith(".zip")){
122
assertTrue(ais instanceof ZipArchiveInputStream);
123
} else if (name.endsWith(".cpio")){
124
assertTrue(ais instanceof CpioArchiveInputStream);
125
// Hack: cpio does not add trailing "/" to directory names
126
for(int i=0; i < expected.size(); i++){
127
String ent = expected.get(i);
128
if (ent.endsWith("/")){
129
expected.set(i, ent.substring(0, ent.length()-1));
132
} else if (name.endsWith(".ar")){
133
assertTrue(ais instanceof ArArchiveInputStream);
134
// CPIO does not store directories or directory names
136
for (String ent : FILELIST) {
137
if (!ent.endsWith("/")) {// not a directory
138
final int lastSlash = ent.lastIndexOf('/');
139
if (lastSlash >= 0) { // extract path name
140
expected.add(ent.substring(lastSlash + 1, ent.length()));
147
fail("Unexpected file type: "+name);
150
checkArchiveContent(ais, expected);
151
} catch (AssertionFailedError e) {
152
fail("Error processing "+file.getName()+" "+e);