Skip to content

Commit 7b9ad18

Browse files
committed
Add tests and some missing conversions to AbstractOrigin subclasses
1 parent ea38b48 commit 7b9ad18

12 files changed

Lines changed: 729 additions & 7 deletions

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ The <action> type attribute can be add,update,fix,remove.
6868
<action dev="ggregory" type="add" due-to="Gary Gregory">
6969
Add AbstractStreamBuilder.setOpenOptions(OpenOption...).
7070
</action>
71+
<action dev="ggregory" type="add" due-to="Gary Gregory">
72+
Add some missing conversions to AbstractOrigin subclasses.
73+
</action>
7174
<!-- UPDATE -->
7275
</release>
7376
<release version="2.12.0" date="2023-05-13" description="Java 8 required.">

src/main/java/org/apache/commons/io/build/AbstractOrigin.java

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.io.File;
2222
import java.io.IOException;
2323
import java.io.InputStream;
24+
import java.io.InputStreamReader;
2425
import java.io.OutputStream;
26+
import java.io.OutputStreamWriter;
2527
import java.io.Reader;
2628
import java.io.Writer;
2729
import java.net.URI;
@@ -32,6 +34,10 @@
3234
import java.nio.file.Paths;
3335
import java.util.Objects;
3436

37+
import org.apache.commons.io.IOUtils;
38+
import org.apache.commons.io.input.ReaderInputStream;
39+
import org.apache.commons.io.output.WriterOutputStream;
40+
3541
/**
3642
* Abstracts the origin of data for builders like a {@link File}, {@link Path}, {@link Reader}, {@link Writer}, {@link InputStream}, {@link OutputStream}, and
3743
* {@link URI}.
@@ -71,6 +77,11 @@ public InputStream getInputStream(final OpenOption... options) throws IOExceptio
7177
return new ByteArrayInputStream(origin);
7278
}
7379

80+
@Override
81+
public Reader getReader(final Charset charset) throws IOException {
82+
return new InputStreamReader(getInputStream(), charset);
83+
}
84+
7485
}
7586

7687
/**
@@ -87,6 +98,12 @@ public CharSequenceOrigin(final CharSequence origin) {
8798
super(origin);
8899
}
89100

101+
@Override
102+
public byte[] getByteArray() {
103+
// TODO Pass in a Charset? Consider if call sites actually need this.
104+
return origin.toString().getBytes(Charset.defaultCharset());
105+
}
106+
90107
@Override
91108
public CharSequence getCharSequence(final Charset charset) {
92109
// No conversion
@@ -101,6 +118,11 @@ public InputStream getInputStream(final OpenOption... options) throws IOExceptio
101118
// return CharSequenceInputStream.builder().setCharSequence(getCharSequence(Charset.defaultCharset())).get();
102119
}
103120

121+
@Override
122+
public Reader getReader(final Charset charset) throws IOException {
123+
return new InputStreamReader(getInputStream(), charset);
124+
}
125+
104126
}
105127

106128
/**
@@ -136,7 +158,7 @@ public Path getPath() {
136158
/**
137159
* An {@link InputStream} origin.
138160
* <p>
139-
* This origin cannot provide other aspects.
161+
* This origin cannot provide some of the other aspects.
140162
* </p>
141163
*/
142164
public static class InputStreamOrigin extends AbstractOrigin<InputStream, InputStreamOrigin> {
@@ -150,18 +172,28 @@ public InputStreamOrigin(final InputStream origin) {
150172
super(origin);
151173
}
152174

175+
@Override
176+
public byte[] getByteArray() throws IOException {
177+
return IOUtils.toByteArray(origin);
178+
}
179+
153180
@Override
154181
public InputStream getInputStream(final OpenOption... options) {
155182
// No conversion
156183
return get();
157184
}
158185

186+
@Override
187+
public Reader getReader(final Charset charset) throws IOException {
188+
return new InputStreamReader(getInputStream(), charset);
189+
}
190+
159191
}
160192

161193
/**
162194
* An {@link OutputStream} origin.
163195
* <p>
164-
* This origin cannot provide other aspects.
196+
* This origin cannot provide some of the other aspects.
165197
* </p>
166198
*/
167199
public static class OutputStreamOrigin extends AbstractOrigin<OutputStream, OutputStreamOrigin> {
@@ -181,6 +213,10 @@ public OutputStream getOutputStream(final OpenOption... options) {
181213
return get();
182214
}
183215

216+
@Override
217+
public Writer getWriter(final Charset charset, final OpenOption... options) throws IOException {
218+
return new OutputStreamWriter(origin, charset);
219+
}
184220
}
185221

