1 package org.apache.maven.shared.io.location;
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.io.IOException;
24
25 import junit.framework.TestCase;
26
27 import org.apache.maven.shared.io.Utils;
28 import org.apache.maven.shared.io.logging.DefaultMessageHolder;
29 import org.apache.maven.shared.io.logging.MessageHolder;
30
31 public class URLLocatorStrategyTest
32 extends TestCase
33 {
34
35 public void testShouldConstructWithNoParams()
36 {
37 new URLLocatorStrategy();
38 }
39
40 public void testShouldConstructWithTempFileOptions()
41 {
42 new URLLocatorStrategy( "prefix.", ".suffix", true );
43 }
44
45 public void testShouldFailToResolveWithMalformedUrl()
46 {
47 MessageHolder mh = new DefaultMessageHolder();
48
49 Location location = new URLLocatorStrategy().resolve( "://www.google.com", mh );
50
51 assertNull( location );
52 assertEquals( 1, mh.size() );
53 }
54
55 public void testShouldResolveUrlForTempFile() throws IOException
56 {
57 File tempFile = File.createTempFile( "prefix.", ".suffix" );
58 tempFile.deleteOnExit();
59
60 String testStr = "This is a test.";
61
62 Utils.writeFileWithEncoding( tempFile, testStr, "US-ASCII" );
63
64 MessageHolder mh = new DefaultMessageHolder();
65
66 Location location = new URLLocatorStrategy().resolve( tempFile.toURL().toExternalForm(), mh );
67
68 assertNotNull( location );
69 assertEquals( 0, mh.size() );
70
71 location.open();
72
73 byte[] buffer = new byte[testStr.length()];
74 location.read( buffer );
75
76 assertEquals( testStr, new String( buffer, "US-ASCII" ) );
77 }
78
79 }