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      public void setUp()
39          throws Exception
40      {
41          super.setUp();
42          homeDir = System.getProperty( "user.dir" );
43          if ( !homeDir.startsWith( "/" ) )
44          {
45              homeDir = "/" + homeDir;
46          }
47      }
48  
49      private void verifyFileName( String fileName )
50          throws Exception
51      {
52          verifyFileName( fileName, fileName );
53      }
54  
55      private void verifyFileName( String fileName, String expectedFileName )
56          throws Exception
57      {
58          File f = new File( homeDir, fileName );
59          URL u = toURL( f );
60          URI uri = u.toURI();
61          File urlFile = new File( uri );
62          String url = u.toString();
63  
64          assertStartsWith( url, "file:" );
65          assertEndsWith( url, expectedFileName );
66          assertEquals( f, urlFile );
67      }
68  
69      private void assertStartsWith( String string, String substring )
70      {
71          assertTrue( "<" + string + "> should start with <" + substring + ">", string.startsWith( substring ) );
72      }
73  
74      private void assertEndsWith( String string, String substring )
75      {
76          assertTrue( "<" + string + "> should end with <" + substring + ">", string.endsWith( substring ) );
77      }
78  
79      public void testTestNoSpecialCharacters()
80          throws Exception
81      {
82          verifyFileName( "foo.txt" );
83          verifyFileName( "qwertyuiopasdfghjklzxcvbnm.txt" );
84          verifyFileName( "QWERTYUIOPASDFGHJKLZXCVBNM.txt" );
85          verifyFileName( "1234567890.txt" );
86          verifyFileName( ")('*~!._-.txt" );
87      }
88  
89      public void testTestWithSpaces()
90          throws Exception
91      {
92          verifyFileName( "foo bar.txt", "foo%20bar.txt" );
93      }
94  
95      public void testTestWithUmlaut()
96          throws Exception
97      {
98          verifyFileName( "fo\u00DC.txt", "fo%c3%9c.txt" );
99      }
100 
101 }