You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by br...@apache.org on 2008/03/19 21:06:29 UTC

svn commit: r638988 - in /maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils: ChecksumsTest.java ResourceUtils.java

Author: brett
Date: Wed Mar 19 13:06:28 2008
New Revision: 638988

URL: http://svn.apache.org/viewvc?rev=638988&view=rev
Log:
[MRM-642] don't rely on relative paths for tests
Submitted by Stephen Gargan

Added:
    maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ResourceUtils.java   (with props)
Modified:
    maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java

Modified: maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java?rev=638988&r1=638987&r2=638988&view=diff
==============================================================================
--- maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java (original)
+++ maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java Wed Mar 19 13:06:28 2008
@@ -27,8 +27,8 @@
 import java.io.FileReader;
 
 /**
- * ChecksumsTest 
- *
+ * ChecksumsTest
+ * 
  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
  * @version $Id$
  */
@@ -256,14 +256,14 @@
     private File createTestableFiles( String md5State, String sha1State )
         throws Exception
     {
-        File sourceDir = new File( "src/test/resources/checksums/" );
+
         File destDir = new File( "target/checksum-tests/" + getName() + "/" );
 
-        FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );
+        FileUtils.copyFileToDirectory( ResourceUtils.getResource( "/checksums/artifact.jar" ), destDir );
 
         if ( md5State != null )
         {
-            File md5File = new File( sourceDir, "artifact.jar.md5-" + md5State );
+            File md5File = ResourceUtils.getResource( "/checksums/artifact.jar.md5-" + md5State );
             assertTrue( "Testable file exists: " + md5File.getName() + ":", md5File.exists() && md5File.isFile() );
             File destFile = new File( destDir, "artifact.jar.md5" );
             FileUtils.copyFile( md5File, destFile );
@@ -271,7 +271,7 @@
 
         if ( sha1State != null )
         {
-            File sha1File = new File( sourceDir, "artifact.jar.sha1-" + sha1State );
+            File sha1File = ResourceUtils.getResource( "/checksums/artifact.jar.sha1-" + sha1State );
             assertTrue( "Testable file exists: " + sha1File.getName() + ":", sha1File.exists() && sha1File.isFile() );
             File destFile = new File( destDir, "artifact.jar.sha1" );
             FileUtils.copyFile( sha1File, destFile );
@@ -284,8 +284,8 @@
     private Checksums lookupChecksums()
         throws Exception
     {
-        Checksums policy = (Checksums) lookup( Checksums.class );
-        assertNotNull( policy );
-        return policy;
+        Checksums checksums = (Checksums) lookup( Checksums.class );
+        assertNotNull( checksums );
+        return checksums;
     }
 }

Added: maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ResourceUtils.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ResourceUtils.java?rev=638988&view=auto
==============================================================================
--- maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ResourceUtils.java (added)
+++ maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ResourceUtils.java Wed Mar 19 13:06:28 2008
@@ -0,0 +1,90 @@
+package org.apache.maven.archiva.common.utils;
+
+/*
+ * 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 org.apache.commons.lang.StringUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+/**
+ * <code>ResourceUtils</code>
+ */
+public class ResourceUtils
+{
+    /**
+     * Lookup resource at the given path relative to the root of the classpath and if it exists return a file object
+     * that can be used to access it.
+     * <p>
+     * At test time the contents of both the src/resources and test/resources dirs are available at the root of the
+     * classpath.
+     * <p>
+     * To retrieve the file src/test/resources/sometest/test.properties use getResource("/sometest/test.properties").
+     * 
+     * @param resourcePath the path to the resource relative to the root of the classpath
+     * @return File a file object pointing to the resource on the classpath or null if the resource cannot be found
+     */
+    public static File getResource( String resourcePath )
+        throws IOException
+    {
+        return getResource( resourcePath, null );
+    }
+
+    /**
+     * Lookup resource at the given path relative to the root of the classpath and if it exists return a file object
+     * that can be used to access it.
+     * <p>
+     * At test time the contents of both the src/resources and test/resources dirs are available at the root of the
+     * classpath.
+     * <p>
+     * To retrieve the file src/test/resources/sometest/test.properties use getResource("/sometest/test.properties").
+     * 
+     * @param resourcePath the path to the resource relative to the root of the classpath
+     * @param classloader the classloader who's classpath should be searched for the resource
+     * @return File a file object pointing to the resource on the classpath or null if the resource cannot be found
+     */
+    public static File getResource( String resourcePath, ClassLoader classloader )
+        throws IOException
+    {
+        File testResource = null;
+
+        if ( StringUtils.isNotBlank( resourcePath ) )
+        {
+            // make sure the retrieval is relative to the root of the classpath
+            resourcePath = resourcePath.startsWith( "/" ) ? resourcePath : "/" + resourcePath;
+
+            URL resourceUrl = getResourceUrl( resourcePath, classloader );
+            if ( resourceUrl == null )
+            {
+                throw new IOException( "Could not find test resource at path '" + resourcePath + "'" );
+            }
+            testResource = new File( resourceUrl.getFile() );
+        }
+
+        return testResource;
+    }
+
+    private static URL getResourceUrl( String resourcePath, ClassLoader classloader )
+    {
+        return classloader != null ? classloader.getResource( resourcePath )
+                        : ResourceUtils.class.getResource( resourcePath );
+    }
+}

Propchange: maven/archiva/branches/archiva-1.0.x/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ResourceUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native