Skip to content

Commit a3c09f7

Browse files
authored
Merge pull request #1317 from tulinkry/xml-tests
adding tests for xml input/output streams
2 parents aea7cca + 062ef19 commit a3c09f7

File tree

3 files changed

+326
-4
lines changed

3 files changed

+326
-4
lines changed

src/org/opensolaris/opengrok/util/XmlEofInputStream.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.util;
2424

@@ -44,7 +44,7 @@ public XmlEofInputStream(InputStream in) {
4444

4545
@Override
4646
public int read(byte[] b, int off, int len) throws IOException {
47-
int r;
47+
int r, i;
4848
if (isEof) {
4949
return -1;
5050
}
@@ -54,8 +54,14 @@ public int read(byte[] b, int off, int len) throws IOException {
5454
}
5555
return r;
5656
}
57-
isEof = b[off + r - 1] == EOF;
58-
return isEof ? (r - 1 == 0 ? -1 : r - 1) : r;
57+
58+
for (i = off; i < off + len && i < off + r; i++) {
59+
if (isEof = (b[i] == EOF)) {
60+
return i == off ? -1 : i - off;
61+
}
62+
}
63+
64+
return r;
5965
}
6066

6167
@Override
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.OutputStream;
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 XmlEofOutputStreamTest {
37+
38+
private byte[][] tests = {
39+
{10, 127, 0x10, 0x7F},
40+
{20, 32, 40, 60, 127, 100},
41+
{30, 0, 0, 0},
42+
{40, 1, 3, 4, 6, 7, 5, 46, 6, 4, 6, 7},
43+
{0x50},
44+
{},
45+
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 5, 4, 5, 65, 6, 8, 7}};
46+
47+
/**
48+
* Test of write method, of class XmlEofOutputStream.
49+
*/
50+
@Test
51+
public void testWriteArray() {
52+
for (byte[] test : tests) {
53+
ByteArrayOutputStream output = new ByteArrayOutputStream();
54+
OutputStream stream = new XmlEofOutputStream(output);
55+
try {
56+
stream.write(test);
57+
stream.close();
58+
} catch (IOException ex) {
59+
Assert.fail("The test should not throw an exception now");
60+
}
61+
byte[] expected = Arrays.copyOfRange(test, 0, test.length + 1);
62+
Assert.assertArrayEquals(expected, output.toByteArray());
63+
}
64+
}
65+
66+
/**
67+
* Test of write method, of class XmlEofOutputStream.
68+
*/
69+
@Test(expected = IndexOutOfBoundsException.class)
70+
public void testWriteArrayOffset() {
71+
int[] offsets = {1, 2, 3, 4, 5, 3, 5};
72+
Assert.assertEquals(tests.length, offsets.length);
73+
74+
for (int i = 0; i < tests.length; i++) {
75+
ByteArrayOutputStream output = new ByteArrayOutputStream();
76+
OutputStream stream = new XmlEofOutputStream(output);
77+
try {
78+
stream.write(tests[i], offsets[i], tests[i].length - offsets[i]);
79+
stream.close();
80+
} catch (IOException ex) {
81+
Assert.fail("The test should not throw an exception now");
82+
}
83+
/* IndexOutOfBoundsException was thrown if the offset was incorrect according to the length */
84+
85+
byte[] expected = Arrays.copyOfRange(tests[i], offsets[i], tests[i].length + 1);
86+
87+
Assert.assertArrayEquals(expected, output.toByteArray());
88+
}
89+
}
90+
91+
/**
92+
* Test of write method, of class XmlEofOutputStream.
93+
*/
94+
@Test
95+
public void testWriteInt() {
96+
for (byte[] test : tests) {
97+
ByteArrayOutputStream output = new ByteArrayOutputStream();
98+
OutputStream stream = new XmlEofOutputStream(output);
99+
try {
100+
for (int j = 0; j < test.length; j++) {
101+
stream.write(test[j]);
102+
}
103+
stream.close();
104+
} catch (IOException ex) {
105+
Assert.fail("The test should not throw an exception now");
106+
}
107+
byte[] expected = Arrays.copyOfRange(test, 0, test.length + 1);
108+
Assert.assertArrayEquals(expected, output.toByteArray());
109+
}
110+
}
111+
112+
/**
113+
* Test of close method, of class XmlEofOutputStream.
114+
*/
115+
@Test
116+
public void testClose() {
117+
ByteArrayOutputStream output = new ByteArrayOutputStream();
118+
OutputStream stream = new XmlEofOutputStream(output);
119+
try {
120+
stream.write(10);
121+
stream.write(30);
122+
stream.close();
123+
stream.close();
124+
} catch (IOException ex) {
125+
Assert.fail("The test should not throw an exception now");
126+
}
127+
128+
Assert.assertEquals(0, output.toByteArray()[output.toByteArray().length - 1]);
129+
130+
try {
131+
stream.write(10);
132+
stream.write(50);
133+
// throws an exception
134+
Assert.fail("Writing after the stream has been closed is not allowed");
135+
} catch (IOException ex) {
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)