View Javadoc
1   package com.atlassian.plugin.osgi.util;
2   
3   import org.apache.commons.io.IOUtils;
4   import org.junit.Test;
5   import org.osgi.framework.Bundle;
6   
7   import java.io.ByteArrayOutputStream;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.net.URL;
11  import java.util.Enumeration;
12  
13  import static org.hamcrest.Matchers.containsString;
14  import static org.hamcrest.Matchers.not;
15  import static org.junit.Assert.assertFalse;
16  import static org.junit.Assert.assertNotNull;
17  import static org.junit.Assert.assertThat;
18  import static org.junit.Assert.assertTrue;
19  import static org.mockito.Mockito.mock;
20  import static org.mockito.Mockito.when;
21  
22  public class TestBundleClassLoaderAccessor {
23  
24      @Test
25      public void testGetResource() {
26          Bundle bundle = mock(Bundle.class);
27          when(bundle.getResource("foo.txt")).thenReturn(getClass().getClassLoader().getResource("foo.txt"));
28  
29          URL url = BundleClassLoaderAccessor.getClassLoader(bundle, null).getResource("foo.txt");
30          assertNotNull(url);
31      }
32  
33      @Test
34      public void testGetResourceAsStream() throws IOException {
35          Bundle bundle = mock(Bundle.class);
36          when(bundle.getResource("foo.txt")).thenReturn(getClass().getClassLoader().getResource("foo.txt"));
37  
38          InputStream in = BundleClassLoaderAccessor.getClassLoader(bundle, null).getResourceAsStream("foo.txt");
39          ByteArrayOutputStream out = new ByteArrayOutputStream();
40          IOUtils.copy(in, out);
41          assertTrue(out.toByteArray().length > 0);
42      }
43  
44      @Test
45      public void testGetResources() throws IOException {
46          Bundle bundle = mock(Bundle.class);
47          when(bundle.getResources("foo.txt")).thenReturn(getClass().getClassLoader().getResources("foo.txt"));
48  
49          Enumeration<URL> e = BundleClassLoaderAccessor.getClassLoader(bundle, null).getResources("foo.txt");
50          assertNotNull(e);
51          assertTrue(e.hasMoreElements());
52      }
53  
54      @Test
55      public void testGetResourcesIfNull() throws IOException {
56          Bundle bundle = mock(Bundle.class);
57          when(bundle.getResources("foo.txt")).thenReturn(null);
58  
59          Enumeration<URL> e = BundleClassLoaderAccessor.getClassLoader(bundle, null).getResources("foo.txt");
60          assertNotNull(e);
61          assertFalse(e.hasMoreElements());
62      }
63  
64      @Test
65      public void testToStringWithSymbolicName() {
66          final Bundle bundle = mock(Bundle.class);
67          when(bundle.getBundleId()).thenReturn(42L);
68          when(bundle.getSymbolicName()).thenReturn("howdy!");
69  
70          final ClassLoader classLoader = BundleClassLoaderAccessor.getClassLoader(bundle, null);
71          final String result = classLoader.toString();
72  
73          assertThat("has class name", result, containsString("BundleClassLoader"));
74          assertThat("has symbolic name", result, containsString("howdy!"));
75          assertThat("has bundle ID", result, containsString("[42]"));
76      }
77  
78      @Test
79      public void testToStringWithoutSymbolicName() {
80          final Bundle bundle = mock(Bundle.class);
81          when(bundle.getBundleId()).thenReturn(42L);
82  
83          final ClassLoader classLoader = BundleClassLoaderAccessor.getClassLoader(bundle, null);
84          final String result = classLoader.toString();
85  
86          assertThat("has class name", result, containsString("BundleClassLoader"));
87          assertThat("doesn't use 'null' symbolic name", result, not(containsString("null")));
88          assertThat("has bundle ID", result, containsString("[42]"));
89      }
90  }