Author: brett
Date: Mon Jul 31 01:15:54 2006
New Revision: 427040

URL: http://svn.apache.org/viewvc?rev=427040&view=rev
Log:
[MSUREFIRE-148] replace file.toURL() with one that properly encodes the URL.
Submitted by: Jeremy Boynes

Added:
    
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/UrlUtils.java
   (with props)
    
maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/util/
    
maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/util/UrlUtilsTest.java
   (with props)
Modified:
    
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java

Added: 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/UrlUtils.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/UrlUtils.java?rev=427040&view=auto
==============================================================================
--- 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/UrlUtils.java
 (added)
+++ 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/UrlUtils.java
 Mon Jul 31 01:15:54 2006
@@ -0,0 +1,82 @@
+package org.apache.maven.surefire.util;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.BitSet;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/**
+ * Utility for dealing with URLs in pre-JDK 1.4.
+ */
+public class UrlUtils
+{
+    private static final BitSet UNRESERVED = new BitSet( 256 );
+
+    static
+    {
+        try
+        {
+            byte[] bytes =
+                
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'():/".getBytes(
 "US-ASCII" );
+            for ( int i = 0; i < bytes.length; i++ )
+            {
+                UNRESERVED.set( bytes[i] );
+            }
+
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            // can't happen as US-ASCII must be present
+        }
+    }
+
+    public static URL getURL( File file )
+        throws MalformedURLException
+    {
+        URL url = file.toURL();
+        // encode any characters that do not comply with RFC 2396
+        // this is primarily to handle Windows where the user's home directory 
contains spaces
+        try
+        {
+            byte[] bytes = url.toString().getBytes( "US-ASCII" );
+            StringBuffer buf = new StringBuffer( bytes.length );
+            for ( int i = 0; i < bytes.length; i++ )
+            {
+                byte b = bytes[i];
+                if ( UNRESERVED.get( b ) )
+                {
+                    buf.append( (char) b );
+                }
+                else
+                {
+                    buf.append( '%' );
+                    buf.append( Character.forDigit( b >>> 4 & 0xf, 16 ) );
+                    buf.append( Character.forDigit( b & 0xf, 16 ) );
+                }
+            }
+            return new URL( buf.toString() );
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            // should not happen as US-ASCII must be present
+            throw new NestedRuntimeException( e );
+        }
+    }
+}

Propchange: 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/UrlUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/UrlUtils.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/util/UrlUtilsTest.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/util/UrlUtilsTest.java?rev=427040&view=auto
==============================================================================
--- 
maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/util/UrlUtilsTest.java
 (added)
+++ 
maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/util/UrlUtilsTest.java
 Mon Jul 31 01:15:54 2006
@@ -0,0 +1,53 @@
+package org.apache.maven.surefire.util;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 junit.framework.TestCase;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+/**
+ * Test the URL utilities.
+ */
+public class UrlUtilsTest
+    extends TestCase
+{
+    public void testTestNoSpecialCharacters()
+        throws IOException
+    {
+        File f = new File( "C:/Temp/foo.txt" );
+        assertEquals( new URL( "file:/C:/Temp/foo.txt" ), UrlUtils.getURL( f ) 
);
+        f = new File( "C:/Temp/qwertyuiopasdfghjklzxcvbnm.txt" );
+        assertEquals( new URL( "file:/C:/Temp/qwertyuiopasdfghjklzxcvbnm.txt" 
), UrlUtils.getURL( f ) );
+        f = new File( "C:/Temp/QWERTYUIOPASDFGHJKLZXCVBNM.txt" );
+        assertEquals( new URL( "file:/C:/Temp/QWERTYUIOPASDFGHJKLZXCVBNM.txt" 
), UrlUtils.getURL( f ) );
+        f = new File( "C:/Temp/1234567890.txt" );
+        assertEquals( new URL( "file:/C:/Temp/1234567890.txt" ), 
UrlUtils.getURL( f ) );
+        f = new File( "C:/Temp/)('*~!._-.txt" );
+        assertEquals( new URL( "file:/C:/Temp/)('*~!._-.txt" ), 
UrlUtils.getURL( f ) );
+    }
+
+    public void testTestWithSpaces()
+        throws IOException
+    {
+        File f = new File( "C:/Temp/foo bar.txt" );
+        assertEquals( new URL( "file:/C:/Temp/foo%20bar.txt" ), 
UrlUtils.getURL( f ) );
+    }
+
+}

Propchange: 
maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/util/UrlUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/util/UrlUtilsTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: 
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java?rev=427040&r1=427039&r2=427040&view=diff
==============================================================================
--- 
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java
 (original)
