예제 #1
0
  @Test
  public void testPostDelegation() throws Exception {
    final VFSClassLoader parent =
        new VFSClassLoader(new FileObject[] {vfs.resolveFile(uri1.toString())}, vfs);

    Class<?> pclass = parent.loadClass("test.HelloWorld");

    ContextManager cm =
        new ContextManager(
            vfs,
            new ReloadingClassLoader() {
              @Override
              public ClassLoader getClassLoader() {
                return parent;
              }
            });

    cm.setContextConfig(
        new ContextsConfig() {
          @Override
          public ContextConfig getContextConfig(String context) {
            if (context.equals("CX1")) {
              return new ContextConfig(uri2.toString(), true);
            } else if (context.equals("CX2")) {
              return new ContextConfig(uri2.toString(), false);
            }
            return null;
          }
        });

    Assert.assertTrue(cm.getClassLoader("CX1").loadClass("test.HelloWorld") == pclass);
    Assert.assertFalse(cm.getClassLoader("CX2").loadClass("test.HelloWorld") == pclass);
  }
예제 #2
0
  @Test
  public void differentContexts() throws Exception {

    ContextManager cm =
        new ContextManager(
            vfs,
            new ReloadingClassLoader() {
              @Override
              public ClassLoader getClassLoader() {
                return ClassLoader.getSystemClassLoader();
              }
            });

    cm.setContextConfig(
        new ContextsConfig() {
          @Override
          public ContextConfig getContextConfig(String context) {
            if (context.equals("CX1")) {
              return new ContextConfig(uri1.toString(), true);
            } else if (context.equals("CX2")) {
              return new ContextConfig(uri2.toString(), true);
            }
            return null;
          }
        });

    FileObject testDir = vfs.resolveFile(folder1.getRoot().toURI().toString());
    FileObject[] dirContents = testDir.getChildren();
    ClassLoader cl1 = cm.getClassLoader("CX1");
    FileObject[] files = ((VFSClassLoader) cl1).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents), files);

    FileObject testDir2 = vfs.resolveFile(folder2.getRoot().toURI().toString());
    FileObject[] dirContents2 = testDir2.getChildren();
    ClassLoader cl2 = cm.getClassLoader("CX2");
    FileObject[] files2 = ((VFSClassLoader) cl2).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents2), files2);

    Class<?> defaultContextClass = cl1.loadClass("test.HelloWorld");
    Object o1 = defaultContextClass.newInstance();
    Assert.assertEquals("Hello World!", o1.toString());

    Class<?> myContextClass = cl2.loadClass("test.HelloWorld");
    Object o2 = myContextClass.newInstance();
    Assert.assertEquals("Hello World!", o2.toString());

    Assert.assertFalse(defaultContextClass.equals(myContextClass));

    cm.removeUnusedContexts(new HashSet<String>());
  }