View Javadoc
1   package org.apache.maven.shared.io.location;
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 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  }