Search in sources :

Example 1 with InterceptingUrlClassLoader

use of org.apache.beam.sdk.testing.InterceptingUrlClassLoader in project beam by apache.

the class AvroCoderTest method testTwoClassLoaders.

/**
 * Tests that {@link AvroCoder} works around issues in Avro where cache classes might be from the
 * wrong ClassLoader, causing confusing "Cannot cast X to X" error messages.
 */
@SuppressWarnings("ReturnValueIgnored")
@Test
public void testTwoClassLoaders() throws Exception {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader loader1 = new InterceptingUrlClassLoader(contextClassLoader, AvroCoderTestPojo.class.getName());
    ClassLoader loader2 = new InterceptingUrlClassLoader(contextClassLoader, AvroCoderTestPojo.class.getName());
    Class<?> pojoClass1 = loader1.loadClass(AvroCoderTestPojo.class.getName());
    Class<?> pojoClass2 = loader2.loadClass(AvroCoderTestPojo.class.getName());
    Object pojo1 = InstanceBuilder.ofType(pojoClass1).withArg(String.class, "hello").build();
    Object pojo2 = InstanceBuilder.ofType(pojoClass2).withArg(String.class, "goodbye").build();
    // Confirm incompatibility
    try {
        pojoClass2.cast(pojo1);
        fail("Expected ClassCastException; without it, this test is vacuous");
    } catch (ClassCastException e) {
    // g2g
    }
    // The first coder is expected to populate the Avro SpecificData cache
    // The second coder is expected to be corrupted if the caching is done wrong.
    AvroCoder<Object> avroCoder1 = (AvroCoder) AvroCoder.of(pojoClass1);
    AvroCoder<Object> avroCoder2 = (AvroCoder) AvroCoder.of(pojoClass2);
    Object cloned1 = CoderUtils.clone(avroCoder1, pojo1);
    Object cloned2 = CoderUtils.clone(avroCoder2, pojo2);
    // Confirming that the uncorrupted coder is fine
    pojoClass1.cast(cloned1);
    // Confirmed to fail prior to the fix
    pojoClass2.cast(cloned2);
}
Also used : InterceptingUrlClassLoader(org.apache.beam.sdk.testing.InterceptingUrlClassLoader) InterceptingUrlClassLoader(org.apache.beam.sdk.testing.InterceptingUrlClassLoader) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with InterceptingUrlClassLoader

use of org.apache.beam.sdk.testing.InterceptingUrlClassLoader in project beam by apache.

the class PipelineOptionsFactoryTest method testPipelineOptionsFactoryUsesTccl.

@Test
public void testPipelineOptionsFactoryUsesTccl() throws Exception {
    final Thread thread = Thread.currentThread();
    final ClassLoader testClassLoader = thread.getContextClassLoader();
    final ClassLoader caseLoader = new InterceptingUrlClassLoader(testClassLoader, name -> name.toLowerCase(ROOT).contains("test"));
    thread.setContextClassLoader(caseLoader);
    PipelineOptionsFactory.resetCache();
    try {
        final PipelineOptions pipelineOptions = PipelineOptionsFactory.create();
        final Class optionType = caseLoader.loadClass("org.apache.beam.sdk.options.PipelineOptionsFactoryTest$ClassLoaderTestOptions");
        final Object options = pipelineOptions.as(optionType);
        assertSame(caseLoader, options.getClass().getClassLoader());
        assertSame(optionType.getClassLoader(), options.getClass().getClassLoader());
        assertSame(testClassLoader, optionType.getInterfaces()[0].getClassLoader());
        assertTrue(Boolean.class.cast(optionType.getMethod("isOption").invoke(options)));
    } finally {
        thread.setContextClassLoader(testClassLoader);
        PipelineOptionsFactory.resetCache();
    }
}
Also used : InterceptingUrlClassLoader(org.apache.beam.sdk.testing.InterceptingUrlClassLoader) InterceptingUrlClassLoader(org.apache.beam.sdk.testing.InterceptingUrlClassLoader) Test(org.junit.Test)

Example 3 with InterceptingUrlClassLoader

use of org.apache.beam.sdk.testing.InterceptingUrlClassLoader in project beam by apache.

the class SerializableUtilsTest method customClassLoader.

@Test
public void customClassLoader() throws Exception {
    // define a classloader with test-classes in it
    final ClassLoader testLoader = Thread.currentThread().getContextClassLoader();
    final ClassLoader loader = new InterceptingUrlClassLoader(testLoader, Foo.class.getName());
    final Class<?> source = loader.loadClass(Foo.class.getName());
    assertNotSame(source.getClassLoader(), Foo.class.getClassLoader());
    // validate if the caller set the classloader that it works well
    final Serializable customLoaderSourceInstance = Serializable.class.cast(source.getConstructor().newInstance());
    final Thread thread = Thread.currentThread();
    thread.setContextClassLoader(loader);
    try {
        assertSerializationClassLoader(loader, customLoaderSourceInstance);
    } finally {
        thread.setContextClassLoader(testLoader);
    }
    // now let beam be a little be more fancy and try to ensure it by itself from the incoming value
    assertSerializationClassLoader(loader, customLoaderSourceInstance);
}
Also used : Serializable(java.io.Serializable) InterceptingUrlClassLoader(org.apache.beam.sdk.testing.InterceptingUrlClassLoader) InterceptingUrlClassLoader(org.apache.beam.sdk.testing.InterceptingUrlClassLoader) Test(org.junit.Test)

Aggregations

InterceptingUrlClassLoader (org.apache.beam.sdk.testing.InterceptingUrlClassLoader)3 Test (org.junit.Test)3 Serializable (java.io.Serializable)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1