|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | + /* |
| 21 | + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 22 | + */ |
| 23 | +package org.opensolaris.opengrok.util; |
| 24 | + |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.util.Arrays; |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +/** |
| 33 | + * |
| 34 | + * @author Krystof Tulinger |
| 35 | + */ |
| 36 | +public class XmlEofInputStreamTest { |
| 37 | + |
| 38 | + private final byte[][] tests = { |
| 39 | + {10, 20, 30, 40, 50, 0}, |
| 40 | + {10, 20, 30, 40, 0, 50}, |
| 41 | + {10, 20, 30, 0, 40, 50}, |
| 42 | + {10, 20, 0, 30, 40, 50}, |
| 43 | + {10, 0, 20, 30, 40, 50}, |
| 44 | + {0, 10, 20, 30, 40, 50}, |
| 45 | + {}, |
| 46 | + {1, 2}, |
| 47 | + {0x01, 0x02, 0x7F, 0x12}, |
| 48 | + {0x01, 0x02, 0x0, 0x12, 1, 2, 3, 4}, |
| 49 | + {1, 2, 0, 3, 4, 0, 5}, |
| 50 | + {0}, |
| 51 | + {1}, |
| 52 | + {0, 0} |
| 53 | + }; |
| 54 | + |
| 55 | + protected int countValidChars(byte[] array) { |
| 56 | + int j = 0; |
| 57 | + while (j < array.length && array[j] != 0) { |
| 58 | + j++; |
| 59 | + } |
| 60 | + return j; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test of read method, of class XmlEofInputStream. |
| 65 | + * |
| 66 | + * @throws java.io.IOException |
| 67 | + */ |
| 68 | + @Test |
| 69 | + public void testReadArray() throws IOException { |
| 70 | + for (byte[] test : tests) { |
| 71 | + ByteArrayInputStream input = new ByteArrayInputStream(test); |
| 72 | + InputStream stream = new XmlEofInputStream(input); |
| 73 | + byte[] buffer = new byte[test.length]; |
| 74 | + int validChars = countValidChars(test); |
| 75 | + if (validChars == 0) { |
| 76 | + // eof from the underlying buffer |
| 77 | + Assert.assertEquals(-1, stream.read(buffer)); |
| 78 | + } else { |
| 79 | + Assert.assertEquals(validChars, stream.read(buffer)); |
| 80 | + } |
| 81 | + Assert.assertArrayEquals(Arrays.copyOfRange(test, 0, validChars), Arrays.copyOfRange(buffer, 0, validChars)); |
| 82 | + if (validChars != test.length) { |
| 83 | + // there was an eof in the test data |
| 84 | + Assert.assertEquals(-1, stream.read(buffer)); |
| 85 | + Assert.assertEquals(-1, stream.read(buffer)); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test of read method, of class XmlEofInputStream. |
| 92 | + * |
| 93 | + * @throws java.io.IOException |
| 94 | + */ |
| 95 | + @Test |
| 96 | + public void testReadArrayOffset() throws IOException { |
| 97 | + int[] offsets = {1, 2, 3, 4, 5, 1, 2, 3, 4, 2, 3, 0, 1, 20}; |
| 98 | + |
| 99 | + Assert.assertEquals(tests.length, offsets.length); |
| 100 | + |
| 101 | + for (int i = 0; i < tests.length; i++) { |
| 102 | + ByteArrayInputStream input = new ByteArrayInputStream(tests[i]); |
| 103 | + InputStream stream = new XmlEofInputStream(input); |
| 104 | + byte[] buffer = new byte[tests[i].length]; |
| 105 | + |
| 106 | + if (tests[i].length - offsets[i] <= 0) { |
| 107 | + try { |
| 108 | + Assert.assertEquals( |
| 109 | + tests[i].length - offsets[i] == 0 ? 0 : -1, |
| 110 | + stream.read(buffer, offsets[i], buffer.length - offsets[i])); |
| 111 | + } catch (IndexOutOfBoundsException ex) { |
| 112 | + } |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + byte[] data = Arrays.copyOfRange(tests[i], 0, tests[i].length - offsets[i]); |
| 117 | + int validChars = countValidChars(data); |
| 118 | + |
| 119 | + if (validChars == 0) { |
| 120 | + // eof from the underlying buffer |
| 121 | + Assert.assertEquals(-1, |
| 122 | + stream.read(buffer, offsets[i], buffer.length - offsets[i])); |
| 123 | + } else { |
| 124 | + Assert.assertEquals(validChars, |
| 125 | + stream.read(buffer, offsets[i], buffer.length - offsets[i])); |
| 126 | + } |
| 127 | + Assert.assertArrayEquals( |
| 128 | + Arrays.copyOfRange(data, 0, validChars), |
| 129 | + Arrays.copyOfRange(buffer, offsets[i], offsets[i] + validChars)); |
| 130 | + |
| 131 | + if (validChars != data.length) { |
| 132 | + // there was an eof in the test data |
| 133 | + Assert.assertEquals(-1, stream.read(buffer, offsets[i], buffer.length - offsets[i])); |
| 134 | + Assert.assertEquals(-1, stream.read(buffer, offsets[i], buffer.length - offsets[i])); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Test of read method, of class XmlEofInputStream. |
| 141 | + * |
| 142 | + * @throws java.io.IOException |
| 143 | + */ |
| 144 | + @Test |
| 145 | + public void testReadOne() throws IOException { |
| 146 | + for (byte[] test : tests) { |
| 147 | + ByteArrayInputStream input = new ByteArrayInputStream(test); |
| 148 | + InputStream stream = new XmlEofInputStream(input); |
| 149 | + boolean eof = false; |
| 150 | + for (int j = 0; j < test.length; j++) { |
| 151 | + int read = stream.read(); |
| 152 | + if (read == -1) { |
| 153 | + eof = true; |
| 154 | + } |
| 155 | + Assert.assertEquals(eof ? -1 : test[j], read); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Test of close method, of class XmlEofInputStream. |
| 162 | + * |
| 163 | + * @throws java.io.IOException |
| 164 | + */ |
| 165 | + @Test |
| 166 | + public void testClose() throws IOException { |
| 167 | + byte[] buf = {10, 30, 50, 10, 50}; |
| 168 | + ByteArrayInputStream input = new ByteArrayInputStream(buf); |
| 169 | + InputStream stream = new XmlEofInputStream(input); |
| 170 | + Assert.assertEquals(10, stream.read()); |
| 171 | + Assert.assertEquals(30, stream.read()); |
| 172 | + stream.close(); |
| 173 | + Assert.assertEquals(-1, stream.read()); |
| 174 | + stream.close(); |
| 175 | + Assert.assertEquals(-1, stream.read()); |
| 176 | + Assert.assertEquals(-1, stream.read()); |
| 177 | + } |
| 178 | +} |
0 commit comments