All but the scanning tools are tested with coverage of 97+%, and both location and...
authorJohn Dennis Casey <jdcasey@apache.org>
Thu, 8 Jun 2006 16:18:03 +0000 (16:18 +0000)
committerJohn Dennis Casey <jdcasey@apache.org>
Thu, 8 Jun 2006 16:18:03 +0000 (16:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@412788 13f79535-47bb-0310-9956-ffa450edef68

25 files changed:
pom.xml
src/main/java/org/apache/maven/shared/io/download/DefaultDownloadManager.java
src/main/java/org/apache/maven/shared/io/download/DownloadManager.java
src/main/java/org/apache/maven/shared/io/location/ArtifactLocation.java
src/main/java/org/apache/maven/shared/io/location/ArtifactLocatorStrategy.java [moved from src/main/java/org/apache/maven/shared/io/location/ArtifactBasedLocatorStrategy.java with 58% similarity]
src/main/java/org/apache/maven/shared/io/location/ClasspathResourceLocatorStrategy.java
src/main/java/org/apache/maven/shared/io/location/FileLocation.java [moved from src/main/java/org/apache/maven/shared/io/location/FileBasedLocation.java with 92% similarity]
src/main/java/org/apache/maven/shared/io/location/FileLocatorStrategy.java
src/main/java/org/apache/maven/shared/io/location/Locator.java
src/main/java/org/apache/maven/shared/io/location/URLLocation.java
src/main/java/org/apache/maven/shared/io/location/URLLocatorStrategy.java [moved from src/main/java/org/apache/maven/shared/io/location/UrlBasedLocatorStrategy.java with 76% similarity]
src/main/resources/META-INF/plexus/components.xml
src/test/java/org/apache/maven/shared/io/MockManager.java [new file with mode: 0644]
src/test/java/org/apache/maven/shared/io/TestUtils.java [new file with mode: 0644]
src/test/java/org/apache/maven/shared/io/download/DefaultDownloadManagerTest.java [new file with mode: 0644]
src/test/java/org/apache/maven/shared/io/download/DownloadFailedExceptionTest.java [new file with mode: 0644]
src/test/java/org/apache/maven/shared/io/location/AbstractLocationTest.java [deleted file]
src/test/java/org/apache/maven/shared/io/location/ArtifactLocationTest.java
src/test/java/org/apache/maven/shared/io/location/ArtifactLocatorStrategyTest.java [new file with mode: 0644]
src/test/java/org/apache/maven/shared/io/location/ClasspathResourceLocatorStrategyTest.java [new file with mode: 0644]
src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java [moved from src/test/java/org/apache/maven/shared/io/location/FileBasedLocationTest.java with 83% similarity]
src/test/java/org/apache/maven/shared/io/location/FileLocatorStrategyTest.java
src/test/java/org/apache/maven/shared/io/location/LocatorTest.java
src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java
src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 5f6a8e1..ba25b3c 100644 (file)
--- a/pom.xml
+++ b/pom.xml
   
   <dependencies>
     <dependency>
+      <groupId>easymock</groupId>
+      <artifactId>easymock</artifactId>
+      <version>1.2_Java1.3</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-artifact</artifactId>
       <version>[2.0,)</version>
     </dependency>
   </dependencies>
 
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-clean-plugin</artifactId>
+        <configuration>
+          <fileSets>
+            <fileSet>
+              <directory>${basedir}</directory>
+              <includes>
+                <include>cobertura.ser</include>
+              </includes>
+            </fileSet>
+          </fileSets>
+        </configuration>
+      </plugin>
+      <plugin>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <excludes>
+            <exclude>**/TestUtils*</exclude>
+          </excludes>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
   <reporting>
     <plugins>
       <plugin>
index 9c76d8e..e88c871 100644 (file)
@@ -5,10 +5,13 @@ import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.maven.artifact.manager.WagonManager;
+import org.apache.maven.shared.io.logging.MessageHolder;
 import org.apache.maven.wagon.ConnectionException;
 import org.apache.maven.wagon.ResourceDoesNotExistException;
 import org.apache.maven.wagon.TransferFailedException;
@@ -19,27 +22,43 @@ import org.apache.maven.wagon.authorization.AuthorizationException;
 import org.apache.maven.wagon.events.TransferListener;
 import org.apache.maven.wagon.repository.Repository;
 
-public class DefaultDownloadManager implements DownloadManager
+public class DefaultDownloadManager
+    implements DownloadManager
 {
-    
+
+    public static final String ROLE_HINT = "default";
+
     private WagonManager wagonManager;
-    
+
+    private Map cache = new HashMap();
+
     public DefaultDownloadManager()
     {
     }
-    
+
     public DefaultDownloadManager( WagonManager wagonManager )
     {
         this.wagonManager = wagonManager;
     }
-    
-    public File download( String url ) throws DownloadFailedException
+
+    public File download( String url, MessageHolder messageHolder )
+        throws DownloadFailedException
     {
-        return download( url, Collections.EMPTY_LIST );
+        return download( url, Collections.EMPTY_LIST, messageHolder );
     }
-    
-    public File download( String url, List transferListeners ) throws DownloadFailedException
+
+    public File download( String url, List transferListeners, MessageHolder messageHolder )
+        throws DownloadFailedException
     {
+        File downloaded = (File) cache.get( url );
+
+        if ( downloaded != null && downloaded.exists() )
+        {
+            messageHolder.addMessage( "Using cached download: " + downloaded.getAbsolutePath() );
+
+            return downloaded;
+        }
+
         URL sourceUrl;
         try
         {
@@ -52,44 +71,75 @@ public class DefaultDownloadManager implements DownloadManager
 
         Wagon wagon = null;
 
+        // Retrieve the correct Wagon instance used to download the remote archive
         try
         {
-            // Retrieve the correct Wagon instance used to download the remote archive
             wagon = wagonManager.getWagon( sourceUrl.getProtocol() );
+        }
+        catch ( UnsupportedProtocolException e )
+        {
+            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
+        }
+
+        messageHolder.addMessage( "Using wagon: " + wagon + " to download: " + url );
 
+        try
+        {
             // create the landing file in /tmp for the downloaded source archive
-            File downloaded = File.createTempFile( "source-archive-", null );
+            downloaded = File.createTempFile( "download-", null );
+
+            // delete when the JVM exits, to avoid polluting the temp dir...
             downloaded.deleteOnExit();
+        }
+        catch ( IOException e )
+        {
+            throw new DownloadFailedException( url, "Failed to create temporary file target for download. Reason: "
+                + e.getMessage(), e );
+        }
 
-            // split the download URL into base URL and remote path for connecting, then retrieving.
-            String remotePath = sourceUrl.getPath();
-            String baseUrl = url.substring( 0, url.length() - remotePath.length() );
+        messageHolder.addMessage( "Download target is: " + downloaded.getAbsolutePath() );
 
-            for ( Iterator it = transferListeners.iterator(); it.hasNext(); )
-            {
-                TransferListener listener = (TransferListener) it.next();
-                wagon.addTransferListener( listener );
-            }
+        // split the download URL into base URL and remote path for connecting, then retrieving.
+        String remotePath = sourceUrl.getPath();
+        String baseUrl = url.substring( 0, url.length() - remotePath.length() );
 
-            // connect to the remote site, and retrieve the archive. Note the separate methods in which
-            // base URL and remote path are used.
-            Repository repo = new Repository( sourceUrl.getHost(), baseUrl );
-            
+        for ( Iterator it = transferListeners.iterator(); it.hasNext(); )
+        {
+            TransferListener listener = (TransferListener) it.next();
+            wagon.addTransferListener( listener );
+        }
+
+        // connect to the remote site, and retrieve the archive. Note the separate methods in which
+        // base URL and remote path are used.
+        Repository repo = new Repository( sourceUrl.getHost(), baseUrl );
+
+        messageHolder.addMessage( "Connecting to: " + repo.getHost() + "(baseUrl: " + repo.getUrl() + ")" );
+
+        try
+        {
             wagon.connect( repo, wagonManager.getAuthenticationInfo( repo.getId() ), wagonManager.getProxy( sourceUrl
                 .getProtocol() ) );
-            
-            wagon.get( remotePath, downloaded );
-            
-            return downloaded;
         }
-        catch ( UnsupportedProtocolException e )
+        catch ( ConnectionException e )
         {
             throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
         }
-        catch ( IOException e )
+        catch ( AuthenticationException e )
         {
             throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
         }
+
+        messageHolder.addMessage( "Getting: " + remotePath );
+
+        try
+        {
+            wagon.get( remotePath, downloaded );
+
+            // cache this for later download requests to the same instance...
+            cache.put( url, downloaded );
+
+            return downloaded;
+        }
         catch ( TransferFailedException e )
         {
             throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
@@ -102,14 +152,6 @@ public class DefaultDownloadManager implements DownloadManager
         {
             throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
         }
-        catch ( ConnectionException e )
-        {
-            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
-        }
-        catch ( AuthenticationException e )
-        {
-            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
-        }
         finally
         {
             // ensure the Wagon instance is closed out properly.
@@ -117,11 +159,13 @@ public class DefaultDownloadManager implements DownloadManager
             {
                 try
                 {
+                    messageHolder.addMessage( "Disconnecting." );
+
                     wagon.disconnect();
                 }
                 catch ( ConnectionException e )
                 {
-//                    getLog().debug( "Failed to disconnect wagon for: " + url, e );
+                    messageHolder.addMessage( "Failed to disconnect wagon for: " + url, e );
                 }
 
                 for ( Iterator it = transferListeners.iterator(); it.hasNext(); )
index 867ecc5..e0ee231 100644 (file)
@@ -3,13 +3,16 @@ package org.apache.maven.shared.io.download;
 import java.io.File;
 import java.util.List;
 
+import org.apache.maven.shared.io.logging.MessageHolder;
+
 public interface DownloadManager
 {
+    String ROLE = DownloadManager.class.getName();
 
-    File download( String url )
+    File download( String url, MessageHolder messageHolder )
         throws DownloadFailedException;
 
-    File download( String url, List transferListeners )
+    File download( String url, List transferListeners, MessageHolder messageHolder )
         throws DownloadFailedException;
 
 }
\ No newline at end of file
index 952bd4f..32a6697 100644 (file)
@@ -3,7 +3,7 @@ package org.apache.maven.shared.io.location;
 import org.apache.maven.artifact.Artifact;
 
 public class ArtifactLocation
-    extends FileBasedLocation
+    extends FileLocation
 {
 
     public ArtifactLocation( Artifact artifact, String specification )
@@ -10,7 +10,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.shared.io.logging.MessageHolder;
 
-public class ArtifactBasedLocatorStrategy
+public class ArtifactLocatorStrategy
     implements LocatorStrategy
 {
     private final ArtifactFactory factory;
@@ -23,8 +23,8 @@ public class ArtifactBasedLocatorStrategy
 
     private final List remoteRepositories;
 
-    public ArtifactBasedLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
-                                         ArtifactRepository localRepository, List remoteRepositories )
+    public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
+                                    ArtifactRepository localRepository, List remoteRepositories )
     {
         this.factory = factory;
         this.resolver = resolver;
@@ -32,9 +32,9 @@ public class ArtifactBasedLocatorStrategy
         this.remoteRepositories = remoteRepositories;
     }
 
-    public ArtifactBasedLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
-                                         ArtifactRepository localRepository, List remoteRepositories,
-                                         String defaultArtifactType )
+    public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
+                                    ArtifactRepository localRepository, List remoteRepositories,
+                                    String defaultArtifactType )
     {
         this.factory = factory;
         this.resolver = resolver;
@@ -43,6 +43,11 @@ public class ArtifactBasedLocatorStrategy
         this.defaultArtifactType = defaultArtifactType;
     }
 
+    /**
+     * Assumes artifact identity is given in a set of comma-delimited tokens of
+     * the form: <code>groupId:artifactId:version:type:classifier</code>, where
+     * type and classifier are optional.
+     */
     public Location resolve( String locationSpecification, MessageHolder messageHolder )
     {
         String[] parts = locationSpecification.split( ":" );
@@ -58,7 +63,10 @@ public class ArtifactBasedLocatorStrategy
             String type = defaultArtifactType;
             if ( parts.length > 3 )
             {
-                type = parts[3];
+                if ( parts[3].trim().length() > 0 )
+                {
+                    type = parts[3];
+                }
             }
 
             String classifier = null;
@@ -67,6 +75,16 @@ public class ArtifactBasedLocatorStrategy
                 classifier = parts[4];
             }
 
+            if ( parts.length > 5 )
+            {
+                messageHolder.newMessage().append( "Location specification has unused tokens: \'" );
+
+                for ( int i = 5; i < parts.length; i++ )
+                {
+                    messageHolder.append( ":" + parts[i] );
+                }
+            }
+
             Artifact artifact;
             if ( classifier == null )
             {
@@ -77,25 +95,36 @@ public class ArtifactBasedLocatorStrategy
                 artifact = factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
             }
 
-            messageHolder.append( "Resolving artifact: " + artifact.getId() );
             try
             {
                 resolver.resolve( artifact, remoteRepositories, localRepository );
-                
+
                 if ( artifact.getFile() != null )
                 {
                     location = new ArtifactLocation( artifact, locationSpecification );
                 }
+                else
+                {
+                    messageHolder.addMessage( "Supposedly resolved artifact: " + artifact.getId()
+                        + " does not have an associated file." );
+                }
             }
             catch ( ArtifactResolutionException e )
             {
-                messageHolder.append( e );
+                messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
+                    + locationSpecification, e );
             }
             catch ( ArtifactNotFoundException e )
             {
-                messageHolder.append( e );
+                messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
+                    + locationSpecification, e );
             }
         }
+        else
+        {
+            messageHolder.addMessage( "Invalid artifact specification: \'" + locationSpecification
+                + "\'. Must contain at least three fields, separated by ':'." );
+        }
 
         return location;
     }
index 640942a..d9db38e 100644 (file)
@@ -31,9 +31,6 @@ public class ClasspathResourceLocatorStrategy
 
         URL resource = cloader.getResource( locationSpecification );
 
-        messageHolder.addMessage( "Resolved url: " + resource + " from classloader: " + cloader + " for location: "
-            + locationSpecification );
-        
         Location location = null;
 
         if ( resource != null )
@@ -41,6 +38,11 @@ public class ClasspathResourceLocatorStrategy
             location = new URLLocation( resource, locationSpecification, tempFilePrefix, tempFileSuffix,
                                         tempFileDeleteOnExit );
         }
