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 junit.framework.TestCase;
23  
24  import java.io.File;
25  import java.net.URI;
26  import java.net.URL;
27  
28  import static org.apache.maven.surefire.util.internal.UrlUtils.toURL;
29  
30  /**
31   * Test the URL utilities.
32   */
33  public class UrlUtilsTest
34      extends TestCase
35  {
36      private String homeDir;
37  
38      @Override
39      public void setUp()
40          throws Exception
41      {
42          super.setUp();
43          homeDir = System.getProperty( "user.dir" );
44          if ( !homeDir.startsWith( "/" ) )
45          {
46              homeDir = "/" + homeDir;
47          }
48      }
49  
50      private void verifyFileName( String fileName )
51          throws Exception
52      {
53          verifyFileName( fileName, fileName );
54      }
55  
56      private void verifyFileName( String fileName, String expectedFileName )
57          throws Exception
58      {
59          File f = new File( homeDir, fileName );
60          URL u = toURL( f );
61          URI uri = u.toURI();
62          File urlFile = new File( uri );
63          String url = u.toString();
64  
65          assertStartsWith( url, "file:" );
66          assertEndsWith( url, expectedFileName );
67          assertEquals( f, urlFile );
68      }
69  
70      private void assertStartsWith( String string, String substring )
71      {
72          assertTrue( "<" + string + "> should start with <" + substring + ">", string.startsWith( substring ) );
73      }
74  
75      private void assertEndsWith( String string, String substring )
76      {
77          assertTrue( "<" + string + "> should end with <" + substring + ">", string.endsWith( substring ) );
78      }
79  
80      public void testTestNoSpecialCharacters()
81          throws Exception
82      {
83          verifyFileName( "foo.txt" );
84          verifyFileName( "qwertyuiopasdfghjklzxcvbnm.txt" );
85          verifyFileName( "QWERTYUIOPASDFGHJKLZXCVBNM.txt" );
86          verifyFileName( "1234567890.txt" );
87          verifyFileName( ")('*~!._-.txt" );
88      }
89  
90      public void testTestWithSpaces()
91          throws Exception
92      {
93          verifyFileName( "foo bar.txt", "foo%20bar.txt" );
94      }
95  
96      public void testTestWithUmlaut()
97          throws Exception
98      {
99          verifyFileName( "fo\u00DC.txt", "fo%c3%9c.txt" );
100     }
101 
102 }