View Javadoc
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    *      https://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.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.io.IOException;
24  import java.io.Reader;
25  import java.nio.charset.StandardCharsets;
26  import java.nio.file.OpenOption;
27  import java.nio.file.StandardOpenOption;
28  
29  import org.apache.commons.io.IOUtils;
30  import org.apache.commons.io.build.AbstractOrigin.CharSequenceOrigin;
31  import org.junit.jupiter.api.Test;
32  import org.junit.jupiter.params.ParameterizedTest;
33  import org.junit.jupiter.params.provider.EnumSource;
34  
35  /**
36   * Tests {@link CharSequenceOrigin}.
37   *
38   * A CharSequenceOrigin can convert into some of the other aspects.
39   *
40   * @see CharSequence
41   */
42  class CharSequenceOriginTest extends AbstractOriginTest<CharSequence, CharSequenceOrigin> {
43  
44      private String getFixtureStringFromFile() throws IOException {
45          return IOUtils.resourceToString(FILE_RES_RO, StandardCharsets.UTF_8);
46      }
47  
48      @Override
49      protected CharSequenceOrigin newOriginRo() throws IOException {
50          return new CharSequenceOrigin(getFixtureStringFromFile());
51      }
52  
53      @Override
54      protected CharSequenceOrigin newOriginRw() {
55          return new CharSequenceOrigin("World");
56      }
57  
58      @Override
59      @Test
60      void testGetFile() {
61          // Cannot convert a CharSequence to a File.
62          assertThrows(UnsupportedOperationException.class, super::testGetFile);
63      }
64  
65      @Override
66      @Test
67      void testGetOutputStream() {
68          // Cannot convert a CharSequence to an OutputStream.
69          assertThrows(UnsupportedOperationException.class, super::testGetOutputStream);
70      }
71  
72      @Override
73      @Test
74      void testGetPath() {
75          // Cannot convert a CharSequence to a Path.
76          assertThrows(UnsupportedOperationException.class, super::testGetPath);
77      }
78  
79      @Override
80      @Test
81      void testGetRandomAccessFile() {
82          // Cannot convert a RandomAccessFile to a File.
83          assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
84      }
85  
86      @Override
87      @ParameterizedTest
88      @EnumSource(StandardOpenOption.class)
89      void testGetRandomAccessFile(final OpenOption openOption) {
90          // Cannot convert a RandomAccessFile to a File.
91          assertThrows(UnsupportedOperationException.class, () -> super.testGetRandomAccessFile(openOption));
92      }
93  
94      @Test
95      void testGetReaderIgnoreCharset() throws IOException {
96          // The CharSequenceOrigin ignores the given Charset.
97          try (Reader reader = getOriginRo().getReader(StandardCharsets.UTF_16LE)) {
98              assertNotNull(reader);
99              assertEquals(getFixtureStringFromFile(), IOUtils.toString(reader));
100         }
101     }
102 
103     @Test
104     void testGetReaderIgnoreCharsetNull() throws IOException {
105         // The CharSequenceOrigin ignores the given Charset.
106         try (Reader reader = getOriginRo().getReader(null)) {
107             assertNotNull(reader);
108             assertEquals(getFixtureStringFromFile(), IOUtils.toString(reader));
109         }
110     }
111 
112     @Override
113     @Test
114     void testGetWriter() {
115         // Cannot convert a CharSequence to a Writer.
116         assertThrows(UnsupportedOperationException.class, super::testGetWriter);
117     }
118 
119 }