You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BootstrapClassLoaderTest.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Sonar Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import static org.fest.assertions.Assertions.assertThat;
  25. public class BootstrapClassLoaderTest {
  26. @Rule
  27. public ExpectedException thrown = ExpectedException.none();
  28. @Test
  29. public void should_restrict_loading_from_parent() throws Exception {
  30. BootstrapClassLoader classLoader = new BootstrapClassLoader(getClass().getClassLoader(), "org.apache.ant");
  31. assertThat(classLoader.canLoadFromParent("org.sonar.runner.internal.batch.Launcher")).isFalse();
  32. assertThat(classLoader.canLoadFromParent("org.sonar.runner.Runner")).isTrue();
  33. assertThat(classLoader.canLoadFromParent("org.objectweb.asm.ClassVisitor")).isFalse();
  34. assertThat(classLoader.canLoadFromParent("org.apache.ant.project.Project")).isTrue();
  35. }
  36. @Test
  37. public void should_use_isolated_system_classloader_when_parent_is_excluded() throws ClassNotFoundException {
  38. thrown.expect(ClassNotFoundException.class);
  39. thrown.expectMessage("org.junit.Test");
  40. ClassLoader parent = getClass().getClassLoader();
  41. BootstrapClassLoader classLoader = new BootstrapClassLoader(parent);
  42. // JUnit is available in the parent classloader (classpath used to execute this test) but not in the core JVM
  43. assertThat(classLoader.loadClass("java.lang.String", false)).isNotNull();
  44. classLoader.loadClass("org.junit.Test", false);
  45. }
  46. @Test
  47. public void should_find_in_parent_when_matches_unmasked_packages() throws ClassNotFoundException {
  48. ClassLoader parent = getClass().getClassLoader();
  49. BootstrapClassLoader classLoader = new BootstrapClassLoader(parent, "org.junit");
  50. // JUnit is available in the parent classloader (classpath used to execute this test) but not in the core JVM
  51. assertThat(classLoader.loadClass("org.junit.Test", false)).isNotNull();
  52. }
  53. }