Search in sources :

Example 1 with BooleanInvoker

use of com.google.template.soy.jbcsrc.restricted.testing.ExpressionTester.BooleanInvoker in project closure-templates by google.

the class MemoryClassLoaderTest method testAsResource.

@Test
public void testAsResource() throws IOException {
    BooleanInvoker invoker = ExpressionTester.createInvoker(BooleanInvoker.class, FALSE);
    byte[] classBytes = ByteStreams.toByteArray(invoker.getClass().getClassLoader().getResourceAsStream(invoker.getClass().getName().replace('.', '/') + ".class"));
    ClassNode node = new ClassNode();
    new ClassReader(classBytes).accept(node, 0);
    assertThat(node.name).isEqualTo(Type.getInternalName(invoker.getClass()));
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) BooleanInvoker(com.google.template.soy.jbcsrc.restricted.testing.ExpressionTester.BooleanInvoker) ClassReader(org.objectweb.asm.ClassReader) Test(org.junit.Test)

Example 2 with BooleanInvoker

use of com.google.template.soy.jbcsrc.restricted.testing.ExpressionTester.BooleanInvoker in project closure-templates by google.

the class MemoryClassLoaderTest method testDelegation.

@Test
public void testDelegation() throws Exception {
    // We want to make sure that for generated types, our classloader doesn't delegate to its parent
    // loader
    ClassData parentClass = ExpressionTester.createClass(BooleanInvoker.class, FALSE);
    ClassData childClass = ExpressionTester.createClass(BooleanInvoker.class, TRUE);
    // sanity check,  they have the same fully qualified class name
    assertThat(parentClass.type()).isEqualTo(childClass.type());
    TypeInfo type = childClass.type();
    MemoryClassLoader parentLoader = new MemoryClassLoader(ImmutableList.of(parentClass));
    MemoryClassLoader childLoader = new MemoryClassLoader(parentLoader, ImmutableList.of(childClass));
    BooleanInvoker fromParent = parentLoader.loadClass(type.className()).asSubclass(BooleanInvoker.class).getConstructor().newInstance();
    assertThat(fromParent.invoke()).isFalse();
    BooleanInvoker fromChild = childLoader.loadClass(type.className()).asSubclass(BooleanInvoker.class).getConstructor().newInstance();
    assertThat(fromChild.invoke()).isTrue();
    assertThat(fromChild.getClass()).isNotEqualTo(fromParent.getClass());
}
Also used : BooleanInvoker(com.google.template.soy.jbcsrc.restricted.testing.ExpressionTester.BooleanInvoker) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo) Test(org.junit.Test)

Example 3 with BooleanInvoker

use of com.google.template.soy.jbcsrc.restricted.testing.ExpressionTester.BooleanInvoker in project closure-templates by google.

the class MemoryClassLoaderTest method testCollectable.

// Our memory classloaders should be garbage collectable when all references to their types
// disapear
@Test
public void testCollectable() {
    BooleanInvoker invoker = ExpressionTester.createInvoker(BooleanInvoker.class, FALSE);
    // sanity, the invoker works
    assertThat(invoker.invoke()).isFalse();
    MemoryClassLoader loader = (MemoryClassLoader) invoker.getClass().getClassLoader();
    WeakReference<MemoryClassLoader> loaderRef = new WeakReference<MemoryClassLoader>(loader);
    // unpin
    invoker = null;
    loader = null;
    GcFinalization.awaitClear(loaderRef);
}
Also used : BooleanInvoker(com.google.template.soy.jbcsrc.restricted.testing.ExpressionTester.BooleanInvoker) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Aggregations

BooleanInvoker (com.google.template.soy.jbcsrc.restricted.testing.ExpressionTester.BooleanInvoker)3 Test (org.junit.Test)3 TypeInfo (com.google.template.soy.jbcsrc.restricted.TypeInfo)1 WeakReference (java.lang.ref.WeakReference)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassNode (org.objectweb.asm.tree.ClassNode)1