Search in sources :

Example 16 with ClassLoaderLookupPolicy

use of org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy in project mule by mulesoft.

the class FineGrainedControlClassLoaderTestCase method usesParentFirstAndChildLookupAndFails.

@Test
public void usesParentFirstAndChildLookupAndFails() throws Exception {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
    when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(PARENT_FIRST);
    expected.expect(CompositeClassNotFoundException.class);
    expected.expectMessage(startsWith("Cannot load class '" + TEST_CLASS_NAME + "': ["));
    FineGrainedControlClassLoader ext = buildFineGrainedControlClassLoader(parent, lookupPolicy);
    expected.expect(expressionMatches((e) -> ((CompositeClassNotFoundException) e).getExceptions(), contains(hasMessage(is(TEST_CLASS_NAME)), expressionMatches((e) -> ((TestClassNotFoundException) e).getClassLoader(), is((ClassLoader) ext)))));
    invokeTestClassMethod(ext);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) System.lineSeparator(java.lang.System.lineSeparator) FunctionExpressionMatcher.expressionMatches(org.mule.tck.junit4.matcher.FunctionExpressionMatcher.expressionMatches) PARENT_ONLY(org.mule.runtime.module.artifact.api.classloader.ParentOnlyLookupStrategy.PARENT_ONLY) URL(java.net.URL) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) PARENT_FIRST(org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST) ClassUtils(org.mule.runtime.core.api.util.ClassUtils) URLClassLoader(java.net.URLClassLoader) AbstractMuleTestCase(org.mule.tck.junit4.AbstractMuleTestCase) SmallTest(org.mule.tck.size.SmallTest) TestClassNotFoundException(org.mule.tck.classlaoder.TestClassLoader.TestClassNotFoundException) Method(java.lang.reflect.Method) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) ThrowableMessageMatcher.hasMessage(org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) CompositeClassNotFoundException(org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException) TestClassLoader(org.mule.tck.classlaoder.TestClassLoader) CHILD_FIRST(org.mule.runtime.module.artifact.api.classloader.ChildFirstLookupStrategy.CHILD_FIRST) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Rule(org.junit.Rule) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) Matchers.contains(org.hamcrest.Matchers.contains) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) TestClassNotFoundException(org.mule.tck.classlaoder.TestClassLoader.TestClassNotFoundException) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) URLClassLoader(java.net.URLClassLoader) TestClassLoader(org.mule.tck.classlaoder.TestClassLoader) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) CompositeClassNotFoundException(org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 17 with ClassLoaderLookupPolicy

use of org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy in project mule by mulesoft.

the class FineGrainedControlClassLoaderTestCase method usesParentOnlyLookupAndFails.

@Test
public void usesParentOnlyLookupAndFails() throws Exception {
    ClassLoader parent = mock(ClassLoader.class);
    final ClassNotFoundException thrownException = new ClassNotFoundException("ERROR");
    when(parent.loadClass(TEST_CLASS_NAME)).thenThrow(thrownException);
    final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
    when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(PARENT_ONLY);
    expected.expect(CompositeClassNotFoundException.class);
    expected.expectMessage(startsWith("Cannot load class '" + TEST_CLASS_NAME + "': [" + lineSeparator() + "\t" + "ERROR]"));
    expected.expect(expressionMatches((e) -> ((CompositeClassNotFoundException) e).getExceptions(), contains(sameInstance(thrownException))));
    FineGrainedControlClassLoader ext = new FineGrainedControlClassLoader(new URL[] { getChildFileResource() }, parent, lookupPolicy);
    ext.loadClass(TEST_CLASS_NAME);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) System.lineSeparator(java.lang.System.lineSeparator) FunctionExpressionMatcher.expressionMatches(org.mule.tck.junit4.matcher.FunctionExpressionMatcher.expressionMatches) PARENT_ONLY(org.mule.runtime.module.artifact.api.classloader.ParentOnlyLookupStrategy.PARENT_ONLY) URL(java.net.URL) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) PARENT_FIRST(org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST) ClassUtils(org.mule.runtime.core.api.util.ClassUtils) URLClassLoader(java.net.URLClassLoader) AbstractMuleTestCase(org.mule.tck.junit4.AbstractMuleTestCase) SmallTest(org.mule.tck.size.SmallTest) TestClassNotFoundException(org.mule.tck.classlaoder.TestClassLoader.TestClassNotFoundException) Method(java.lang.reflect.Method) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) ThrowableMessageMatcher.hasMessage(org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) CompositeClassNotFoundException(org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException) TestClassLoader(org.mule.tck.classlaoder.TestClassLoader) CHILD_FIRST(org.mule.runtime.module.artifact.api.classloader.ChildFirstLookupStrategy.CHILD_FIRST) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Rule(org.junit.Rule) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) Matchers.contains(org.hamcrest.Matchers.contains) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) URLClassLoader(java.net.URLClassLoader) TestClassLoader(org.mule.tck.classlaoder.TestClassLoader) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) CompositeClassNotFoundException(org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException) TestClassNotFoundException(org.mule.tck.classlaoder.TestClassLoader.TestClassNotFoundException) CompositeClassNotFoundException(org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 18 with ClassLoaderLookupPolicy

use of org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy in project mule by mulesoft.

the class FineGrainedControlClassLoaderTestCase method usesParentFirstThenChildLookup.

@Test
public void usesParentFirstThenChildLookup() throws Exception {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
    when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(PARENT_FIRST);
    when(lookupPolicy.getClassLookupStrategy(Object.class.getName())).thenReturn(PARENT_FIRST);
    when(lookupPolicy.getClassLookupStrategy(String.class.getName())).thenReturn(PARENT_FIRST);
    FineGrainedControlClassLoader ext = new FineGrainedControlClassLoader(new URL[] { getChildFileResource() }, parent, lookupPolicy);
    assertEquals(EXPECTED_CHILD_MESSAGE, invokeTestClassMethod(ext));
}
Also used : FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) URLClassLoader(java.net.URLClassLoader) TestClassLoader(org.mule.tck.classlaoder.TestClassLoader) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 19 with ClassLoaderLookupPolicy

