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