1515 */
1616package org .eclipse .jdt .core .groovy .tests .builder ;
1717
18+ import java .io .BufferedReader ;
19+ import java .io .InputStreamReader ;
20+ import java .io .UnsupportedEncodingException ;
21+ import java .net .URL ;
1822import java .util .Arrays ;
1923import java .util .List ;
2024import java .util .Vector ;
2125
26+ import org .codehaus .groovy .activator .GroovyActivator ;
27+ import org .codehaus .jdt .groovy .model .GroovyNature ;
2228import org .codehaus .jdt .groovy .model .ModuleNodeMapper ;
29+ import org .eclipse .core .resources .IFile ;
30+ import org .eclipse .core .resources .IProject ;
31+ import org .eclipse .core .resources .IProjectDescription ;
32+ import org .eclipse .core .resources .ResourcesPlugin ;
33+ import org .eclipse .core .runtime .CoreException ;
34+ import org .eclipse .core .runtime .FileLocator ;
2335import org .eclipse .core .runtime .IPath ;
36+ import org .eclipse .core .runtime .Platform ;
2437import org .eclipse .jdt .core .IClasspathEntry ;
2538import org .eclipse .jdt .core .ICompilationUnit ;
2639import org .eclipse .jdt .core .JavaCore ;
40+ import org .eclipse .jdt .core .JavaModelException ;
2741import org .eclipse .jdt .core .compiler .CharOperation ;
2842import org .eclipse .jdt .core .tests .builder .EfficiencyCompilerRequestor ;
2943import org .eclipse .jdt .core .tests .builder .Problem ;
30- import org .eclipse .jdt .core .tests .builder .TestingEnvironment ;
3144import org .eclipse .jdt .core .tests .junit .extension .TestCase ;
3245import org .eclipse .jdt .core .tests .util .TestVerifier ;
46+ import org .eclipse .jdt .groovy .core .util .ReflectionUtils ;
3347import org .eclipse .jdt .internal .compiler .Compiler ;
3448import org .eclipse .jdt .internal .core .JavaModelManager ;
49+ import org .eclipse .jdt .internal .core .util .Util ;
3550import org .junit .After ;
3651import org .junit .Assert ;
3752import org .junit .Before ;
@@ -42,7 +57,7 @@ public abstract class BuilderTestSuite {
4257
4358 protected static TestingEnvironment env ;
4459 protected final int moduleNodeMapperCacheSize = ModuleNodeMapper .size ();
45- protected EfficiencyCompilerRequestor debugRequestor = new EfficiencyCompilerRequestor ();
60+ protected final EfficiencyCompilerRequestor debugRequestor = new EfficiencyCompilerRequestor ();
4661
4762 @ Rule
4863 public final TestName test = new TestName ();
@@ -185,9 +200,9 @@ protected void executeClass(IPath projectPath, String className, String expectin
185200 //--------------------------------------------------------------------------
186201
187202 protected final void expectingCompiledClasses (String ... expected ) {
188- String [] actual = debugRequestor .getCompiledClasses ( );
189- org . eclipse . jdt . internal . core . util . Util .sort (actual );
190- org . eclipse . jdt . internal . core . util . Util .sort (expected );
203+ String [] actual = ( String []) ReflectionUtils . executeNoArgPrivateMethod ( debugRequestor .getClass (), "getCompiledClasses" , debugRequestor );
204+ Util .sort (actual );
205+ Util .sort (expected );
191206 expectingCompiling (actual , expected , "unexpected recompiled units. lenExpected=" + expected .length + " lenActual=" + actual .length );
192207 }
193208
@@ -288,4 +303,165 @@ private static String toString(Iterable<?> seq) {
288303 }
289304 return buf .toString ();
290305 }
306+
307+ //--------------------------------------------------------------------------
308+
309+ protected static class TestingEnvironment extends org .eclipse .jdt .core .tests .builder .TestingEnvironment {
310+
311+ public IPath addProject (String projectName , String compliance ) {
312+ IPath projectPath = super .addProject (projectName , compliance );
313+ addGroovyNature (projectName );
314+ return projectPath ;
315+ }
316+
317+ public void addGroovyNature (String projectName ) {
318+ try {
319+ IProject project = getProject (projectName );
320+ IProjectDescription description = project .getDescription ();
321+ description .setNatureIds (new String [] { JavaCore .NATURE_ID , GroovyNature .GROOVY_NATURE });
322+ project .setDescription (description , null );
323+ } catch (CoreException e ) {
324+ handleCoreException (e );
325+ }
326+ }
327+
328+ public void removeGroovyNature (String projectName ) {
329+ try {
330+ IProject project = getProject (projectName );
331+ IProjectDescription description = project .getDescription ();
332+ description .setNatureIds (new String [] { JavaCore .NATURE_ID });
333+ project .setDescription (description , null );
334+ } catch (CoreException e ) {
335+ handleCoreException (e );
336+ }
337+ }
338+
339+ public void addGroovyJars (IPath projectPath ) throws Exception {
340+ addExternalJar (projectPath , GroovyActivator .GROOVY_ALL_JAR_URL .getFile ());
341+ }
342+
343+ public void addJar (IPath projectPath , String path ) throws Exception {
344+ URL jar = Platform .getBundle ("org.eclipse.jdt.groovy.core.tests.builder" ).getEntry (path );
345+ addExternalJar (projectPath , FileLocator .resolve (jar ).getFile ());
346+ }
347+
348+ public void addEntry (IPath projectPath , IClasspathEntry entryPath ) throws JavaModelException {
349+ IClasspathEntry [] classpath = getClasspath (projectPath );
350+ // first look to see if the entry already exists
351+ for (IClasspathEntry entry : classpath ) {
352+ if (entry .equals (entryPath )) {
353+ return ;
354+ }
355+ }
356+ super .addEntry (projectPath , entryPath );
357+ }
358+
359+ /**
360+ * Adds a groovy class with the given contents to the given
361+ * package in the workspace. The package is created
362+ * if necessary. If a class with the same name already
363+ * exists, it is replaced. A workspace must be open,
364+ * and the given class name must not end with ".java".
365+ * Returns the path of the added class.
366+ */
367+ public IPath addGroovyClass (IPath packagePath , String className , String contents ) {
368+ return addGroovyClassExtension (packagePath , className , contents , null );
369+ }
370+
371+ /**
372+ * Adds a groovy class with the given contents to the given
373+ * package in the workspace, the file will use the specified file suffix.
374+ * The package is created if necessary. If a class with the same name already
375+ * exists, it is replaced.
376+ * Returns the path of the added class.
377+ */
378+ public IPath addGroovyClassWithSuffix (IPath packagePath , String className , String suffix , String contents ) {
379+ return addGroovyClassExtension (packagePath , className , suffix , contents , suffix );
380+ }
381+
382+ /**
383+ * Adds a groovy class with the given contents to the given
384+ * package in the workspace. The package is created
385+ * if necessary. If a class with the same name already
386+ * exists, it is replaced. A workspace must be open,
387+ * and the given class name must not end with ".java".
388+ * Returns the path of the added class.
389+ * @param fileExtension file extension of the groovy class to create (without a '.')
390+ */
391+ public IPath addGroovyClassExtension (IPath packagePath , String className , String contents , String fileExtension ) {
392+ //checkAssertion("a workspace must be open", fIsOpen);
393+ if (fileExtension == null ) {
394+ fileExtension = "groovy" ;
395+ }
396+ IPath classPath = packagePath .append (className + "." + fileExtension );
397+ try {
398+ createFile (classPath , contents .getBytes ("UTF8" ));
399+ } catch (UnsupportedEncodingException e ) {
400+ e .printStackTrace ();
401+ Assert .fail ("e1" );
402+ }
403+ return classPath ;
404+ }
405+
406+ /**
407+ * Adds a groovy class with the given contents to the given
408+ * package in the workspace. The package is created
409+ * if necessary. If a class with the same name already
410+ * exists, it is replaced. A workspace must be open,
411+ * and the given class name must not end with ".java".
412+ * Returns the path of the added class.
413+ */
414+ public IPath addGroovyClass (IPath packageFragmentRootPath , String packageName , String className , String contents ) {
415+ return addGroovyClassExtension (packageFragmentRootPath , packageName , className , contents , null );
416+ }
417+
418+ public IPath addGroovyClassWithSuffix (IPath packageFragmentRootPath , String packageName , String className , String suffix , String contents ) {
419+ return addGroovyClassExtension (packageFragmentRootPath , packageName , className , contents , suffix );
420+ }
421+
422+ /**
423+ * Adds a groovy class with the given contents to the given
424+ * package in the workspace. The package is created
425+ * if necessary. If a class with the same name already
426+ * exists, it is replaced. A workspace must be open,
427+ * and the given class name must not end with ".java".
428+ * Returns the path of the added class.
429+ * @param fileExtension file extension of the groovy class to create (without a '.')
430+ */
431+ public IPath addGroovyClassExtension (IPath packageFragmentRootPath , String packageName , String className ,
432+ String contents , String fileExtension ) {
433+ // make sure the package exists
434+ if (packageName != null && packageName .length () > 0 ) {
435+ IPath packagePath = addPackage (packageFragmentRootPath , packageName );
436+ return addGroovyClassExtension (packagePath , className , contents , fileExtension );
437+ }
438+ return addGroovyClassExtension (packageFragmentRootPath , className , contents , fileExtension );
439+ }
440+
441+ @ SuppressWarnings ("unchecked" )
442+ public <U extends ICompilationUnit > U getUnit (IPath path ) {
443+ IFile file = ResourcesPlugin .getWorkspace ().getRoot ().getFile (path );
444+ ICompilationUnit unit = JavaCore .createCompilationUnitFrom (file );
445+ return (U ) unit ;
446+ }
447+
448+ public String readTextFile (IPath path ) {
449+ IFile file = getWorkspace ().getRoot ().getFile (path );
450+ try {
451+ BufferedReader br = new BufferedReader (new InputStreamReader (file .getContents (), file .getCharset ()));
452+ StringBuilder sb = new StringBuilder (300 );
453+ try {
454+ int read = 0 ;
455+ while ((read = br .read ()) != -1 )
456+ sb .append ((char ) read );
457+ } finally {
458+ br .close ();
459+ }
460+ return sb .toString ();
461+ } catch (Exception ex ) {
462+ handle (ex );
463+ return null ;
464+ }
465+ }
466+ }
291467}
0 commit comments