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.internal.aether;
20  
21  import static org.hamcrest.CoreMatchers.equalTo;
22  import static org.hamcrest.CoreMatchers.nullValue;
23  import static org.hamcrest.CoreMatchers.sameInstance;
24  import static org.hamcrest.MatcherAssert.assertThat;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  import java.io.File;
29  import org.eclipse.aether.RepositorySystemSession;
30  import org.eclipse.aether.RequestTrace;
31  import org.eclipse.aether.artifact.Artifact;
32  import org.eclipse.aether.collection.CollectStepData;
33  import org.eclipse.aether.repository.LocalRepository;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * UT for {@link ReverseTreeRepositoryListener}.
38   */
39  public class ReverseTreeRepositoryListenerTest {
40      @Test
41      public void isLocalRepositoryArtifactTest() {
42          File baseDir = new File("local/repository");
43          LocalRepository localRepository = new LocalRepository(baseDir);
44          RepositorySystemSession session = mock(RepositorySystemSession.class);
45          when(session.getLocalRepository()).thenReturn(localRepository);
46  
47          Artifact localRepositoryArtifact = mock(Artifact.class);
48          when(localRepositoryArtifact.getFile()).thenReturn(new File(baseDir, "some/path/within"));
49  
50          Artifact nonLocalReposioryArtifact = mock(Artifact.class);
51          when(nonLocalReposioryArtifact.getFile()).thenReturn(new File("something/completely/different"));
52  
53          assertThat(
54                  ReverseTreeRepositoryListener.isLocalRepositoryArtifact(session, localRepositoryArtifact),
55                  equalTo(true));
56          assertThat(
57                  ReverseTreeRepositoryListener.isLocalRepositoryArtifact(session, nonLocalReposioryArtifact),
58                  equalTo(false));
59      }
60  
61      @Test
62      public void lookupCollectStepDataTest() {
63          RequestTrace doesNotHaveIt =
64                  RequestTrace.newChild(null, "foo").newChild("bar").newChild("baz");
65          assertThat(ReverseTreeRepositoryListener.lookupCollectStepData(doesNotHaveIt), nullValue());
66  
67          final CollectStepData data = mock(CollectStepData.class);
68  
69          RequestTrace haveItFirst = RequestTrace.newChild(null, data)
70                  .newChild("foo")
71                  .newChild("bar")
72                  .newChild("baz");
73          assertThat(ReverseTreeRepositoryListener.lookupCollectStepData(haveItFirst), sameInstance(data));
74  
75          RequestTrace haveItLast = RequestTrace.newChild(null, "foo")
76                  .newChild("bar")
77                  .newChild("baz")
78                  .newChild(data);
79          assertThat(ReverseTreeRepositoryListener.lookupCollectStepData(haveItLast), sameInstance(data));
80  
81          RequestTrace haveIt = RequestTrace.newChild(null, "foo")
82                  .newChild("bar")
83                  .newChild(data)
84                  .newChild("baz");
85          assertThat(ReverseTreeRepositoryListener.lookupCollectStepData(haveIt), sameInstance(data));
86      }
87  }