This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 7b9ad18b Add tests and some missing conversions to AbstractOrigin subclasses 7b9ad18b is described below commit 7b9ad18b6f99cda629c32d12727e26c28dc06daf Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun May 21 01:06:41 2023 -0400 Add tests and some missing conversions to AbstractOrigin subclasses --- src/changes/changes.xml | 3 + .../apache/commons/io/build/AbstractOrigin.java | 73 ++++++++++++-- .../commons/io/build/AbstractOriginTest.java | 108 +++++++++++++++++++++ .../commons/io/build/ByteArrayOriginTest.java | 66 +++++++++++++ .../commons/io/build/CharSequenceOriginTest.java | 66 +++++++++++++ .../apache/commons/io/build/FileOriginTest.java | 37 +++++++ .../commons/io/build/InputStreamOriginTest.java | 70 +++++++++++++ .../commons/io/build/OutputStreamOriginTest.java | 84 ++++++++++++++++ .../apache/commons/io/build/PathOriginTest.java | 38 ++++++++ .../apache/commons/io/build/ReaderOriginTest.java | 70 +++++++++++++ .../org/apache/commons/io/build/URIOriginTest.java | 38 ++++++++ .../commons/io/build/WriterStreamOriginTest.java | 83 ++++++++++++++++ 12 files changed, 729 insertions(+), 7 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b364456c..3a86179f 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -68,6 +68,9 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="add" due-to="Gary Gregory"> Add AbstractStreamBuilder.setOpenOptions(OpenOption...). </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add some missing conversions to AbstractOrigin subclasses. + </action> <!-- UPDATE --> </release> <release version="2.12.0" date="2023-05-13" description="Java 8 required."> diff --git a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java index da4652ef..f2acd8a3 100644 --- a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java +++ b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java @@ -21,7 +21,9 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.net.URI; @@ -32,6 +34,10 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Objects; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.ReaderInputStream; +import org.apache.commons.io.output.WriterOutputStream; + /** * Abstracts the origin of data for builders like a {@link File}, {@link Path}, {@link Reader}, {@link Writer}, {@link InputStream}, {@link OutputStream}, and * {@link URI}. @@ -71,6 +77,11 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends return new ByteArrayInputStream(origin); } + @Override + public Reader getReader(final Charset charset) throws IOException { + return new InputStreamReader(getInputStream(), charset); + } + } /** @@ -87,6 +98,12 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends super(origin); } + @Override + public byte[] getByteArray() { + // TODO Pass in a Charset? Consider if call sites actually need this. + return origin.toString().getBytes(Charset.defaultCharset()); + } + @Override public CharSequence getCharSequence(final Charset charset) { // No conversion @@ -101,6 +118,11 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends // return CharSequenceInputStream.builder().setCharSequence(getCharSequence(Charset.defaultCharset())).get(); } + @Override + public Reader getReader(final Charset charset) throws IOException { + return new InputStreamReader(getInputStream(), charset); + } + } /** @@ -136,7 +158,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends /** * An {@link InputStream} origin. * <p> - * This origin cannot provide other aspects. + * This origin cannot provide some of the other aspects. * </p> */ public static class InputStreamOrigin extends AbstractOrigin<InputStream, InputStreamOrigin> { @@ -150,18 +172,28 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends super(origin); } + @Override + public byte[] getByteArray() throws IOException { + return IOUtils.toByteArray(origin); + } + @Override public InputStream getInputStream(final OpenOption... options) { // No conversion return get(); } + @Override + public Reader getReader(final Charset charset) throws IOException { + return new InputStreamReader(getInputStream(), charset); + } + } /** * An {@link OutputStream} origin. * <p> - * This origin cannot provide other aspects. + * This origin cannot provide some of the other aspects. * </p> */ public static class OutputStreamOrigin extends AbstractOrigin<OutputStream, OutputStreamOrigin> { @@ -181,6 +213,10 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends return get(); } + @Override + public Writer getWriter(final Charset charset, final OpenOption... options) throws IOException { + return new OutputStreamWriter(origin, charset); + } } /** @@ -230,6 +266,23 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends super(origin); } + @Override + public byte[] getByteArray() throws IOException { + // TODO Pass in a Charset? Consider if call sites actually need this. + return IOUtils.toByteArray(origin, Charset.defaultCharset()); + } + + @Override + public CharSequence getCharSequence(final Charset charset) throws IOException { + return IOUtils.toString(origin); + } + + @Override + public InputStream getInputStream(final OpenOption... options) throws IOException { + // TODO Pass in a Charset? Consider if call sites actually need this. + return ReaderInputStream.builder().setReader(origin).setCharset(Charset.defaultCharset()).get(); + } + @Override public Reader getReader(final Charset charset) throws IOException { // No conversion @@ -280,6 +333,12 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends super(origin); } + @Override + public OutputStream getOutputStream(final OpenOption... options) throws IOException { + // TODO Pass in a Charset? Consider if call sites actually need this. + return WriterOutputStream.builder().setWriter(origin).setCharset(Charset.defaultCharset()).get(); + } + @Override public Writer getWriter(final Charset charset, final OpenOption... options) throws IOException { // No conversion @@ -315,7 +374,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends * Gets this origin as a byte array, if possible. * * @return this origin as a byte array, if possible. - * @throws IOException if an I/O error occurs. + * @throws IOException if an I/O error occurs. * @throws UnsupportedOperationException if the origin cannot be converted to a Path. */ public byte[] getByteArray() throws IOException { @@ -327,7 +386,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends * * @param charset The charset to use if conversion from bytes is needed. * @return this origin as a byte array, if possible. - * @throws IOException if an I/O error occurs. + * @throws IOException if an I/O error occurs. * @throws UnsupportedOperationException if the origin cannot be converted to a Path. */ public CharSequence getCharSequence(final Charset charset) throws IOException { @@ -349,7 +408,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends * * @param options options specifying how the file is opened * @return this origin as an InputStream, if possible. - * @throws IOException if an I/O error occurs. + * @throws IOException if an I/O error occurs. * @throws UnsupportedOperationException if the origin cannot be converted to a Path. */ public InputStream getInputStream(final OpenOption... options) throws IOException { @@ -361,7 +420,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends * * @param options options specifying how the file is opened * @return this origin as an OutputStream, if possible. - * @throws IOException if an I/O error occurs. + * @throws IOException if an I/O error occurs. * @throws UnsupportedOperationException if the origin cannot be converted to a Path. */ public OutputStream getOutputStream(final OpenOption... options) throws IOException { @@ -395,7 +454,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends * @param charset the charset to use for encoding * @param options options specifying how the file is opened * @return a new Writer on the origin. - * @throws IOException if an I/O error occurs opening or creating the file. + * @throws IOException if an I/O error occurs opening or creating the file. * @throws UnsupportedOperationException if the origin cannot be converted to a Path. */ public Writer getWriter(final Charset charset, final OpenOption... options) throws IOException { diff --git a/src/test/java/org/apache/commons/io/build/AbstractOriginTest.java b/src/test/java/org/apache/commons/io/build/AbstractOriginTest.java new file mode 100644 index 00000000..c9412fcb --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/AbstractOriginTest.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.nio.charset.Charset; +import java.util.Objects; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link AbstractOrigin} and subclasses. + * + * @param <T> the type of instances to build. + * @param <B> the type of builder subclass. + */ +public abstract class AbstractOriginTest<T, B extends AbstractOrigin<T, B>> { + + protected static final String FILE_NAME_RO = "src/test/resources/org/apache/commons/io/test-file-20byteslength.bin"; + protected static final String FILE_NAME_RW = "target/" + AbstractOriginTest.class.getSimpleName() + ".txt"; + + protected AbstractOrigin<T, B> originRo; + protected AbstractOrigin<T, B> originRw; + + protected AbstractOrigin<T, B> getOriginRo() { + return Objects.requireNonNull(originRo, "originRo"); + } + + protected AbstractOrigin<T, B> getOriginRw() { + return Objects.requireNonNull(originRw, "originRw"); + } + + protected void setOriginRo(final AbstractOrigin<T, B> origin) { + this.originRo = origin; + } + + protected void setOriginRw(final AbstractOrigin<T, B> origin) { + this.originRw = origin; + } + + @Test + public void testGetByteArray() throws IOException { + assertNotNull(getOriginRo().getByteArray()); + } + + @Test + public void testGetCharSequence() throws IOException { + assertNotNull(getOriginRo().getCharSequence(Charset.defaultCharset())); + } + + @Test + public void testGetFile() { + assertNotNull(getOriginRo().getFile()); + } + + @Test + public void testGetInputStream() throws IOException { + try (final InputStream inputStream = getOriginRo().getInputStream()) { + assertNotNull(inputStream); + } + } + + @Test + public void testGetOutputStream() throws IOException { + try (final OutputStream output = getOriginRw().getOutputStream()) { + assertNotNull(output); + } + } + + @Test + public void testGetPath() { + assertNotNull(getOriginRo().getPath()); + } + + @Test + public void testGetReader() throws IOException { + try (final Reader reader = getOriginRo().getReader(Charset.defaultCharset())) { + assertNotNull(reader); + } + } + + @Test + public void testGetWriter() throws IOException { + try (final Writer writer = getOriginRw().getWriter(Charset.defaultCharset())) { + assertNotNull(writer); + } + } +} diff --git a/src/test/java/org/apache/commons/io/build/ByteArrayOriginTest.java b/src/test/java/org/apache/commons/io/build/ByteArrayOriginTest.java new file mode 100644 index 00000000..6a432d56 --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/ByteArrayOriginTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link ByteArrayOrigin}. + * + * A ByteArrayOrigin can convert into some of the other aspects. + */ +public class ByteArrayOriginTest extends AbstractOriginTest<byte[], ByteArrayOrigin> { + + @BeforeEach + public void beforeEach() { + setOriginRo(new ByteArrayOrigin(new byte[] { 0 })); + setOriginRw(new ByteArrayOrigin(new byte[] { 1 })); + } + + @Override + @Test + public void testGetFile() { + // Cannot convert a byte[] to a File. + assertThrows(UnsupportedOperationException.class, super::testGetFile); + } + + @Override + @Test + public void testGetOutputStream() { + // Cannot convert a byte[] to an OutputStream. + assertThrows(UnsupportedOperationException.class, super::testGetOutputStream); + } + + @Override + @Test + public void testGetPath() { + // Cannot convert a byte[] to a Path. + assertThrows(UnsupportedOperationException.class, super::testGetPath); + } + + @Override + @Test + public void testGetWriter() { + // Cannot convert a byte[] to a Writer. + assertThrows(UnsupportedOperationException.class, super::testGetWriter); + } + +} diff --git a/src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java b/src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java new file mode 100644 index 00000000..ae6315ca --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.apache.commons.io.build.AbstractOrigin.CharSequenceOrigin; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link CharSequenceOrigin}. + * + * A CharSequenceOrigin can convert into some of the other aspects. + */ +public class CharSequenceOriginTest extends AbstractOriginTest<CharSequence, CharSequenceOrigin> { + + @BeforeEach + public void beforeEach() { + setOriginRo(new CharSequenceOrigin("Hello")); + setOriginRw(new CharSequenceOrigin("World")); + } + + @Override + @Test + public void testGetFile() { + // Cannot convert a CharSequence to a File. + assertThrows(UnsupportedOperationException.class, super::testGetFile); + } + + @Override + @Test + public void testGetOutputStream() { + // Cannot convert a CharSequence to an OutputStream. + assertThrows(UnsupportedOperationException.class, super::testGetOutputStream); + } + + @Override + @Test + public void testGetPath() { + // Cannot convert a CharSequence to a Path. + assertThrows(UnsupportedOperationException.class, super::testGetPath); + } + + @Override + @Test + public void testGetWriter() { + // Cannot convert a CharSequence to a Writer. + assertThrows(UnsupportedOperationException.class, super::testGetWriter); + } + +} diff --git a/src/test/java/org/apache/commons/io/build/FileOriginTest.java b/src/test/java/org/apache/commons/io/build/FileOriginTest.java new file mode 100644 index 00000000..ec57f389 --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/FileOriginTest.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import java.io.File; + +import org.apache.commons.io.build.AbstractOrigin.FileOrigin; +import org.junit.jupiter.api.BeforeEach; + +/** + * Tests {@link FileOrigin}. + * + * A FileOrigin can convert into all other aspects. + */ +public class FileOriginTest extends AbstractOriginTest<File, FileOrigin> { + + @BeforeEach + public void beforeEach() { + setOriginRo(new FileOrigin(new File(FILE_NAME_RO))); + setOriginRw(new FileOrigin(new File(FILE_NAME_RW))); + } + +} diff --git a/src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java b/src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java new file mode 100644 index 00000000..7228b7b1 --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.InputStream; + +import org.apache.commons.io.build.AbstractOrigin.InputStreamOrigin; +import org.apache.commons.io.input.CharSequenceInputStream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link InputStreamOrigin}. + * + * A InputStreamOrigin can convert into some of the other aspects. + */ +public class InputStreamOriginTest extends AbstractOriginTest<InputStream, InputStreamOrigin> { + + @SuppressWarnings("resource") + @BeforeEach + public void beforeEach() { + setOriginRo(new InputStreamOrigin(CharSequenceInputStream.builder().setCharSequence("Hello").get())); + setOriginRw(new InputStreamOrigin(CharSequenceInputStream.builder().setCharSequence("World").get())); + } + + @Override + @Test + public void testGetFile() { + // Cannot convert a InputStream to a File. + assertThrows(UnsupportedOperationException.class, super::testGetFile); + } + + @Override + @Test + public void testGetOutputStream() { + // Cannot convert a InputStream to an OutputStream. + assertThrows(UnsupportedOperationException.class, super::testGetOutputStream); + } + + @Override + @Test + public void testGetPath() { + // Cannot convert a InputStream to a Path. + assertThrows(UnsupportedOperationException.class, super::testGetPath); + } + + @Override + @Test + public void testGetWriter() { + // Cannot convert a InputStream to a Writer. + assertThrows(UnsupportedOperationException.class, super::testGetWriter); + } + +} diff --git a/src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java b/src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java new file mode 100644 index 00000000..1a77fd48 --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.OutputStream; + +import org.apache.commons.io.build.AbstractOrigin.OutputStreamOrigin; +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link OutputStreamOrigin}. + * + * A OutputStreamOrigin can convert into some of the other aspects. + */ +public class OutputStreamOriginTest extends AbstractOriginTest<OutputStream, OutputStreamOrigin> { + + @SuppressWarnings("resource") + @BeforeEach + public void beforeEach() { + setOriginRo(new OutputStreamOrigin(new ByteArrayOutputStream())); + setOriginRw(new OutputStreamOrigin(new ByteArrayOutputStream())); + } + + @Override + @Test + public void testGetByteArray() { + // Cannot convert a OutputStream to a byte[]. + assertThrows(UnsupportedOperationException.class, super::testGetByteArray); + } + + @Override + @Test + public void testGetCharSequence() { + // Cannot convert a OutputStream to a CharSequence. + assertThrows(UnsupportedOperationException.class, super::testGetCharSequence); + } + + @Override + @Test + public void testGetFile() { + // Cannot convert a OutputStream to a File. + assertThrows(UnsupportedOperationException.class, super::testGetFile); + } + + @Override + @Test + public void testGetInputStream() { + // Cannot convert a OutputStream to an InputStream. + assertThrows(UnsupportedOperationException.class, super::testGetInputStream); + } + + @Override + @Test + public void testGetPath() { + // Cannot convert a OutputStream to a Path. + assertThrows(UnsupportedOperationException.class, super::testGetPath); + } + + @Override + @Test + public void testGetReader() { + // Cannot convert a OutputStream to a Reader. + assertThrows(UnsupportedOperationException.class, super::testGetReader); + } + +} diff --git a/src/test/java/org/apache/commons/io/build/PathOriginTest.java b/src/test/java/org/apache/commons/io/build/PathOriginTest.java new file mode 100644 index 00000000..b94f2ea5 --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/PathOriginTest.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.commons.io.build.AbstractOrigin.PathOrigin; +import org.junit.jupiter.api.BeforeEach; + +/** + * Tests {@link PathOrigin}. + * + * A PathOrigin can convert into all other aspects. + */ +public class PathOriginTest extends AbstractOriginTest<Path, PathOrigin> { + + @BeforeEach + public void beforeEach() { + setOriginRo(new PathOrigin(Paths.get(FILE_NAME_RO))); + setOriginRw(new PathOrigin(Paths.get(FILE_NAME_RW))); + } + +} diff --git a/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java b/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java new file mode 100644 index 00000000..10af055b --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.Reader; + +import org.apache.commons.io.build.AbstractOrigin.ReaderOrigin; +import org.apache.commons.io.input.CharSequenceReader; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link ReaderOrigin}. + * + * A ReaderOrigin can convert into some of the other aspects. + */ +public class ReaderOriginTest extends AbstractOriginTest<Reader, ReaderOrigin> { + + @SuppressWarnings("resource") + @BeforeEach + public void beforeEach() { + setOriginRo(new ReaderOrigin(new CharSequenceReader("Hello"))); + setOriginRw(new ReaderOrigin(new CharSequenceReader("World"))); + } + + @Override + @Test + public void testGetFile() { + // Cannot convert a Reader to a File. + assertThrows(UnsupportedOperationException.class, super::testGetFile); + } + + @Override + @Test + public void testGetOutputStream() { + // Cannot convert a Reader to an OutputStream. + assertThrows(UnsupportedOperationException.class, super::testGetOutputStream); + } + + @Override + @Test + public void testGetPath() { + // Cannot convert a Reader to a Path. + assertThrows(UnsupportedOperationException.class, super::testGetPath); + } + + @Override + @Test + public void testGetWriter() { + // Cannot convert a Reader to a Writer. + assertThrows(UnsupportedOperationException.class, super::testGetWriter); + } + +} diff --git a/src/test/java/org/apache/commons/io/build/URIOriginTest.java b/src/test/java/org/apache/commons/io/build/URIOriginTest.java new file mode 100644 index 00000000..f609d7a2 --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/URIOriginTest.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import java.net.URI; +import java.nio.file.Paths; + +import org.apache.commons.io.build.AbstractOrigin.URIOrigin; +import org.junit.jupiter.api.BeforeEach; + +/** + * Tests {@link URIOrigin}. + * + * A URIOrigin can convert into all other aspects. + */ +public class URIOriginTest extends AbstractOriginTest<URI, URIOrigin> { + + @BeforeEach + public void beforeEach() { + setOriginRo(new URIOrigin(Paths.get(FILE_NAME_RO).toUri())); + setOriginRw(new URIOrigin(Paths.get(FILE_NAME_RW).toUri())); + } + +} diff --git a/src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java b/src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java new file mode 100644 index 00000000..1037f922 --- /dev/null +++ b/src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.build; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.StringWriter; +import java.io.Writer; + +import org.apache.commons.io.build.AbstractOrigin.WriterOrigin; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link WriterOrigin}. + * + * A WriterOrigin can convert into some of the other aspects. + */ +public class WriterStreamOriginTest extends AbstractOriginTest<Writer, WriterOrigin> { + + @BeforeEach + public void beforeEach() { + setOriginRo(new WriterOrigin(new StringWriter())); + setOriginRw(new WriterOrigin(new StringWriter())); + } + + @Override + @Test + public void testGetByteArray() { + // Cannot convert a Writer to a byte[]. + assertThrows(UnsupportedOperationException.class, super::testGetByteArray); + } + + @Override + @Test + public void testGetCharSequence() { + // Cannot convert a Writer to a CharSequence. + assertThrows(UnsupportedOperationException.class, super::testGetCharSequence); + } + + @Override + @Test + public void testGetFile() { + // Cannot convert a Writer to a File. + assertThrows(UnsupportedOperationException.class, super::testGetFile); + } + + @Override + @Test + public void testGetInputStream() { + // Cannot convert a Writer to an InputStream. + assertThrows(UnsupportedOperationException.class, super::testGetInputStream); + } + + @Override + @Test + public void testGetPath() { + // Cannot convert a Writer to a Path. + assertThrows(UnsupportedOperationException.class, super::testGetPath); + } + + @Override + @Test + public void testGetReader() { + // Cannot convert a Writer to a Reader. + assertThrows(UnsupportedOperationException.class, super::testGetReader); + } + +}