1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.eclipse.it;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.io.StringReader;
26  import java.net.MalformedURLException;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.Properties;
34  import java.util.Map.Entry;
35  
36  import junit.framework.AssertionFailedError;
37  
38  import org.apache.maven.artifact.Artifact;
39  import org.apache.maven.artifact.factory.ArtifactFactory;
40  import org.apache.maven.artifact.factory.DefaultArtifactFactory;
41  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
42  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
43  import org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager;
44  import org.apache.maven.artifact.repository.ArtifactRepository;
45  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
46  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
47  import org.apache.maven.plugin.MojoExecutionException;
48  import org.apache.maven.plugin.eclipse.ExecutionFailedException;
49  import org.apache.maven.plugin.eclipse.Messages;
50  import org.apache.maven.plugin.ide.IdeUtils;
51  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
52  import org.apache.maven.project.MavenProject;
53  import org.apache.maven.shared.invoker.InvocationRequest;
54  import org.apache.maven.shared.invoker.InvocationResult;
55  import org.apache.maven.shared.test.plugin.BuildTool;
56  import org.apache.maven.shared.test.plugin.PluginTestTool;
57  import org.apache.maven.shared.test.plugin.ProjectTool;
58  import org.apache.maven.shared.test.plugin.RepositoryTool;
59  import org.apache.maven.shared.test.plugin.TestToolsException;
60  import org.codehaus.classworlds.ClassRealm;
61  import org.codehaus.plexus.PlexusContainer;
62  import org.codehaus.plexus.util.FileUtils;
63  import org.codehaus.plexus.util.IOUtil;
64  import org.codehaus.plexus.util.StringUtils;
65  import org.custommonkey.xmlunit.XMLAssert;
66  import org.custommonkey.xmlunit.XMLUnit;
67  import org.xml.sax.EntityResolver;
68  import org.xml.sax.InputSource;
69  import org.xml.sax.SAXException;
70  
71  /**
72   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
73   * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
74   * @version $Id: AbstractEclipsePluginIT.java 751904 2009-03-09 23:13:33Z aheritier $
75   */
76  public abstract class AbstractEclipsePluginIT
77      extends AbstractMojoTestCase
78  {
79  
80      private BuildTool buildTool;
81  
82      private ProjectTool projectTool;
83  
84      private RepositoryTool repositoryTool;
85  
86      /**
87       * Test repository directory.
88       */
89      protected static File localRepositoryDirectory = getTestFile( "target/test-classes/m2repo" );
90  
91      /**
92       * Pom File
93       */
94      protected static File PomFile = new File( getBasedir(), "pom.xml" );
95  
96      /**
97       * Group-Id for running test builds.
98       */
99      protected static final String GROUP_ID = "org.apache.maven.plugins";
100 
101     /**
102      * Artifact-Id for running test builds.
103      */
104     protected static final String ARTIFACT_ID = "maven-eclipse-plugin";
105 
106     /**
107      * Version under which the plugin was installed to the test-time local repository for running test builds.
108      */
109     protected static final String VERSION = "test";
110 
111     private static final String BUILD_OUTPUT_DIRECTORY = "target/surefire-reports/build-output";
112 
113     private static boolean installed = false;
114 
115     /**
116      * The name of the directory used for comparison of expected output.
117      */
118     private static final String EXPECTED_DIRECTORY_NAME = "expected";
119 
120     /**
121      * The XML Header used to check if the file contains XML content.
122      */
123     private static final String XML_HEADER = "<?xml";
124 
125     /**
126      * @see org.codehaus.plexus.PlexusTestCase#setUp()
127      */
128     protected void setUp()
129         throws Exception
130     {
131         if ( !installed )
132         {
133             System.out.println( "*** Running test builds; output will be directed to: " + BUILD_OUTPUT_DIRECTORY + "\n" );
134         }
135 
136         super.setUp();
137 
138         buildTool = (BuildTool) lookup( BuildTool.ROLE, "default" );
139 
140         projectTool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
141 
142         repositoryTool = (RepositoryTool) lookup( RepositoryTool.ROLE, "default" );
143 
144         String mavenHome = System.getProperty( "maven.home" );
145 
146         // maven.home is set by surefire when the test is run with maven, but better make the test
147         // run in IDEs without the need of additional properties
148         if ( mavenHome == null )
149         {
150             String path = System.getProperty( "java.library.path" );
151             String[] paths = StringUtils.split( path, System.getProperty( "path.separator" ) );
152             for ( int j = 0; j < paths.length; j++ )
153             {
154                 String pt = paths[j];
155                 if ( new File( pt, "mvn" ).exists() )
156                 {
157                     System.setProperty( "maven.home", new File( pt ).getAbsoluteFile().getParent() );
158                     break;
159                 }
160 
161             }
162         }
163 
164         System.setProperty( "MAVEN_TERMINATE_CMD", "on" );
165 
166         synchronized ( AbstractEclipsePluginIT.class )
167         {
168             if ( !installed )
169             {
170                 PluginTestTool pluginTestTool = (PluginTestTool) lookup( PluginTestTool.ROLE, "default" );
171 
172                 localRepositoryDirectory =
173                     pluginTestTool.preparePluginForUnitTestingWithMavenBuilds( PomFile, "test",
174                                                                                localRepositoryDirectory );
175 
176                 System.out.println( "*** Installed test-version of the Eclipse plugin to: " + localRepositoryDirectory
177                     + "\n" );
178 
179                 // Hack: to work around proxys and DTDs retrievals.
180                 EntityResolver ignoreDtds = new EntityResolver()
181                 {
182 
183                     public InputSource resolveEntity( String publicId, String systemId )
184                         throws SAXException, IOException
185                     {
186                         return new InputSource( new StringReader( "<!ELEMENT ignored (#PCDATA)>" ) );
187                     }
188 
189                 };
190                 XMLUnit.setTestEntityResolver( ignoreDtds );
191                 XMLUnit.setControlEntityResolver( ignoreDtds );
192 
193                 installed = true;
194             }
195         }
196 
197     }
198 
199     /**
200      * @see org.codehaus.plexus.PlexusTestCase#tearDown()
201      */
202     protected void tearDown()
203         throws Exception
204     {
205         super.tearDown();
206 
207         List containers = new ArrayList();
208 
209         containers.add( getContainer() );
210 
211         for ( Iterator iter = containers.iterator(); iter.hasNext(); )
212         {
213             PlexusContainer container = (PlexusContainer) iter.next();
214 
215             if ( container != null )
216             {
217                 container.dispose();
218 
219                 ClassRealm realm = container.getContainerRealm();
220 
221                 if ( realm != null )
222                 {
223                     realm.getWorld().disposeRealm( realm.getId() );
224                 }
225             }
226         }
227     }
228 
229     /**
230      * Execute the eclipse:eclipse goal on a test project and verify generated files.
231      *
232      * @param projectName project directory
233      * @throws Exception any exception generated during test
234      */
235     protected void testProject( String projectName )
236         throws Exception
237     {
238         testProject( projectName, new Properties(), "clean", "eclipse" );
239     }
240 
241     /**
242      * Execute the eclipse:eclipse goal on a test project and verify generated files.
243      *
244      * @param basedir basedir of mvn execution
245      * @throws Exception any exception generated during test
246      */
247     protected void testProject( File basedir )
248         throws Exception
249     {
250         testProject( basedir, new Properties(), "clean", "eclipse" );
251     }
252 
253     /**
254      * Execute the eclipse:eclipse goal on a test project and verify generated files.
255      *
256      * @param projectName project directory
257      * @param properties additional properties
258      * @param cleanGoal TODO
259      * @param genGoal TODO
260      * @throws Exception any exception generated during test
261      */
262     protected void testProject( String projectName, Properties properties, String cleanGoal, String genGoal )
263         throws Exception
264     {
265         File basedir = getTestFile( "target/test-classes/projects/" + projectName );
266         testProject( basedir, properties, cleanGoal, genGoal );
267     }
268 
269     /**
270      * Execute the eclipse:eclipse goal on a test project and verify generated files.
271      *
272      * @param basedir basedir of mvn execution
273      * @param properties additional properties
274      * @param cleanGoal TODO
275      * @param genGoal TODO
276      * @throws Exception any exception generated during test
277      */
278     protected void testProject( File basedir, Properties properties, String cleanGoal, String genGoal )
279         throws Exception
280     {
281         File pom = new File( basedir, "pom.xml" );
282 
283         String pluginSpec = getPluginCLISpecification();
284 
285         List goals = new ArrayList();
286 
287         goals.add( pluginSpec + cleanGoal );
288         goals.add( pluginSpec + genGoal );
289 
290         executeMaven( pom, properties, goals );
291 
292         MavenProject project = readProject( pom );
293 
294         String outputDirPath =
295             IdeUtils.getPluginSetting( project, "org.apache.maven.plugins:maven-eclipse-plugin", "outputDir", null );
296         File projectOutputDir = basedir;
297 
298         if ( outputDirPath != null )
299         {
300             File outputDir = new File( basedir, outputDirPath );
301             outputDir.mkdirs();
302             projectOutputDir = new File( outputDir, project.getArtifactId() );
303         }
304 
305         compareDirectoryContent( basedir, projectOutputDir );
306     }
307 
308     /**
309      * Execute the eclipse:configure-workspace goal on a test project and verify generated files.
310      *
311      * @param projectName project directory
312      * @throws Exception any exception generated during test
313      */
314     protected void testWorkspace( String projectName )
315         throws Exception
316     {
317         testWorkspace( projectName, new Properties(), "configure-workspace" );
318     }
319 
320     /**
321      * Execute the eclipse:configure-workspace goal on a test project and verify generated files.
322      *
323      * @param projectName project directory
324      * @throws Exception any exception generated during test
325      */
326     protected void testWorkspace( String projectName, String goal )
327         throws Exception
328     {
329         testWorkspace( projectName, new Properties(), goal );
330     }
331 
332     /**
333      * Execute the eclipse:configure-workspace goal on a test project and verify generated files.
334      *
335      * @param projectName project directory
336      * @param properties additional properties
337      * @param cleanGoal TODO
338      * @param genGoal TODO
339      * @throws Exception any exception generated during test
340      */
341     protected void testWorkspace( String projectName, Properties properties, String genGoal )
342         throws Exception
343     {
344         File basedir = getOutputDirectory( projectName );
345 
346         File pom = new File( basedir, "pom.xml" );
347 
348         String pluginSpec = getPluginCLISpecification();
349 
350         List goals = new ArrayList();
351 
352         goals.add( pluginSpec + genGoal );
353 
354         executeMaven( pom, properties, goals );
355 
356         MavenProject project = readProject( pom );
357 
358         String outputDirPath =
359             IdeUtils.getPluginSetting( project, "org.apache.maven.plugins:maven-eclipse-plugin", "outputDir", null );
360         File outputDir;
361         File projectOutputDir = basedir;
362 
363         if ( outputDirPath == null )
364         {
365             outputDir = basedir;
366         }
367         else
368         {
369             outputDir = new File( basedir, outputDirPath );
370             outputDir.mkdirs();
371             projectOutputDir = new File( outputDir, project.getArtifactId() );
372         }
373 
374         compareDirectoryContent( basedir, projectOutputDir );
375 
376     }
377 
378     protected File getOutputDirectory( String projectName )
379     {
380         return getTestFile( "target/test-classes/projects/" + projectName );
381     }
382 
383     protected File getTestWorkspaceWorkDirectory( String projectName )
384     {
385         return new File( this.getOutputDirectory( projectName ), ".metadata" );
386     }
387 
388     protected void executeMaven( File pom, Properties properties, List goals )
389         throws TestToolsException, ExecutionFailedException
390     {
391         executeMaven( pom, properties, goals, true );
392     }
393 
394     protected void executeMaven( File pom, Properties properties, List goals, boolean switchLocalRepo )
395         throws TestToolsException, ExecutionFailedException
396     {
397         System.out.println( "  Building " + pom.getParentFile().getName() );
398 
399         new File( BUILD_OUTPUT_DIRECTORY ).mkdirs();
400 
401         NullPointerException npe = new NullPointerException();
402         StackTraceElement[] trace = npe.getStackTrace();
403 
404         File buildLog = null;
405 
406         for ( int i = 0; i < trace.length; i++ )
407         {
408             StackTraceElement element = trace[i];
409 
410             String methodName = element.getMethodName();
411 
412             if ( methodName.startsWith( "test" ) && !methodName.equals( "testProject" ) )
413             {
414                 String classname = element.getClassName();
415 
416                 buildLog = new File( BUILD_OUTPUT_DIRECTORY, classname + "_" + element.getMethodName() + ".build.log" );
417 
418                 break;
419             }
420         }
421 
422         if ( buildLog == null )
423         {
424             buildLog = new File( BUILD_OUTPUT_DIRECTORY, "unknown.build.log" );
425         }
426 
427         InvocationRequest request = buildTool.createBasicInvocationRequest( pom, properties, goals, buildLog );
428         request.setUpdateSnapshots( false );
429         request.setShowErrors( true );
430 
431         request.setDebug( true );
432 
433         if ( switchLocalRepo )
434         {
435             request.setLocalRepositoryDirectory( localRepositoryDirectory );
436         }
437 
438         InvocationResult result = buildTool.executeMaven( request );
439 
440         if ( result.getExitCode() != 0 )
441         {
442             String buildLogUrl = buildLog.getAbsolutePath();
443 
444             try
445             {
446                 buildLogUrl = buildLog.toURL().toExternalForm();
447             }
448             catch ( MalformedURLException e )
449             {
450             }
451 
452             throw new ExecutionFailedException( "Failed to execute build.\nPOM: " + pom + "\nGoals: "
453                 + StringUtils.join( goals.iterator(), ", " ) + "\nExit Code: " + result.getExitCode() + "\nError: "
454                 + result.getExecutionException() + "\nBuild Log: " + buildLogUrl + "\n", result );
455         }
456     }
457 
458     protected MavenProject readProject( File pom )
459         throws TestToolsException
460     {
461         return projectTool.readProject( pom, localRepositoryDirectory );
462     }
463 
464     protected String getPluginCLISpecification()
465     {
466         String pluginSpec = GROUP_ID + ":" + ARTIFACT_ID + ":";
467 
468         // String pluginVersion = System.getProperty( "pluginVersion" );
469         //
470         // if ( pluginVersion != null )
471         // {
472         // pluginSpec += pluginVersion + ":";
473         // }
474         //
475         // System.out.println( "\n\nUsing Eclipse plugin version: " + pluginVersion + "\n\n" );
476 
477         // try using the test-version installed during setUp()
478         pluginSpec += VERSION + ":";
479 
480         return pluginSpec;
481     }
482 
483     /**
484      * @param basedir the base directory of the project
485      * @param projectOutputDir the directory where the eclipse plugin will write the output files.
486      * @throws MojoExecutionException
487      */
488     protected void compareDirectoryContent( File basedir, File projectOutputDir )
489         throws MojoExecutionException
490     {
491         File[] expectedDirectories = getExpectedDirectories( basedir );
492 
493         for ( int i = 0; i < expectedDirectories.length; i++ )
494         {
495             File expectedDirectory = expectedDirectories[i];
496             File[] expectedFilesToCompare = getExpectedFilesToCompare( expectedDirectory );
497 
498             for ( int j = 0; j < expectedFilesToCompare.length; j++ )
499             {
500                 File expectedFile = expectedFilesToCompare[j];
501                 File actualFile = getActualFile( projectOutputDir, basedir, expectedFile );
502 
503                 if ( !actualFile.exists() )
504                 {
505                     throw new AssertionFailedError( "Expected file not found: " + actualFile.getAbsolutePath() );
506                 }
507 
508                 assertFileEquals( expectedFile, actualFile );
509 
510             }
511         }
512     }
513 
514     protected void assertFileEquals( File expectedFile, File actualFile )
515         throws MojoExecutionException
516     {
517         if ( !actualFile.exists() )
518         {
519             throw new AssertionFailedError( "Expected file not found: " + actualFile.getAbsolutePath() );
520         }
521 
522         HashMap variableReplacement = new HashMap();
523         variableReplacement.put( "${basedir}",
524                                  IdeUtils.fixSeparator( IdeUtils.getCanonicalPath( new File( getBasedir() ) ) ) );
525         variableReplacement.put( "${M2_REPO}",
526                                  IdeUtils.fixSeparator( IdeUtils.getCanonicalPath( localRepositoryDirectory ) ) );
527 
528         String expectedFileContents = preprocess( expectedFile, variableReplacement );
529         String actualFileContents = preprocess( actualFile, null );
530 
531         if ( isXml( expectedFile ) )
532         {
533             assertXmlFileEquals( expectedFile, expectedFileContents, actualFile, actualFileContents );
534         }
535         else
536         {
537             assertTextFileEquals( expectedFile, expectedFileContents, actualFile, actualFileContents );
538         }
539     }
540 
541     /**
542      * Assert that two XML files are equal.
543      *
544      * @param expectedFile the expected file - only used for path information
545      * @param expectedFileContents the contents of the expected file
546      * @param actualFile the actual file - only used for path information
547      * @param actualFileContents the contents of the actual fiel
548      * @throws MojoExecutionException failures.
549      */
550     private void assertXmlFileEquals( File expectedFile, String expectedFileContents, File actualFile,
551                                       String actualFileContents )
552         throws MojoExecutionException
553     {
554         try
555         {
556             XMLAssert.assertXMLEqual( "Comparing '" + IdeUtils.getCanonicalPath( actualFile ) + "' against '"
557                 + IdeUtils.getCanonicalPath( expectedFile ), expectedFileContents, actualFileContents );
558         }
559         catch ( IOException e )
560         {
561             throw new MojoExecutionException( IdeUtils.getCanonicalPath( expectedFile )
562                 + "assertXmlFileEquals failure: IO " + e.getMessage(), e );
563         }
564         catch ( SAXException e )
565         {
566             throw new MojoExecutionException( "assertXmlFileEquals failure: SAX " + e.getMessage(), e );
567         }
568     }
569 
570     /**
571      * Assert that two text files are equals. Lines that start with # are comments and ignored.
572      *
573      * @param expectedFile the expected file - only used for path information
574      * @param expectedFileContents the contents of the expected file
575      * @param actualFile the actual file - only used for path information
576      * @param actualFileContents the contents of the actual fiel
577      * @throws MojoExecutionException failures.
578      */
579     private void assertTextFileEquals( File expectedFile, String expectedFileContents, File actualFile,
580                                        String actualFileContents )
581         throws MojoExecutionException
582     {
583         List expectedLines = getLines( expectedFileContents );
584         List actualLines = getLines( actualFileContents );
585         for ( int i = 0; i < expectedLines.size(); i++ )
586         {
587             String expected = expectedLines.get( i ).toString();
588             if ( actualLines.size() <= i )
589             {
590                 fail( "Too few lines in the actual file. Was " + actualLines.size() + ", expected: "
591                     + expectedLines.size() );
592             }
593             String actual = actualLines.get( i ).toString();
594             if ( expected.startsWith( "#" ) && actual.startsWith( "#" ) )
595             {
596                 // ignore comments, for settings file
597                 continue;
598             }
599             assertEquals( "Comparing '" + IdeUtils.getCanonicalPath( actualFile ) + "' against '"
600                 + IdeUtils.getCanonicalPath( expectedFile ) + "' at line #" + ( i + 1 ), expected, actual );
601         }
602         assertTrue( "Unequal number of lines.", expectedLines.size() == actualLines.size() );
603     }
604 
605     /**
606      * Preprocess the file so that equals comparison can be done. Preprocessing may vary based on filename.
607      *
608      * @param file the file being processed
609      * @param variables if not null, then replace all keys with the corresponding values in the expected string.
610      *
611      * @return processed input
612      */
613     private String preprocess( File file, Map variables )
614         throws MojoExecutionException
615     {
616         String result = null;
617         try
618         {
619             result = FileUtils.fileRead( file, "UTF-8" );
620         }
621         catch ( IOException ex )
622         {
623             throw new MojoExecutionException( "Unable to read file", ex );
624         }
625         result = replaceVariables( result, variables );
626         result = IdeUtils.fixWindowsDriveURI( result );
627 
628         /*
629          * NOTE: This is another hack to compensate for some metadata files that contain a complete XML file as the
630          * value for a key like "org.eclipse.jdt.ui.formatterprofiles" from "org.eclipse.jdt.ui.prefs". Line terminators
631          * in this value are platform-dependent.
632          *
633          */
634         if ( file.getName().endsWith( ".prefs" ) )
635         {
636             result = normalizeNewlineTerminators( result );
637         }
638 
639         /*
640          * NOTE: This is a hack to compensate for files that contain generated values like dependent-object in
641          * org.eclipse.wst.common.component.
642          *
643          * Regex would be a better solution.
644          */
645         if ( file.getName().equals( "org.eclipse.wst.common.component" ) || file.getName().equals( ".modulemaps" )
646             || file.getName().equals( "application.xml" ) )
647         {
648             result = result.replaceAll( "_\\d+", "" );
649         }
650         return result;
651     }
652 
653     /**
654      * Normalize line terminators into \n. \r\n, \r, \n all get changed into \n.
655      *
656      * @param input the string to normalize
657      * @return string with line terminators normalized
658      */
659     private String normalizeNewlineTerminators( String input )
660     {
661         return input.replaceAll( "(\\\\r\\\\n)|(\\\\n)|(\\\\r)", "\\n" );
662     }
663 
664     /**
665      * @param str input string
666      * @param variables map of variables (keys) and replacement value (values)
667      * @return the string with all variable values replaced.
668      */
669     private String replaceVariables( String str, Map variables )
670     {
671         String result = str;
672         if ( variables != null && !variables.isEmpty() )
673         {
674             Iterator iter = variables.entrySet().iterator();
675             while ( iter.hasNext() )
676             {
677                 Map.Entry entry = (Entry) iter.next();
678                 String variable = (String) entry.getKey();
679                 String replacement = (String) entry.getValue();
680                 result = StringUtils.replace( result, variable, replacement );
681             }
682         }
683 
684         return result;
685     }
686 
687     protected void assertContains( String message, String full, String substring )
688     {
689         if ( full == null || full.indexOf( substring ) == -1 )
690         {
691             StringBuffer buf = new StringBuffer();
692             if ( message != null )
693             {
694                 buf.append( message );
695             }
696             buf.append( ". " );
697             buf.append( "Expected \"" );
698             buf.append( substring );
699             buf.append( "\" not found" );
700             fail( buf.toString() );
701         }
702     }
703 
704     protected void assertDoesNotContain( String message, String full, String substring )
705     {
706         if ( full == null || full.indexOf( substring ) != -1 )
707         {
708             StringBuffer buf = new StringBuffer();
709             if ( message != null )
710             {
711                 buf.append( message );
712             }
713             buf.append( ". " );
714             buf.append( "Unexpected \"" );
715             buf.append( substring );
716             buf.append( "\" found" );
717             fail( buf.toString() );
718         }
719     }
720 
721     private List getLines( String input )
722         throws MojoExecutionException
723     {
724         try
725 
726         {
727             List lines = new ArrayList();
728 
729             BufferedReader reader = new BufferedReader( new StringReader( input ) );
730 
731             String line;
732 
733             while ( ( line = reader.readLine() ) != null )
734             {
735                 lines.add( line );
736             }
737 
738             IOUtil.close( reader );
739 
740             return lines;
741         }
742         catch ( IOException e )
743         {
744             throw new MojoExecutionException( "failed to getLines", e );
745         }
746     }
747 
748     /**
749      * @param basedir base directory to search for directories named "expected"
750      * @return an array of directories that match "expected"
751      */
752     private File[] getExpectedDirectories( File basedir )
753     {
754         List expectedDirectories = new ArrayList();
755         List subdirectories = new ArrayList();
756 
757         File[] allFiles = basedir.listFiles();
758         if ( allFiles != null )
759         {
760             for ( int i = 0; i < allFiles.length; i++ )
761             {
762                 File currentFile = allFiles[i];
763                 if ( currentFile.isDirectory() )
764                 {
765                     if ( currentFile.getName().equals( EXPECTED_DIRECTORY_NAME ) )
766                     {
767                         expectedDirectories.add( currentFile );
768                     }
769                     else
770                     {
771                         subdirectories.add( currentFile );
772                     }
773                 }
774             }
775         }
776         if ( !subdirectories.isEmpty() )
777         {
778             for ( Iterator iter = subdirectories.iterator(); iter.hasNext(); )
779             {
780                 File subdirectory = (File) iter.next();
781                 File[] subdirectoryFiles = getExpectedDirectories( subdirectory );
782                 expectedDirectories.addAll( Arrays.asList( subdirectoryFiles ) );
783             }
784         }
785         return (File[]) expectedDirectories.toArray( new File[expectedDirectories.size()] );
786     }
787 
788     /**
789      * @param expectedDirectory the expected directory to locate expected Files
790      * @return an array of Files found under the expectedDirectory - will recurse through the directory structure.
791      */
792     private File[] getExpectedFilesToCompare( File expectedDirectory )
793     {
794         List expectedFiles = new ArrayList();
795         List subdirectories = new ArrayList();
796 
797         File[] allFiles = expectedDirectory.listFiles();
798         if ( allFiles != null )
799         {
800             for ( int i = 0; i < allFiles.length; i++ )
801             {
802                 File currentFile = allFiles[i];
803                 if ( currentFile.isDirectory() )
804                 {
805                     subdirectories.add( currentFile );
806                 }
807                 else
808                 {
809                     expectedFiles.add( currentFile );
810                 }
811             }
812         }
813         if ( !subdirectories.isEmpty() )
814         {
815             for ( Iterator iter = subdirectories.iterator(); iter.hasNext(); )
816             {
817                 File subdirectory = (File) iter.next();
818                 File[] subdirectoryFiles = getExpectedFilesToCompare( subdirectory );
819                 expectedFiles.addAll( Arrays.asList( subdirectoryFiles ) );
820             }
821         }
822 
823         return (File[]) expectedFiles.toArray( new File[expectedFiles.size()] );
824     }
825 
826     /**
827      * Locate the actual file needed for comparison. The expectedFile has the baseDir prefix removed and the resulting
828      * relative path used to locate the file within the projectOutputDir.
829      *
830      * @param projectOutputDir the directory where the eclipse plugin writes files to
831      * @param basedir the base dir of the project being tested
832      * @param expectedFile the expected file used to compare to the actual file
833      * @return the actual file needed for comparison against the expectedFile
834      * @throws MojoExecutionException failures for obtaining actual file.
835      */
836     private File getActualFile( File projectOutputDir, File basedir, File expectedFile )
837         throws MojoExecutionException
838     {
839         String relativePath = IdeUtils.toRelativeAndFixSeparator( basedir, expectedFile, false );
840         relativePath = relativePath.replaceFirst( EXPECTED_DIRECTORY_NAME, "" );
841         File actualFile = new File( projectOutputDir, relativePath );
842         try
843         {
844             return actualFile.getCanonicalFile();
845         }
846         catch ( IOException e )
847         {
848             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantcanonicalize", actualFile.getAbsolutePath() ), e ); //$NON-NLS-1$
849         }
850     }
851 
852     /**
853      * Test if the file contains xml content.
854      *
855      * @param f the file to test
856      * @return true if the file contains xml content, false otherwise.
857      */
858     private boolean isXml( File f )
859     {
860         FileReader reader = null;
861         try
862         {
863             reader = new FileReader( f );
864             char[] header = new char[XML_HEADER.length()];
865             reader.read( header );
866             return XML_HEADER.equals( new String( header ) );
867         }
868         catch ( Exception e )
869         {
870             return false;
871         }
872         finally
873         {
874             IOUtil.close( reader );
875         }
876     }
877 
878     /**
879      * Return the not available marker file for the specified artifact details.
880      *
881      * @param groupId group id of artifact
882      * @param artifactId artifact id of artifact
883      * @param version version of artifact
884      * @param classifier the classifier of the artifact
885      * @param inClassifier the sources/javadocs to be attached
886      * @return the not available marker file
887      * @throws Exception failures.
888      * @see IdeUtils#createArtifactWithClassifier(String, String, String, String, String, ArtifactFactory)
889      */
890     protected File getNotAvailableMarkerFile( String groupId, String artifactId, String version,
891                                                      String classifier, String inClassifier )
892         throws Exception
893     {
894         // HACK: START
895         // TODO: Work out how to use Plexus to obtain these values
896         String url = "file://" + localRepositoryDirectory;
897         ArtifactRepository localRepository =
898             new DefaultArtifactRepository( "local", url, new DefaultRepositoryLayout() );
899 
900         ArtifactFactory artifactFactory = new DefaultArtifactFactory();
901 
902         DefaultArtifactHandler javaSourceArtifactHandler = new DefaultArtifactHandler( "java-source" );
903         setVariableValueToObject( javaSourceArtifactHandler, "extension", "jar");
904 
905         DefaultArtifactHandler javadocArtifactHandler = new DefaultArtifactHandler( "javadoc" );
906         setVariableValueToObject( javadocArtifactHandler, "extension", "jar");
907 
908         Map artifactHandlers = new HashMap();
909         artifactHandlers.put( "java-source", javaSourceArtifactHandler );
910         artifactHandlers.put( "javadoc", javadocArtifactHandler );
911 
912         ArtifactHandlerManager artifactHandlerManager = new DefaultArtifactHandlerManager();
913         setVariableValueToObject( artifactHandlerManager, "artifactHandlers", artifactHandlers);
914         setVariableValueToObject( artifactFactory, "artifactHandlerManager", artifactHandlerManager);
915         // HACK: END
916 
917         Artifact artifact =
918             IdeUtils.createArtifactWithClassifier( groupId, artifactId, version, classifier, inClassifier,
919                                                    artifactFactory);
920         return IdeUtils.getNotAvailableMarkerFile( localRepository, artifact );
921     }
922 
923     /**
924      * Assert that the not available marker file exists for the specified artifact details.
925      *
926      * @param groupId group id of artifact
927      * @param artifactId artifact id of artifact
928      * @param version version of artifact
929      * @param classifier the classifier of the artifact
930      * @param inClassifier the sources/javadocs to be attached
931      * @throws Exception failures
932      */
933     protected void assertNotAvailableMarkerFileExists( String groupId, String artifactId, String version,
934                                                        String classifier, String inClassifier ) throws Exception
935     {
936         File markerFile = getNotAvailableMarkerFile( groupId, artifactId, version, classifier, inClassifier );
937         assertTrue( "The \"Not Available\" marker file does not exist: " + markerFile, markerFile.exists() );
938     }
939 
940     /**
941      * Assert that the not available marker file does not exist for the specified artifact details.
942      *
943      * @param groupId group id of artifact
944      * @param artifactId artifact id of artifact
945      * @param version version of artifact
946      * @param classifier the classifier of the artifact
947      * @param inClassifier the sources/javadocs to be attached
948      * @throws Exception failures
949      */
950     protected void assertNotAvailableMarkerFileDoesNotExist( String groupId, String artifactId, String version,
951                                                        String classifier, String inClassifier ) throws Exception
952     {
953         File markerFile = getNotAvailableMarkerFile( groupId, artifactId, version, classifier, inClassifier );
954         assertTrue( "The \"Not Available\" marker file incorrectly exists: " + markerFile, !markerFile.exists() );
955     }
956 
957 }