~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/utils/IOUtilsTest.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2014-05-19 18:13:11 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20140519181311-qw0qtmfj7oahttca
Tags: 1.8.1-1
* New upstream release
* Removed 01-optional-xz-dependency.patch
* Updated the OSGi metadata
* Moved the package to Git

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.utils;
 
19
 
 
20
import java.io.ByteArrayInputStream;
 
21
import java.io.FilterInputStream;
 
22
import java.io.InputStream;
 
23
import java.io.IOException;
 
24
 
 
25
import org.junit.Assert;
 
26
import org.junit.Test;
 
27
 
 
28
public class IOUtilsTest {
 
29
 
 
30
    private interface StreamWrapper {
 
31
        InputStream wrap(InputStream toWrap);
 
32
    }
 
33
 
 
34
    @Test
 
35
    public void skipUsingSkip() throws Exception {
 
36
        skip(new StreamWrapper() {
 
37
                public InputStream wrap(InputStream toWrap) {
 
38
                    return toWrap;
 
39
                }
 
40
            });
 
41
    }
 
42
 
 
43
    @Test
 
44
    public void skipUsingRead() throws Exception {
 
45
        skip(new StreamWrapper() {
 
46
                public InputStream wrap(InputStream toWrap) {
 
47
                    return new FilterInputStream(toWrap) {
 
48
                        public long skip(long s) {
 
49
                            return 0;
 
50
                        }
 
51
                    };
 
52
                }
 
53
            });
 
54
    }
 
55
 
 
56
    @Test
 
57
    public void skipUsingSkipAndRead() throws Exception {
 
58
        skip(new StreamWrapper() {
 
59
                public InputStream wrap(final InputStream toWrap) {
 
60
                    return new FilterInputStream(toWrap) {
 
61
                        boolean skipped;
 
62
                        public long skip(long s) throws IOException {
 
63
                            if (!skipped) {
 
64
                                toWrap.skip(5);
 
65
                                skipped = true;
 
66
                                return 5;
 
67
                            }
 
68
                            return 0;
 
69
                        }
 
70
                    };
 
71
                }
 
72
            });
 
73
    }
 
74
 
 
75
    private void skip(StreamWrapper wrapper) throws Exception {
 
76
        ByteArrayInputStream in = new ByteArrayInputStream(new byte[] {
 
77
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
 
78
            });
 
79
        InputStream sut = wrapper.wrap(in);
 
80
        Assert.assertEquals(10, IOUtils.skip(sut, 10));
 
81
        Assert.assertEquals(11, sut.read());
 
82
    }
 
83
 
 
84
}