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