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.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          // write test bytes to file.
62          try (FileOutputStream fos = new FileOutputStream(file);
63                  OutputStream os = new BufferedOutputStream(fos)) {
64              os.write(src);
65          }
66  
67          // test that all bytes written to file.
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  }