first step at component unit-tests

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1400523 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Herve Boutemy 2012-10-20 21:44:11 +00:00
parent 9f28c88bc9
commit 5f92449d5d
12 changed files with 648 additions and 0 deletions

View File

@ -65,6 +65,20 @@ under the License.
<groupId>org.sonatype.aether</groupId>
<artifactId>aether-impl</artifactId>
</dependency>
<dependency>
<groupId>org.sonatype.aether</groupId>
<artifactId>aether-connector-wagon</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-file</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-inject-plexus</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>

View File

@ -0,0 +1,98 @@
package org.apache.maven.repository.internal;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.net.MalformedURLException;
import org.apache.maven.repository.internal.util.ConsoleRepositoryListener;
import org.apache.maven.repository.internal.util.ConsoleTransferListener;
import org.codehaus.plexus.PlexusTestCase;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.collection.CollectResult;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.repository.LocalRepository;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
public class RepositorySystemTest
extends PlexusTestCase
{
protected RepositorySystem system;
protected RepositorySystemSession session;
@Override
protected void setUp()
throws Exception
{
system = lookup( RepositorySystem.class );
session = newMavenRepositorySystemSession( system );
}
@Override
protected void tearDown()
throws Exception
{
session = null;
system = null;
super.tearDown();
}
public void testCollectDependencies()
throws Exception
{
String artifactCoords = "ut.simple:artifact:1.0"; // TODO test extension:classifier
Artifact artifact = new DefaultArtifact( artifactCoords );
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot( new Dependency( artifact, "" ) );
collectRequest.addRepository( newTestRepository() );
CollectResult collectResult = system.collectDependencies( session, collectRequest );
assertEquals( 1, collectResult.getRoot().getChildren().size() );
Artifact dep = collectResult.getRoot().getChildren().get( 0 ).getDependency().getArtifact();
assertEquals( "ut.simple", dep.getGroupId() );
assertEquals( "dependency", dep.getArtifactId() );
assertEquals( "1.0", dep.getVersion() );
}
public static RepositorySystemSession newMavenRepositorySystemSession( RepositorySystem system )
{
MavenRepositorySystemSession session = new MavenRepositorySystemSession();
LocalRepository localRepo = new LocalRepository( "target/local-repo" );
session.setLocalRepositoryManager( system.newLocalRepositoryManager( localRepo ) );
session.setTransferListener( new ConsoleTransferListener() );
session.setRepositoryListener( new ConsoleRepositoryListener() );
return session;
}
public static RemoteRepository newTestRepository()
throws MalformedURLException
{
return new RemoteRepository( "repo", "default", getTestFile( "target/test-classes/repo" ).toURI().toURL().toString() );
}
}

View File

@ -0,0 +1,132 @@
package org.apache.maven.repository.internal.util;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.PrintStream;
import org.sonatype.aether.AbstractRepositoryListener;
import org.sonatype.aether.RepositoryEvent;
public class ConsoleRepositoryListener
extends AbstractRepositoryListener
{
private PrintStream out;
public ConsoleRepositoryListener()
{
this( null );
}
public ConsoleRepositoryListener( PrintStream out )
{
this.out = ( out != null ) ? out : System.out;
}
public void artifactDeployed( RepositoryEvent event )
{
println( "artifactDeployed", event.getArtifact() + " to " + event.getRepository() );
}
public void artifactDeploying( RepositoryEvent event )
{
println( "artifactDeploying", event.getArtifact() + " to " + event.getRepository() );
}
public void artifactDescriptorInvalid( RepositoryEvent event )
{
println( "artifactDescriptorInvalid", "for " + event.getArtifact() + ": " + event.getException().getMessage() );
}
public void artifactDescriptorMissing( RepositoryEvent event )
{
println( "artifactDescriptorMissing", "for " + event.getArtifact() );
}
public void artifactInstalled( RepositoryEvent event )
{
println( "artifactInstalled", event.getArtifact() + " to " + event.getFile() );
}
public void artifactInstalling( RepositoryEvent event )
{
println( "artifactInstalling", event.getArtifact() + " to " + event.getFile() );
}
public void artifactResolved( RepositoryEvent event )
{
println( "artifactResolved", event.getArtifact() + " from " + event.getRepository() );
}
public void artifactDownloading( RepositoryEvent event )
{
println( "artifactDownloading", event.getArtifact() + " from " + event.getRepository() );
}
public void artifactDownloaded( RepositoryEvent event )
{
println( "artifactDownloaded", event.getArtifact() + " from " + event.getRepository() );
}
public void artifactResolving( RepositoryEvent event )
{
println( "artifactResolving", event.getArtifact().toString() );
}
public void metadataDeployed( RepositoryEvent event )
{
println( "metadataDeployed", event.getMetadata() + " to " + event.getRepository() );
}
public void metadataDeploying( RepositoryEvent event )
{
println( "metadataDeploying", event.getMetadata() + " to " + event.getRepository() );
}
public void metadataInstalled( RepositoryEvent event )
{
println( "metadataInstalled", event.getMetadata() + " to " + event.getFile() );
}
public void metadataInstalling( RepositoryEvent event )
{
println( "metadataInstalling", event.getMetadata() + " to " + event.getFile() );
}
public void metadataInvalid( RepositoryEvent event )
{
println( "metadataInvalid", event.getMetadata().toString() );
}
public void metadataResolved( RepositoryEvent event )
{
println( "metadataResolved", event.getMetadata() + " from " + event.getRepository() );
}
public void metadataResolving( RepositoryEvent event )
{
println( "metadataResolving", event.getMetadata() + " from " + event.getRepository() );
}
private void println( String event, String message )
{
out.println( "Aether Repository - " + event + ": " + message );
}
}

