Ejemplo n.º 1
0
  @Test
  public void can_list_all_classes_reachable_in_a_classloader() throws Exception {
    ClassLoader classLoader =
        ClassLoaders.inMemoryClassLoader()
            .withParent(jdkClassLoader())
            .withClassDefinition("a.A", SimpleClassGenerator.makeMarkerInterface("a.A"))
            .withClassDefinition("a.b.B", SimpleClassGenerator.makeMarkerInterface("a.b.B"))
            .withClassDefinition("c.C", SimpleClassGenerator.makeMarkerInterface("c.C"))
            //                .withCodeSourceUrlOf(ClassLoaders.class)
            .build();

    assertThat(ClassLoaders.in(classLoader).listOwnedClasses()).containsOnly("a.A", "a.b.B", "c.C");
    assertThat(ClassLoaders.in(classLoader).omit("b", "c").listOwnedClasses()).containsOnly("a.A");
  }
Ejemplo n.º 2
0
  @Test
  public void can_load_a_class_in_memory_from_bytes() throws Exception {
    // given
    ClassLoader cl =
        ClassLoaders.inMemoryClassLoader()
            .withClassDefinition("yop.Dude", SimpleClassGenerator.makeMarkerInterface("yop.Dude"))
            .build();

    // when
    Class<?> aClass = cl.loadClass("yop.Dude");

    // then
    assertThat(aClass).isNotNull();
    assertThat(aClass.getClassLoader()).isEqualTo(cl);
    assertThat(aClass.getName()).isEqualTo("yop.Dude");
  }
Ejemplo n.º 3
0
  @Test
  public void can_not_load_a_class_not_previously_registered_in_builder() throws Exception {
    // given
    ClassLoader cl =
        ClassLoaders.inMemoryClassLoader()
            .withClassDefinition("yop.Dude", SimpleClassGenerator.makeMarkerInterface("yop.Dude"))
            .build();

    // when
    try {
      cl.loadClass("not.Defined");
      fail();
    } catch (ClassNotFoundException e) {
      // then
      assertThat(e.getMessage()).contains("not.Defined");
    }
  }