1010
1111import java .io .File ;
1212import java .io .IOException ;
13+ import java .util .ArrayList ;
1314import java .util .HashMap ;
15+ import java .util .List ;
1416import java .util .Map ;
17+ import java .util .stream .Collectors ;
1518
1619import org .gradle .api .Project ;
1720import org .gradle .api .Task ;
1821import org .gradle .api .file .FileTree ;
1922import org .gradle .api .tasks .SourceTask ;
20- import org .gradle .api .tasks .TaskCollection ;
2123import org .gradle .api .tasks .compile .JavaCompile ;
2224import org .gradle .testfixtures .ProjectBuilder ;
2325import org .junit .Before ;
2426import org .junit .Rule ;
2527import org .junit .Test ;
2628import org .junit .rules .TemporaryFolder ;
2729
28- import org .javacc .plugin .gradle .javacc .CompileJavaccTask ;
29-
3030public class JavaccCompilerInputOutputConfigurationTest {
3131
3232 @ Rule
@@ -74,14 +74,19 @@ public void getJavaSourceTreeReturnsJavaCompileSourceWhenSingleJavaCompileTask()
7474 CompilerInputOutputConfiguration configuration = builder
7575 .withInputDirectory (inputDirectory )
7676 .withOutputDirectory (outputDirectory )
77- .withJavaCompileTasks ( project . getTasks (). withType ( JavaCompile . class ))
77+ .withJavaSources ( getJavaSources ( project ))
7878 .build ();
7979
8080 FileTree javaSourceTree = configuration .getJavaSourceTree ();
8181
8282 assertThat (javaSourceTree , contains (javaFile .getCanonicalFile ()));
8383 }
8484
85+ private List <FileTree > getJavaSources (Project project ) {
86+ return project .getTasks ().withType (JavaCompile .class )
87+ .stream ().map (JavaCompile ::getSource ).collect (Collectors .toList ());
88+ }
89+
8590 private File addTaskWithSourceFile (Project project , String taskName , String sourceFileName , Class <? extends SourceTask > taskType ) throws IOException {
8691 Map <String , Object > options = new HashMap <>();
8792 options .put (Task .TASK_TYPE , taskType );
@@ -102,7 +107,7 @@ public void getJavaSourceTreeReturnsAggregatedJavaCompileSourceWhenMultipleJavaC
102107 CompilerInputOutputConfiguration configuration = builder
103108 .withInputDirectory (inputDirectory )
104109 .withOutputDirectory (outputDirectory )
105- .withJavaCompileTasks ( project . getTasks (). withType ( JavaCompile . class ))
110+ .withJavaSources ( getJavaSources ( project ))
106111 .build ();
107112
108113 FileTree javaSourceTree = configuration .getJavaSourceTree ();
@@ -118,7 +123,7 @@ public void getJavaSourceTreeExcludesOutputFolder() throws IOException {
118123 CompilerInputOutputConfiguration configuration = builder
119124 .withInputDirectory (inputDirectory )
120125 .withOutputDirectory (outputDirectory )
121- .withJavaCompileTasks ( project . getTasks (). withType ( JavaCompile . class ))
126+ .withJavaSources ( getJavaSources ( project ))
122127 .build ();
123128
124129 FileTree javaSourceTree = configuration .getJavaSourceTree ();
@@ -127,61 +132,11 @@ public void getJavaSourceTreeExcludesOutputFolder() throws IOException {
127132 assertThat (javaSourceTree , not (contains (outputFile .getCanonicalFile ())));
128133 }
129134
130- @ Test
131- public void getCompleteSourceTreeReturnsSourceWhenNoJavaCompileTask () throws IOException {
132- Project project = ProjectBuilder .builder ().withProjectDir (testFolder .getRoot ()).build ();
133- File javaccFile = addTaskWithSourceFile (project , "compileJavacc" , "input/TestClass.jj" , CompileJavaccTask .class );
134- CompilerInputOutputConfiguration configuration = builder
135- .withInputDirectory (inputDirectory )
136- .withOutputDirectory (outputDirectory )
137- .withSource (((SourceTask ) project .getTasks ().getByName ("compileJavacc" )).getSource ())
138- .build ();
139-
140- FileTree completeSourceTree = configuration .getCompleteSourceTree ();
141-
142- assertThat (completeSourceTree , contains (javaccFile .getCanonicalFile ()));
143- }
144-
145- @ Test
146- public void getCompleteSourceTreeReturnsSourceAndJavaSourceWhenProjectHasJavaCompileTasks () throws IOException {
147- Project project = ProjectBuilder .builder ().withProjectDir (testFolder .getRoot ()).build ();
148- File javaccFile = addTaskWithSourceFile (project , "compileJavacc" , "input/TestClass.jj" , CompileJavaccTask .class );
149- testFolder .newFolder ("inputJava" );
150- File javaFile = addTaskWithSourceFile (project , "compileJava" , "inputJava/MyClass.java" , JavaCompile .class );
151- CompilerInputOutputConfiguration configuration = builder
152- .withInputDirectory (inputDirectory )
153- .withOutputDirectory (outputDirectory )
154- .withSource (((SourceTask ) project .getTasks ().getByName ("compileJavacc" )).getSource ())
155- .withJavaCompileTasks (project .getTasks ().withType (JavaCompile .class ))
156- .build ();
157-
158- FileTree completeSourceTree = configuration .getCompleteSourceTree ();
159-
160- assertThat (completeSourceTree , containsInAnyOrder (javaccFile .getCanonicalFile (), javaFile .getCanonicalFile ()));
161- }
162-
163- @ Test
164- public void getCompleteSourceTreeExcludesOutputFolder () throws IOException {
165- Project project = ProjectBuilder .builder ().withProjectDir (testFolder .getRoot ()).build ();
166- File javaccFile = addTaskWithSourceFile (project , "compileJavacc" , "input/TestClass.jj" , CompileJavaccTask .class );
167- File outputFile = addTaskWithSourceFile (project , "compileJavaccGenerated" , "output/Generated.java" , JavaCompile .class );
168- CompilerInputOutputConfiguration configuration = builder
169- .withInputDirectory (inputDirectory )
170- .withOutputDirectory (outputDirectory )
171- .withSource (((SourceTask ) project .getTasks ().getByName ("compileJavacc" )).getSource ())
172- .withJavaCompileTasks (project .getTasks ().withType (JavaCompile .class ))
173- .build ();
174-
175- FileTree completeSourceTree = configuration .getCompleteSourceTree ();
176-
177- assertThat (completeSourceTree , contains (javaccFile .getCanonicalFile ()));
178- assertThat (completeSourceTree , not (contains (outputFile .getCanonicalFile ())));
179- }
180135
181136 private static class JavaccConfigurationBuilder {
182137 private File inputDirectory ;
183138 private File outputDirectory ;
184- private TaskCollection < JavaCompile > javaCompileTasks ;
139+ private List < FileTree > javaSourceTrees = new ArrayList <>() ;
185140 private FileTree source ;
186141
187142 private JavaccConfigurationBuilder () {
@@ -203,20 +158,14 @@ public JavaccConfigurationBuilder withOutputDirectory(File outputDirectory) {
203158 return this ;
204159 }
205160
206- public JavaccConfigurationBuilder withJavaCompileTasks (TaskCollection <JavaCompile > javaCompileTasks ) {
207- this .javaCompileTasks = javaCompileTasks ;
208-
209- return this ;
210- }
211-
212- public JavaccConfigurationBuilder withSource (FileTree source ) {
213- this .source = source ;
161+ public JavaccConfigurationBuilder withJavaSources (List <FileTree > javaSourceTrees ) {
162+ this .javaSourceTrees .addAll (javaSourceTrees );
214163
215164 return this ;
216165 }
217166
218167 public JavaccCompilerInputOutputConfiguration build () {
219- return new JavaccCompilerInputOutputConfiguration (inputDirectory , outputDirectory , source , javaCompileTasks );
168+ return new JavaccCompilerInputOutputConfiguration (inputDirectory , outputDirectory , source , javaSourceTrees );
220169 }
221170 }
222171}
0 commit comments