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.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.io.Reader;
27  import java.io.Writer;
28  import java.nio.charset.Charset;
29  import java.nio.file.Files;
30  import java.nio.file.Paths;
31  import java.util.Objects;
32  
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Tests {@link AbstractOrigin} and subclasses.
37   *
38   * @param <T> the type of instances to build.
39   * @param <B> the type of builder subclass.
40   */
41  public abstract class AbstractOriginTest<T, B extends AbstractOrigin<T, B>> {
42  
43      protected static final String FILE_RES_RO = "/org/apache/commons/io/test-file-20byteslength.bin";
44      protected static final String FILE_NAME_RO = "src/test/resources" + FILE_RES_RO;
45      protected static final String FILE_NAME_RW = "target/" + AbstractOriginTest.class.getSimpleName() + ".txt";
46  
47      protected AbstractOrigin<T, B> originRo;
48      protected AbstractOrigin<T, B> originRw;
49  
50      protected AbstractOrigin<T, B> getOriginRo() {
51          return Objects.requireNonNull(originRo, "originRo");
52      }
53  
54      protected AbstractOrigin<T, B> getOriginRw() {
55          return Objects.requireNonNull(originRw, "originRw");
56      }
57  
58      protected void setOriginRo(final AbstractOrigin<T, B> origin) {
59          this.originRo = origin;
60      }
61  
62      protected void setOriginRw(final AbstractOrigin<T, B> origin) {
63          this.originRw = origin;
64      }
65  
66      @Test
67      public void testGetByteArray() throws IOException {
68          assertArrayEquals(Files.readAllBytes(Paths.get(FILE_NAME_RO)), getOriginRo().getByteArray());
69      }
70  
71      @Test
72      public void testGetByteArrayAt_0_0() throws IOException {
73          assertArrayEquals(new byte[] {}, getOriginRo().getByteArray(0, 0));
74      }
75  
76      @Test
77      public void testGetByteArrayAt_0_1() throws IOException {
78          assertArrayEquals(new byte[] { '1' }, getOriginRo().getByteArray(0, 1));
79      }
80  
81      @Test
82      public void testGetByteArrayAt_1_1() throws IOException {
83          assertArrayEquals(new byte[] { '2' }, getOriginRo().getByteArray(1, 1));
84      }
85  
86      @Test
87      public void testGetCharSequence() throws IOException {
88          assertNotNull(getOriginRo().getCharSequence(Charset.defaultCharset()));
89      }
90  
91      @Test
92      public void testGetFile() {
93          assertNotNull(getOriginRo().getFile());
94      }
95  
96      @Test
97      public void testGetInputStream() throws IOException {
98          try (final InputStream inputStream = getOriginRo().getInputStream()) {
99              assertNotNull(inputStream);
100         }
101     }
102 
103     @Test
104     public void testGetOutputStream() throws IOException {
105         try (final OutputStream output = getOriginRw().getOutputStream()) {
106             assertNotNull(output);
107         }
108     }
109 
110     @Test
111     public void testGetPath() {
112         assertNotNull(getOriginRo().getPath());
113     }
114 
115     @Test
116     public void testGetReader() throws IOException {
117         try (final Reader reader = getOriginRo().getReader(Charset.defaultCharset())) {
118             assertNotNull(reader);
119         }
120     }
121 
122     @Test
123     public void testGetWriter() throws IOException {
124         try (final Writer writer = getOriginRw().getWriter(Charset.defaultCharset())) {
125             assertNotNull(writer);
126         }
127     }
128 
129     @Test
130     public void testSize() throws IOException {
131         assertEquals(Files.size(Paths.get(FILE_NAME_RO)), getOriginRo().getByteArray().length);
132     }
133 }