2121import java .util .Arrays ;
2222import java .util .Collections ;
2323import java .util .List ;
24- import java .util .Optional ;
2524
2625import org .codehaus .groovy .eclipse .core .model .GroovyRuntime ;
2726import org .codehaus .groovy .runtime .StringGroovyMethods ;
3029import org .eclipse .core .resources .IFile ;
3130import org .eclipse .core .resources .IProject ;
3231import org .eclipse .core .resources .IProjectDescription ;
33- import org .eclipse .core .resources .ProjectScope ;
3432import org .eclipse .core .resources .ResourcesPlugin ;
3533import org .eclipse .core .runtime .CoreException ;
3634import org .eclipse .core .runtime .FileLocator ;
4947import org .eclipse .jdt .groovy .core .util .ReflectionUtils ;
5048import org .eclipse .jdt .internal .compiler .Compiler ;
5149import org .eclipse .jdt .internal .core .JavaModelManager ;
52- import org .eclipse .jdt .internal .core .util .Util ;
5350import org .eclipse .jdt .launching .JavaRuntime ;
5451import org .junit .After ;
5552import org .junit .Assert ;
@@ -131,7 +128,7 @@ protected final void fullBuild() {
131128 }
132129 }
133130
134- protected final void fullBuild (IPath projectPath ) {
131+ protected final void fullBuild (final IPath projectPath ) {
135132 debugRequestor .clearResult ();
136133 debugRequestor .activate ();
137134 try {
@@ -151,7 +148,7 @@ protected final void incrementalBuild() {
151148 }
152149 }
153150
154- protected final void incrementalBuild (IPath projectPath ) {
151+ protected final void incrementalBuild (final IPath projectPath ) {
155152 debugRequestor .clearResult ();
156153 debugRequestor .activate ();
157154 try {
@@ -161,8 +158,7 @@ protected final void incrementalBuild(IPath projectPath) {
161158 }
162159 }
163160
164- @ SuppressWarnings ("cast" )
165- protected void executeClass (IPath projectPath , String className , String expectingOutput , String expectedError ) {
161+ protected void executeClass (final IPath projectPath , final String className , final String expectingOutput , final String expectedError ) {
166162 List <String > classpath = new ArrayList <>();
167163 IPath workspacePath = env .getWorkspaceRootPath ();
168164 classpath .add (workspacePath .append (env .getOutputLocation (projectPath )).toOSString ());
@@ -209,34 +205,18 @@ protected void executeClass(IPath projectPath, String className, String expectin
209205
210206 //--------------------------------------------------------------------------
211207
212- protected final void expectingCompiledClasses (String ... expected ) {
208+ protected final void expectingCompiledClasses (final String ... expected ) {
213209 String [] actual = ReflectionUtils .executePrivateMethod (debugRequestor .getClass (), "getCompiledClasses" , debugRequestor );
214- Util .sort (actual );
215- Util .sort (expected );
216- expectingCompiling ( actual , expected , "unexpected recompiled units. lenExpected=" + expected . length + " lenActual=" + actual . length );
210+ Arrays .sort (actual );
211+ Arrays .sort (expected );
212+ Assert . assertArrayEquals ( expected , actual );
217213 }
218214
219- private void expectingCompiling (String [] actual , String [] expected , String message ) {
220- StringBuilder actualBuffer = new StringBuilder ("{" );
221- for (int i = 0 ; i < actual .length ; i += 1 ) {
222- if (i > 0 ) actualBuffer .append ("," );
223- actualBuffer .append (actual [i ]);
224- }
225- actualBuffer .append ("}" );
226- StringBuilder expectedBuffer = new StringBuilder ("{" );
227- for (int i = 0 ; i < expected .length ; i += 1 ) {
228- if (i > 0 ) expectedBuffer .append ("," );
229- expectedBuffer .append (expected [i ]);
230- }
231- expectedBuffer .append ("}" );
232- Assert .assertEquals (message , expectedBuffer .toString (), actualBuffer .toString ());
233- }
234-
235- protected final void expectingProblemsFor (IPath root , List <String > expected ) {
215+ protected final void expectingProblemsFor (final IPath root , final List <String > expected ) {
236216 expectingProblemsFor (new IPath [] {root }, expected );
237217 }
238218
239- protected final void expectingProblemsFor (IPath [] roots , List <String > expected ) {
219+ protected final void expectingProblemsFor (final IPath [] roots , final List <String > expected ) {
240220 Problem [] allProblems = getSortedProblems (roots );
241221 TestCase .assertStringEquals (toString (expected ), toString (Arrays .asList (allProblems )), false );
242222 }
@@ -245,16 +225,16 @@ protected final void expectingNoProblems() {
245225 expectingNoProblemsFor (env .getWorkspaceRootPath ());
246226 }
247227
248- protected final void expectingNoProblemsFor (IPath ... roots ) {
228+ protected final void expectingNoProblemsFor (final IPath ... roots ) {
249229 Problem [] allProblems = getSortedProblems (roots );
250230 TestCase .assertStringEquals ("" , toString (Arrays .asList (allProblems )), false );
251231 }
252232
253- protected final void expectingSpecificProblemFor (IPath root , Problem problem ) {
233+ protected final void expectingSpecificProblemFor (final IPath root , final Problem problem ) {
254234 expectingSpecificProblemsFor (root , new Problem [] {problem });
255235 }
256236
257- protected void expectingSpecificProblemsFor (IPath root , Problem [] problems ) {
237+ protected void expectingSpecificProblemsFor (final IPath root , final Problem [] problems ) {
258238 Problem [] rootProblems = env .getProblemsFor (root );
259239 next : for (int i = 0 ; i < problems .length ; i += 1 ) {
260240 Problem problem = problems [i ];
@@ -275,15 +255,15 @@ protected void expectingSpecificProblemsFor(IPath root, Problem[] problems) {
275255 }
276256 }
277257
278- protected final void printProblemsFor (IPath ... roots ) {
258+ protected final void printProblemsFor (final IPath ... roots ) {
279259 for (IPath root : roots ) {
280260 Problem [] problems = env .getProblemsFor (root );
281261 System .out .println (toString (Arrays .asList (problems )));
282262 System .out .println ();
283263 }
284264 }
285265
286- private Problem [] getSortedProblems (IPath [] roots ) {
266+ private Problem [] getSortedProblems (final IPath [] roots ) {
287267 List <Problem > allProblems = new ArrayList <>();
288268 for (IPath root : roots ) {
289269 Collections .addAll (allProblems , env .getProblemsFor (root ));
@@ -294,7 +274,7 @@ private Problem[] getSortedProblems(IPath[] roots) {
294274 return allProblems .toArray (new Problem [0 ]);
295275 }
296276
297- private static String toString (Iterable <?> seq ) {
277+ private static String toString (final Iterable <?> seq ) {
298278 StringBuilder buf = new StringBuilder ();
299279 for (Object obj : seq ) {
300280 buf .append (obj ).append ('\n' );
@@ -307,17 +287,19 @@ private static String toString(Iterable<?> seq) {
307287 protected static class TestingEnvironment extends org .eclipse .jdt .core .tests .builder .TestingEnvironment {
308288
309289 @ Override
310- public IPath addProject (String projectName ) {
290+ public IPath addProject (final String projectName ) {
311291 return addProject (projectName , "1.6" );
312292 }
313293
314294 @ Override
315- public IPath addProject (String projectName , String compliance ) {
295+ public IPath addProject (final String projectName , final String compliance ) {
316296 try {
317297 IPath projectPath = super .addProject (projectName , compliance );
298+ removePackageFragmentRoot (projectPath , "" );
299+ addPackageFragmentRoot (projectPath , "src" );
300+ addGroovyNature (projectName );
318301
319- new ProjectScope (getProject (projectName )).getNode (JavaRuntime .ID_PLUGIN )
320- .put (JavaRuntime .PREF_COMPILER_COMPLIANCE_DOES_NOT_MATCH_JRE , JavaCore .IGNORE );
302+ // add JRE container to classpath
321303 IClasspathAttribute [] attributes ;
322304 if (JavaCore .compareJavaVersions (compliance , "9" ) < 0 ) {
323305 attributes = new IClasspathAttribute [0 ];
@@ -326,15 +308,13 @@ public IPath addProject(String projectName, String compliance) {
326308 }
327309 addEntry (projectPath , JavaCore .newContainerEntry (JavaRuntime .newDefaultJREContainerPath (), new IAccessRule [0 ], attributes , false ));
328310
329- addGroovyNature (projectName );
330-
331311 return projectPath ;
332312 } catch (JavaModelException e ) {
333313 throw new RuntimeException (e );
334314 }
335315 }
336316
337- public void addGroovyNature (String projectName ) {
317+ public void addGroovyNature (final String projectName ) {
338318 try {
339319 IProject project = getProject (projectName );
340320 IProjectDescription description = project .getDescription ();
@@ -345,7 +325,7 @@ public void addGroovyNature(String projectName) {
345325 }
346326 }
347327
348- public void removeGroovyNature (String projectName ) {
328+ public void removeGroovyNature (final String projectName ) {
349329 try {
350330 IProject project = getProject (projectName );
351331 IProjectDescription description = project .getDescription ();
@@ -356,7 +336,7 @@ public void removeGroovyNature(String projectName) {
356336 }
357337 }
358338
359- public void addGroovyJars (IPath projectPath ) throws Exception {
339+ public void addGroovyJars (final IPath projectPath ) throws Exception {
360340 boolean minimal = false , modular = false ;
361341 for (IClasspathEntry cpe : getJavaProject (projectPath ).getRawClasspath ()) {
362342 if (cpe .getEntryKind () == IClasspathEntry .CPE_CONTAINER &&
@@ -373,96 +353,36 @@ public void addGroovyJars(IPath projectPath) throws Exception {
373353 addEntry (projectPath , GroovyRuntime .newGroovyClasspathContainerEntry (minimal , modular , null ));
374354 }
375355
376- public void addJar (IPath projectPath , String path ) throws Exception {
377- URL jar = Platform .getBundle ("org.eclipse.jdt.groovy.core.tests.builder" ).getEntry (path );
356+ /**
357+ * @param jarPath resource in builder tests project, e.g. "lib/xyz.jar"
358+ */
359+ public void addJar (final IPath projectPath , final String jarPath ) throws Exception {
360+ URL jar = Platform .getBundle ("org.eclipse.jdt.groovy.core.tests.builder" ).getEntry (jarPath );
378361 addExternalJar (projectPath , FileLocator .resolve (jar ).getFile ());
379362 }
380363
381364 @ Override
382- public void addEntry (IPath projectPath , IClasspathEntry entryPath ) throws JavaModelException {
383- IClasspathEntry [] classpath = getClasspath (projectPath );
384- // first look to see if the entry already exists
385- for (IClasspathEntry entry : classpath ) {
386- if (entry .equals (entryPath )) {
387- return ;
388- }
365+ public void addEntry (final IPath projectPath , final IClasspathEntry entry ) throws JavaModelException {
366+ if (Arrays .stream (getClasspath (projectPath )).noneMatch (entry ::equals )) {
367+ super .addEntry (projectPath , entry );
389368 }
390- super .addEntry (projectPath , entryPath );
391369 }
392370
393- /**
394- * Adds a groovy class with the given contents to the given
395- * package in the workspace. The package is created
396- * if necessary. If a class with the same name already
397- * exists, it is replaced. A workspace must be open,
398- * and the given class name must not end with ".java".
399- * Returns the path of the added class.
400- */
401- public IPath addGroovyClass (IPath packagePath , String className , String contents ) {
402- return addGroovyClassExtension (packagePath , className , contents , null );
403- }
371+ public IPath addGroovyClass (final IPath packagePath , final String className , final String contents ) {
372+ IPath filePath = packagePath .append (className .endsWith (".groovy" ) ? className : className + ".groovy" );
373+ createFile (filePath , contents .getBytes (StandardCharsets .US_ASCII ));
404374
405- /**
406- * Adds a groovy class with the given contents to the given
407- * package in the workspace. The package is created
408- * if necessary. If a class with the same name already
409- * exists, it is replaced. A workspace must be open,
410- * and the given class name must not end with ".java".
411- * Returns the path of the added class.
412- */
413- public IPath addGroovyClass (IPath packageFragmentRootPath , String packageName , String className , String contents ) {
414- return addGroovyClassExtension (packageFragmentRootPath , packageName , className , contents , null );
415- }
416-
417- /**
418- * Adds a groovy class with the given contents to the given
419- * package in the workspace, the file will use the specified file suffix.
420- * The package is created if necessary. If a class with the same name already
421- * exists, it is replaced.
422- * Returns the path of the added class.
423- */
424- public IPath addGroovyClassWithSuffix (IPath packagePath , String className , String suffix , String contents ) {
425- return addGroovyClassExtension (packagePath , className , suffix , contents , suffix );
426- }
427-
428- public IPath addGroovyClassWithSuffix (IPath packageFragmentRootPath , String packageName , String className , String suffix , String contents ) {
429- return addGroovyClassExtension (packageFragmentRootPath , packageName , className , contents , suffix );
375+ return filePath ;
430376 }
431377
432- /**
433- * Adds a groovy class with the given contents to the given
434- * package in the workspace. The package is created
435- * if necessary. If a class with the same name already
436- * exists, it is replaced. A workspace must be open,
437- * and the given class name must not end with ".java".
438- * Returns the path of the added class.
439- * @param fileExtension file extension of the groovy class to create (without a '.')
440- */
441- public IPath addGroovyClassExtension (IPath packagePath , String className , String contents , String fileExtension ) {
442- IPath classPath = packagePath .append (className + "." + Optional .ofNullable (fileExtension ).orElse ("groovy" ));
443- createFile (classPath , contents .getBytes (StandardCharsets .US_ASCII ));
444- return classPath ;
445- }
446-
447- /**
448- * Adds a groovy class with the given contents to the given
449- * package in the workspace. The package is created
450- * if necessary. If a class with the same name already
451- * exists, it is replaced. A workspace must be open,
452- * and the given class name must not end with ".java".
453- * Returns the path of the added class.
454- * @param fileExtension file extension of the groovy class to create (without a '.')
455- */
456- public IPath addGroovyClassExtension (IPath packageFragmentRootPath , String packageName , String className , String contents , String fileExtension ) {
457- // make sure the package exists
458- if (packageName != null && packageName .length () > 0 ) {
459- IPath packagePath = addPackage (packageFragmentRootPath , packageName );
460- return addGroovyClassExtension (packagePath , className , contents , fileExtension );
378+ public IPath addGroovyClass (final IPath packageFragmentRootPath , final String packageName , final String className , final String contents ) {
379+ if (packageName == null || packageName .length () < 1 ) {
380+ return addGroovyClass (packageFragmentRootPath , className , contents );
461381 }
462- return addGroovyClassExtension ( packageFragmentRootPath , className , contents , fileExtension );
382+ return addGroovyClass ( addPackage ( packageFragmentRootPath , packageName ), className , contents );
463383 }
464384
465- public <U extends ICompilationUnit > U getUnit (IPath path ) {
385+ public <U extends ICompilationUnit > U getUnit (final IPath path ) {
466386 IFile file = ResourcesPlugin .getWorkspace ().getRoot ().getFile (path );
467387 @ SuppressWarnings ("unchecked" )
468388 U unit = (U ) JavaCore .createCompilationUnitFrom (file );
0 commit comments