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 org.apache.maven.shared.io.Utils;
23  import org.apache.maven.shared.utils.io.IOUtil;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.nio.ByteBuffer;
30  
31  import junit.framework.TestCase;
32  
33  public class FileLocationTest
34      extends TestCase
35  {
36  
37      public void testShouldConstructWithFileThenRetrieveSameFile() throws IOException
38      {
39          File file = File.createTempFile( "test.", ".file-location" );
40          file.deleteOnExit();
41  
42          FileLocation location = new FileLocation( file, file.getAbsolutePath() );
43  
44          assertSame( file, location.getFile() );
45          assertEquals( file.getAbsolutePath(), location.getSpecification() );
46      }
47  
48      public void testShouldReadFileContentsUsingByteBuffer() throws IOException
49      {
50          File file = File.createTempFile( "test.", ".file-location" );
51          file.deleteOnExit();
52  
53          String testStr = "This is a test";
54  
55          Utils.writeFileWithEncoding( file, testStr, "US-ASCII" );
56  
57          FileLocation location = new FileLocation( file, file.getAbsolutePath() );
58  
59          location.open();
60  
61          ByteBuffer buffer = ByteBuffer.allocate( testStr.length() );
62          location.read( buffer );
63  
64          assertEquals( testStr, new String( buffer.array(), "US-ASCII" ) );
65      }
66  
67      public void testShouldReadFileContentsUsingStream() throws IOException
68      {
69          File file = File.createTempFile( "test.", ".file-location" );
70          file.deleteOnExit();
71  
72          String testStr = "This is a test";
73  
74          Utils.writeFileWithEncoding( file, testStr, "US-ASCII" );
75  
76          FileLocation location = new FileLocation( file, file.getAbsolutePath() );
77  
78          location.open();
79  
80          InputStream stream = location.getInputStream();
81          ByteArrayOutputStream out = new ByteArrayOutputStream();
82          IOUtil.copy( stream, out );
83  
84          assertEquals( testStr, new String(out.toByteArray(), "US-ASCII" ) );
85      }
86  
87      public void testShouldReadFileContentsUsingByteArray() throws IOException
88      {
89          File file = File.createTempFile( "test.", ".file-location" );
90          file.deleteOnExit();
91  
92          String testStr = "This is a test";
93  
94          Utils.writeFileWithEncoding( file, testStr, "US-ASCII" );
95  
96          FileLocation location = new FileLocation( file, file.getAbsolutePath() );
97  
98          location.open();
99  
100         byte[] buffer = new byte[ testStr.length() ];
101         location.read( buffer );
102 
103         assertEquals( testStr, new String( buffer, "US-ASCII" ) );
104     }
105 
106     public void testShouldReadThenClose() throws IOException
107     {
108         File file = File.createTempFile( "test.", ".file-location" );
109         file.deleteOnExit();
110 
111         String testStr = "This is a test";
112 
113         Utils.writeFileWithEncoding( file, testStr, "US-ASCII" );
114 
115         FileLocation location = new FileLocation( file, file.getAbsolutePath() );
116 
117         location.open();
118 
119         byte[] buffer = new byte[ testStr.length() ];
120         location.read( buffer );
121 
122         assertEquals( testStr, new String( buffer, "US-ASCII" ) );
123 
124         location.close();
125     }
126 
127     public void testShouldOpenThenFailToSetFile() throws IOException
128     {
129         File file = File.createTempFile( "test.", ".file-location" );
130         file.deleteOnExit();
131 
132         TestFileLocation location = new TestFileLocation( file.getAbsolutePath() );
133 
134         location.open();
135 
136         try
137         {
138             location.setFile( file );
139 
140             fail( "should not succeed." );
141         }
142         catch( IllegalStateException e )
143         {
144         }
145     }
146 
147     public void testShouldConstructWithoutFileThenSetFileThenOpen() throws IOException
148     {
149         File file = File.createTempFile( "test.", ".file-location" );
150         file.deleteOnExit();
151 
152         TestFileLocation location = new TestFileLocation( file.getAbsolutePath() );
153 
154         location.setFile( file );
155         location.open();
156     }
157 
158     public void testShouldConstructWithLocationThenRetrieveEquivalentFile() throws IOException
159     {
160         File file = File.createTempFile( "test.", ".file-location" );
161         file.deleteOnExit();
162 
163         Location location = new TestFileLocation( file.getAbsolutePath() );
164 
165         assertEquals( file, location.getFile() );
166         assertEquals( file.getAbsolutePath(), location.getSpecification() );
167     }
168 
169     private static final class TestFileLocation extends FileLocation
170     {
171 
172         TestFileLocation( String specification )
173         {
174             super( specification );
175         }
176 
177     }
178 
179 }