You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2008/07/22 18:01:32 UTC
svn commit: r678785 - in
/maven/core-integration-testing/trunk/core-integration-tests/src/test:
java/org/apache/maven/integrationtests/
resources/mng-3652-user-agent/test-plugin/
resources/mng-3652-user-agent/test-plugin/src/main/java/org/apache/maven/...
Author: jdcasey
Date: Tue Jul 22 09:01:31 2008
New Revision: 678785
URL: http://svn.apache.org/viewvc?rev=678785&view=rev
Log:
Improve the user-agent IT to directly test the User-Agent HTTP header passed to the server when attempting to resolve an artifact.
Modified:
maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenITmng3652UserAgentHeader.java
maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/pom.xml
maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/src/main/java/org/apache/maven/its/mng3652/MyMojo.java
Modified: maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenITmng3652UserAgentHeader.java
URL: http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenITmng3652UserAgentHeader.java?rev=678785&r1=678784&r2=678785&view=diff
==============================================================================
--- maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenITmng3652UserAgentHeader.java (original)
+++ maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenITmng3652UserAgentHeader.java Tue Jul 22 09:01:31 2008
@@ -1,11 +1,21 @@
package org.apache.maven.integrationtests;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.it.Verifier;
import org.apache.maven.it.util.ResourceExtractor;
-import org.apache.maven.it.util.FileUtils;
-
-import java.io.File;
public class MavenITmng3652UserAgentHeader
extends AbstractMavenIntegrationTestCase
@@ -32,12 +42,159 @@
verifier.verifyErrorFreeLog();
verifier.resetStreams();
+ int port = ( Math.abs( new Random().nextInt() ) % 2048 ) + 1024;
+
+ Server s = new Server( port );
+ Thread t = new Thread( s );
+ t.setDaemon( true );
+ t.start();
+
+ System.out.println( "\n\nHTTP Recording server started on port: " + port + "\n\n" );
+
verifier = new Verifier( projectDir.getAbsolutePath() );
- verifier.executeGoal( "process-sources" );
- verifier.verifyErrorFreeLog();
+
+ List cliOptions = new ArrayList();
+ cliOptions.add( "-DtestPort=" + port );
+ verifier.setCliOptions( cliOptions );
+
+ verifier.executeGoal( "validate" );
verifier.resetStreams();
+
+ t.interrupt();
- assertEquals( "Apache Maven/" + System.getProperty( "maven.version" ), FileUtils.fileRead( new File( projectDir, "target/touch.txt" ) ) );
+ String userAgent = s.userAgent;
+ assertNotNull( userAgent );
+
+ File touchFile = new File( projectDir, "target/touch.txt" );
+ assertTrue( touchFile.exists() );
+
+ List lines = verifier.loadFile( touchFile, false );
+
+ // NOTE: system property for maven.version may not exist if you use -Dtest
+ // surefire parameter to run this single test. Therefore, the plugin writes
+ // the maven version into the check file.
+ String mavenVersion = (String) lines.get( 0 );
+ String javaVersion = (String) lines.get( 1 );
+ String os = (String) lines.get( 2 );
+
+ String minorVersion = mavenVersion.substring( 0, 3 );
+
+ System.out.println( "Found User-Agent of: \'" + userAgent + "\'" );
+
+ assertTrue( "User-Agent:\n\n\'" + userAgent + "\'\n\nshould have a main value of:\n\n\'ApacheMavenArtifact/" + minorVersion + "\'", userAgent.startsWith( "ApacheMavenArtifact/" + minorVersion ) );
+ assertTrue( "User-Agent:\n\n\'" + userAgent + "\'\n\nshould contain:\n\n\'Apache Maven " + mavenVersion + "\'", userAgent.indexOf( "Apache Maven " + mavenVersion ) > -1 );
+ assertTrue( "User-Agent:\n\n\'" + userAgent + "\'\n\nshould contain:\n\n\'JDK " + javaVersion + "\'", userAgent.indexOf( "JDK " + javaVersion ) > -1 );
+ assertTrue( "User-Agent:\n\n\'" + userAgent + "\'\n\nshould contain:\n\n\'" +os + "\'", userAgent.indexOf( os ) > -1 );
}
-}
+ private static final class Server
+ implements Runnable
+ {
+
+ private final int port;
+
+ private String userAgent;
+
+ private Server( int port )
+ {
+ this.port = port;
+ }
+
+ public void run()
+ {
+ ServerSocket ssock = null;
+ try
+ {
+ try
+ {
+ ssock = new ServerSocket( port );
+ ssock.setSoTimeout( 2000 );
+ }
+ catch ( IOException e )
+ {
+ Logger.getLogger( Server.class.getName() ).log( Level.SEVERE, "", e );
+ return;
+ }
+
+ while ( !Thread.currentThread().isInterrupted() )
+ {
+ Socket sock = null;
+ BufferedReader r = null;
+ try
+ {
+ try
+ {
+ sock = ssock.accept();
+ }
+ catch ( SocketTimeoutException e )
+ {
+ continue;
+ }
+
+ r = new BufferedReader( new InputStreamReader( sock.getInputStream() ) );
+
+ String line = null;
+ int count = 0;
+ while ( ( line = r.readLine() ) != null )
+ {
+ System.out.println( line );
+ if ( line.startsWith( "User-Agent:" ) )
+ {
+ userAgent = line.substring( "User-Agent: ".length() );
+ while( userAgent.startsWith( " " ) )
+ {
+ userAgent = userAgent.substring( 1 );
+ }
+
+ break;
+ }
+ count++;
+ }
+
+ System.out.println( "Read in " + count + " passes." );
+ }
+ catch ( IOException e )
+ {
+ Logger.getLogger( Server.class.getName() ).log( Level.SEVERE, "", e );
+ }
+ finally
+ {
+ if ( r != null )
+ {
+ try
+ {
+ r.close();
+ }
+ catch ( IOException e )
+ {
+ }
+ }
+ if ( sock != null )
+ {
+ try
+ {
+ sock.close();
+ }
+ catch ( IOException e )
+ {
+ }
+ }
+ }
+ }
+ }
+ finally
+ {
+ if ( ssock != null )
+ {
+ try
+ {
+ ssock.close();
+ }
+ catch ( IOException e )
+ {
+ }
+ }
+ }
+ }
+ }
+}
Modified: maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/pom.xml?rev=678785&r1=678784&r2=678785&view=diff
==============================================================================
--- maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/pom.xml (original)
+++ maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/pom.xml Tue Jul 22 09:01:31 2008
@@ -24,5 +24,10 @@
<version>3.8.1</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-core</artifactId>
+ <version>2.0.9</version>
+ </dependency>
</dependencies>
</project>
Modified: maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/src/main/java/org/apache/maven/its/mng3652/MyMojo.java
URL: http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/src/main/java/org/apache/maven/its/mng3652/MyMojo.java?rev=678785&r1=678784&r2=678785&view=diff
==============================================================================
--- maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/src/main/java/org/apache/maven/its/mng3652/MyMojo.java (original)
+++ maven/core-integration-testing/trunk/core-integration-tests/src/test/resources/mng-3652-user-agent/test-plugin/src/main/java/org/apache/maven/its/mng3652/MyMojo.java Tue Jul 22 09:01:31 2008
@@ -1,94 +1,130 @@
package org.apache.maven.its.mng3652;
-import org.apache.maven.artifact.manager.WagonManager;
-import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.wagon.Wagon;
-
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
-import java.lang.reflect.Field;
-import java.util.Properties;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.manager.WagonManager;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.execution.RuntimeInformation;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.wagon.ResourceDoesNotExistException;
+import org.apache.maven.wagon.TransferFailedException;
+import org.codehaus.plexus.util.IOUtil;
/**
- * Goal which touches a timestamp file.
+ * Goal which attempts to download a dummy artifact from a repository on localhost
+ * at the specified port. This is used to allow the unit test class to record the
+ * User-Agent HTTP header in use. It will also write the maven version in use to
+ * a file in the output directory, for comparison in the unit tests assertions.
*
* @goal touch
*
- * @phase process-sources
+ * @phase validate
*/
public class MyMojo
extends AbstractMojo
{
+
+ private static final String LS = System.getProperty( "line.separator" );
+
/**
- * Location of the file.
- * @parameter expression="${project.build.directory}"
- * @required
+ * @parameter default-value="${project.build.directory}/touch.txt"
*/
- private File outputDirectory;
-
+ private File touchFile;
+
/**
* @component
*/
private WagonManager wagonManager;
-
+
+ /**
+ * @component
+ */
+ private ArtifactFactory artifactFactory;
+
+ /**
+ * @component
+ */
+ private ArtifactRepositoryFactory repositoryFactory;
+
+ /**
+ * @component
+ */
+ private ArtifactRepositoryLayout layout;
+
+ /**
+ * @component
+ */
+ private RuntimeInformation runtimeInformation;
+
+ /**
+ * @parameter expression="${testPort}"
+ * @required
+ */
+ private String testPort;
+
public void execute()
throws MojoExecutionException
{
- String userAgent = null;
+ ArtifactRepository remote =
+ repositoryFactory.createArtifactRepository( "test", "http://localhost:" + testPort, layout,
+ new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() );
+
+ Artifact artifact = artifactFactory.createArtifact( "bad.group", "missing-artifact", "1", null, "jar" );
+
+ File tempArtifactFile;
try
{
- Wagon wagon = wagonManager.getWagon( "http" );
-
- Field f = wagon.getClass().getDeclaredField( "httpHeaders" );
- f.setAccessible( true );
- Properties properties = (Properties) f.get( wagon );
- f.setAccessible( false );
-
- if ( properties != null )
- {
- userAgent = properties.getProperty( "User-Agent" );
- }
+ tempArtifactFile = File.createTempFile( "artifact-temp.", ".jar" );
}
- catch ( Exception e )
+ catch ( IOException e )
{
- throw new MojoExecutionException( e.getMessage(), e );
+ throw new MojoExecutionException( "Failed to create temp file for artifact transfer attempt.", e );
}
-
- File f = outputDirectory;
-
- if ( !f.exists() )
+
+ tempArtifactFile.deleteOnExit();
+
+ artifact.setFile( tempArtifactFile );
+
+ try
{
- f.mkdirs();
+ wagonManager.getArtifact( artifact, remote );
}
-
- File touch = new File( f, "touch.txt" );
-
+ catch ( TransferFailedException e )
+ {
+ }
+ catch ( ResourceDoesNotExistException e )
+ {
+ }
+
FileWriter w = null;
try
{
- w = new FileWriter( touch );
-
- w.write( userAgent );
+ touchFile.getParentFile().mkdirs();
+ w = new FileWriter( touchFile );
+
+ w.write( runtimeInformation.getApplicationVersion().toString() );
+ w.write( LS );
+ w.write( System.getProperty( "java.version" ) );
+ w.write( LS );
+ w.write( System.getProperty( "os.name" ) );
+ w.write( LS );
+ w.write( System.getProperty( "os.version" ) );
}
catch ( IOException e )
{
- throw new MojoExecutionException( "Error creating file " + touch, e );
+ throw new MojoExecutionException( "Failed to write touch-file: " + touchFile, e );
}
finally
{
- if ( w != null )
- {
- try
- {
- w.close();
- }
- catch ( IOException e )
- {
- // ignore
- }
- }
+ IOUtil.close( w );
}
}
}