/** * There was a bug where `BuildTargetSourcePath` sources were written to the classes file using * their string representation, rather than their resolved path. */ @Test public void shouldWriteResolvedBuildTargetSourcePathsToClassesFile() throws IOException, InterruptedException { BuildRuleResolver resolver = new BuildRuleResolver(); SourcePathResolver pathResolver = new SourcePathResolver(resolver); BuildRule rule = new FakeBuildRule("//:fake", pathResolver); resolver.addToIndex(rule); Jsr199Javac javac = createJavac(/* withSyntaxError */ false); ExecutionContext executionContext = TestExecutionContext.newInstance(); int exitCode = javac.buildWithClasspath( executionContext, createProjectFilesystem(), PATH_RESOLVER, BuildTargetFactory.newInstance("//some:example"), ImmutableList.<String>of(), SOURCE_PATHS, Optional.of(pathToSrcsList), Optional.<Path>absent()); assertEquals("javac should exit with code 0.", exitCode, 0); File srcsListFile = pathToSrcsList.toFile(); assertTrue(srcsListFile.exists()); assertTrue(srcsListFile.isFile()); assertEquals("Example.java", Files.toString(srcsListFile, Charsets.UTF_8).trim()); }
@Test public void shouldUseSpecifiedJavacJar() throws Exception { BuildRuleResolver resolver = new BuildRuleResolver(); SourcePathResolver pathResolver = new SourcePathResolver(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.<String, Class<?>>of( "com.sun.tools.javac.api.JavacTool", MockJavac.class)); executionContext .getClassLoaderCache() .injectClassLoader( ClassLoader.getSystemClassLoader(), ImmutableList.of(fakeJavacJar.toUri().toURL()), mockClassLoader); Jsr199Javac javac = createJavac(/* withSyntaxError */ false, Optional.of(fakeJavacJar)); boolean caught = false; try { javac.buildWithClasspath( executionContext, createProjectFilesystem(), PATH_RESOLVER, BuildTargetFactory.newInstance("//some:example"), ImmutableList.<String>of(), SOURCE_PATHS, Optional.of(pathToSrcsList), Optional.<Path>absent()); fail("Did not expect compilation to succeed"); } catch (UnsupportedOperationException ex) { if (ex.toString().contains("abcdef")) { caught = true; } } assertTrue("mock Java compiler should throw", caught); }