|
| 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 | +package org.apache.commons.io.input; |
| 18 | + |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | + |
| 21 | +import junit.framework.TestCase; |
| 22 | + |
| 23 | +import org.apache.commons.io.IOUtils; |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests for {@link BoundedInputStream}. |
| 27 | + * |
| 28 | + * @version $Id$ |
| 29 | + */ |
| 30 | +public class BoundedInputStreamTest extends TestCase { |
| 31 | + |
| 32 | + public BoundedInputStreamTest(String name) { |
| 33 | + super(name); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Test {@link BoundedInputStream#read()). |
| 38 | + */ |
| 39 | + public void testReadSingle() throws Exception { |
| 40 | + BoundedInputStream bounded = null; |
| 41 | + byte[] helloWorld = "Hello World".getBytes(); |
| 42 | + byte[] hello = "Hello".getBytes(); |
| 43 | + |
| 44 | + // limit = length |
| 45 | + bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length); |
| 46 | + for (int i = 0; i < helloWorld.length; i++) { |
| 47 | + assertEquals("limit = length byte[" + i + "]", helloWorld[i], bounded.read()); |
| 48 | + } |
| 49 | + assertEquals("limit = length end", -1, bounded.read()); |
| 50 | + |
| 51 | + // limit > length |
| 52 | + bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length + 1); |
| 53 | + for (int i = 0; i < helloWorld.length; i++) { |
| 54 | + assertEquals("limit > length byte[" + i + "]", helloWorld[i], bounded.read()); |
| 55 | + } |
| 56 | + assertEquals("limit > length end", -1, bounded.read()); |
| 57 | + |
| 58 | + // limit < length |
| 59 | + bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), hello.length); |
| 60 | + for (int i = 0; i < hello.length; i++) { |
| 61 | + assertEquals("limit < length byte[" + i + "]", hello[i], bounded.read()); |
| 62 | + } |
| 63 | + assertEquals("limit < length end", -1, bounded.read()); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Test {@link BoundedInputStream#read(byte[], int, int))). |
| 68 | + */ |
| 69 | + public void testReadArray() throws Exception { |
| 70 | + |
| 71 | + BoundedInputStream bounded = null; |
| 72 | + byte[] helloWorld = "Hello World".getBytes(); |
| 73 | + byte[] hello = "Hello".getBytes(); |
| 74 | + |
| 75 | + bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld)); |
| 76 | + compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded)); |
| 77 | + |
| 78 | + bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), 0); |
| 79 | + compare("limit = 0", new byte[0], IOUtils.toByteArray(bounded)); |
| 80 | + |
| 81 | + bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length); |
| 82 | + compare("limit = length", helloWorld, IOUtils.toByteArray(bounded)); |
| 83 | + |
| 84 | + bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length + 1); |
| 85 | + compare("limit > length", helloWorld, IOUtils.toByteArray(bounded)); |
| 86 | + |
| 87 | + bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length - 6); |
| 88 | + compare("limit < length", hello, IOUtils.toByteArray(bounded)); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Compare byte arrays. |
| 93 | + */ |
| 94 | + private void compare(String msg, byte[] expected, byte[] actual) { |
| 95 | + assertEquals(msg + " length", expected.length, actual.length); |
| 96 | + for (int i = 0; i < expected.length; i++) { |
| 97 | + assertEquals(msg + " byte[" + i + "]", expected[i], actual[i]); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments