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 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
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
66 Method toURI = URL.class.getMethod( "toURI", null );
67 Object uri = toURI.invoke( u, null );
68
69
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
78
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 }