Search in sources :

Example 6 with JavaFileParser

use of com.facebook.buck.jvm.java.JavaFileParser in project buck by facebook.

the class JavaSymbolsRuleTest method ensureJsonFilesGetWritten.

@Test
public void ensureJsonFilesGetWritten() throws IOException, InterruptedException {
    TestDataHelper.createProjectWorkspaceForScenario(this, "java_library_symbols_finder", tmp).setUp();
    ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmp.getRoot());
    ImmutableSortedSet<SourcePath> srcs = ImmutableSortedSet.<SourcePath>naturalOrder().addAll(Stream.of("Example1.java", "Example2.java").map(Paths::get).map(p -> new PathSourcePath(projectFilesystem, p)).iterator()).add(new DefaultBuildTargetSourcePath(BuildTargetFactory.newInstance("//foo:bar"))).build();
    JavaFileParser javaFileParser = JavaFileParser.createJavaFileParser(JavacOptions.builder().setSourceLevel("7").setTargetLevel("7").build());
    JavaLibrarySymbolsFinder symbolsFinder = new JavaLibrarySymbolsFinder(srcs, javaFileParser, /* shouldRecordRequiredSymbols */
    true);
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//:examples");
    JavaSymbolsRule javaSymbolsRule = new JavaSymbolsRule(buildTarget, symbolsFinder, /* generatedSymbols */
    ImmutableSortedSet.of("com.example.generated.Example"), ObjectMappers.newDefaultInstance(), projectFilesystem);
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    resolver.addToIndex(javaSymbolsRule);
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    List<Step> buildSteps = javaSymbolsRule.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), /* buildableContext */
    null);
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    ObjectMapper objectMapper = ObjectMappers.newDefaultInstance();
    for (Step step : buildSteps) {
        step.execute(executionContext);
    }
    JsonNode jsonNode = objectMapper.readTree(projectFilesystem.resolve(BuildTargets.getGenPath(javaSymbolsRule.getProjectFilesystem(), buildTarget.withFlavors(JavaSymbolsRule.JAVA_SYMBOLS), "__%s__.json")).toFile());
    assertTrue(jsonNode instanceof ObjectNode);
    assertEquals(ImmutableSet.of("com.example.Example1", "com.example.Example2", "com.example.generated.Example"), StreamSupport.stream(jsonNode.get("provided").spliterator(), false).map(JsonNode::textValue).collect(MoreCollectors.toImmutableSet()));
    assertEquals(ImmutableSet.of("com.example.other.Bar", "com.example.other.Foo"), StreamSupport.stream(jsonNode.get("required").spliterator(), false).map(JsonNode::textValue).collect(MoreCollectors.toImmutableSet()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PathSourcePath(com.facebook.buck.rules.PathSourcePath) JsonNode(com.fasterxml.jackson.databind.JsonNode) JavaFileParser(com.facebook.buck.jvm.java.JavaFileParser) Step(com.facebook.buck.step.Step) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) SourcePath(com.facebook.buck.rules.SourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BuildTarget(com.facebook.buck.model.BuildTarget) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JavaFileParser (com.facebook.buck.jvm.java.JavaFileParser)6 PathSourcePath (com.facebook.buck.rules.PathSourcePath)4 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)3 BuildTarget (com.facebook.buck.model.BuildTarget)3 SourcePath (com.facebook.buck.rules.SourcePath)3 JavacOptions (com.facebook.buck.jvm.java.JavacOptions)2 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)2 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)2 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)2 IOException (java.io.IOException)2 MutableDirectedGraph (com.facebook.buck.graph.MutableDirectedGraph)1 JavaBuckConfig (com.facebook.buck.jvm.java.JavaBuckConfig)1 JavaLibraryDescription (com.facebook.buck.jvm.java.JavaLibraryDescription)1 JavaDepsFinder (com.facebook.buck.jvm.java.autodeps.JavaDepsFinder)1 BuildTargetFactory (com.facebook.buck.model.BuildTargetFactory)1 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)1 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)1 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)1 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)1 RuleKey (com.facebook.buck.rules.RuleKey)1