use of org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy in project mule by mulesoft.

the class FineGrainedControlClassLoaderTestCase method usesChildFirstThenParentLookupAndFails.

@Test
public void usesChildFirstThenParentLookupAndFails() throws Exception {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
    when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(CHILD_FIRST);
    expected.expect(CompositeClassNotFoundException.class);
    expected.expectMessage(startsWith("Cannot load class '" + TEST_CLASS_NAME + "': ["));
    FineGrainedControlClassLoader ext = buildFineGrainedControlClassLoader(parent, lookupPolicy);
    expected.expect(expressionMatches((e) -> ((CompositeClassNotFoundException) e).getExceptions(), contains(expressionMatches((e) -> ((TestClassNotFoundException) e).getClassLoader(), is((ClassLoader) ext)), hasMessage(is(TEST_CLASS_NAME)))));
    invokeTestClassMethod(ext);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) System.lineSeparator(java.lang.System.lineSeparator) FunctionExpressionMatcher.expressionMatches(org.mule.tck.junit4.matcher.FunctionExpressionMatcher.expressionMatches) PARENT_ONLY(org.mule.runtime.module.artifact.api.classloader.ParentOnlyLookupStrategy.PARENT_ONLY) URL(java.net.URL) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) PARENT_FIRST(org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST) ClassUtils(org.mule.runtime.core.api.util.ClassUtils) URLClassLoader(java.net.URLClassLoader) AbstractMuleTestCase(org.mule.tck.junit4.AbstractMuleTestCase) SmallTest(org.mule.tck.size.SmallTest) TestClassNotFoundException(org.mule.tck.classlaoder.TestClassLoader.TestClassNotFoundException) Method(java.lang.reflect.Method) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) ThrowableMessageMatcher.hasMessage(org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) CompositeClassNotFoundException(org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException) TestClassLoader(org.mule.tck.classlaoder.TestClassLoader) CHILD_FIRST(org.mule.runtime.module.artifact.api.classloader.ChildFirstLookupStrategy.CHILD_FIRST) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Rule(org.junit.Rule) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) Matchers.contains(org.hamcrest.Matchers.contains) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) TestClassNotFoundException(org.mule.tck.classlaoder.TestClassLoader.TestClassNotFoundException) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) URLClassLoader(java.net.URLClassLoader) TestClassLoader(org.mule.tck.classlaoder.TestClassLoader) FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) CompositeClassNotFoundException(org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 20 with ClassLoaderLookupPolicy

use of org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy in project mule by mulesoft.

the class ContainerClassLoaderFactoryTestCase method createsClassLoaderLookupPolicy.

@Test
public void createsClassLoaderLookupPolicy() throws Exception {
    final ModuleRepository moduleRepository = mock(ModuleRepository.class);
    final ContainerClassLoaderFactory factory = new ContainerClassLoaderFactory(moduleRepository);
    final List<MuleModule> modules = new ArrayList<>();
    modules.add(new TestModuleBuilder("module1").exportingPackages("org.foo1", "org.foo1.bar").build());
    modules.add(new TestModuleBuilder("module2").exportingPackages("org.foo2").build());
    when(moduleRepository.getModules()).thenReturn(modules);
    final ArtifactClassLoader containerClassLoader = factory.createContainerClassLoader(this.getClass().getClassLoader());
    final ClassLoaderLookupPolicy classLoaderLookupPolicy = containerClassLoader.getClassLoaderLookupPolicy();
    assertThat(classLoaderLookupPolicy.getClassLookupStrategy("org.foo1.Foo"), instanceOf(ContainerOnlyLookupStrategy.class));
    assertThat(classLoaderLookupPolicy.getClassLookupStrategy("org.foo1.bar.Bar"), instanceOf(ContainerOnlyLookupStrategy.class));
    assertThat(classLoaderLookupPolicy.getClassLookupStrategy("org.foo2.Fo"), instanceOf(ContainerOnlyLookupStrategy.class));
    assertThat(classLoaderLookupPolicy.getClassLookupStrategy("org.foo2.bar.Bar"), sameInstance(CHILD_FIRST));
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) ArrayList(java.util.ArrayList) ModuleRepository(org.mule.runtime.container.api.ModuleRepository) MuleModule(org.mule.runtime.container.api.MuleModule) Test(org.junit.Test)

Aggregations

ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)32 Test (org.junit.Test)19 SmallTest (org.mule.tck.size.SmallTest)16 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)12 URLClassLoader (java.net.URLClassLoader)8 FineGrainedControlClassLoader (org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader)8 URL (java.net.URL)6 HashMap (java.util.HashMap)5 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)5 LookupStrategy (org.mule.runtime.module.artifact.api.classloader.LookupStrategy)5 PARENT_FIRST (org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST)5 RegionClassLoader (org.mule.runtime.module.artifact.api.classloader.RegionClassLoader)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 MuleModule (org.mule.runtime.container.api.MuleModule)4 ArtifactClassLoaderFilter (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter)4 ArtifactDescriptor (org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor)4 TestClassLoader (org.mule.tck.classlaoder.TestClassLoader)4 System.lineSeparator (java.lang.System.lineSeparator)3