View Javadoc
1   package org.apache.maven.surefire.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.lang.reflect.Constructor;
24  import java.lang.reflect.Method;
25  import java.net.URL;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * Test the URL utilities.
31   */
32  public class UrlUtilsTest
33      extends TestCase
34  {
35      private String homeDir;
36  
37      public void setUp()
38          throws Exception
39      {
40          super.setUp();
41          homeDir = System.getProperty( "user.dir" );
42          if ( !homeDir.startsWith( "/" ) )
43          {
44              homeDir = "/" + homeDir;
45          }
46      }
47  
48      private void verifyFileName( String fileName )
49          throws Exception
50      {
51          verifyFileName( fileName, fileName );
52      }
53  
54      private void verifyFileName( String fileName, String expectedFileName )
55          throws Exception
56      {
57          File f = new File( homeDir, fileName );
58          URL u = UrlUtils.getURL( f );
59          String url = u.toString();
60          assertStartsWith( url, "file:" );
61          assertEndsWith( url, expectedFileName );
62  
63          try
64          {
65              // use reflection to do "URI uri = u.toURI()" if JDK 1.5+
66              Method toURI = URL.class.getMethod( "toURI", null );
67              Object uri = toURI.invoke( u, null );
68  
69              // use reflection to do "File urlFile = new File( uri )" if JDK 1.4+
70              Constructor newFile = File.class.getConstructor( new Class[]{ uri.getClass() } );
71              File urlFile = (File) newFile.newInstance( uri );
72  
73              assertEquals( f, urlFile );
74          }
75          catch ( NoSuchMethodException e )
76          {
77              // URL.toURI() method in JDK 1.5+, not available currently
78              // we won't be able to check for file equality...
79          }
80      }
81  
82      private void assertStartsWith( String string, String substring )
83      {
84          assertTrue( "<" + string + "> should start with <" + substring + ">", string.startsWith( substring ) );
85      }
86  
87      private void assertEndsWith( String string, String substring )
88      {
89          assertTrue( "<" + string + "> should end with <" + substring + ">", string.endsWith( substring ) );
90      }
91  
92      public void testTestNoSpecialCharacters()
93          throws Exception
94      {
95          verifyFileName( "foo.txt" );
96          verifyFileName( "qwertyuiopasdfghjklzxcvbnm.txt" );
97          verifyFileName( "QWERTYUIOPASDFGHJKLZXCVBNM.txt" );
98          verifyFileName( "1234567890.txt" );
99          verifyFileName( ")('*~!._-.txt" );
100     }
101 
102     public void testTestWithSpaces()
103         throws Exception
104     {
105         verifyFileName( "foo bar.txt", "foo%20bar.txt" );
106     }
107 
108     public void testTestWithUmlaut()
109         throws Exception
110     {
111         verifyFileName( "fo\u00DC.txt", "fo%c3%9c.txt" );
112     }
113 
114 }