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