+++ 
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java
 Mon Jul 31 01:15:54 2006
@@ -16,6 +16,22 @@
  * limitations under the License.
  */
 
+import org.apache.maven.surefire.Surefire;
+import org.apache.maven.surefire.booter.output.FileOutputConsumerProxy;
+import org.apache.maven.surefire.booter.output.ForkingStreamConsumer;
+import org.apache.maven.surefire.booter.output.OutputConsumer;
+import org.apache.maven.surefire.booter.output.StandardOutputConsumer;
+import 
org.apache.maven.surefire.booter.output.SupressFooterOutputConsumerProxy;
+import 
org.apache.maven.surefire.booter.output.SupressHeaderOutputConsumerProxy;
+import org.apache.maven.surefire.testset.TestSetFailedException;
+import org.apache.maven.surefire.util.NestedRuntimeException;
+import org.apache.maven.surefire.util.UrlUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.cli.CommandLineUtils;
+import org.codehaus.plexus.util.cli.StreamConsumer;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -33,21 +49,6 @@
 import java.util.Map;
 import java.util.Properties;
 
-import org.apache.maven.surefire.Surefire;
-import org.apache.maven.surefire.booter.output.FileOutputConsumerProxy;
-import org.apache.maven.surefire.booter.output.ForkingStreamConsumer;
-import org.apache.maven.surefire.booter.output.OutputConsumer;
-import org.apache.maven.surefire.booter.output.StandardOutputConsumer;
-import 
org.apache.maven.surefire.booter.output.SupressFooterOutputConsumerProxy;
-import 
org.apache.maven.surefire.booter.output.SupressHeaderOutputConsumerProxy;
-import org.apache.maven.surefire.testset.TestSetFailedException;
-import org.apache.maven.surefire.util.NestedRuntimeException;
-import org.codehaus.plexus.util.IOUtil;
-import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.cli.CommandLineException;
-import org.codehaus.plexus.util.cli.CommandLineUtils;
-import org.codehaus.plexus.util.cli.StreamConsumer;
-
 /**
  * @author Jason van Zyl
  * @author Emmanuel Venisse
@@ -64,7 +65,7 @@
     private List surefireBootClassPathUrls = new ArrayList();
 
     private List testSuites = new ArrayList();
-    
+
     private boolean redirectTestOutputToFile = false;
 
     // ----------------------------------------------------------------------
@@ -130,11 +131,11 @@
             surefireClassPathUrls.add( path );
         }
     }
-    
+
     /**
      * When forking, setting this to true will make the test output to be 
saved in a file
-     * instead of showing it on the standard output 
-     *  
+     * instead of showing it on the standard output
+     *
      * @param redirectTestOutputToFile
      */
     public void setRedirectTestOutputToFile( boolean redirectTestOutputToFile )
@@ -144,7 +145,7 @@
 
     /**
      * Set the directory where reports will be saved
-     * 
+     *
      * @param reportsDirectory the directory
      */
     public void setReportsDirectory( File reportsDirectory )
@@ -575,7 +576,7 @@
             if ( url != null )
             {
                 File f = new File( url );
-                urls.add( f.toURL() );
+                urls.add( UrlUtils.getURL( f ) );
             }
         }
 
@@ -665,8 +666,10 @@
         if ( paramProperty != null )
         {
             // bit of a glitch that it need sto be done twice to do an odd 
number of vertical bars (eg |||, |||||).
-            String[] params = StringUtils.split( StringUtils.replace( 
StringUtils.replace( paramProperty, "||", "| |" ), "||", "| |" ), "|" );
-            String[] types = StringUtils.split( StringUtils.replace( 
StringUtils.replace( typeProperty, "||", "| |" ), "||", "| |" ), "|" );
+            String[] params = StringUtils.split(
+                StringUtils.replace( StringUtils.replace( paramProperty, "||", 
"| |" ), "||", "| |" ), "|" );
+            String[] types = StringUtils.split(
+                StringUtils.replace( StringUtils.replace( typeProperty, "||", 
"| |" ), "||", "| |" ), "|" );
 
             paramObjects = new Object[params.length];
 
@@ -810,7 +813,8 @@
         addSurefireClassPathUrl( path );
     }
 
-    private StreamConsumer getForkingStreamConsumer( boolean showHeading, 
boolean showFooter, boolean redirectTestOutputToFile )
+    private StreamConsumer getForkingStreamConsumer( boolean showHeading, 
boolean showFooter,
+                                                     boolean 
redirectTestOutputToFile )
     {
         OutputConsumer outputConsumer = new StandardOutputConsumer();
 


Reply via email to