コード例 #1
0
  @Test
  public void testWithParentClassPathPropagatesWithNoParentContainer() throws IOException {
    File testCompJar = TestHelper.getTestCompJarFile();

    URLClassLoader classLoader =
        new URLClassLoader(new URL[] {testCompJar.toURL()}, this.getClass().getClassLoader());
    Class<?> testComp = null;

    try {
      testComp = classLoader.loadClass("TestComp");
    } catch (ClassNotFoundException ex) {
      fail("Unable to load test component from the jar using a url classloader");
    }
    Reader script =
        new StringReader(
            "container(:parent => $parent) {\n" + "  component(:class => \"TestComp\")\n" + "}");

    PicoContainer pico = buildContainer(new JRubyContainerBuilder(script, classLoader), null, null);
    assertNotNull(pico);
    Object testCompInstance = pico.getComponent(testComp.getName());
    assertSame(testCompInstance.getClass(), testComp);
  }
コード例 #2
0
  /**
   * Script will fail with not finding class TestComp if the classloader propagation is not working.
   *
   * @throws MalformedURLException
   */
  @Test
  public void testWithParentClassPathPropagatesWithToInterpreter() throws MalformedURLException {
    Reader script =
        new StringReader(
            ""
                + "container(:parent => $parent) {\n"
                + "       component(:class => \"TestComp\")\n"
                + "}\n");

    File testCompJar = TestHelper.getTestCompJarFile();
    assertTrue(
        "Cannot find TestComp.jar. "
            + testCompJar.getAbsolutePath()
            + " Please set testcomp.jar system property before running.",
        testCompJar.exists());
    // System.err.println("--> " + testCompJar.getAbsolutePath());
    URLClassLoader classLoader =
        new URLClassLoader(
            new URL[] {testCompJar.toURI().toURL()}, this.getClass().getClassLoader());
    Class<?> testComp = null;
    PicoContainer parent = new DefaultPicoContainer();

    try {
      testComp = classLoader.loadClass("TestComp");
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
      fail("Unable to load test component from the jar using a url classloader");
    }

    PicoContainer pico =
        buildContainer(new JRubyContainerBuilder(script, classLoader), parent, "SOME_SCOPE");
    assertNotNull(pico);
    Object testCompInstance = pico.getComponent("TestComp");
    assertNotNull(testCompInstance);
    assertEquals(testComp.getName(), testCompInstance.getClass().getName());
  }