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          @Override
46          public ByteSource getByteSource(final byte[] src) throws IOException {
47              final File file = createTempFile(src);
48  
49              // test that all bytes written to file.
50              assertEquals(src.length, file.length());
51  
52              return ByteSource.file(file);
53          }
54      }
55  
56      private final class ByteSourceInputStreamFileFactory implements ByteSourceFactory {
57          @Override
58          public ByteSource getByteSource(final byte[] src) throws IOException {
59              final File file = createTempFile(src);
60  
61              final FileInputStream is = new FileInputStream(file);
62  
63              return ByteSource.inputStream(is, null);
64          }
65      }
66  
67      private static final class ByteSourceInputStreamRawFactory implements ByteSourceFactory {
68          @Override
69          public ByteSource getByteSource(final byte[] src) throws IOException {
70              final ByteArrayInputStream is = new ByteArrayInputStream(src);
71  
72              return ByteSource.inputStream(is, null);
73          }
74  
75      }
76  
77      private final class ByteSourcePathFactory implements ByteSourceFactory {
78          @Override
79          public ByteSource getByteSource(final byte[] src) throws IOException {
80              final Path file = createTempFile(src).toPath();
81  
82              // test that all bytes written to file.
83              assertEquals(src.length, Files.size(file));
84  
85              return ByteSource.path(file);
86          }
87      }
88  
89      public static Stream<byte[]> data() {
90          return Arrays.asList(getTestByteArrays()).stream();
91      }
92  
93      @ParameterizedTest
94      @MethodSource("data")
95      public void testByteSourceFileFactory(final byte[] testByteArray) throws Exception {
96          writeAndReadBytes(new ByteSourceFileFactory(), testByteArray);
97      }
98  
99      @ParameterizedTest
100     @MethodSource("data")
101     public void testByteSourceInputStreamFileFactory(final byte[] testByteArray) throws Exception {
102         writeAndReadBytes(new ByteSourceInputStreamFileFactory(), testByteArray);
103     }
104 
105     @ParameterizedTest
106     @MethodSource("data")
107     public void testByteSourceInputStreamRawFactory(final byte[] testByteArray) throws Exception {
108         writeAndReadBytes(new ByteSourceInputStreamRawFactory(), testByteArray);
109     }
110 
111     @ParameterizedTest
112     @MethodSource("data")
113     public void testByteSourcePathFactory(final byte[] testByteArray) throws Exception {
114         writeAndReadBytes(new ByteSourcePathFactory(), testByteArray);
115     }
116 
117     protected void writeAndReadBytes(final ByteSourceFactory byteSourceFactory, final byte[] src) throws IOException {
118         final ByteSource byteSource = byteSourceFactory.getByteSource(src);
119 
120         // test cache during interrupted read cache by reading only first N
121         // bytes.
122         {
123             try (InputStream is = byteSource.getInputStream()) {
124                 final byte[] prefix = new byte[256];
125                 final int read = is.read(prefix);
126 
127                 assertTrue(read <= src.length);
128                 for (int i = 0; i < read; i++) {
129                     assertEquals(src[i], prefix[i]);
130                 }
131             }
132         }
133 
134         // test cache by completely reading InputStream N times.
135         for (int j = 0; j < 5; j++) {
136             try (final InputStream is = byteSource.getInputStream()) {
137                 final byte[] dst = IOUtils.toByteArray(is);
138 
139                 assertArrayEquals(src, dst);
140             }
141         }
142 
143         if (src.length > 2) {
144             // test optional start param to getInputStream()
145 
146             final int start = src.length / 2;
147 
148             try (InputStream is = byteSource.getInputStream(start)) {
149                 final byte[] dst = IOUtils.toByteArray(is);
150 
151                 assertEquals(src.length, dst.length + start);
152                 for (int i = 0; i < dst.length; i++) {
153                     assertEquals(dst[i], src[i + start]);
154                 }
155             }
156         }
157 
158     }
159 }