Author: jdcasey
Date: Tue Mar 28 12:47:28 2006
New Revision: 389600

URL: http://svn.apache.org/viewcvs?rev=389600&view=rev
Log:
Checking in small changes to Locator stuff, along with some tests.

Added:
    
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/LocatorTest.java
   (with props)
    maven/shared/trunk/maven-shared-io/src/test/resources/
    maven/shared/trunk/maven-shared-io/src/test/resources/META-INF/
    maven/shared/trunk/maven-shared-io/src/test/resources/META-INF/maven/
    
maven/shared/trunk/maven-shared-io/src/test/resources/META-INF/maven/test.properties
   (with props)
Modified:
    
maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/Locator.java

Modified: 
maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/Locator.java
URL: 
http://svn.apache.org/viewcvs/maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/Locator.java?rev=389600&r1=389599&r2=389600&view=diff
==============================================================================
--- 
maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/Locator.java
 (original)
+++ 
maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/Locator.java
 Tue Mar 28 12:47:28 2006
@@ -53,6 +53,30 @@
      * </p>
      * 
      * @param location the location string to match against.
+     * @return the File of the resolved location.
+     * @throws IOException if file is unable to be found or copied into 
<code>localfile</code> destination.
+     */
+    public File resolveLocation( String location )
+        throws IOException
+    {
+        File tmpFile = File.createTempFile( "locator.resolveLocation", 
".result" );
+        tmpFile.deleteOnExit();
+        
+        return resolveLocation( location, tmpFile );
+    }
+
+    /**
+     * <p>
+     * Attempts to resolve a location parameter into a real file.
+     * </p>
+     * 
+     * <p>
+     * Checks a location string to for a resource, URL, or File that matches.
+     * If a resource or URL is found, then a local file is created with that
+     * locations contents.
+     * </p>
+     * 
+     * @param location the location string to match against.
      * @param localfile the local file to use in case of resource or URL.
      * @return the File of the resolved location.
      * @throws IOException if file is unable to be found or copied into 
<code>localfile</code> destination.
@@ -60,19 +84,40 @@
     public File resolveLocation( String location, String localfile )
         throws IOException
     {
+        File retFile = new File( localfile );
+        return resolveLocation( location, retFile );
+    }
+
+    /**
+     * <p>
+     * Attempts to resolve a location parameter into a real file.
+     * </p>
+     * 
+     * <p>
+     * Checks a location string to for a resource, URL, or File that matches.
+     * If a resource or URL is found, then a local file is created with that
+     * locations contents.
+     * </p>
+     * 
+     * @param location the location string to match against.
+     * @param localfile the local file to use in case of resource or URL.
+     * @return the File of the resolved location.
+     * @throws IOException if file is unable to be found or copied into 
<code>localfile</code> destination.
+     */
+    public File resolveLocation( String location, File localfile )
+        throws IOException
+    {
         if ( StringUtils.isEmpty( location ) )
         {
             return null;
         }
 
-        File retFile = new File( localfile );
-
         // Attempt a URL
         if ( location.indexOf( "://" ) > 1 )
         {
             // Found a URL
             URL url = new URL( location );
-            FileUtils.copyURLToFile( url, retFile );
+            FileUtils.copyURLToFile( url, localfile );
         }
         else
         {
@@ -81,7 +126,7 @@
             if ( fileLocation.exists() )
             {
                 // Found a File.
-                FileUtils.copyFile( fileLocation, retFile );
+                FileUtils.copyFile( fileLocation, localfile );
             }
             else
             {
@@ -90,7 +135,7 @@
                 if ( url != null )
                 {
                     // Found a Resource.
-                    FileUtils.copyURLToFile( url, retFile );
+                    FileUtils.copyURLToFile( url, localfile );
                 }
                 else
                 {
@@ -99,16 +144,16 @@
             }
         }
 
-        if ( !retFile.exists() )
+        if ( !localfile.exists() )
         {
             throw new FileNotFoundException( "Destination file does not 
exist." );
         }
 
-        if ( retFile.length() <= 0 )
+        if ( localfile.length() <= 0 )
         {
             throw new IOException( "Destination file has no content." );
         }
 
-        return retFile;
+        return localfile;
     }
 }

Added: 
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/LocatorTest.java
URL: 
http://svn.apache.org/viewcvs/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/LocatorTest.java?rev=389600&view=auto
==============================================================================
--- 
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/LocatorTest.java
 (added)
+++ 
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/LocatorTest.java
 Tue Mar 28 12:47:28 2006
@@ -0,0 +1,27 @@
+package org.apache.maven.shared.io;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+public class LocatorTest
+    extends TestCase
+{
+    
+    public void testClasspathResource()
+    {
+        String url = getClass().getName().replace( '.', '/' ) + ".class";
+        
+        Locator locator = new Locator();
+        try
+        {
+            locator.resolveLocation( url );
+        }
+        catch ( IOException e )
+        {
+            e.printStackTrace();
+            fail( "Cannot resolve location for this test class." );
+        }
+    }
+
+}

Propchange: 
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/LocatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/LocatorTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: 
maven/shared/trunk/maven-shared-io/src/test/resources/META-INF/maven/test.properties
URL: 
http://svn.apache.org/viewcvs/maven/shared/trunk/maven-shared-io/src/test/resources/META-INF/maven/test.properties?rev=389600&view=auto
==============================================================================
--- 
maven/shared/trunk/maven-shared-io/src/test/resources/META-INF/maven/test.properties
 (added)
+++ 
maven/shared/trunk/maven-shared-io/src/test/resources/META-INF/maven/test.properties
 Tue Mar 28 12:47:28 2006
@@ -0,0 +1 @@
+test=successful

Propchange: 
maven/shared/trunk/maven-shared-io/src/test/resources/META-INF/maven/test.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/shared/trunk/maven-shared-io/src/test/resources/META-INF/maven/test.properties
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"


Reply via email to