View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.spi;
6   
7   import java.io.File;
8   import java.nio.charset.StandardCharsets;
9   import java.nio.file.Files;
10  import java.nio.file.Path;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  import org.junit.jupiter.api.io.TempDir;
15  
16  /**
17   * Test case for {@link Binary}.
18   *
19   * @since 0.24
20   */
21  final class BinaryTest {
22  
23      @Test
24      void detectsPngFileAsBinary(@TempDir final Path dir) throws Exception {
25          final File file = dir.resolve("pixel.png").toFile();
26          Files.write(
27              file.toPath(),
28              new byte[] {
29                  (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
30                  0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
31              }
32          );
33          MatcherAssert.assertThat(
34              "PNG header must not be treated as a text file",
35              new Binary(file).yes(),
36              Matchers.is(true)
37          );
38      }
39  
40      @Test
41      void detectsJavaSourceAsNotBinary(@TempDir final Path dir)
42          throws Exception {
43          final File file = dir.resolve("Foo.java").toFile();
44          Files.writeString(
45              file.toPath(),
46              "class Foo { int bar = 42; }".concat(String.valueOf('\n')),
47              StandardCharsets.UTF_8
48          );
49          MatcherAssert.assertThat(
50              "plain Java source cannot be treated as binary",
51              new Binary(file).yes(),
52              Matchers.is(false)
53          );
54      }
55  
56      @Test
57      void treatsUnicodeTextWithAccentsAsNotBinary(@TempDir final Path dir)
58          throws Exception {
59          final File file = dir.resolve("notes.txt").toFile();
60          Files.writeString(
61              file.toPath(),
62              "voilà — árvíztűrő tükörfúrógép".concat(String.valueOf('\n')),
63              StandardCharsets.UTF_8
64          );
65          MatcherAssert.assertThat(
66              "UTF-8 multibyte characters must not be mistaken for binary data",
67              new Binary(file).yes(),
68              Matchers.is(false)
69          );
70      }
71  
72      @Test
73      void treatsEmptyFileAsNotBinary(@TempDir final Path dir) throws Exception {
74          final File file = dir.resolve("empty.txt").toFile();
75          Files.write(file.toPath(), new byte[0]);
76          MatcherAssert.assertThat(
77              "empty file cannot be classified as binary",
78              new Binary(file).yes(),
79              Matchers.is(false)
80          );
81      }
82  
83      @Test
84      void detectsFileWithNullByteAsBinary(@TempDir final Path dir)
85          throws Exception {
86          final File file = dir.resolve("blob.dat").toFile();
87          Files.write(
88              file.toPath(),
89              new byte[] {'h', 'e', 'l', 'l', 'o', 0x00, 'w', 'o', 'r', 'l', 'd'}
90          );
91          MatcherAssert.assertThat(
92              "file containing a NULL byte must be classified as binary",
93              new Binary(file).yes(),
94              Matchers.is(true)
95          );
96      }
97  
98      @Test
99      void treatsMissingFileAsNotBinary(@TempDir final Path dir) {
100         MatcherAssert.assertThat(
101             "non-existing file cannot be classified as binary",
102             new Binary(dir.resolve("missing").toFile()).yes(),
103             Matchers.is(false)
104         );
105     }
106 }