Search in sources :

Example 1 with MockClassLoader

use of com.facebook.buck.util.MockClassLoader in project buck by facebook.

the class Jsr199JavacIntegrationTest method shouldUseSpecifiedJavacJar.

@Test
public void shouldUseSpecifiedJavacJar() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    BuildRule rule = new FakeBuildRule("//:fake", pathResolver);
    resolver.addToIndex(rule);
    Path fakeJavacJar = Paths.get("ae036e57-77a7-4356-a79c-0f85b1a3290d", "fakeJavac.jar");
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    MockClassLoader mockClassLoader = new MockClassLoader(ClassLoader.getSystemClassLoader(), ImmutableMap.of(JavacOptions.COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL, MockJavac.class));
    executionContext.getClassLoaderCache().injectClassLoader(ClassLoader.getSystemClassLoader(), ImmutableList.of(fakeJavacJar.toUri().toURL()), mockClassLoader);
    Jsr199Javac javac = createJavac(/* withSyntaxError */
    false, Optional.of(fakeJavacJar));
    JavacExecutionContext javacExecutionContext = JavacExecutionContext.of(new JavacEventSinkToBuckEventBusBridge(executionContext.getBuckEventBus()), executionContext.getStdErr(), executionContext.getClassLoaderCache(), executionContext.getObjectMapper(), executionContext.getVerbosity(), executionContext.getCellPathResolver(), executionContext.getJavaPackageFinder(), createProjectFilesystem(), NoOpClassUsageFileWriter.instance(), executionContext.getEnvironment(), executionContext.getProcessExecutor(), ImmutableList.of(fakeJavacJar), Optional.empty());
    boolean caught = false;
    try {
        javac.buildWithClasspath(javacExecutionContext, BuildTargetFactory.newInstance("//some:example"), ImmutableList.of(), ImmutableList.of(), SOURCE_PATHS, pathToSrcsList, Optional.empty(), JavacOptions.AbiGenerationMode.CLASS);
        fail("Did not expect compilation to succeed");
    } catch (UnsupportedOperationException ex) {
        if (ex.toString().contains("abcdef")) {
            caught = true;
        }
    }
    assertTrue("mock Java compiler should throw", caught);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) MockClassLoader(com.facebook.buck.util.MockClassLoader) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Aggregations

BuildRule (com.facebook.buck.rules.BuildRule)1 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)1 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)1 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)1 PathSourcePath (com.facebook.buck.rules.PathSourcePath)1 SourcePath (com.facebook.buck.rules.SourcePath)1 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)1 ExecutionContext (com.facebook.buck.step.ExecutionContext)1 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)1 MockClassLoader (com.facebook.buck.util.MockClassLoader)1 Path (java.nio.file.Path)1 Test (org.junit.Test)1