+        else
+        {
+            messageHolder.addMessage( "Failed to resolve classpath resource: " + locationSpecification
+                + " from classloader: " + cloader );
+        }
 
         return location;
     }
@@ -7,7 +7,7 @@ import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 
 
-public class FileBasedLocation
+public class FileLocation
     implements Location
 {
 
@@ -15,13 +15,13 @@ public class FileBasedLocation
     private FileChannel channel;
     private final String specification;
     
-    public FileBasedLocation( File file, String specification )
+    public FileLocation( File file, String specification )
     {
         this.file = file;
         this.specification = specification;
     }
     
-    protected FileBasedLocation( String specification )
+    protected FileLocation( String specification )
     {
         this.specification = specification;
     }
index 6307447..73772b5 100644 (file)
@@ -1,7 +1,6 @@
 package org.apache.maven.shared.io.location;
 
 import java.io.File;
-import java.io.IOException;
 
 import org.apache.maven.shared.io.logging.MessageHolder;
 
@@ -13,21 +12,11 @@ public class FileLocatorStrategy
     {
         File file = new File( locationSpecification );
         
-        try
-        {
-            File canFile = file.getCanonicalFile();
-            file = canFile;
-        }
-        catch ( IOException e )
-        {
-            messageHolder.addMessage( "Failed to canonicalize: " + file.getAbsolutePath(), e );
-        }
-        
         Location location = null;
         
         if ( file.exists() )
         {
-            location = new FileBasedLocation( file, locationSpecification );
+            location = new FileLocation( file, locationSpecification );
         }
         else
         {
index 3550599..83b4789 100644 (file)
@@ -25,6 +25,11 @@ public final class Locator
         this.strategies = new ArrayList();
     }
     
+    public MessageHolder getMessageHolder()
+    {
+        return messageHolder;
+    }
+    
     public void addStrategy( LocatorStrategy strategy )
     {
         this.strategies.add( strategy );
index c00930c..eb8ac33 100644 (file)
@@ -7,7 +7,7 @@ import java.net.URL;
 import org.codehaus.plexus.util.FileUtils;
 
 public class URLLocation
-    extends FileBasedLocation
+    extends FileLocation
 {
 
     private final URL url;
@@ -5,7 +5,7 @@ import java.net.URL;
 
 import org.apache.maven.shared.io.logging.MessageHolder;
 
-public class UrlBasedLocatorStrategy
+public class URLLocatorStrategy
     implements LocatorStrategy
 {
 
@@ -15,11 +15,11 @@ public class UrlBasedLocatorStrategy
 
     private boolean tempFileDeleteOnExit = true;
 
-    public UrlBasedLocatorStrategy()
+    public URLLocatorStrategy()
     {
     }
 
-    public UrlBasedLocatorStrategy( String tempFilePrefix, String tempFileSuffix, boolean tempFileDeleteOnExit )
+    public URLLocatorStrategy( String tempFilePrefix, String tempFileSuffix, boolean tempFileDeleteOnExit )
     {
         this.tempFilePrefix = tempFilePrefix;
         this.tempFileSuffix = tempFileSuffix;
@@ -30,7 +30,6 @@ public class UrlBasedLocatorStrategy
     {
         Location location = null;
 
-        messageHolder.append( "Building URL from location: " + locationSpecification );
         try
         {
             URL url = new URL( locationSpecification );
@@ -40,7 +39,7 @@ public class UrlBasedLocatorStrategy
         }
         catch ( MalformedURLException e )
         {
-            messageHolder.append( e );
+            messageHolder.addMessage( "Building URL from location: " + locationSpecification, e );
         }
 
         return location;
index a64e6f0..7509482 100644 (file)
@@ -1,9 +1,9 @@
 <component-set>
   <components>
     <component>
-      <role>org.apache.maven.shared.io.DownloadManager</role>
+      <role>org.apache.maven.shared.io.download.DownloadManager</role>
       <role-hint>default</role-hint>
-      <implementation>org.apache.maven.shared.io.DefaultDownloadManager</implementation>
+      <implementation>org.apache.maven.shared.io.download.DefaultDownloadManager</implementation>
       <requirements>
         <requirement>
           <role>org.apache.maven.artifact.manager.WagonManager</role>
diff --git a/src/test/java/org/apache/maven/shared/io/MockManager.java b/src/test/java/org/apache/maven/shared/io/MockManager.java
new file mode 100644 (file)
index 0000000..6d67a3c
--- /dev/null
@@ -0,0 +1,39 @@
+package org.apache.maven.shared.io;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.easymock.MockControl;
+
+public class MockManager
+{
+    
+    private List mockControls = new ArrayList();
+    
+    public void add( MockControl control )
+    {
+        mockControls.add( control );
+    }
+    
+    public void replayAll()
+    {
+        for ( Iterator it = mockControls.iterator(); it.hasNext(); )
+        {
+            MockControl control = (MockControl) it.next();
+            
+            control.replay();
+        }
+    }
+    
+    public void verifyAll()
+    {
+        for ( Iterator it = mockControls.iterator(); it.hasNext(); )
+        {
+            MockControl control = (MockControl) it.next();
+            
+            control.verify();
+        }
+    }
+
+}
diff --git a/src/test/java/org/apache/maven/shared/io/TestUtils.java b/src/test/java/org/apache/maven/shared/io/TestUtils.java
new file mode 100644 (file)
index 0000000..f2dbc8d
--- /dev/null
@@ -0,0 +1,65 @@
+package org.apache.maven.shared.io;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.codehaus.plexus.util.IOUtil;
+
+public final class TestUtils
+{
+
+    private TestUtils()
+    {
+    }
+
+    public static void writeToFile( File file, String testStr )
+        throws IOException
+    {
+        FileWriter fw = null;
+        try
+        {
+            fw = new FileWriter( file );
+            fw.write( testStr );
+        }
+        finally
+        {
+            IOUtil.close( fw );
+        }
+    }
+    
+    public static String readFile( File file ) throws IOException
+    {
+        StringBuffer buffer = new StringBuffer();
+        
+        BufferedReader reader = new BufferedReader( new FileReader( file ) );
+        
+        String line = null;
+        
+        while( ( line = reader.readLine() ) != null )
+        {
+            if ( buffer.length() > 0 )
+            {
+                buffer.append( '\n' );
+            }
+            
+            buffer.append( line );
+        }
+        
+        return buffer.toString();
+    }
+
+    public static String toString( Throwable error )
+    {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter( sw );
+        
+        error.printStackTrace( pw );
+        
+        return sw.toString();
+    }
+}
diff --git a/src/test/java/org/apache/maven/shared/io/download/DefaultDownloadManagerTest.java b/src/test/java/org/apache/maven/shared/io/download/DefaultDownloadManagerTest.java
new file mode 100644 (file)
index 0000000..a3f9b45
--- /dev/null
@@ -0,0 +1,594 @@
+package org.apache.maven.shared.io.download;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+
+import org.apache.maven.artifact.manager.WagonManager;
+import org.apache.maven.shared.io.MockManager;
+import org.apache.maven.shared.io.TestUtils;
+import org.apache.maven.shared.io.logging.DefaultMessageHolder;
+import org.apache.maven.shared.io.logging.MessageHolder;
+import org.apache.maven.wagon.ConnectionException;
+import org.apache.maven.wagon.ResourceDoesNotExistException;
+import org.apache.maven.wagon.TransferFailedException;
+import org.apache.maven.wagon.UnsupportedProtocolException;
+import org.apache.maven.wagon.Wagon;
+import org.apache.maven.wagon.authentication.AuthenticationException;
+import org.apache.maven.wagon.authentication.AuthenticationInfo;
+import org.apache.maven.wagon.authorization.AuthorizationException;
+import org.apache.maven.wagon.events.TransferListener;
+import org.apache.maven.wagon.proxy.ProxyInfo;
+import org.apache.maven.wagon.repository.Repository;
+import org.codehaus.plexus.PlexusTestCase;
+import org.easymock.MockControl;
+
+public class DefaultDownloadManagerTest
+    extends PlexusTestCase
+{
+
+    private MockManager mockManager;
+
+    private MockControl wagonManagerControl;
+
+    private WagonManager wagonManager;
+
+    private MockControl wagonControl;
+
+    private Wagon wagon;
+
+    public void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        mockManager = new MockManager();
+
+        wagonManagerControl = MockControl.createControl( WagonManager.class );
+        mockManager.add( wagonManagerControl );
+
+        wagonManager = (WagonManager) wagonManagerControl.getMock();
+
+        wagonControl = MockControl.createControl( Wagon.class );
+        mockManager.add( wagonControl );
+
+        wagon = (Wagon) wagonControl.getMock();
+    }
+
+    public void testShouldConstructWithNoParamsAndHaveNonNullMessageHolder()
+    {
+        new DefaultDownloadManager();
+    }
+
+    public void testShouldConstructWithWagonManager()
+    {
+        MockManager mockManager = new MockManager();
+
+        MockControl ctl = MockControl.createControl( WagonManager.class );
+        mockManager.add( ctl );
+
+        WagonManager wagonManager = (WagonManager) ctl.getMock();
+
+        mockManager.replayAll();
+
+        new DefaultDownloadManager( wagonManager );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldLookupInstanceDefaultRoleHint()
+        throws Exception
+    {
+        lookup( DownloadManager.ROLE, DefaultDownloadManager.ROLE_HINT );
+    }
+
+    public void testShouldFailToDownloadMalformedURL()
+    {
+        MockManager mockManager = new MockManager();
+
+        MockControl ctl = MockControl.createControl( WagonManager.class );
+        mockManager.add( ctl );
+
+        WagonManager wagonManager = (WagonManager) ctl.getMock();
+
+        mockManager.replayAll();
+
+        DownloadManager mgr = new DefaultDownloadManager( wagonManager );
+
+        try
+        {
+            mgr.download( "://nothing.com/index.html", new DefaultMessageHolder() );
+
+            fail( "Should not download with invalid URL." );
+        }
+        catch ( DownloadFailedException e )
+        {
+            assertTrue( e.getMessage().indexOf( "invalid URL" ) > -1 );
+        }
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldDownloadFromTempFileWithNoTransferListeners()
+        throws IOException, DownloadFailedException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupDefaultMockConfiguration();
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldDownloadFromTempFileTwiceAndUseCache()
+        throws IOException, DownloadFailedException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupDefaultMockConfiguration();
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        File first = downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
+
+        MessageHolder mh = new DefaultMessageHolder();
+
+        File second = downloadManager.download( tempFile.toURL().toExternalForm(), mh );
+
+        assertSame( first, second );
+        assertEquals( 1, mh.size() );
+        assertTrue( mh.render().indexOf( "Using cached" ) > -1 );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldDownloadFromTempFileWithOneTransferListener()
+        throws IOException, DownloadFailedException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupDefaultMockConfiguration();
+
+        MockControl transferListenerControl = MockControl.createControl( TransferListener.class );
+        mockManager.add( transferListenerControl );
+
+        TransferListener transferListener = (TransferListener) transferListenerControl.getMock();
+
+        wagon.addTransferListener( transferListener );
+
+        wagon.removeTransferListener( transferListener );
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        downloadManager.download( tempFile.toURL().toExternalForm(), Collections.singletonList( transferListener ),
+                                  new DefaultMessageHolder() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFailToDownloadWhenWagonProtocolNotFound()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupMocksWithWagonManagerGetException( new UnsupportedProtocolException( "not supported" ) );
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        try
+        {
+            downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
+
+            fail( "should have failed to retrieve wagon." );
+        }
+        catch ( DownloadFailedException e )
+        {
+            assertTrue( TestUtils.toString( e ).indexOf( "UnsupportedProtocolException" ) > -1 );
+        }
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFailToDownloadWhenWagonConnectThrowsConnectionException()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupMocksWithWagonConnectionException( new ConnectionException( "connect error" ) );
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        try
+        {
+            downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
+
+            fail( "should have failed to connect wagon." );
+        }
+        catch ( DownloadFailedException e )
+        {
+            assertTrue( TestUtils.toString( e ).indexOf( "ConnectionException" ) > -1 );
+        }
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFailToDownloadWhenWagonConnectThrowsAuthenticationException()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupMocksWithWagonConnectionException( new AuthenticationException( "bad credentials" ) );
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        try
+        {
+            downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
+
+            fail( "should have failed to connect wagon." );
+        }
+        catch ( DownloadFailedException e )
+        {
+            assertTrue( TestUtils.toString( e ).indexOf( "AuthenticationException" ) > -1 );
+        }
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFailToDownloadWhenWagonGetThrowsTransferFailedException()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupMocksWithWagonGetException( new TransferFailedException( "bad transfer" ) );
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        try
+        {
+            downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
+
+            fail( "should have failed to get resource." );
+        }
+        catch ( DownloadFailedException e )
+        {
+            assertTrue( TestUtils.toString( e ).indexOf( "TransferFailedException" ) > -1 );
+        }
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFailToDownloadWhenWagonGetThrowsResourceDoesNotExistException()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupMocksWithWagonGetException( new ResourceDoesNotExistException( "bad resource" ) );
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        try
+        {
+            downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
+
+            fail( "should have failed to get resource." );
+        }
+        catch ( DownloadFailedException e )
+        {
+            assertTrue( TestUtils.toString( e ).indexOf( "ResourceDoesNotExistException" ) > -1 );
+        }
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFailToDownloadWhenWagonGetThrowsAuthorizationException()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupMocksWithWagonGetException( new AuthorizationException( "bad transfer" ) );
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        try
+        {
+            downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
+
+            fail( "should have failed to get resource." );
+        }
+        catch ( DownloadFailedException e )
+        {
+            assertTrue( TestUtils.toString( e ).indexOf( "AuthorizationException" ) > -1 );
+        }
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFailToDownloadWhenWagonDisconnectThrowsConnectionException()
+        throws IOException, DownloadFailedException
+    {
+        File tempFile = File.createTempFile( "download-source", "test" );
+        tempFile.deleteOnExit();
+
+        setupMocksWithWagonDisconnectException( new ConnectionException( "not connected" ) );
+
+        mockManager.replayAll();
+
+        DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
+
+        MessageHolder mh = new DefaultMessageHolder();
+        
+        downloadManager.download( tempFile.toURL().toExternalForm(), mh );
+        
+        assertTrue( mh.render().indexOf( "ConnectionException" ) > -1 );
+
+        mockManager.verifyAll();
+    }
+
+    private void setupDefaultMockConfiguration()
+    {
+        try
+        {
+            wagonManager.getWagon( "file" );
+            wagonManagerControl.setReturnValue( wagon );
+        }
+        catch ( UnsupportedProtocolException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        wagonManager.getAuthenticationInfo( "" );
+        wagonManagerControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        wagonManagerControl.setReturnValue( null );
+
+        wagonManager.getProxy( "" );
+        wagonManagerControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        wagonManagerControl.setReturnValue( null );
+
+        try
+        {
+            wagon.connect( new Repository(), new AuthenticationInfo(), new ProxyInfo() );
+            wagonControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        }
+        catch ( ConnectionException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( AuthenticationException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        try
+        {
+            wagon.get( "file:///some/path", new File( "." ) );
+            wagonControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        }
+        catch ( TransferFailedException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( AuthorizationException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        try
+        {
+            wagon.disconnect();
+        }
+        catch ( ConnectionException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+    }
+
+    private void setupMocksWithWagonManagerGetException( Throwable error )
+    {
+        try
+        {
+            wagonManager.getWagon( "file" );
+            wagonManagerControl.setThrowable( error );
+        }
+        catch ( UnsupportedProtocolException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+    }
+
+    private void setupMocksWithWagonConnectionException( Throwable error )
+    {
+        try
+        {
+            wagonManager.getWagon( "file" );
+            wagonManagerControl.setReturnValue( wagon );
+        }
+        catch ( UnsupportedProtocolException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        wagonManager.getAuthenticationInfo( "" );
+        wagonManagerControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        wagonManagerControl.setReturnValue( null );
+
+        wagonManager.getProxy( "" );
+        wagonManagerControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        wagonManagerControl.setReturnValue( null );
+
+        try
+        {
+            wagon.connect( new Repository(), new AuthenticationInfo(), new ProxyInfo() );
+            wagonControl.setMatcher( MockControl.ALWAYS_MATCHER );
+            wagonControl.setThrowable( error );
+        }
+        catch ( ConnectionException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( AuthenticationException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+    }
+
+    private void setupMocksWithWagonGetException( Throwable error )
+    {
+        try
+        {
+            wagonManager.getWagon( "file" );
+            wagonManagerControl.setReturnValue( wagon );
+        }
+        catch ( UnsupportedProtocolException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        wagonManager.getAuthenticationInfo( "" );
+        wagonManagerControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        wagonManagerControl.setReturnValue( null );
+
+        wagonManager.getProxy( "" );
+        wagonManagerControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        wagonManagerControl.setReturnValue( null );
+
+        try
+        {
+            wagon.connect( new Repository(), new AuthenticationInfo(), new ProxyInfo() );
+            wagonControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        }
+        catch ( ConnectionException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( AuthenticationException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        try
+        {
+            wagon.get( "file:///some/path", new File( "." ) );
+            wagonControl.setMatcher( MockControl.ALWAYS_MATCHER );
+            wagonControl.setThrowable( error );
+        }
+        catch ( TransferFailedException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( AuthorizationException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        try
+        {
+            wagon.disconnect();
+        }
+        catch ( ConnectionException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+    }
+
+    private void setupMocksWithWagonDisconnectException( Throwable error )
+    {
+        try
+        {
+            wagonManager.getWagon( "file" );
+            wagonManagerControl.setReturnValue( wagon );
+        }
+        catch ( UnsupportedProtocolException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        wagonManager.getAuthenticationInfo( "" );
+        wagonManagerControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        wagonManagerControl.setReturnValue( null );
+
+        wagonManager.getProxy( "" );
+        wagonManagerControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        wagonManagerControl.setReturnValue( null );
+
+        try
+        {
+            wagon.connect( new Repository(), new AuthenticationInfo(), new ProxyInfo() );
+            wagonControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        }
+        catch ( ConnectionException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( AuthenticationException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        try
+        {
+            wagon.get( "file:///some/path", new File( "." ) );
+            wagonControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        }
+        catch ( TransferFailedException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+        catch ( AuthorizationException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+
+        try
+        {
+            wagon.disconnect();
+            wagonControl.setThrowable( error );
+        }
+        catch ( ConnectionException e )
+        {
+            fail( "This shouldn't happen!!" );
+        }
+    }
+}
diff --git a/src/test/java/org/apache/maven/shared/io/download/DownloadFailedExceptionTest.java b/src/test/java/org/apache/maven/shared/io/download/DownloadFailedExceptionTest.java
new file mode 100644 (file)
index 0000000..351963c
--- /dev/null
@@ -0,0 +1,25 @@
+package org.apache.maven.shared.io.download;
+
+import junit.framework.TestCase;
+
+public class DownloadFailedExceptionTest
+    extends TestCase
+{
+    
+    public void testShouldConstructWithUrlAndMessage()
+    {
+        new DownloadFailedException( "http://www.google.com", "can't find." );
+    }
+
+    public void testShouldConstructWithUrlMessageAndException()
+    {
+        new DownloadFailedException( "http://www.google.com", "can't find.", new NullPointerException() );
+    }
+
+    public void testShouldRetrieveUrlFromConstructor()
+    {
+        String url = "http://www.google.com";
+        assertEquals( url, new DownloadFailedException( url, "can't find." ).getUrl() );
+    }
+
+}
diff --git a/src/test/java/org/apache/maven/shared/io/location/AbstractLocationTest.java b/src/test/java/org/apache/maven/shared/io/location/AbstractLocationTest.java
deleted file mode 100644 (file)
index 5a4d5ff..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-package org.apache.maven.shared.io.location;
-
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-
-import org.codehaus.plexus.util.IOUtil;
-
-import junit.framework.TestCase;
-
-public class AbstractLocationTest
-    extends TestCase
-{
-
-    public AbstractLocationTest()
-    {
-        super();
-    }
-
-    protected void writeToFile( File file, String testStr )
-        throws IOException
-    {
-        FileWriter fw = null;
-        try
-        {
-            fw = new FileWriter( file );
-            fw.write( testStr );
-        }
-        finally
-        {
-            IOUtil.close( fw );
-        }        
-    }
-
-}
\ No newline at end of file
index d7b4a2b..6ff94aa 100644 (file)
@@ -3,19 +3,23 @@ package org.apache.maven.shared.io.location;
 import java.io.File;
 import java.io.IOException;
 
+import junit.framework.TestCase;
+
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.DefaultArtifact;
 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
 import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.shared.io.TestUtils;
 
 public class ArtifactLocationTest
-    extends AbstractLocationTest
+    extends TestCase
 {
     
     public void testShouldConstructFromTempFileSpecification()
         throws IOException
     {
         File f = File.createTempFile( "artifact-location.", ".test" );
+        f.deleteOnExit();
 
         Artifact a = new DefaultArtifact( "group", "artifact", VersionRange.createFromVersion( "1" ), null, "jar",
                                           null, new DefaultArtifactHandler() );
@@ -31,10 +35,11 @@ public class ArtifactLocationTest
         throws IOException
     {
         File f = File.createTempFile( "url-location.", ".test" );
+        f.deleteOnExit();
 
         String testStr = "This is a test";
 
-        writeToFile( f, testStr );
+        TestUtils.writeToFile( f, testStr );
 
         Artifact a = new DefaultArtifact( "group", "artifact", VersionRange.createFromVersion( "1" ), null, "jar",
                                           null, new DefaultArtifactHandler() );
diff --git a/src/test/java/org/apache/maven/shared/io/location/ArtifactLocatorStrategyTest.java b/src/test/java/org/apache/maven/shared/io/location/ArtifactLocatorStrategyTest.java
new file mode 100644 (file)
index 0000000..1f756bf
--- /dev/null
@@ -0,0 +1,548 @@
+package org.apache.maven.shared.io.location;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+
+import junit.framework.TestCase;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.shared.io.MockManager;
+import org.apache.maven.shared.io.logging.DefaultMessageHolder;
+import org.apache.maven.shared.io.logging.MessageHolder;
+import org.easymock.MockControl;
+
+public class ArtifactLocatorStrategyTest
+    extends TestCase
+{
+    
+    private MockManager mockManager = new MockManager();
+
+    private MockControl factoryControl;
+
+    private ArtifactFactory factory;
+
+    private MockControl resolverControl;
+
+    private ArtifactResolver resolver;
+
+    private MockControl localRepositoryControl;
+
+    private ArtifactRepository localRepository;
+
+    public void setUp()
+    {
+        factoryControl = MockControl.createControl( ArtifactFactory.class );
+        mockManager.add( factoryControl );
+
+        factory = (ArtifactFactory) factoryControl.getMock();
+
+        resolverControl = MockControl.createControl( ArtifactResolver.class );
+        mockManager.add( resolverControl );
+
+        resolver = (ArtifactResolver) resolverControl.getMock();
+
+        localRepositoryControl = MockControl.createControl( ArtifactRepository.class );
+        mockManager.add( localRepositoryControl );
+
+        localRepository = (ArtifactRepository) localRepositoryControl.getMock();
+    }
+
+    public void testShouldConstructWithoutDefaultArtifactType()
+    {
+        mockManager.replayAll();
+
+        new ArtifactLocatorStrategy( factory, resolver, localRepository, Collections.EMPTY_LIST );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldConstructWithDefaultArtifactType()
+    {
+        mockManager.replayAll();
+
+        new ArtifactLocatorStrategy( factory, resolver, localRepository, Collections.EMPTY_LIST, "zip" );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFailToResolveSpecWithOneToken()
+    {
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST, "zip" );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "one-token", mh );
+
+        assertNull( location );
+        assertEquals( 1, mh.size() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFailToResolveSpecWithTwoTokens()
+    {
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST, "zip" );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "two:tokens", mh );
+
+        assertNull( location );
+        assertEquals( 1, mh.size() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldResolveSpecWithThreeTokensUsingDefaultType()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "artifact-location.", ".temp" );
+        tempFile.deleteOnExit();
+
+        MockControl artifactControl = MockControl.createControl( Artifact.class );
+        mockManager.add( artifactControl );
+
+        Artifact artifact = (Artifact) artifactControl.getMock();
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+
+        factory.createArtifact( "group", "artifact", "version", null, "jar" );
+        factoryControl.setReturnValue( artifact );
+
+        try
+        {
+            resolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "group:artifact:version", mh );
+
+        assertNotNull( location );
+        assertEquals( 0, mh.size() );
+
+        assertSame( tempFile, location.getFile() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldResolveSpecWithThreeTokensUsingCustomizedDefaultType()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "artifact-location.", ".temp" );
+        tempFile.deleteOnExit();
+
+        MockControl artifactControl = MockControl.createControl( Artifact.class );
+        mockManager.add( artifactControl );
+
+        Artifact artifact = (Artifact) artifactControl.getMock();
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+
+        factory.createArtifact( "group", "artifact", "version", null, "zip" );
+        factoryControl.setReturnValue( artifact );
+
+        try
+        {
+            resolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST, "zip" );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "group:artifact:version", mh );
+
+        assertNotNull( location );
+        assertEquals( 0, mh.size() );
+
+        assertSame( tempFile, location.getFile() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldResolveSpecWithFourTokens()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "artifact-location.", ".temp" );
+        tempFile.deleteOnExit();
+
+        MockControl artifactControl = MockControl.createControl( Artifact.class );
+        mockManager.add( artifactControl );
+
+        Artifact artifact = (Artifact) artifactControl.getMock();
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+
+        factory.createArtifact( "group", "artifact", "version", null, "zip" );
+        factoryControl.setReturnValue( artifact );
+
+        try
+        {
+            resolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "group:artifact:version:zip", mh );
+
+        assertNotNull( location );
+        assertEquals( 0, mh.size() );
+
+        assertSame( tempFile, location.getFile() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldResolveSpecWithFiveTokens()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "artifact-location.", ".temp" );
+        tempFile.deleteOnExit();
+
+        MockControl artifactControl = MockControl.createControl( Artifact.class );
+        mockManager.add( artifactControl );
+
+        Artifact artifact = (Artifact) artifactControl.getMock();
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+
+        factory.createArtifactWithClassifier( "group", "artifact", "version", "zip", "classifier" );
+        factoryControl.setReturnValue( artifact );
+
+        try
+        {
+            resolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "group:artifact:version:zip:classifier", mh );
+
+        assertNotNull( location );
+        assertEquals( 0, mh.size() );
+
+        assertSame( tempFile, location.getFile() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldResolveSpecWithFiveTokensAndEmptyTypeToken()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "artifact-location.", ".temp" );
+        tempFile.deleteOnExit();
+
+        MockControl artifactControl = MockControl.createControl( Artifact.class );
+        mockManager.add( artifactControl );
+
+        Artifact artifact = (Artifact) artifactControl.getMock();
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+
+        factory.createArtifactWithClassifier( "group", "artifact", "version", "jar", "classifier" );
+        factoryControl.setReturnValue( artifact );
+
+        try
+        {
+            resolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "group:artifact:version::classifier", mh );
+
+        assertNotNull( location );
+        assertEquals( 0, mh.size() );
+
+        assertSame( tempFile, location.getFile() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldResolveSpecWithMoreThanFiveTokens()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "artifact-location.", ".temp" );
+        tempFile.deleteOnExit();
+
+        MockControl artifactControl = MockControl.createControl( Artifact.class );
+        mockManager.add( artifactControl );
+
+        Artifact artifact = (Artifact) artifactControl.getMock();
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+        artifact.getFile();
+        artifactControl.setReturnValue( tempFile );
+
+        factory.createArtifactWithClassifier( "group", "artifact", "version", "zip", "classifier" );
+        factoryControl.setReturnValue( artifact );
+
+        try
+        {
+            resolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "group:artifact:version:zip:classifier:six:seven", mh );
+
+        assertNotNull( location );
+        assertEquals( 1, mh.size() );
+
+        assertTrue( mh.render().indexOf( ":six:seven" ) > -1 );
+
+        assertSame( tempFile, location.getFile() );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldNotResolveSpecToArtifactWithNullFile()
+        throws IOException
+    {
+        MockControl artifactControl = MockControl.createControl( Artifact.class );
+        mockManager.add( artifactControl );
+
+        Artifact artifact = (Artifact) artifactControl.getMock();
+        artifact.getFile();
+        artifactControl.setReturnValue( null );
+        artifact.getId();
+        artifactControl.setReturnValue( "<some-artifact-id>" );
+
+        factory.createArtifact( "group", "artifact", "version", null, "jar" );
+        factoryControl.setReturnValue( artifact );
+
+        try
+        {
+            resolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "group:artifact:version", mh );
+
+        assertNull( location );
+        assertEquals( 1, mh.size() );
+
+        assertTrue( mh.render().indexOf( "<some-artifact-id>" ) > -1 );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldNotResolveWhenArtifactNotFoundExceptionThrown()
+        throws IOException
+    {
+        MockControl artifactControl = MockControl.createControl( Artifact.class );
+        mockManager.add( artifactControl );
+
+        Artifact artifact = (Artifact) artifactControl.getMock();
+
+        artifact.getId();
+        artifactControl.setReturnValue( "<some-artifact-id>" );
+
+        factory.createArtifact( "group", "artifact", "version", null, "jar" );
+        factoryControl.setReturnValue( artifact );
+
+        try
+        {
+            resolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
+            resolverControl.setThrowable( new ArtifactNotFoundException( "not found", "group", "artifact", "version",
+                                                                         "jar", Collections.EMPTY_LIST,
+                                                                         "http://nowhere.com", Collections.EMPTY_LIST,
+                                                                         new NullPointerException() ) );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "group:artifact:version", mh );
+
+        assertNull( location );
+        assertEquals( 1, mh.size() );
+
+        assertTrue( mh.render().indexOf( "<some-artifact-id>" ) > -1 );
+        assertTrue( mh.render().indexOf( "not found" ) > -1 );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldNotResolveWhenArtifactResolutionExceptionThrown()
+        throws IOException
+    {
+        MockControl artifactControl = MockControl.createControl( Artifact.class );
+        mockManager.add( artifactControl );
+
+        Artifact artifact = (Artifact) artifactControl.getMock();
+
+        artifact.getId();
+        artifactControl.setReturnValue( "<some-artifact-id>" );
+
+        factory.createArtifact( "group", "artifact", "version", null, "jar" );
+        factoryControl.setReturnValue( artifact );
+
+        try
+        {
+            resolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
+            resolverControl.setThrowable( new ArtifactResolutionException( "resolution failed", "group", "artifact",
+                                                                           "version", "jar", Collections.EMPTY_LIST,
+                                                                           Collections.EMPTY_LIST,
+                                                                           new NullPointerException() ) );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            // should never happen
+            fail( "This should NEVER happen. It's a mock!" );
+        }
+
+        mockManager.replayAll();
+
+        LocatorStrategy strategy = new ArtifactLocatorStrategy( factory, resolver, localRepository,
+                                                                Collections.EMPTY_LIST );
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = strategy.resolve( "group:artifact:version", mh );
+
+        assertNull( location );
+        assertEquals( 1, mh.size() );
+
+        assertTrue( mh.render().indexOf( "<some-artifact-id>" ) > -1 );
+        assertTrue( mh.render().indexOf( "resolution failed" ) > -1 );
+
+        mockManager.verifyAll();
+    }
+
+}
diff --git a/src/test/java/org/apache/maven/shared/io/location/ClasspathResourceLocatorStrategyTest.java b/src/test/java/org/apache/maven/shared/io/location/ClasspathResourceLocatorStrategyTest.java
new file mode 100644 (file)
index 0000000..06ad891
--- /dev/null
@@ -0,0 +1,40 @@
+package org.apache.maven.shared.io.location;
+
+import org.apache.maven.shared.io.logging.DefaultMessageHolder;
+import org.apache.maven.shared.io.logging.MessageHolder;
+
+import junit.framework.TestCase;
+
+public class ClasspathResourceLocatorStrategyTest
+    extends TestCase
+{
+    
+    public void testShouldConstructWithNoParams()
+    {
+        new ClasspathResourceLocatorStrategy();
+    }
+
+    public void testShouldConstructWithTempFileOptions()
+    {
+        new ClasspathResourceLocatorStrategy( "prefix.", ".suffix", true );
+    }
+
+    public void testShouldFailToResolveMissingClasspathResource()
+    {
+        MessageHolder mh = new DefaultMessageHolder();
+        Location location = new ClasspathResourceLocatorStrategy().resolve( "/some/missing/path", mh );
+        
+        assertNull( location );
+        assertEquals( 1, mh.size() );
+    }
+    
+    public void testShouldResolveExistingClasspathResourceWithoutPrecedingSlash()
+    {
+        MessageHolder mh = new DefaultMessageHolder();
+        Location location = new ClasspathResourceLocatorStrategy().resolve( "META-INF/maven/test.properties", mh );
+        
+        assertNotNull( location );
+        assertEquals( 0, mh.size() );
+    }
+    
+}
@@ -4,8 +4,12 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 
-public class FileBasedLocationTest
-    extends AbstractLocationTest
+import org.apache.maven.shared.io.TestUtils;
+
+import junit.framework.TestCase;
+
+public class FileLocationTest
+    extends TestCase
 {
     
     public void testShouldConstructWithFileThenRetrieveSameFile() throws IOException
@@ -13,7 +17,7 @@ public class FileBasedLocationTest
         File file = File.createTempFile( "test.", ".file-location" );
         file.deleteOnExit();
         
-        FileBasedLocation location = new FileBasedLocation( file, file.getAbsolutePath() );
+        FileLocation location = new FileLocation( file, file.getAbsolutePath() );
         
         assertSame( file, location.getFile() );
         assertEquals( file.getAbsolutePath(), location.getSpecification() );
@@ -26,9 +30,9 @@ public class FileBasedLocationTest
         
         String testStr = "This is a test";
         
-        writeToFile( file, testStr );
+        TestUtils.writeToFile( file, testStr );
         
-        FileBasedLocation location = new FileBasedLocation( file, file.getAbsolutePath() );
+        FileLocation location = new FileLocation( file, file.getAbsolutePath() );
         
         location.open();
         
@@ -45,9 +49,9 @@ public class FileBasedLocationTest
         
         String testStr = "This is a test";
         
-        writeToFile( file, testStr );
+        TestUtils.writeToFile( file, testStr );
         
-        FileBasedLocation location = new FileBasedLocation( file, file.getAbsolutePath() );
+        FileLocation location = new FileLocation( file, file.getAbsolutePath() );
         
         location.open();
         
@@ -64,9 +68,9 @@ public class FileBasedLocationTest
         
         String testStr = "This is a test";
         
-        writeToFile( file, testStr );
+        TestUtils.writeToFile( file, testStr );
         
-        FileBasedLocation location = new FileBasedLocation( file, file.getAbsolutePath() );
+        FileLocation location = new FileLocation( file, file.getAbsolutePath() );
         
         location.open();
         
@@ -120,7 +124,7 @@ public class FileBasedLocationTest
         assertEquals( file.getAbsolutePath(), location.getSpecification() );
     }
     
-    private static final class TestFileLocation extends FileBasedLocation
+    private static final class TestFileLocation extends FileLocation
     {
 
         TestFileLocation( String specification )
index f287695..25d2007 100644 (file)
@@ -8,14 +8,6 @@ import org.apache.maven.shared.io.logging.MessageHolder;
 
 import junit.framework.TestCase;
 
-/*
- * NOTE: Coverage reporting shows that this strategy is a bit on the low side.
- * However, looking deeper it becomes apparent that the reason for this is the 
- * try/catch when trying to canonicalize the file...and I haven't been able to 
- * find a reliable way to break canonicalization. Either way, it will only change
- * the message output, not the resulting location's reachability...so this is
- * non-critical.
- */
 public class FileLocatorStrategyTest
     extends TestCase
 {
@@ -51,6 +43,8 @@ public class FileLocatorStrategyTest
         
         assertNull( location );
         
+        System.out.println( mh.render() );
+        
         assertEquals( 1, mh.size() );
     }
 
index 5b2dbeb..a1862ec 100644 (file)
 package org.apache.maven.shared.io.location;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.shared.io.MockManager;
+import org.apache.maven.shared.io.logging.DefaultMessageHolder;
+import org.apache.maven.shared.io.logging.MessageHolder;
+import org.easymock.MockControl;
+
 import junit.framework.TestCase;
 
 public class LocatorTest
     extends TestCase
 {
+
+    public void testShouldConstructWithNoParams()
+    {
+        new Locator();
+    }
+
+    public void testShouldConstructWithStrategyStackAndMessageHolder()
+    {
+        new Locator( Collections.EMPTY_LIST, new DefaultMessageHolder() );
+    }
+
+    public void testShouldAllowModificationOfStrategiesAfterConstructionWithUnmodifiableStack()
+    {
+        Locator locator = new Locator( Collections.unmodifiableList( Collections.EMPTY_LIST ),
+                                       new DefaultMessageHolder() );
+
+        locator.addStrategy( new FileLocatorStrategy() );
+
+        assertEquals( 1, locator.getStrategies().size() );
+    }
+
+    public void testShouldRetrieveNonNullMessageHolderWhenConstructedWithoutParams()
+    {
+        assertNotNull( new Locator().getMessageHolder() );
+    }
+
+    public void testSetStrategiesShouldClearAnyPreExistingStrategiesOut()
+    {
+        MockManager mgr = new MockManager();
+
+        MockControl originalStrategyControl = MockControl.createControl( LocatorStrategy.class );
+
+        mgr.add( originalStrategyControl );
+
+        LocatorStrategy originalStrategy = (LocatorStrategy) originalStrategyControl.getMock();
+
+        MockControl replacementStrategyControl = MockControl.createControl( LocatorStrategy.class );
+
+        mgr.add( replacementStrategyControl );
+
+        LocatorStrategy replacementStrategy = (LocatorStrategy) replacementStrategyControl.getMock();
+
+        mgr.replayAll();
+
+        Locator locator = new Locator();
+        locator.addStrategy( originalStrategy );
+
+        locator.setStrategies( Collections.singletonList( replacementStrategy ) );
+
+        List strategies = locator.getStrategies();
+
+        assertFalse( strategies.contains( originalStrategy ) );
+        assertTrue( strategies.contains( replacementStrategy ) );
+
+        mgr.verifyAll();
+    }
+
+    public void testShouldRemovePreviouslyAddedStrategy()
+    {
+        MockManager mgr = new MockManager();
+
+        MockControl originalStrategyControl = MockControl.createControl( LocatorStrategy.class );
+
+        mgr.add( originalStrategyControl );
+
+        LocatorStrategy originalStrategy = (LocatorStrategy) originalStrategyControl.getMock();
+
+        mgr.replayAll();
+
+        Locator locator = new Locator();
+        locator.addStrategy( originalStrategy );
+
+        List strategies = locator.getStrategies();
+
+        assertTrue( strategies.contains( originalStrategy ) );
+
+        locator.removeStrategy( originalStrategy );
+
+        strategies = locator.getStrategies();
+
+        assertFalse( strategies.contains( originalStrategy ) );
+
+        mgr.verifyAll();
+    }
     
-    public void testClasspathResource()
+    public void testResolutionFallsThroughStrategyStackAndReturnsNullIfNotResolved()
     {
-        String url = getClass().getName().replace( '.', '/' ) + ".class";
+        List strategies = new ArrayList();
+        
+        strategies.add( new LoggingLocatorStrategy() );
+        strategies.add( new LoggingLocatorStrategy() );
+        strategies.add( new LoggingLocatorStrategy() );
+        
+        MessageHolder mh = new DefaultMessageHolder();
+        
+        Locator locator = new Locator( strategies, mh );
+
+        Location location = locator.resolve( "some-specification" );
+        
+        assertNull( location );
+        
+        assertEquals( 3, mh.size() );
+    }
+    
+    public static final class LoggingLocatorStrategy implements LocatorStrategy
+    {
+        
+        static int instanceCounter = 0;
+        
+        int counter = instanceCounter++;
+
+        public Location resolve( String locationSpecification, MessageHolder messageHolder )
+        {
+            messageHolder.addMessage( "resolve hit on strategy-" + (counter) );
+            return null;
+        }
         
-        Locator locator = new Locator();
-        locator.resolve( url );
     }
 
 }
index b99ac25..0b7c7a2 100644 (file)
@@ -4,13 +4,18 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 
+import org.apache.maven.shared.io.TestUtils;
+
+import junit.framework.TestCase;
+
 public class URLLocationTest
-    extends AbstractLocationTest
+    extends TestCase
 {
     
     public void testShouldConstructFromUrlAndTempFileSpecifications() throws IOException
     {
         File f = File.createTempFile( "url-location.", ".test" );
+        f.deleteOnExit();
         
         URL url = f.toURL();
         
@@ -20,6 +25,7 @@ public class URLLocationTest
     public void testShouldTransferFromTempFile() throws IOException
     {
         File f = File.createTempFile( "url-location.", ".test" );
+        f.deleteOnExit();
         
         URL url = f.toURL();
         
@@ -32,10 +38,11 @@ public class URLLocationTest
     public void testShouldTransferFromTempFileThenRead() throws IOException
     {
         File f = File.createTempFile( "url-location.", ".test" );
+        f.deleteOnExit();
         
         String testStr = "This is a test";
         
-        writeToFile( f, testStr );
+        TestUtils.writeToFile( f, testStr );
         
         URL url = f.toURL();
         
diff --git a/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java b/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java
new file mode 100644 (file)
index 0000000..0f9232d
--- /dev/null
@@ -0,0 +1,60 @@
+package org.apache.maven.shared.io.location;
+
+import java.io.File;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.maven.shared.io.TestUtils;
+import org.apache.maven.shared.io.logging.DefaultMessageHolder;
+import org.apache.maven.shared.io.logging.MessageHolder;
+
+public class URLLocatorStrategyTest
+    extends TestCase
+{
+
+    public void testShouldConstructWithNoParams()
+    {
+        new URLLocatorStrategy();
+    }
+
+    public void testShouldConstructWithTempFileOptions()
+    {
+        new URLLocatorStrategy( "prefix.", ".suffix", true );
+    }
+
+    public void testShouldFailToResolveWithMalformedUrl()
+    {
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = new URLLocatorStrategy().resolve( "://www.google.com", mh );
+
+        assertNull( location );
+        assertEquals( 1, mh.size() );
+    }
+
+    public void testShouldResolveUrlForTempFile() throws IOException
+    {
+        File tempFile = File.createTempFile( "prefix.", ".suffix" );
+        tempFile.deleteOnExit();
+        
+        String testStr = "This is a test.";
+        
+        TestUtils.writeToFile( tempFile, testStr );
+
+        MessageHolder mh = new DefaultMessageHolder();
+
+        Location location = new URLLocatorStrategy().resolve( tempFile.toURL().toExternalForm(), mh );
+
+        assertNotNull( location );
+        assertEquals( 0, mh.size() );
+        
+        location.open();
+        
+        byte[] buffer = new byte[testStr.length()];
+        location.read( buffer );
+        
+        assertEquals( testStr, new String( buffer ) );
+    }
+
+}