View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transport.classpath;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.net.URI;
24  import java.nio.charset.StandardCharsets;
25  
26  import org.eclipse.aether.DefaultRepositorySystemSession;
27  import org.eclipse.aether.internal.test.util.TestFileUtils;
28  import org.eclipse.aether.internal.test.util.TestUtils;
29  import org.eclipse.aether.repository.RemoteRepository;
30  import org.eclipse.aether.spi.connector.transport.GetTask;
31  import org.eclipse.aether.spi.connector.transport.PeekTask;
32  import org.eclipse.aether.spi.connector.transport.PutTask;
33  import org.eclipse.aether.spi.connector.transport.Transporter;
34  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
35  import org.eclipse.aether.transfer.NoTransporterException;
36  import org.eclipse.aether.transfer.TransferCancelledException;
37  import org.junit.jupiter.api.AfterEach;
38  import org.junit.jupiter.api.BeforeEach;
39  import org.junit.jupiter.api.Test;
40  
41  import static org.junit.jupiter.api.Assertions.*;
42  
43  /**
44   */
45  public class ClasspathTransporterTest {
46  
47      private DefaultRepositorySystemSession session;
48  
49      private TransporterFactory factory;
50  
51      private Transporter transporter;
52  
53      private RemoteRepository newRepo(String url) {
54          return new RemoteRepository.Builder("test", "default", url).build();
55      }
56  
57      private void newTransporter(String url) throws Exception {
58          if (transporter != null) {
59              transporter.close();
60              transporter = null;
61          }
62          transporter = factory.newInstance(session, newRepo(url));
63      }
64  
65      @BeforeEach
66      void setUp() throws Exception {
67          session = TestUtils.newSession();
68          factory = new ClasspathTransporterFactory();
69          newTransporter("classpath:/repository");
70      }
71  
72      @AfterEach
73      void tearDown() {
74          if (transporter != null) {
75              transporter.close();
76              transporter = null;
77          }
78          factory = null;
79          session = null;
80      }
81  
82      @Test
83      void testClassify() {
84          assertEquals(Transporter.ERROR_OTHER, transporter.classify(new FileNotFoundException()));
85          assertEquals(Transporter.ERROR_NOT_FOUND, transporter.classify(new ResourceNotFoundException("test")));
86      }
87  
88      @Test
89      void testPeek() throws Exception {
90          transporter.peek(new PeekTask(URI.create("file.txt")));
91      }
92  
93      @Test
94      void testPeek_NotFound() throws Exception {
95          try {
96              transporter.peek(new PeekTask(URI.create("missing.txt")));
97              fail("Expected error");
98          } catch (ResourceNotFoundException e) {
99              assertEquals(Transporter.ERROR_NOT_FOUND, transporter.classify(e));
100         }
101     }
102 
103     @Test
104     void testPeek_Closed() throws Exception {
105         transporter.close();
106         try {
107             transporter.peek(new PeekTask(URI.create("missing.txt")));
108             fail("Expected error");
109         } catch (IllegalStateException e) {
110             assertEquals(Transporter.ERROR_OTHER, transporter.classify(e));
111         }
112     }
113 
114     @Test
115     void testGet_ToMemory() throws Exception {
116         RecordingTransportListener listener = new RecordingTransportListener();
117         GetTask task = new GetTask(URI.create("file.txt")).setListener(listener);
118         transporter.get(task);
119         assertEquals("test", task.getDataString());
120         assertEquals(0L, listener.dataOffset);
121         assertEquals(4L, listener.dataLength);
122         assertEquals(1, listener.startedCount);
123         assertTrue(listener.progressedCount > 0, "Count: " + listener.progressedCount);
124         assertEquals(task.getDataString(), new String(listener.baos.toByteArray(), StandardCharsets.UTF_8));
125     }
126 
127     @Test
128     void testGet_ToFile() throws Exception {
129         File file = TestFileUtils.createTempFile("failure");
130         RecordingTransportListener listener = new RecordingTransportListener();
131         GetTask task = new GetTask(URI.create("file.txt")).setDataFile(file).setListener(listener);
132         transporter.get(task);
133         assertEquals("test", TestFileUtils.readString(file));
134         assertEquals(0L, listener.dataOffset);
135         assertEquals(4L, listener.dataLength);
136         assertEquals(1, listener.startedCount);
137         assertTrue(listener.progressedCount > 0, "Count: " + listener.progressedCount);
138         assertEquals("test", new String(listener.baos.toByteArray(), StandardCharsets.UTF_8));
139     }
140 
141     @Test
142     void testGet_EmptyResource() throws Exception {
143         File file = TestFileUtils.createTempFile("failure");
144         RecordingTransportListener listener = new RecordingTransportListener();
145         GetTask task = new GetTask(URI.create("empty.txt")).setDataFile(file).setListener(listener);
146         transporter.get(task);
147         assertEquals("", TestFileUtils.readString(file));
148         assertEquals(0L, listener.dataOffset);
149         assertEquals(0L, listener.dataLength);
150         assertEquals(1, listener.startedCount);
151         assertEquals(0, listener.progressedCount);
152         assertEquals("", new String(listener.baos.toByteArray(), StandardCharsets.UTF_8));
153     }
154 
155     @Test
156     void testGet_EncodedResourcePath() throws Exception {
157         GetTask task = new GetTask(URI.create("some%20space.txt"));
158         transporter.get(task);
159         assertEquals("space", task.getDataString());
160     }
161 
162     @Test
163     void testGet_Fragment() throws Exception {
164         GetTask task = new GetTask(URI.create("file.txt#ignored"));
165         transporter.get(task);
166         assertEquals("test", task.getDataString());
167     }
168 
169     @Test
170     void testGet_Query() throws Exception {
171         GetTask task = new GetTask(URI.create("file.txt?ignored"));
172         transporter.get(task);
173         assertEquals("test", task.getDataString());
174     }
175 
176     @Test
177     void testGet_FileHandleLeak() throws Exception {
178         for (int i = 0; i < 100; i++) {
179             File file = TestFileUtils.createTempFile("failure");
180             transporter.get(new GetTask(URI.create("file.txt")).setDataFile(file));
181             assertTrue(file.delete(), i + ", " + file.getAbsolutePath());
182         }
183     }
184 
185     @Test
186     void testGet_NotFound() throws Exception {
187         try {
188             transporter.get(new GetTask(URI.create("missing.txt")));
189             fail("Expected error");
190         } catch (ResourceNotFoundException e) {
191             assertEquals(Transporter.ERROR_NOT_FOUND, transporter.classify(e));
192         }
193     }
194 
195     @Test
196     void testGet_Closed() throws Exception {
197         transporter.close();
198         try {
199             transporter.get(new GetTask(URI.create("file.txt")));
200             fail("Expected error");
201         } catch (IllegalStateException e) {
202             assertEquals(Transporter.ERROR_OTHER, transporter.classify(e));
203         }
204     }
205 
206     @Test
207     void testGet_StartCancelled() throws Exception {
208         RecordingTransportListener listener = new RecordingTransportListener();
209         listener.cancelStart = true;
210         GetTask task = new GetTask(URI.create("file.txt")).setListener(listener);
211         try {
212             transporter.get(task);
213             fail("Expected error");
214         } catch (TransferCancelledException e) {
215             assertEquals(Transporter.ERROR_OTHER, transporter.classify(e));
216         }
217         assertEquals(0L, listener.dataOffset);
218         assertEquals(4L, listener.dataLength);
219         assertEquals(1, listener.startedCount);
220         assertEquals(0, listener.progressedCount);
221     }
222 
223     @Test
224     void testGet_ProgressCancelled() throws Exception {
225         RecordingTransportListener listener = new RecordingTransportListener();
226         listener.cancelProgress = true;
227         GetTask task = new GetTask(URI.create("file.txt")).setListener(listener);
228         try {
229             transporter.get(task);
230             fail("Expected error");
231         } catch (TransferCancelledException e) {
232             assertEquals(Transporter.ERROR_OTHER, transporter.classify(e));
233         }
234         assertEquals(0L, listener.dataOffset);
235         assertEquals(4L, listener.dataLength);
236         assertEquals(1, listener.startedCount);
237         assertEquals(1, listener.progressedCount);
238     }
239 
240     @Test
241     void testPut() throws Exception {
242         try {
243             transporter.put(new PutTask(URI.create("missing.txt")));
244             fail("Expected error");
245         } catch (UnsupportedOperationException e) {
246             assertEquals(Transporter.ERROR_OTHER, transporter.classify(e));
247         }
248     }
249 
250     @Test
251     void testPut_Closed() throws Exception {
252         transporter.close();
253         try {
254             transporter.put(new PutTask(URI.create("missing.txt")));
255             fail("Expected error");
256         } catch (IllegalStateException e) {
257             assertEquals(Transporter.ERROR_OTHER, transporter.classify(e));
258         }
259     }
260 
261     @Test
262     void testInit_BadProtocol() {
263         assertThrows(NoTransporterException.class, () -> newTransporter("bad:/void"));
264     }
265 
266     @Test
267     void testInit_CaseInsensitiveProtocol() throws Exception {
268         newTransporter("classpath:/void");
269         newTransporter("CLASSPATH:/void");
270         newTransporter("ClassPath:/void");
271     }
272 
273     @Test
274     void testInit_OpaqueUrl() throws Exception {
275         testInit("classpath:repository");
276     }
277 
278     @Test
279     void testInit_OpaqueUrlTrailingSlash() throws Exception {
280         testInit("classpath:repository/");
281     }
282 
283     @Test
284     void testInit_OpaqueUrlSpaces() throws Exception {
285         testInit("classpath:repo%20space");
286     }
287 
288     @Test
289     void testInit_HierarchicalUrl() throws Exception {
290         testInit("classpath:/repository");
291     }
292 
293     @Test
294     void testInit_HierarchicalUrlTrailingSlash() throws Exception {
295         testInit("classpath:/repository/");
296     }
297 
298     @Test
299     void testInit_HierarchicalUrlSpaces() throws Exception {
300         testInit("classpath:/repo%20space");
301     }
302 
303     @Test
304     void testInit_HierarchicalUrlRoot() throws Exception {
305         testInit("classpath:/");
306     }
307 
308     @Test
309     void testInit_HierarchicalUrlNoPath() throws Exception {
310         testInit("classpath://reserved");
311     }
312 
313     private void testInit(String base) throws Exception {
314         newTransporter(base);
315         GetTask task = new GetTask(URI.create("file.txt"));
316         transporter.get(task);
317         assertEquals("test", task.getDataString());
318     }
319 }