View Javadoc
1   /*-
2    * #%L
3    * io.earcam.instrumental.reflect
4    * %%
5    * Copyright (C) 2018 earcam
6    * %%
7    * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8    * 
9    * You <b>must</b> choose to accept, in full - any individual or combination of 
10   * the following licenses:
11   * <ul>
12   * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13   * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14   * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15   * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16   * </ul>
17   * #L%
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  		// EARCAM_SNIPPET_BEGIN: source-jar
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  		// EARCAM_SNIPPET_END: source-jar
74  	}
75  
76  
77  	@Test
78  	void sourceOfResourceIsATheFileSystem()
79  	{
80  		// EARCAM_SNIPPET_BEGIN: source-dir
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  		// EARCAM_SNIPPET_END: source-dir
87  	}
88  }