1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
121
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
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
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 }