View File

@ -0,0 +1,186 @@
package org.apache.maven.repository.internal.util;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.sonatype.aether.transfer.AbstractTransferListener;
import org.sonatype.aether.transfer.TransferEvent;
import org.sonatype.aether.transfer.TransferResource;
public class ConsoleTransferListener
extends AbstractTransferListener
{
private PrintStream out;
private Map<TransferResource, Long> downloads = new ConcurrentHashMap<TransferResource, Long>();
private int lastLength;
public ConsoleTransferListener()
{
this( null );
}
public ConsoleTransferListener( PrintStream out )
{
this.out = ( out != null ) ? out : System.out;
}
@Override
public void transferInitiated( TransferEvent event )
{
String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
println( "transferInitiated", message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() );
}
@Override
public void transferProgressed( TransferEvent event )
{
TransferResource resource = event.getResource();
downloads.put( resource, Long.valueOf( event.getTransferredBytes() ) );
StringBuilder buffer = new StringBuilder( 64 );
for ( Map.Entry<TransferResource, Long> entry : downloads.entrySet() )
{
long total = entry.getKey().getContentLength();
long complete = entry.getValue().longValue();
buffer.append( getStatus( complete, total ) ).append( " " );
}
int pad = lastLength - buffer.length();
lastLength = buffer.length();
pad( buffer, pad );
buffer.append( '\r' );
print( "transferProgressed", buffer.toString() );
}
private String getStatus( long complete, long total )
{
if ( total >= 1024 )
{
return toKB( complete ) + "/" + toKB( total ) + " KB ";
}
else if ( total >= 0 )
{
return complete + "/" + total + " B ";
}
else if ( complete >= 1024 )
{
return toKB( complete ) + " KB ";
}
else
{
return complete + " B ";
}
}
private void pad( StringBuilder buffer, int spaces )
{
String block = " ";
while ( spaces > 0 )
{
int n = Math.min( spaces, block.length() );
buffer.append( block, 0, n );
spaces -= n;
}
}
@Override
public void transferSucceeded( TransferEvent event )
{
transferCompleted( event );
TransferResource resource = event.getResource();
long contentLength = event.getTransferredBytes();
if ( contentLength >= 0 )
{
String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" );
String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B";
String throughput = "";
long duration = System.currentTimeMillis() - resource.getTransferStartTime();
if ( duration > 0 )
{
DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
double kbPerSec = ( contentLength / 1024.0 ) / ( duration / 1000.0 );
throughput = " at " + format.format( kbPerSec ) + " KB/sec";
}
println( "transferSucceeded", type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " ("
+ len + throughput + ")" );
}
}
@Override
public void transferFailed( TransferEvent event )
{
transferCompleted( event );
println( "transferFailed", event.getException().getClass() + ": " + event.getException().getMessage() );
}
private void transferCompleted( TransferEvent event )
{
downloads.remove( event.getResource() );
StringBuilder buffer = new StringBuilder( 64 );
pad( buffer, lastLength );
buffer.append( '\r' );
out.println( buffer );
}
@Override
public void transferCorrupted( TransferEvent event )
{
println( "transferCorrupted", event.getException().getClass() + ": " + event.getException().getMessage() );
}
protected long toKB( long bytes )
{
return ( bytes + 1023 ) / 1024;
}
private void println( String event, String message )
{
print( event, message );
out.println();
}
private void print( String event, String message )
{
out.print( "Aether Transfer - " + event );
if ( message != null )
{
out.print( ": " );
out.print( message );
}
}
}

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ut.simple</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>artifact</artifactId>
<name>Simple Unit Test Artifact</name>
<dependencies>
<dependency>
<groupId>ut.simple</groupId>
<artifactId>dependency</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<metadata xmlns="http://maven.apache.org/METADATA/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/METADATA/1.1.0 http://maven.apache.org/xsd/metadata-1.1.0.xsd">
<groupId>ut.simple</groupId>
<artifactId>artifact</artifactId>
<versioning>
<latest>1.0</latest>
<release>1.0</release>
<versions>
<version>1.0</version>
</versions>
<lastUpdated>20111123122038</lastUpdated>
</versioning>
</metadata>

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ut.simple</groupId>
<artifactId>dependency</artifactId>
<version>1.0</version>
<name>Simple Unit Test Dependency</name>
</project>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<metadata xmlns="http://maven.apache.org/METADATA/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/METADATA/1.1.0 http://maven.apache.org/xsd/metadata-1.1.0.xsd">
<groupId>ut.simple</groupId>
<artifactId>dependency</artifactId>
<versioning>
<latest>1.0</latest>
<release>1.0</release>
<versions>
<version>1.0</version>
</versions>
<lastUpdated>20111123122038</lastUpdated>
</versioning>
</metadata>

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ut.simple</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>Simple Unit Test Parent</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>ut.simple</groupId>
<artifactId>dependency</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<metadata xmlns="http://maven.apache.org/METADATA/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/METADATA/1.1.0 http://maven.apache.org/xsd/metadata-1.1.0.xsd">
<groupId>ut.simple</groupId>
<artifactId>parent</artifactId>
<versioning>
<latest>1.0</latest>
<release>1.0</release>
<versions>
<version>1.0</version>
</versions>
<lastUpdated>20111123122038</lastUpdated>
</versioning>
</metadata>