1 package org.apache.maven.surefire.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }