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.artifact.resolver;
20  
21  import java.io.File;
22  import java.io.InputStream;
23  
24  import org.apache.maven.wagon.ResourceDoesNotExistException;
25  import org.apache.maven.wagon.TransferFailedException;
26  import org.apache.maven.wagon.authorization.AuthorizationException;
27  import org.apache.maven.wagon.events.TransferListener;
28  import org.apache.maven.wagon.providers.file.FileWagon;
29  import org.apache.maven.wagon.resource.Resource;
30  
31  /**
32   * Wagon used for test cases that annotates some methods. Note that this is not a thread-safe implementation.
33   */
34  public class TestFileWagon extends FileWagon {
35      private TestTransferListener testTransferListener;
36      private boolean insideGet;
37  
38      protected void getTransfer(Resource resource, File destination, InputStream input, boolean closeInput, int maxSize)
39              throws TransferFailedException {
40          addTransfer("getTransfer " + resource.getName());
41          super.getTransfer(resource, destination, input, closeInput, maxSize);
42      }
43  
44      public void get(String resourceName, File destination)
45              throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
46          addTransfer("get " + resourceName);
47  
48          insideGet = true;
49  
50          super.get(resourceName, destination);
51  
52          insideGet = false;
53      }
54  
55      private void addTransfer(String resourceName) {
56          if (testTransferListener != null) {
57              testTransferListener.addTransfer(resourceName);
58          }
59      }
60  
61      public boolean getIfNewer(String resourceName, File destination, long timestamp)
62              throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
63          if (!insideGet) {
64              addTransfer("getIfNewer " + resourceName);
65          }
66          return super.getIfNewer(resourceName, destination, timestamp);
67      }
68  
69      public void addTransferListener(TransferListener listener) {
70          if (listener instanceof TestTransferListener) {
71              testTransferListener = (TestTransferListener) listener;
72          }
73          super.addTransferListener(listener);
74      }
75  }