001package org.eclipse.aether.util.graph.selector;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static java.util.Objects.requireNonNull;
023import static org.junit.Assert.*;
024
025import org.eclipse.aether.RepositorySystemSession;
026import org.eclipse.aether.artifact.DefaultArtifact;
027import org.eclipse.aether.collection.DependencyCollectionContext;
028import org.eclipse.aether.collection.DependencySelector;
029import org.eclipse.aether.graph.Dependency;
030import org.eclipse.aether.internal.test.util.TestUtils;
031import org.junit.Test;
032
033import java.util.Collections;
034
035public class AndDependencySelectorTest
036{
037
038    static class DummyDependencySelector
039        implements DependencySelector
040    {
041
042        private final boolean select;
043
044        private final DependencySelector child;
045
046        public DummyDependencySelector()
047        {
048            this( true );
049        }
050
051        public DummyDependencySelector( boolean select )
052        {
053            this.select = select;
054            this.child = this;
055        }
056
057        public DummyDependencySelector( boolean select, DependencySelector child )
058        {
059            this.select = select;
060            this.child = child;
061        }
062
063        public boolean selectDependency( Dependency dependency )
064        {
065            requireNonNull( dependency, "dependency cannot be null" );
066            return select;
067        }
068
069        public DependencySelector deriveChildSelector( DependencyCollectionContext context )
070        {
071            requireNonNull( context, "context cannot be null" );
072            return child;
073        }
074
075        @Override
076        public String toString()
077        {
078            return "Dummy(" + select + ')';
079        }
080    }
081
082    @Test
083    public void testNewInstance()
084    {
085        assertNull( AndDependencySelector.newInstance( null, null ) );
086        DependencySelector selector = new DummyDependencySelector();
087        assertSame( selector, AndDependencySelector.newInstance( selector, null ) );
088        assertSame( selector, AndDependencySelector.newInstance( null, selector ) );
089        assertSame( selector, AndDependencySelector.newInstance( selector, selector ) );
090        assertNotNull( AndDependencySelector.newInstance( selector, new DummyDependencySelector() ) );
091    }
092
093    @Test
094    public void testTraverseDependency()
095    {
096        Dependency dependency = new Dependency( new DefaultArtifact( "g:a:v:1" ), "runtime" );
097
098        DependencySelector selector = new AndDependencySelector();
099        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}