1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package io.earcam.instrumental.reflect;
19
20 import static java.nio.charset.StandardCharsets.UTF_8;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.*;
23 import static org.hamcrest.io.FileMatchers.*;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29
30 import javax.annotation.concurrent.Immutable;
31
32 import org.junit.jupiter.api.Test;
33
34 import io.earcam.utilitarian.io.IoStreams;
35
36 public class ResourcesTest {
37
38 @Test
39 void readsAllBytesAlbeitOneAtATime()
40 {
41 byte[] bytes = "blah blah blah".getBytes(UTF_8);
42 byte[] read = Resources.readAllBytes(new ByteArrayInputStream(bytes));
43
44 assertThat(read, is(equalTo(bytes)));
45 }
46
47
48 @Test
49 void classAsBytes() throws IOException
50 {
51 byte[] read = Resources.classAsBytes(ResourcesTest.class);
52
53 String resource = ResourcesTest.class.getCanonicalName().replace('.', '/') + ".class";
54 try(InputStream in = ResourcesTest.class.getClassLoader().getResourceAsStream(resource)) {
55 byte[] expected = IoStreams.readAllBytes(in);
56
57 assertThat(read, is(equalTo(expected)));
58 }
59 }
60
61
62 @Test
63 void sourceOfResourceIsAJar()
64 {
65
66 String resource = Resources.sourceOfResource(Immutable.class);
67
68 assertThat(new File(resource), is(aReadableFile()));
69
70 assertThat(resource, allOf(
71 containsString("/com/google/code/findbugs/jsr305/"),
72 endsWith(".jar")));
73
74 }
75
76
77 @Test
78 void sourceOfResourceIsATheFileSystem()
79 {
80
81 String resource = Resources.sourceOfResource(ResourcesTest.class);
82
83 assertThat(resource, new File(resource), is(anExistingDirectory()));
84
85 assertThat(resource, endsWith("/target/test-classes/"));
86
87 }
88 }