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  
18  package org.apache.commons.imaging.bytesource;
19  
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.File;
26  import java.io.FileInputStream;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  import java.util.Arrays;
32  import java.util.stream.Stream;
33  
34  import org.apache.commons.io.IOUtils;
35  import org.junit.jupiter.params.ParameterizedTest;
36  import org.junit.jupiter.params.provider.MethodSource;
37  
38  public class ByteSourceDataTest extends AbstractByteSourceTest {
39  
40      private interface ByteSourceFactory {
41          ByteSource getByteSource(byte[] src) throws IOException;
42      }
43  
44      private final class ByteSourceFileFactory implements ByteSourceFactory {
45  
46          @Override
47          public ByteSource getByteSource(final byte[] src) throws IOException {
48              final File file = createTempFile(src);
49              // test that all bytes written to file.
50              assertEquals(src.length, file.length());
51              return ByteSource.file(file);
52          }
53      }
54  
55      private final class ByteSourceInputStreamFileFactory implements ByteSourceFactory {
56  
57          @Override
58          public ByteSource getByteSource(final byte[] src) throws IOException {
59              final File file = createTempFile(src);
60              final FileInputStream is = new FileInputStream(file);
61              return ByteSource.inputStream(is, null);
62          }
63      }
64  
65      private static final class ByteSourceInputStreamRawFactory implements ByteSourceFactory {
66  
67          @Override
68          public ByteSource getByteSource(final byte[] src) throws IOException {
69              final ByteArrayInputStream is = new ByteArrayInputStream(src);
70              return ByteSource.inputStream(is, null);
71          }
72  
73      }
74  
75      private final class ByteSourcePathFactory implements ByteSourceFactory {
76  
77          @Override
78          public ByteSource getByteSource(final byte[] src) throws IOException {
79              final Path file = createTempFile(src).toPath();
80              // test that all bytes written to file.
81              assertEquals(src.length, Files.size(file));
82              return ByteSource.path(file);
83          }
84      }
85  
86      public static Stream<byte[]> data() {
87          return Arrays.asList(getTestByteArrays()).stream();
88      }
89  
90      @ParameterizedTest
91      @MethodSource("data")
92      public void testByteSourceFileFactory(final byte[] testByteArray) throws Exception {
93          writeAndReadBytes(new ByteSourceFileFactory(), testByteArray);
94      }
95  
96      @ParameterizedTest
97      @MethodSource("data")
98      public void testByteSourceInputStreamFileFactory(final byte[] testByteArray) throws Exception {
99          writeAndReadBytes(new ByteSourceInputStreamFileFactory(), testByteArray);
100     }
101 
102     @ParameterizedTest
103     @MethodSource("data")
104     public void testByteSourceInputStreamRawFactory(final byte[] testByteArray) throws Exception {
105         writeAndReadBytes(new ByteSourceInputStreamRawFactory(), testByteArray);
106     }
107 
108     @ParameterizedTest
109     @MethodSource("data")
110     public void testByteSourcePathFactory(final byte[] testByteArray) throws Exception {
111         writeAndReadBytes(new ByteSourcePathFactory(), testByteArray);
112     }
113 
114     protected void writeAndReadBytes(final ByteSourceFactory byteSourceFactory, final byte[] src) throws IOException {
115         final ByteSource byteSource = byteSourceFactory.getByteSource(src);
116         // test cache during interrupted read cache by reading only first N
117         // bytes.
118         {
119             try (InputStream is = byteSource.getInputStream()) {
120                 final byte[] prefix = new byte[256];
121                 final int read = is.read(prefix);
122                 assertTrue(read <= src.length);
123                 for (int i = 0; i < read; i++) {
124                     assertEquals(src[i], prefix[i]);
125                 }
126             }
127         }
128         // test cache by completely reading InputStream N times.
129         for (int j = 0; j < 5; j++) {
130             try (InputStream is = byteSource.getInputStream()) {
131                 final byte[] dst = IOUtils.toByteArray(is);
132                 assertArrayEquals(src, dst);
133             }
134         }
135         if (src.length > 2) {
136             // test optional start param to getInputStream()
137             final int start = src.length / 2;
138             try (InputStream is = ByteSource.getInputStream(byteSource, start)) {
139                 final byte[] dst = IOUtils.toByteArray(is);
140                 assertEquals(src.length, dst.length + start);
141                 for (int i = 0; i < dst.length; i++) {
142                     assertEquals(dst[i], src[i + start]);
143                 }
144             }
145         }
146 
147     }
148 }