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.artifact.Artifact;
28  import org.apache.maven.artifact.DefaultArtifact;
29  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  import org.apache.maven.shared.io.Utils;
32  
33  public class ArtifactLocationTest
34      extends TestCase
35  {
36  
37      public void testShouldConstructFromTempFileSpecification()
38          throws IOException
39      {
40          File f = File.createTempFile( "artifact-location.", ".test" );
41          f.deleteOnExit();
42  
43          Artifact a = new DefaultArtifact( "group", "artifact", VersionRange.createFromVersion( "1" ), null, "jar",
44                                            null, new DefaultArtifactHandler() );
45  
46          a.setFile( f );
47  
48          ArtifactLocation location = new ArtifactLocation( a, f.getAbsolutePath() );
49  
50          assertSame( f, location.getFile() );
51      }
52  
53      public void testShouldRead()
54          throws IOException
55      {
56          File f = File.createTempFile( "url-location.", ".test" );
57          f.deleteOnExit();
58  
59          String testStr = "This is a test";
60  
61          Utils.writeFileWithEncoding( f, testStr, "US-ASCII" );
62  
63          Artifact a = new DefaultArtifact( "group", "artifact", VersionRange.createFromVersion( "1" ), null, "jar",
64                                            null, new DefaultArtifactHandler() );
65  
66          a.setFile( f );
67  
68          ArtifactLocation location = new ArtifactLocation( a, f.getAbsolutePath() );
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  }