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  import java.net.URL;
25  
26  import org.apache.maven.shared.io.Utils;
27  
28  import junit.framework.TestCase;
29  
30  public class URLLocationTest
31      extends TestCase
32  {
33  
34      public void testShouldConstructFromUrlAndTempFileSpecifications() throws IOException
35      {
36          File f = File.createTempFile( "url-location.", ".test" );
37          f.deleteOnExit();
38  
39          URL url = f.toURL();
40  
41          new URLLocation( url, f.getAbsolutePath(), "prefix.", ".suffix", true );
42      }
43  
44      public void testShouldTransferFromTempFile() throws IOException
45      {
46          File f = File.createTempFile( "url-location.", ".test" );
47          f.deleteOnExit();
48  
49          URL url = f.toURL();
50  
51          URLLocation location = new URLLocation( url, f.getAbsolutePath(), "prefix.", ".suffix", true );
52  
53          assertNotNull( location.getFile() );
54          assertFalse( f.equals( location.getFile() ) );
55      }
56  
57      public void testShouldTransferFromTempFileThenRead() throws IOException
58      {
59          File f = File.createTempFile( "url-location.", ".test" );
60          f.deleteOnExit();
61  
62          String testStr = "This is a test";
63  
64          Utils.writeFileWithEncoding( f, testStr, "US-ASCII" );
65  
66          URL url = f.toURL();
67  
68          URLLocation location = new URLLocation( url, f.getAbsolutePath(), "prefix.", ".suffix", true );
69  
70          location.open();
71  
72          byte[] buffer = new byte[ testStr.length() ];
73  
74          int read = location.read( buffer );
75  
76          assertEquals( testStr.length(), read );
77  
78          assertEquals( testStr, new String( buffer, "US-ASCII" ) );
79      }
80  
81  }