1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.bytesource;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21
22 import java.io.BufferedOutputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.nio.file.Files;
29
30 import org.apache.commons.imaging.AbstractImagingTest;
31 import org.apache.commons.imaging.ImagingConstants;
32 import org.junit.jupiter.api.Test;
33
34 public abstract class AbstractByteSourceTest extends AbstractImagingTest {
35 protected static byte[][] getTestByteArrays() {
36 final byte[] single = new byte[1];
37 for (int i = 0; i < single.length; i++) {
38 single[i] = (byte) i;
39 }
40
41 final byte[] simple = new byte[256];
42 for (int i = 0; i < simple.length; i++) {
43 simple[i] = (byte) i;
44 }
45
46 final byte[] zeroes = new byte[256];
47
48 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
49 for (int i = 0; i < 256 * 256; i++) {
50 baos.write(0xff & i);
51 baos.write(0xff & i >> 8);
52 }
53 final byte[] longArray = baos.toByteArray();
54
55 return new byte[][] { ImagingConstants.EMPTY_BYTE_ARRAY, single, simple, zeroes, longArray, };
56 }
57
58 protected File createTempFile(final byte[] src) throws IOException {
59 final File file = Files.createTempFile("raw_", ".bin").toFile();
60
61
62 try (FileOutputStream fos = new FileOutputStream(file);
63 OutputStream os = new BufferedOutputStream(fos)) {
64 os.write(src);
65 }
66
67
68 assertEquals(src.length, file.length());
69
70 return file;
71 }
72
73 @Test
74 public void testGetInputStreamThrowsNullPointerException() {
75 assertThrows(NullPointerException.class, () -> ByteSource.array(null));
76 }
77
78 }