~ubuntu-branches/debian/stretch/libcommons-compress-java/stretch

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/compress/archivers/LongSymLinkTest.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-08-31 23:22:38 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20150831232238-gz3ynyvs68tok1a3
Tags: 1.10-1
* New upstream release
* Updated the OSGi metadata

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.archivers;
 
20
 
 
21
import static org.junit.Assert.assertTrue;
 
22
import static org.junit.Assert.fail;
 
23
 
 
24
import java.io.BufferedInputStream;
 
25
import java.io.BufferedReader;
 
26
import java.io.File;
 
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;
 
32
 
 
33
import junit.framework.AssertionFailedError;
 
34
 
 
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;
 
47
 
 
48
/**
 
49
 * Test that can read various tar file examples.
 
50
 * 
 
51
  * Files must be in resources/longsymlink, and there must be a file.txt containing
 
52
 * the list of files in the archives.
 
53
*/
 
54
@RunWith(Parameterized.class)
 
55
public class LongSymLinkTest extends AbstractTestCase {
 
56
 
 
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>();
 
60
 
 
61
    private File file;
 
62
 
 
63
    public LongSymLinkTest(String file){
 
64
        this.file = new File(ARCDIR, file);
 
65
    }
 
66
 
 
67
    @BeforeClass
 
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));
 
73
        String line;
 
74
        while ((line=br.readLine())!=null){
 
75
            if (!line.startsWith("#")){
 
76
                FILELIST.add(line);
 
77
            }
 
78
        }
 
79
        br.close();
 
80
    }
 
81
 
 
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");
 
88
            }
 
89
        })) 
 
90
        {
 
91
            params.add(new Object[] { f });
 
92
        }
 
93
      return params;
 
94
    }
 
95
 
 
96
 
 
97
    @Override
 
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();
 
103
            }
 
104
        }
 
105
        return entry.getName();
 
106
    }
 
107
 
 
108
    @Test
 
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");
 
116
        }
 
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));
 
130
                }
 
131
            }
 
132
        } else if (name.endsWith(".ar")){
 
133
            assertTrue(ais instanceof ArArchiveInputStream);
 
134
            // CPIO does not store directories or directory names
 
135
            expected.clear();
 
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()));
 
141
                    } else {
 
142
                        expected.add(ent);
 
143
                    }
 
144
                }
 
145
            }
 
146
        } else {
 
147
            fail("Unexpected file type: "+name);
 
148
        }
 
149
        try {
 
150
            checkArchiveContent(ais, expected);
 
151
        } catch (AssertionFailedError e) {
 
152
            fail("Error processing "+file.getName()+" "+e);
 
153
        } finally {
 
154
            ais.close();
 
155
        }
 
156
    }
 
157
}