~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/zip/ScatterZipOutputStreamTest.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
package org.apache.commons.compress.archivers.zip;
 
19
 
 
20
import org.apache.commons.compress.parallel.InputStreamSupplier;
 
21
import org.apache.commons.compress.utils.IOUtils;
 
22
import org.junit.After;
 
23
import org.junit.Test;
 
24
 
 
25
import java.io.ByteArrayInputStream;
 
26
import java.io.File;
 
27
import java.io.InputStream;
 
28
 
 
29
import static org.apache.commons.compress.AbstractTestCase.tryHardToDelete;
 
30
import static org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest.createZipArchiveEntryRequest;
 
31
import static org.junit.Assert.assertArrayEquals;
 
32
import static org.junit.Assert.assertEquals;
 
33
 
 
34
public class ScatterZipOutputStreamTest {
 
35
 
 
36
    private File scatterFile = null;
 
37
    private File target = null;
 
38
 
 
39
    @After
 
40
    public void cleanup() {
 
41
        tryHardToDelete(scatterFile);
 
42
        tryHardToDelete(target);
 
43
    }
 
44
    
 
45
    @Test
 
46
    public void putArchiveEntry() throws Exception {
 
47
        scatterFile = File.createTempFile("scattertest", ".notzip");
 
48
        ScatterZipOutputStream scatterZipOutputStream = ScatterZipOutputStream.fileBased(scatterFile);
 
49
        final byte[] B_PAYLOAD = "RBBBBBBS".getBytes();
 
50
        final byte[] A_PAYLOAD = "XAAY".getBytes();
 
51
 
 
52
        ZipArchiveEntry zab = new ZipArchiveEntry("b.txt");
 
53
        zab.setMethod(ZipArchiveEntry.DEFLATED);
 
54
        final ByteArrayInputStream payload = new ByteArrayInputStream(B_PAYLOAD);
 
55
        scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zab, createPayloadSupplier(payload)));
 
56
 
 
57
        ZipArchiveEntry zae = new ZipArchiveEntry("a.txt");
 
58
        zae.setMethod(ZipArchiveEntry.DEFLATED);
 
59
        ByteArrayInputStream payload1 = new ByteArrayInputStream(A_PAYLOAD);
 
60
        scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zae, createPayloadSupplier(payload1)));
 
61
 
 
62
        target = File.createTempFile("scattertest", ".zip");
 
63
        ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(target);
 
64
        scatterZipOutputStream.writeTo( outputStream);
 
65
        outputStream.close();
 
66
        scatterZipOutputStream.close();
 
67
 
 
68
        ZipFile zf = new ZipFile(target);
 
69
        final ZipArchiveEntry b_entry = zf.getEntries("b.txt").iterator().next();
 
70
        assertEquals(8, b_entry.getSize());
 
71
        assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(zf.getInputStream(b_entry)));
 
72
 
 
73
        final ZipArchiveEntry a_entry = zf.getEntries("a.txt").iterator().next();
 
74
        assertEquals(4, a_entry.getSize());
 
75
        assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(zf.getInputStream(a_entry)));
 
76
        zf.close();
 
77
    }
 
78
 
 
79
    private InputStreamSupplier createPayloadSupplier(final ByteArrayInputStream payload) {
 
80
        return new InputStreamSupplier() {
 
81
            public InputStream get() {
 
82
                return payload;
 
83
            }
 
84
        };
 
85
    }
 
86
}