Author: jvanzyl Date: Thu Oct 12 10:25:46 2006 New Revision: 463336 URL: http://svn.apache.org/viewvc?view=rev&rev=463336 Log: o integrating more of dan fabulich's work to get the ITs setup as real tests
Modified: maven/components/trunk/maven-core-it-verifier/pom.xml maven/components/trunk/maven-core-it-verifier/src/main/java/org/apache/maven/it/Verifier.java maven/components/trunk/maven-core-it-verifier/src/main/java/org/codehaus/plexus/util/FileUtils.java maven/components/trunk/mavenexecute.pl Modified: maven/components/trunk/maven-core-it-verifier/pom.xml URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-core-it-verifier/pom.xml?view=diff&rev=463336&r1=463335&r2=463336 ============================================================================== --- maven/components/trunk/maven-core-it-verifier/pom.xml (original) +++ maven/components/trunk/maven-core-it-verifier/pom.xml Thu Oct 12 10:25:46 2006 @@ -26,4 +26,11 @@ </parent> <artifactId>maven-core-it-verifier</artifactId> <packaging>jar</packaging> + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.1</version> + </dependency> + </dependencies> </project> Modified: maven/components/trunk/maven-core-it-verifier/src/main/java/org/apache/maven/it/Verifier.java URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-core-it-verifier/src/main/java/org/apache/maven/it/Verifier.java?view=diff&rev=463336&r1=463335&r2=463336 ============================================================================== --- maven/components/trunk/maven-core-it-verifier/src/main/java/org/apache/maven/it/Verifier.java (original) +++ maven/components/trunk/maven-core-it-verifier/src/main/java/org/apache/maven/it/Verifier.java Thu Oct 12 10:25:46 2006 @@ -1,42 +1,18 @@ package org.apache.maven.it; -import org.codehaus.plexus.util.cli.CommandLineException; -import org.codehaus.plexus.util.cli.CommandLineUtils; -import org.codehaus.plexus.util.cli.Commandline; -import org.codehaus.plexus.util.cli.StreamConsumer; -import org.codehaus.plexus.util.cli.WriterStreamConsumer; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.StringUtils; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import org.xml.sax.helpers.DefaultHandler; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.FilenameFilter; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintStream; -import java.io.Writer; -import java.net.MalformedURLException; -import java.net.URL; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; -import java.util.StringTokenizer; +import java.io.*; +import java.net.*; +import java.text.*; +import java.util.*; + +import javax.xml.parsers.*; + +import junit.framework.*; + +import org.codehaus.plexus.util.*; +import org.codehaus.plexus.util.cli.*; +import org.xml.sax.*; +import org.xml.sax.helpers.*; /** * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl </a> @@ -48,7 +24,7 @@ { private static final String LOG_FILENAME = "log.txt"; - private static String localRepo; + public String localRepo; private final String basedir; @@ -59,11 +35,17 @@ private final PrintStream originalOut; private final PrintStream originalErr; + + private List cliOptions = new ArrayList(); + + private Properties systemProperties = new Properties(); + + private Properties verifierProperties = new Properties(); // TODO: needs to be configurable private static String localRepoLayout = "default"; - public Verifier( String basedir ) + public Verifier( String basedir , String settingsFile) throws VerificationException { this.basedir = basedir; @@ -74,6 +56,12 @@ originalErr = System.err; System.setErr( new PrintStream( errStream ) ); + + findLocalRepo(settingsFile); + } + + public Verifier (String basedir) throws VerificationException { + this(basedir, null); } public void resetStreams() @@ -122,22 +110,27 @@ if ( chokeOnErrorOutput ) { - lines = loadFile( basedir, LOG_FILENAME, false ); + verifyErrorFreeLog(); + } + } - for ( Iterator i = lines.iterator(); i.hasNext(); ) - { - String line = (String) i.next(); + public void verifyErrorFreeLog() throws VerificationException { + List lines; + lines = loadFile( basedir, LOG_FILENAME, false ); - // A hack to keep stupid velocity resource loader errors from triggering failure - if ( line.indexOf( "[ERROR]" ) >= 0 && line.indexOf( "VM_global_library.vm" ) == -1 ) - { - throw new VerificationException( "Error in execution." ); - } + for ( Iterator i = lines.iterator(); i.hasNext(); ) + { + String line = (String) i.next(); + + // A hack to keep stupid velocity resource loader errors from triggering failure + if ( line.indexOf( "[ERROR]" ) >= 0 && line.indexOf( "VM_global_library.vm" ) == -1 ) + { + throw new VerificationException( "Error in execution." ); } } } - private Properties loadProperties( String filename ) + public Properties loadProperties( String filename ) throws VerificationException { Properties properties = new Properties(); @@ -164,13 +157,13 @@ return properties; } - private static List loadFile( String basedir, String filename, boolean hasCommand ) + public List loadFile( String basedir, String filename, boolean hasCommand ) throws VerificationException { return loadFile( new File( basedir, filename ), hasCommand ); } - private static List loadFile( File file, boolean hasCommand ) + public List loadFile( File file, boolean hasCommand ) throws VerificationException { List lines = new ArrayList(); @@ -209,7 +202,7 @@ return lines; } - private static List replaceArtifacts( String line, boolean hasCommand ) + private List replaceArtifacts( String line, boolean hasCommand ) { String MARKER = "${artifact:"; int index = line.indexOf( MARKER ); @@ -223,7 +216,7 @@ } String artifact = line.substring( newLine.length() + MARKER.length(), index ); - newLine += convertArtifact( artifact ); + newLine += getArtifactPath( artifact ); newLine += line.substring( index + 1 ); List l = new ArrayList(); @@ -285,7 +278,7 @@ } } - private static String convertArtifact( String artifact ) + private String getArtifactPath( String artifact ) { StringTokenizer tok = new StringTokenizer( artifact, ":" ); if ( tok.countTokens() != 4 ) @@ -299,18 +292,25 @@ a[i] = tok.nextToken(); } + String org = a[0]; + String name = a[1]; + String version = a[2]; String ext = a[3]; - if ( "maven-plugin".equals( a[3] ) ) + return getArtifactPath(org, name, version, ext); + } + + private String getArtifactPath(String org, String name, String version, String ext) { + if ( "maven-plugin".equals( ext ) ) { ext = "jar"; } String classifier = null; - if ( "coreit-artifact".equals( a[3] ) ) + if ( "coreit-artifact".equals( ext ) ) { ext = "jar"; classifier = "it"; } - if ( "test-jar".equals( a[3] ) ) + if ( "test-jar".equals( ext ) ) { ext = "jar"; classifier = "tests"; @@ -319,13 +319,13 @@ String repositoryPath; if ( "legacy".equals( localRepoLayout ) ) { - repositoryPath = a[0] + "/" + a[3] + "s/" + a[1] + "-" + a[2] + "." + ext; + repositoryPath = org + "/" + ext + "s/" + name + "-" + version + "." + ext; } else if ( "default".equals( localRepoLayout ) ) { - repositoryPath = a[0].replace( '.', '/' ); - repositoryPath = repositoryPath + "/" + a[1] + "/" + a[2]; - repositoryPath = repositoryPath + "/" + a[1] + "-" + a[2]; + repositoryPath = org.replace( '.', '/' ); + repositoryPath = repositoryPath + "/" + name + "/" + version; + repositoryPath = repositoryPath + "/" + name + "-" + version; if ( classifier != null ) { repositoryPath = repositoryPath + "-" + classifier; @@ -339,6 +339,16 @@ return localRepo + "/" + repositoryPath; } + + public List getArtifactFileNameList(String org, String name, String version, String ext) { + List files = new ArrayList(); + String artifactPath = getArtifactPath(org, name, version, ext); + File dir = new File( artifactPath ); + files.add(artifactPath); + addMetadataToList( dir, false, files, null ); + addMetadataToList( dir.getParentFile(), false, files, null ); + return files; + } public void executeHook( String filename ) throws VerificationException @@ -419,44 +429,48 @@ } else if ( "svn".equals( cmd ) ) { - try - { + launchSubversion(line, basedir); + } + else + { + throw new VerificationException( "unknown command: " + cmd ); + } + } - Commandline cli = new Commandline( line ); + public static void launchSubversion(String line, String basedir) throws VerificationException { + try + { - cli.setWorkingDirectory( basedir ); + Commandline cli = new Commandline( line ); - Writer logWriter = new FileWriter( new File( basedir, LOG_FILENAME ) ); + cli.setWorkingDirectory( basedir ); - StreamConsumer out = new WriterStreamConsumer( logWriter ); + Writer logWriter = new FileWriter( new File( basedir, LOG_FILENAME ) ); - StreamConsumer err = new WriterStreamConsumer( logWriter ); + StreamConsumer out = new WriterStreamConsumer( logWriter ); - System.out.println( "Command: " + Commandline.toString( cli.getCommandline() ) ); + StreamConsumer err = new WriterStreamConsumer( logWriter ); - int ret = CommandLineUtils.executeCommandLine( cli, out, err ); + System.out.println( "Command: " + Commandline.toString( cli.getCommandline() ) ); - logWriter.close(); + int ret = CommandLineUtils.executeCommandLine( cli, out, err ); - if ( ret > 0 ) - { - System.err.println( "Exit code: " + ret ); + logWriter.close(); - throw new VerificationException(); - } - } - catch ( CommandLineException e ) + if ( ret > 0 ) { - throw new VerificationException( e ); - } - catch ( IOException e ) - { - throw new VerificationException( e ); + System.err.println( "Exit code: " + ret ); + + throw new VerificationException(); } } - else + catch ( CommandLineException e ) { - throw new VerificationException( "unknown command: " + cmd ); + throw new VerificationException( e ); + } + catch ( IOException e ) + { + throw new VerificationException( e ); } } @@ -494,7 +508,51 @@ return repo; } + + public void deleteArtifact(String org, String name, String version, String ext) throws IOException { + List files = getArtifactFileNameList(org, name, version, ext); + for (Iterator i = files.iterator(); i.hasNext();) { + String fileName = (String) i.next(); + FileUtils.deleteFile(new File(fileName)); + } + } + public void assertFilePresent(String file) { + try { + verifyExpectedResult(file, true); + } catch (VerificationException e) { + Assert.fail(e.getMessage()); + } + } + + public void assertFileNotPresent(String file) { + try { + verifyExpectedResult(file, false); + } catch (VerificationException e) { + Assert.fail(e.getMessage()); + } + } + + private void verifyArtifactPresence(boolean wanted, String org, String name, String version, String ext) { + List files = getArtifactFileNameList(org, name, version, ext); + for (Iterator i = files.iterator(); i.hasNext();) { + String fileName = (String) i.next(); + try { + verifyExpectedResult(fileName, wanted); + } catch (VerificationException e) { + Assert.fail(e.getMessage()); + } + } + } + + public void assertArtifactPresent(String org, String name, String version, String ext) { + verifyArtifactPresence(true, org, name, version, ext); + } + + public void assertArtifactNotPresent(String org, String name, String version, String ext) { + verifyArtifactPresence(false, org, name, version, ext); + } + private void verifyExpectedResult( String line ) throws VerificationException { @@ -505,6 +563,10 @@ wanted = false; } + verifyExpectedResult(line, wanted); + } + + private void verifyExpectedResult(String line, boolean wanted) throws VerificationException { if ( line.indexOf( "!/" ) > 0 ) { String urlString = "jar:file:" + basedir + "/" + line; @@ -629,15 +691,13 @@ // // ---------------------------------------------------------------------- - public void executeGoals( Properties properties, Properties controlProperties, String filename ) - throws VerificationException - { + public void executeGoal(String goal) throws VerificationException { + executeGoals(Arrays.asList(new String[]{goal})); + } + + public void executeGoals(List goals) throws VerificationException { String mavenHome = System.getProperty( "maven.home" ); - List goals = loadFile( basedir, filename, false ); - - List cliOptions = loadFile( basedir, "cli-options.txt", false ); - if ( goals.size() == 0 ) { throw new VerificationException( "No goals specified" ); @@ -684,13 +744,13 @@ cli.createArgument().setValue( "--batch-mode" ); - for ( Iterator i = properties.keySet().iterator(); i.hasNext(); ) + for ( Iterator i = systemProperties.keySet().iterator(); i.hasNext(); ) { String key = (String) i.next(); - cli.createArgument().setLine( "-D" + key + "=" + properties.getProperty( key ) ); + cli.createArgument().setLine( "-D" + key + "=" + systemProperties.getProperty( key ) ); } - boolean useMavenRepoLocal = Boolean.valueOf( controlProperties.getProperty( "use.mavenRepoLocal", "true" ) ).booleanValue(); + boolean useMavenRepoLocal = Boolean.valueOf( verifierProperties.getProperty( "use.mavenRepoLocal", "true" ) ).booleanValue(); if ( useMavenRepoLocal ) { @@ -729,7 +789,7 @@ { System.err.println( "Exit code: " + ret ); - throw new VerificationException(); + throw new VerificationException("Exit code was non-zero: " + ret); } } @@ -842,38 +902,15 @@ } } - if ( localRepo == null ) - { - localRepo = System.getProperty( "maven.repo.local" ); - } - - if ( localRepo == null ) - { - localRepo = retrieveLocalRepo( settingsFile ); - } - - if ( localRepo == null ) - { - localRepo = System.getProperty( "user.home" ) + "/.m2/repository"; - } - - File repoDir = new File( localRepo ); - if ( !repoDir.exists() ) - { - repoDir.mkdirs(); - } - - System.out.println( "Using default local repository: " + localRepo ); - if ( argsList.size() == 0 ) { if ( FileUtils.fileExists( basedir + File.separator + "integration-tests.txt" ) ) { try { - tests = loadFile( basedir, "integration-tests.txt", false ); + tests = FileUtils.loadFile( new File(basedir, "integration-tests.txt") ); } - catch ( VerificationException e ) + catch ( IOException e ) { System.err.println( "Unable to load integration tests file" ); @@ -949,29 +986,14 @@ } Verifier verifier = new Verifier( dir ); + verifier.findLocalRepo(settingsFile); - try - { - verifier.executeHook( "prebuild-hook.txt" ); - - Properties properties = verifier.loadProperties( "system.properties" ); + System.out.println( "Using default local repository: " + verifier.localRepo ); - Properties controlProperties = verifier.loadProperties( "verifier.properties" ); - boolean chokeOnErrorOutput = - Boolean.valueOf( controlProperties.getProperty( "failOnErrorOutput", "true" ) ).booleanValue(); - - verifier.executeGoals( properties, controlProperties, "goals.txt" ); - - verifier.executeHook( "postbuild-hook.txt" ); - - System.out.println( "*** Verifying: fail when [ERROR] detected? " + chokeOnErrorOutput + " ***" ); - - verifier.verify( chokeOnErrorOutput ); - - verifier.resetStreams(); - - System.out.println( "OK" ); + try + { + runIntegrationTest(verifier); } catch ( Throwable e ) { @@ -1002,6 +1024,62 @@ System.exit( exitCode ); } + private void findLocalRepo(String settingsFile) throws VerificationException { + if ( localRepo == null ) + { + localRepo = System.getProperty( "maven.repo.local" ); + } + + if ( localRepo == null ) + { + localRepo = retrieveLocalRepo( settingsFile ); + } + + if ( localRepo == null ) + { + localRepo = System.getProperty( "user.home" ) + "/.m2/repository"; + } + + File repoDir = new File( localRepo ); + if ( !repoDir.exists() ) + { + repoDir.mkdirs(); + } + } + + private static void runIntegrationTest(Verifier verifier) throws VerificationException { + verifier.executeHook( "prebuild-hook.txt" ); + + Properties properties = verifier.loadProperties( "system.properties" ); + + Properties controlProperties = verifier.loadProperties( "verifier.properties" ); + + boolean chokeOnErrorOutput = + Boolean.valueOf( controlProperties.getProperty( "failOnErrorOutput", "true" ) ).booleanValue(); + + List goals = verifier.loadFile( verifier.basedir, "goals.txt", false ); + + List cliOptions = verifier.loadFile( verifier.basedir, "cli-options.txt", false ); + + verifier.setCliOptions(cliOptions); + + verifier.setSystemProperties(properties); + + verifier.setVerifierProperties(controlProperties); + + verifier.executeGoals(goals); + + verifier.executeHook( "postbuild-hook.txt" ); + + System.out.println( "*** Verifying: fail when [ERROR] detected? " + chokeOnErrorOutput + " ***" ); + + verifier.verify( chokeOnErrorOutput ); + + verifier.resetStreams(); + + System.out.println( "OK" ); + } + static class UserModelReader extends DefaultHandler { @@ -1101,6 +1179,30 @@ this.currentBody = null; this.localRepository = null; } + } + + public List getCliOptions() { + return cliOptions; + } + + public void setCliOptions(List cliOptions) { + this.cliOptions = cliOptions; + } + + public Properties getSystemProperties() { + return systemProperties; + } + + public void setSystemProperties(Properties systemProperties) { + this.systemProperties = systemProperties; + } + + public Properties getVerifierProperties() { + return verifierProperties; + } + + public void setVerifierProperties(Properties verifierProperties) { + this.verifierProperties = verifierProperties; } } Modified: maven/components/trunk/maven-core-it-verifier/src/main/java/org/codehaus/plexus/util/FileUtils.java URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-core-it-verifier/src/main/java/org/codehaus/plexus/util/FileUtils.java?view=diff&rev=463336&r1=463335&r2=463336 ============================================================================== --- maven/components/trunk/maven-core-it-verifier/src/main/java/org/codehaus/plexus/util/FileUtils.java (original) +++ maven/components/trunk/maven-core-it-verifier/src/main/java/org/codehaus/plexus/util/FileUtils.java Thu Oct 12 10:25:46 2006 @@ -55,6 +55,7 @@ * */ +import java.io.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -76,6 +77,8 @@ import java.util.Random; import java.util.Vector; +import org.apache.maven.it.*; + /** * This class provides basic facilities for manipulating files and file paths. * @@ -1180,7 +1183,7 @@ * Others possible. If the delete does not work, call System.gc(), * wait a little and try again. */ - private static boolean deleteFile( File file ) + public static boolean deleteFile( File file ) throws IOException { if ( file.isDirectory() ) @@ -1670,6 +1673,33 @@ } } + public static List loadFile( File file ) throws IOException + { + List lines = new ArrayList(); + + if ( file.exists() ) + { + BufferedReader reader = new BufferedReader( new FileReader( file ) ); + + String line = reader.readLine(); + + while ( line != null ) + { + line = line.trim(); + + if ( !line.startsWith( "#" ) && line.length() != 0 ) + { + lines.add ( line ); + } + line = reader.readLine(); + } + + reader.close(); + } + + return lines; + } + /** * Renames a file, even if that involves crossing file system boundaries. * Modified: maven/components/trunk/mavenexecute.pl URL: http://svn.apache.org/viewvc/maven/components/trunk/mavenexecute.pl?view=diff&rev=463336&r1=463335&r2=463336 ============================================================================== --- maven/components/trunk/mavenexecute.pl (original) +++ maven/components/trunk/mavenexecute.pl Thu Oct 12 10:25:46 2006 @@ -15,6 +15,63 @@ $comment{$name} = $value; } + +$preamble = <<EOF; +package org.apache.maven.it; + +import java.io.*; +import java.util.*; + +import junit.framework.*; + +import org.apache.maven.it.*; +import org.codehaus.plexus.util.*; + +public class IntegrationTests extends TestCase +{ + private static final String rootdir = System.getProperty("maven.it.dir", "maven-core-it"); + + private Verifier verifier; + + public IntegrationTests(String name) + { + super(name); + } + + public static Test suite() + { + String[] tests = new String[] + { +EOF + +$postamble = <<EOF; + public void tearDown() throws VerificationException + { + verifier.resetStreams(); + + } +} + +EOF + +print $preamble; + +$/ = "\n"; +open( TESTS, "maven-core-it/integration-tests.txt" ) or die; + +while ( <TESTS> ) +{ + chomp; + if ( /^\#/ ) + { + print "//"; + } + + print "\"" . $_ . "\"," . "\n"; +} + +print "};" . "\n"; + opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($filename = readdir(DIR))) { next unless (-d "$dirname/$filename"); @@ -151,4 +208,7 @@ print "}\n\n"; } + +print $postamble; + closedir(DIR);