@Test(expected = NoSuchElementException.class)
 public void testMultipleParentEnumerationNotFound() throws Exception {
   ClassLoader classLoader =
       new MultipleParentClassLoader.Builder().append(first, second, null).build();
   Enumeration<URL> enumeration = classLoader.getResources(BAZ);
   assertThat(enumeration.hasMoreElements(), is(false));
   enumeration.nextElement();
 }
예제 #2
0
 @Override
 public String call(Integer i) throws Exception {
   ClassLoader ccl = Thread.currentThread().getContextClassLoader();
   InputStream in = ccl.getResourceAsStream("test.resource");
   byte[] bytes = ByteStreams.toByteArray(in);
   in.close();
   return new String(bytes, 0, bytes.length, "UTF-8");
 }
 @Test
 public void testMultipleParentClassLoading() throws Exception {
   ClassLoader classLoader =
       new MultipleParentClassLoader.Builder().append(first, second, null).build();
   assertThat(classLoader.loadClass(FOO), CoreMatchers.<Class<?>>is(Foo.class));
   assertThat(classLoader.loadClass(BAR), CoreMatchers.<Class<?>>is(BarFirst.class));
   assertThat(classLoader.loadClass(QUX), CoreMatchers.<Class<?>>is(Qux.class));
   verify(first).loadClass(FOO);
   verify(first).loadClass(BAR);
   verify(first).loadClass(QUX);
   verifyNoMoreInteractions(first);
   verify(second).loadClass(QUX);
   verifyNoMoreInteractions(second);
 }
 @Test
 @IntegrationRule.Enforce
 public void testMultipleParentURL() throws Exception {
   ClassLoader classLoader =
       new MultipleParentClassLoader.Builder().append(first, second, null).build();
   assertThat(classLoader.getResource(FOO), is(fooUrl));
   assertThat(classLoader.getResource(BAR), is(barFirstUrl));
   assertThat(classLoader.getResource(QUX), is(quxUrl));
   verify(first).getResource(FOO);
   verify(first).getResource(BAR);
   verify(first).getResource(QUX);
   verifyNoMoreInteractions(first);
   verify(second).getResource(QUX);
   verifyNoMoreInteractions(second);
 }
 @Test
 @IntegrationRule.Enforce
 public void testMultipleParentEnumerationURL() throws Exception {
   ClassLoader classLoader =
       new MultipleParentClassLoader.Builder().append(first, second, null).build();
   Enumeration<URL> foo = classLoader.getResources(FOO);
   assertThat(foo.hasMoreElements(), is(true));
   assertThat(foo.nextElement(), is(fooUrl));
   assertThat(foo.hasMoreElements(), is(false));
   Enumeration<URL> bar = classLoader.getResources(BAR);
   assertThat(bar.hasMoreElements(), is(true));
   assertThat(bar.nextElement(), is(barFirstUrl));
   assertThat(bar.hasMoreElements(), is(true));
   assertThat(bar.nextElement(), is(barSecondUrl));
   assertThat(bar.hasMoreElements(), is(false));
   Enumeration<URL> qux = classLoader.getResources(QUX);
   assertThat(qux.hasMoreElements(), is(true));
   assertThat(qux.nextElement(), is(quxUrl));
   assertThat(qux.hasMoreElements(), is(false));
 }
 @Before
 public void setUp() throws Exception {
   doReturn(Foo.class).when(first).loadClass(FOO);
   doReturn(BarFirst.class).when(first).loadClass(BAR);
   when(first.loadClass(QUX)).thenThrow(new ClassNotFoundException());
   when(first.loadClass(BAZ)).thenThrow(new ClassNotFoundException());
   doReturn(BarSecond.class).when(second).loadClass(BAR);
   doReturn(Qux.class).when(second).loadClass(QUX);
   when(second.loadClass(BAZ)).thenThrow(new ClassNotFoundException());
   fooUrl = new URL(SCHEME + FOO);
   barFirstUrl = new URL(SCHEME + BAR);
   barSecondUrl = new URL(SCHEME + BAZ);
   quxUrl = new URL(SCHEME + QUX);
   when(first.getResource(FOO)).thenReturn(fooUrl);
   when(first.getResource(BAR)).thenReturn(barFirstUrl);
   when(second.getResource(BAR)).thenReturn(barSecondUrl);
   when(second.getResource(QUX)).thenReturn(quxUrl);
   when(first.getResources(FOO)).thenReturn(new SingleElementEnumeration(fooUrl));
   when(first.getResources(BAR)).thenReturn(new SingleElementEnumeration(barFirstUrl));
   when(second.getResources(BAR)).thenReturn(new SingleElementEnumeration(barSecondUrl));
   when(second.getResources(QUX)).thenReturn(new SingleElementEnumeration(quxUrl));
 }