Skip to content

Commit e896a18

Browse files
committed
Added a couple of tests to increase coverage somewhat
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1687613 13f79535-47bb-0310-9956-ffa450edef68
1 parent c4d46c5 commit e896a18

3 files changed

Lines changed: 100 additions & 1 deletion

File tree

src/main/java/org/apache/commons/io/output/ChunkedWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.io.Writer;
2222

2323
/**
24-
* OutputStream which breaks larger output blocks into chunks.
24+
* Writer which breaks larger output blocks into chunks.
2525
* Native code may need to copy the input array; if the write buffer
2626
* is very large this can cause OOME.
2727
*
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.output;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import java.util.concurrent.atomic.AtomicInteger;
22+
23+
import org.junit.Test;
24+
25+
/**
26+
* Test the chunked output stream
27+
*/
28+
public class ChunkedOutputStreamTest {
29+
30+
@Test
31+
public void write_four_chunks() throws Exception {
32+
final AtomicInteger numWrites = new AtomicInteger();
33+
ByteArrayOutputStream baos = new ByteArrayOutputStream() {
34+
@Override
35+
public void write(byte[] b, int off, int len) {
36+
numWrites.incrementAndGet();
37+
super.write(b, off, len);
38+
}
39+
};
40+
ChunkedOutputStream chunked = new ChunkedOutputStream(baos, 10);
41+
chunked.write("0123456789012345678901234567891".getBytes());
42+
assertEquals(4, numWrites.get());
43+
}
44+
45+
@Test(expected = IllegalArgumentException.class)
46+
public void negative_chunksize_not_permitted() {
47+
new ChunkedOutputStream(new ByteArrayOutputStream(), 0);
48+
}
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.output;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import java.io.IOException;
22+
import java.io.OutputStreamWriter;
23+
import java.util.concurrent.atomic.AtomicInteger;
24+
25+
import org.junit.Test;
26+
27+
public class ChunkedWriterTest {
28+
@Test
29+
public void write_four_chunks() throws Exception {
30+
final AtomicInteger numWrites = new AtomicInteger();
31+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
32+
OutputStreamWriter osw = new OutputStreamWriter(baos) {
33+
@Override
34+
public void write(char[] cbuf, int off, int len) throws IOException {
35+
numWrites.incrementAndGet();
36+
super.write(cbuf, off, len);
37+
}
38+
};
39+
40+
ChunkedWriter chunked = new ChunkedWriter(osw, 10);
41+
chunked.write("0123456789012345678901234567891".toCharArray());
42+
chunked.flush();
43+
assertEquals(4, numWrites.get());
44+
}
45+
46+
@Test(expected = IllegalArgumentException.class)
47+
public void negative_chunksize_not_permitted() {
48+
new ChunkedWriter(new OutputStreamWriter(new ByteArrayOutputStream()), 0);
49+
}
50+
}

0 commit comments

Comments
 (0)