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    *      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.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.io.Reader;
26  import java.nio.charset.StandardCharsets;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.apache.commons.io.build.AbstractOrigin.CharSequenceOrigin;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests {@link CharSequenceOrigin}.
35   *
36   * A CharSequenceOrigin can convert into some of the other aspects.
37   */
38  public class CharSequenceOriginTest extends AbstractOriginTest<CharSequence, CharSequenceOrigin> {
39  
40      @BeforeEach
41      public void beforeEach() throws FileNotFoundException, IOException {
42          setOriginRo(new CharSequenceOrigin(getFixtureStringFromFile()));
43          setOriginRw(new CharSequenceOrigin("World"));
44      }
45  
46      private String getFixtureStringFromFile() throws IOException {
47          return IOUtils.resourceToString(FILE_RES_RO, StandardCharsets.UTF_8);
48      }
49  
50      @Override
51      @Test
52      public void testGetFile() {
53          // Cannot convert a CharSequence to a File.
54          assertThrows(UnsupportedOperationException.class, super::testGetFile);
55      }
56  
57      @Override
58      @Test
59      public void testGetOutputStream() {
60          // Cannot convert a CharSequence to an OutputStream.
61          assertThrows(UnsupportedOperationException.class, super::testGetOutputStream);
62      }
63  
64      @Override
65      @Test
66      public void testGetPath() {
67          // Cannot convert a CharSequence to a Path.
68          assertThrows(UnsupportedOperationException.class, super::testGetPath);
69      }
70  
71      @Test
72      public void testGetReaderIgnoreCharset() throws IOException {
73          // The CharSequenceOrigin ignores the given Charset.
74          try (final Reader reader = getOriginRo().getReader(StandardCharsets.UTF_16LE)) {
75              assertNotNull(reader);
76              assertEquals(getFixtureStringFromFile(), IOUtils.toString(reader));
77          }
78      }
79  
80      @Override
81      @Test
82      public void testGetWriter() {
83          // Cannot convert a CharSequence to a Writer.
84          assertThrows(UnsupportedOperationException.class, super::testGetWriter);
85      }
86  
87  }