View Javadoc
1   package org.eclipse.aether.util.graph.selector;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.util.Objects.requireNonNull;
23  import static org.junit.Assert.*;
24  
25  import org.eclipse.aether.RepositorySystemSession;
26  import org.eclipse.aether.artifact.DefaultArtifact;
27  import org.eclipse.aether.collection.DependencyCollectionContext;
28  import org.eclipse.aether.collection.DependencySelector;
29  import org.eclipse.aether.graph.Dependency;
30  import org.eclipse.aether.internal.test.util.TestUtils;
31  import org.junit.Test;
32  
33  import java.util.Collections;
34  
35  public class AndDependencySelectorTest
36  {
37  
38      static class DummyDependencySelector
39          implements DependencySelector
40      {
41  
42          private final boolean select;
43  
44          private final DependencySelector child;
45  
46          public DummyDependencySelector()
47          {
48              this( true );
49          }
50  
51          public DummyDependencySelector( boolean select )
52          {
53              this.select = select;
54              this.child = this;
55          }
56  
57          public DummyDependencySelector( boolean select, DependencySelector child )
58          {
59              this.select = select;
60              this.child = child;
61          }
62  
63          public boolean selectDependency( Dependency dependency )
64          {
65              requireNonNull( dependency, "dependency cannot be null" );
66              return select;
67          }
68  
69          public DependencySelector deriveChildSelector( DependencyCollectionContext context )
70          {
71              requireNonNull( context, "context cannot be null" );
72              return child;
73          }
74  
75          @Override
76          public String toString()
77          {
78              return "Dummy(" + select + ')';
79          }
80      }
81  
82      @Test
83      public void testNewInstance()
84      {
85          assertNull( AndDependencySelector.newInstance( null, null ) );
86          DependencySelector selector = new DummyDependencySelector();
87          assertSame( selector, AndDependencySelector.newInstance( selector, null ) );
88          assertSame( selector, AndDependencySelector.newInstance( null, selector ) );
89          assertSame( selector, AndDependencySelector.newInstance( selector, selector ) );
90          assertNotNull( AndDependencySelector.newInstance( selector, new DummyDependencySelector() ) );
91      }
92  
93      @Test
94      public void testTraverseDependency()
95      {
96          Dependency dependency = new Dependency( new DefaultArtifact( "g:a:v:1" ), "runtime" );
97  
98          DependencySelector selector = new AndDependencySelector();
99          assertTrue( selector.selectDependency( dependency ) );
100 
101         selector =
102             new AndDependencySelector( new DummyDependencySelector( false ), new DummyDependencySelector( false ) );
103         assertFalse( selector.selectDependency( dependency ) );
104 
105         selector =
106             new AndDependencySelector( new DummyDependencySelector( true ), new DummyDependencySelector( false ) );
107         assertFalse( selector.selectDependency( dependency ) );
108 
109         selector = new AndDependencySelector( new DummyDependencySelector( true ), new DummyDependencySelector( true ) );
110         assertTrue( selector.selectDependency( dependency ) );
111     }
112 
113     @Test
114     public void testDeriveChildSelector_Unchanged()
115     {
116         DependencySelector other1 = new DummyDependencySelector( true );
117         DependencySelector other2 = new DummyDependencySelector( false );
118         DependencySelector selector = new AndDependencySelector( other1, other2 );
119         RepositorySystemSession session = TestUtils.newSession();
120         DependencyCollectionContext context = TestUtils.newCollectionContext( session,
121                 null,
122                 Collections.emptyList() );
123         assertSame( selector, selector.deriveChildSelector( context ) );
124     }
125 
126     @Test
127     public void testDeriveChildSelector_OneRemaining()
128     {
129         DependencySelector other1 = new DummyDependencySelector( true );
130         DependencySelector other2 = new DummyDependencySelector( false, null );
131         DependencySelector selector = new AndDependencySelector( other1, other2 );
132         RepositorySystemSession session = TestUtils.newSession();
133         DependencyCollectionContext context = TestUtils.newCollectionContext( session,
134                 null,
135                 Collections.emptyList() );
136         assertSame( other1, selector.deriveChildSelector( context ) );
137     }
138 
139     @Test
140     public void testDeriveChildSelector_ZeroRemaining()
141     {
142         DependencySelector other1 = new DummyDependencySelector( true, null );
143         DependencySelector other2 = new DummyDependencySelector( false, null );
144         DependencySelector selector = new AndDependencySelector( other1, other2 );
145         RepositorySystemSession session = TestUtils.newSession();
146         DependencyCollectionContext context = TestUtils.newCollectionContext( session,
147                 null,
148                 Collections.emptyList() );
149         assertNull( selector.deriveChildSelector( context ) );
150     }
151 
152     @Test
153     public void testEquals()
154     {
155         DependencySelector other1 = new DummyDependencySelector( true );
156         DependencySelector other2 = new DummyDependencySelector( false );
157         DependencySelector selector1 = new AndDependencySelector( other1, other2 );
158         DependencySelector selector2 = new AndDependencySelector( other2, other1 );
159         DependencySelector selector3 = new AndDependencySelector( other1 );
160         assertEquals( selector1, selector1 );
161         assertEquals( selector1, selector2 );
162         assertNotEquals( selector1, selector3 );
163         assertNotEquals( selector1, this );
164         assertNotEquals( selector1, null );
165     }
166 
167     @Test
168     public void testHashCode()
169     {
170         DependencySelector other1 = new DummyDependencySelector( true );
171         DependencySelector other2 = new DummyDependencySelector( false );
172         DependencySelector selector1 = new AndDependencySelector( other1, other2 );
173         DependencySelector selector2 = new AndDependencySelector( other2, other1 );
174         assertEquals( selector1.hashCode(), selector2.hashCode() );
175     }
176 
177     @Test
178     public void testToString()
179     {
180         DependencySelector andSelector = new AndDependencySelector(
181             new DummyDependencySelector( true ),
182             new DummyDependencySelector( false )
183         );
184         assertEquals("AndDependencySelector(Dummy(true) && Dummy(false))", andSelector.toString());
185     }
186 
187 }