186222
/**
@@ -230,6 +266,23 @@ public ReaderOrigin(final Reader origin) {
230266
super(origin);
231267
}
232268

269+
@Override
270+
public byte[] getByteArray() throws IOException {
271+
// TODO Pass in a Charset? Consider if call sites actually need this.
272+
return IOUtils.toByteArray(origin, Charset.defaultCharset());
273+
}
274+
275+
@Override
276+
public CharSequence getCharSequence(final Charset charset) throws IOException {
277+
return IOUtils.toString(origin);
278+
}
279+
280+
@Override
281+
public InputStream getInputStream(final OpenOption... options) throws IOException {
282+
// TODO Pass in a Charset? Consider if call sites actually need this.
283+
return ReaderInputStream.builder().setReader(origin).setCharset(Charset.defaultCharset()).get();
284+
}
285+
233286
@Override
234287
public Reader getReader(final Charset charset) throws IOException {
235288
// No conversion
@@ -280,6 +333,12 @@ public WriterOrigin(final Writer origin) {
280333
super(origin);
281334
}
282335

336+
@Override
337+
public OutputStream getOutputStream(final OpenOption... options) throws IOException {
338+
// TODO Pass in a Charset? Consider if call sites actually need this.
339+
return WriterOutputStream.builder().setWriter(origin).setCharset(Charset.defaultCharset()).get();
340+
}
341+
283342
@Override
284343
public Writer getWriter(final Charset charset, final OpenOption... options) throws IOException {
285344
// No conversion
@@ -315,7 +374,7 @@ public T get() {
315374
* Gets this origin as a byte array, if possible.
316375
*
317376
* @return this origin as a byte array, if possible.
318-
* @throws IOException if an I/O error occurs.
377+
* @throws IOException if an I/O error occurs.
319378
* @throws UnsupportedOperationException if the origin cannot be converted to a Path.
320379
*/
321380
public byte[] getByteArray() throws IOException {
@@ -327,7 +386,7 @@ public byte[] getByteArray() throws IOException {
327386
*
328387
* @param charset The charset to use if conversion from bytes is needed.
329388
* @return this origin as a byte array, if possible.
330-
* @throws IOException if an I/O error occurs.
389+
* @throws IOException if an I/O error occurs.
331390
* @throws UnsupportedOperationException if the origin cannot be converted to a Path.
332391
*/
333392
public CharSequence getCharSequence(final Charset charset) throws IOException {
@@ -349,7 +408,7 @@ public File getFile() {
349408
*
350409
* @param options options specifying how the file is opened
351410
* @return this origin as an InputStream, if possible.
352-
* @throws IOException if an I/O error occurs.
411+
* @throws IOException if an I/O error occurs.
353412
* @throws UnsupportedOperationException if the origin cannot be converted to a Path.
354413
*/
355414
public InputStream getInputStream(final OpenOption... options) throws IOException {
@@ -361,7 +420,7 @@ public InputStream getInputStream(final OpenOption... options) throws IOExceptio
361420
*
362421
* @param options options specifying how the file is opened
363422
* @return this origin as an OutputStream, if possible.
364-
* @throws IOException if an I/O error occurs.
423+
* @throws IOException if an I/O error occurs.
365424
* @throws UnsupportedOperationException if the origin cannot be converted to a Path.
366425
*/
367426
public OutputStream getOutputStream(final OpenOption... options) throws IOException {
@@ -395,7 +454,7 @@ public Reader getReader(final Charset charset) throws IOException {
395454
* @param charset the charset to use for encoding
396455
* @param options options specifying how the file is opened
397456
* @return a new Writer on the origin.
398-
* @throws IOException if an I/O error occurs opening or creating the file.
457+
* @throws IOException if an I/O error occurs opening or creating the file.
399458
* @throws UnsupportedOperationException if the origin cannot be converted to a Path.
400459
*/
401460
public Writer getWriter(final Charset charset, final OpenOption... options) throws IOException {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.build;
18+
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.OutputStream;
24+
import java.io.Reader;
25+
import java.io.Writer;
26+
import java.nio.charset.Charset;
27+
import java.util.Objects;
28+
29+
import org.junit.jupiter.api.Test;
30+
31+
/**
32+
* Tests {@link AbstractOrigin} and subclasses.
33+
*
34+
* @param <T> the type of instances to build.
35+
* @param <B> the type of builder subclass.
36+
*/
37+
public abstract class AbstractOriginTest<T, B extends AbstractOrigin<T, B>> {
38+
39+
protected static final String FILE_NAME_RO = "src/test/resources/org/apache/commons/io/test-file-20byteslength.bin";
40+
protected static final String FILE_NAME_RW = "target/" + AbstractOriginTest.class.getSimpleName() + ".txt";
41+
42+
protected AbstractOrigin<T, B> originRo;
43+
protected AbstractOrigin<T, B> originRw;
44+
45+
protected AbstractOrigin<T, B> getOriginRo() {
46+
return Objects.requireNonNull(originRo, "originRo");
47+
}
48+
49+
protected AbstractOrigin<T, B> getOriginRw() {
50+
return Objects.requireNonNull(originRw, "originRw");
51+
}
52+
53+
protected void setOriginRo(final AbstractOrigin<T, B> origin) {
54+
this.originRo = origin;
55+
}
56+
57+
protected void setOriginRw(final AbstractOrigin<T, B> origin) {
58+
this.originRw = origin;
59+
}
60+
61+
@Test
62+
public void testGetByteArray() throws IOException {
63+
assertNotNull(getOriginRo().getByteArray());
64+
}
65+
66+
@Test
67+
public void testGetCharSequence() throws IOException {
68+
assertNotNull(getOriginRo().getCharSequence(Charset.defaultCharset()));
69+
}
70+
71+
@Test
72+
public void testGetFile() {
73+
assertNotNull(getOriginRo().getFile());
74+
}
75+
76+
@Test
77+
public void testGetInputStream() throws IOException {
78+
try (final InputStream inputStream = getOriginRo().getInputStream()) {
79+
assertNotNull(inputStream);
80+
}
81+
}
82+
83+
@Test
84+
public void testGetOutputStream() throws IOException {
85+
try (final OutputStream output = getOriginRw().getOutputStream()) {
86+
assertNotNull(output);
87+
}
88+
}
89+
90+
@Test
91+
public void testGetPath() {
92+
assertNotNull(getOriginRo().getPath());
93+
}
94+
95+
@Test
96+
public void testGetReader() throws IOException {
97+
try (final Reader reader = getOriginRo().getReader(Charset.defaultCharset())) {
98+
assertNotNull(reader);
99+
}
100+
}
101+
102+
@Test
103+
public void testGetWriter() throws IOException {
104+
try (final Writer writer = getOriginRw().getWriter(Charset.defaultCharset())) {
105+
assertNotNull(writer);
106+
}
107+
}
108+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.build;
18+
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
21+
import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
/**
26+
* Tests {@link ByteArrayOrigin}.
27+
*
28+
* A ByteArrayOrigin can convert into some of the other aspects.
29+
*/
30+
public class ByteArrayOriginTest extends AbstractOriginTest<byte[], ByteArrayOrigin> {
31+
32+
@BeforeEach
33+
public void beforeEach() {
34+
setOriginRo(new ByteArrayOrigin(new byte[] { 0 }));
35+
setOriginRw(new ByteArrayOrigin(new byte[] { 1 }));
36+
}
37+
38+
@Override
39+
@Test
40+
public void testGetFile() {
41+
// Cannot convert a byte[] to a File.
42+
assertThrows(UnsupportedOperationException.class, super::testGetFile);
43+
}
44+
45+
@Override
46+
@Test
47+
public void testGetOutputStream() {
48+
// Cannot convert a byte[] to an OutputStream.
49+
assertThrows(UnsupportedOperationException.class, super::testGetOutputStream);
50+
}
51+
52+
@Override
53+
@Test
54+
public void testGetPath() {
55+
// Cannot convert a byte[] to a Path.
56+
assertThrows(UnsupportedOperationException.class, super::testGetPath);
57+
}
58+
59+
@Override
60+
@Test
61+
public void testGetWriter() {
62+
// Cannot convert a byte[] to a Writer.
63+
assertThrows(UnsupportedOperationException.class, super::testGetWriter);
64+
}
65+
66+
}

0 commit comments

Comments
 (0)