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.apache.maven.cli.transfer;
20  
21  import java.io.File;
22  
23  import org.eclipse.aether.DefaultRepositorySystemSession;
24  import org.eclipse.aether.transfer.TransferCancelledException;
25  import org.eclipse.aether.transfer.TransferEvent;
26  import org.eclipse.aether.transfer.TransferListener;
27  import org.eclipse.aether.transfer.TransferResource;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertThrows;
31  
32  class SimplexTransferListenerTest {
33      @Test
34      void cancellation() throws InterruptedException {
35          TransferListener delegate = new TransferListener() {
36              @Override
37              public void transferInitiated(TransferEvent event) throws TransferCancelledException {
38                  throw new TransferCancelledException();
39              }
40  
41              @Override
42              public void transferStarted(TransferEvent event) throws TransferCancelledException {
43                  throw new TransferCancelledException();
44              }
45  
46              @Override
47              public void transferProgressed(TransferEvent event) throws TransferCancelledException {
48                  throw new TransferCancelledException();
49              }
50  
51              @Override
52              public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
53                  throw new TransferCancelledException();
54              }
55  
56              @Override
57              public void transferSucceeded(TransferEvent event) {}
58  
59              @Override
60              public void transferFailed(TransferEvent event) {}
61          };
62  
63          SimplexTransferListener listener = new SimplexTransferListener(delegate);
64  
65          TransferResource resource =
66                  new TransferResource(null, null, "http://maven.org/test/test-resource", new File("file"), null);
67          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(h -> false); // no close handle
68  
69          // for technical reasons we cannot throw here, even if delegate does cancel transfer
70          listener.transferInitiated(new TransferEvent.Builder(session, resource)
71                  .setType(TransferEvent.EventType.INITIATED)
72                  .build());
73  
74          Thread.sleep(500); // to make sure queue is processed, cancellation applied
75  
76          // subsequent call will cancel
77          assertThrows(
78                  TransferCancelledException.class,
79                  () -> listener.transferStarted(new TransferEvent.Builder(session, resource)
80                          .resetType(TransferEvent.EventType.STARTED)
81                          .build()));
82      }
83  }