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.util.graph.selector;
20  
21  import java.util.Collections;
22  
23  import org.eclipse.aether.RepositorySystemSession;
24  import org.eclipse.aether.artifact.DefaultArtifact;
25  import org.eclipse.aether.collection.DependencyCollectionContext;
26  import org.eclipse.aether.collection.DependencySelector;
27  import org.eclipse.aether.graph.Dependency;
28  import org.eclipse.aether.internal.test.util.TestUtils;
29  import org.junit.Test;
30  
31  import static java.util.Objects.requireNonNull;
32  import static org.junit.Assert.*;
33  
34  public class AndDependencySelectorTest {
35  
36      static class DummyDependencySelector implements DependencySelector {
37  
38          private final boolean select;
39  
40          private final DependencySelector child;
41  
42          public DummyDependencySelector() {
43              this(true);
44          }
45  
46          public DummyDependencySelector(boolean select) {
47              this.select = select;
48              this.child = this;
49          }
50  
51          public DummyDependencySelector(boolean select, DependencySelector child) {
52              this.select = select;
53              this.child = child;
54          }
55  
56          public boolean selectDependency(Dependency dependency) {
57              requireNonNull(dependency, "dependency cannot be null");
58              return select;
59          }
60  
61          public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
62              requireNonNull(context, "context cannot be null");
63              return child;
64          }
65  
66          @Override
67          public String toString() {
68              return "Dummy(" + select + ')';
69          }
70      }
71  
72      @Test
73      public void testNewInstance() {
74          assertNull(AndDependencySelector.newInstance(null, null));
75          DependencySelector selector = new DummyDependencySelector();
76          assertSame(selector, AndDependencySelector.newInstance(selector, null));
77          assertSame(selector, AndDependencySelector.newInstance(null, selector));
78          assertSame(selector, AndDependencySelector.newInstance(selector, selector));
79          assertNotNull(AndDependencySelector.newInstance(selector, new DummyDependencySelector()));
80      }
81  
82      @Test
83      public void testTraverseDependency() {
84          Dependency dependency = new Dependency(new DefaultArtifact("g:a:v:1"), "runtime");
85  
86          DependencySelector selector = new AndDependencySelector();
87          assertTrue(selector.selectDependency(dependency));
88  
89          selector = new AndDependencySelector(new DummyDependencySelector(false), new DummyDependencySelector(false));
90          assertFalse(selector.selectDependency(dependency));
91  
92          selector = new AndDependencySelector(new DummyDependencySelector(true), new DummyDependencySelector(false));
93          assertFalse(selector.selectDependency(dependency));
94  
95          selector = new AndDependencySelector(new DummyDependencySelector(true), new DummyDependencySelector(true));
96          assertTrue(selector.selectDependency(dependency));
97      }
98  
99      @Test
100     public void testDeriveChildSelector_Unchanged() {
101         DependencySelector other1 = new DummyDependencySelector(true);
102         DependencySelector other2 = new DummyDependencySelector(false);
103         DependencySelector selector = new AndDependencySelector(other1, other2);
104         RepositorySystemSession session = TestUtils.newSession();
105         DependencyCollectionContext context = TestUtils.newCollectionContext(session, null, Collections.emptyList());
106         assertSame(selector, selector.deriveChildSelector(context));
107     }
108 
109     @Test
110     public void testDeriveChildSelector_OneRemaining() {
111         DependencySelector other1 = new DummyDependencySelector(true);
112         DependencySelector other2 = new DummyDependencySelector(false, null);
113         DependencySelector selector = new AndDependencySelector(other1, other2);
114         RepositorySystemSession session = TestUtils.newSession();
115         DependencyCollectionContext context = TestUtils.newCollectionContext(session, null, Collections.emptyList());
116         assertSame(other1, selector.deriveChildSelector(context));
117     }
118 
119     @Test
120     public void testDeriveChildSelector_ZeroRemaining() {
121         DependencySelector other1 = new DummyDependencySelector(true, null);
122         DependencySelector other2 = new DummyDependencySelector(false, null);
123         DependencySelector selector = new AndDependencySelector(other1, other2);
124         RepositorySystemSession session = TestUtils.newSession();
125         DependencyCollectionContext context = TestUtils.newCollectionContext(session, null, Collections.emptyList());
126         assertNull(selector.deriveChildSelector(context));
127     }
128 
129     @Test
130     public void testEquals() {
131         DependencySelector other1 = new DummyDependencySelector(true);
132         DependencySelector other2 = new DummyDependencySelector(false);
133         DependencySelector selector1 = new AndDependencySelector(other1, other2);
134         DependencySelector selector2 = new AndDependencySelector(other2, other1);
135         DependencySelector selector3 = new AndDependencySelector(other1);
136         assertEquals(selector1, selector1);
137         assertEquals(selector1, selector2);
138         assertNotEquals(selector1, selector3);
139         assertNotEquals(selector1, this);
140         assertNotEquals(selector1, null);
141     }
142 
143     @Test
144     public void testHashCode() {
145         DependencySelector other1 = new DummyDependencySelector(true);
146         DependencySelector other2 = new DummyDependencySelector(false);
147         DependencySelector selector1 = new AndDependencySelector(other1, other2);
148         DependencySelector selector2 = new AndDependencySelector(other2, other1);
149         assertEquals(selector1.hashCode(), selector2.hashCode());
150     }
151 
152     @Test
153     public void testToString() {
154         DependencySelector andSelector =
155                 new AndDependencySelector(new DummyDependencySelector(true), new DummyDependencySelector(false));
156         assertEquals("AndDependencySelector(Dummy(true) && Dummy(false))", andSelector.toString());
157     }
158 }