View Javadoc
1   package net.trajano.commons.testing.test;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.io.InputStream;
9   
10  import org.junit.Test;
11  import org.mockito.Mockito;
12  
13  import net.trajano.commons.testing.ResourceUtil;
14  
15  public class ResourceUtilTest {
16  
17      @SuppressWarnings("unchecked")
18      @Test(expected = AssertionError.class)
19      public void testBadStream() throws Exception {
20  
21          final InputStream mock = Mockito.mock(InputStream.class);
22          Mockito.when(mock.read())
23                  .thenThrow(IOException.class);
24          ResourceUtil.streamToBytes(mock);
25      }
26  
27      @Test
28      public void testCopy() throws Exception {
29  
30          final File f = ResourceUtil.getResourceCopy(ResourceUtilTest.class, "deep.txt");
31          assertTrue(f.exists());
32          f.delete();
33      }
34  
35      @Test
36      public void testCopyContextClassLoader() throws Exception {
37  
38          final File f = ResourceUtil.getResourceCopy("sample.txt");
39          assertTrue(f.exists());
40          f.delete();
41      }
42  
43      @Test(expected = AssertionError.class)
44      public void testNonExistentFile() throws Exception {
45  
46          ResourceUtil.getResourceAsString(ResourceUtilTest.class, "not-there.txt");
47      }
48  
49      @Test(expected = AssertionError.class)
50      public void testNonExistentFileContextClassLoader() throws Exception {
51  
52          ResourceUtil.getResourceAsString("not-there.txt");
53      }
54  
55      @Test
56      public void testReadBinary() throws Exception {
57  
58          {
59              final byte[] bytes = ResourceUtil.getResourceAsBytes("sample.bin");
60              assertEquals(67646, bytes.length);
61          }
62          {
63              final byte[] bytes = ResourceUtil.getResourceAsBytes("sample.bin");
64              assertEquals(67646, bytes.length);
65          }
66      }
67  
68      @Test
69      public void testReadString() throws Exception {
70  
71          assertEquals("hello world", ResourceUtil.getResourceAsString("sample.txt"));
72          assertEquals("hello world", ResourceUtil.getResourceAsString("sample.txt"));
73      }
74  
75      @Test
76      public void testReadStringWithClass() throws Exception {
77  
78          assertEquals("deep inside", ResourceUtil.getResourceAsString(ResourceUtilTest.class, "deep.txt"));
79      }
80  
81  }