001package org.eclipse.aether.internal.impl.filter;
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 org.eclipse.aether.DefaultRepositorySystemSession;
023import org.eclipse.aether.artifact.Artifact;
024import org.eclipse.aether.artifact.DefaultArtifact;
025import org.eclipse.aether.internal.test.util.TestUtils;
026import org.eclipse.aether.repository.RemoteRepository;
027import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
028import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilterSource;
029import org.junit.Before;
030import org.junit.Test;
031
032import static org.hamcrest.MatcherAssert.assertThat;
033import static org.hamcrest.Matchers.containsString;
034import static org.hamcrest.Matchers.equalTo;
035import static org.hamcrest.Matchers.notNullValue;
036import static org.hamcrest.Matchers.nullValue;
037
038/**
039 * UT helper for {@link RemoteRepositoryFilterSource} UTs.
040 */
041public abstract class RemoteRepositoryFilterSourceTestSupport
042{
043    private final Artifact acceptedArtifact = new DefaultArtifact( "org.one:aid:1.0" );
044
045    private final Artifact notAcceptedArtifact = new DefaultArtifact( "org.two:aid:1.0" );
046
047    private DefaultRepositorySystemSession session;
048
049    private RemoteRepository remoteRepository;
050
051    private RemoteRepositoryFilterSource subject;
052
053    @Before
054    public void setup()
055    {
056        remoteRepository = new RemoteRepository.Builder( "test", "default", "https://irrelevant.com" ).build();
057        session = TestUtils.newSession();
058        subject = getRemoteRepositoryFilterSource( session, remoteRepository );
059    }
060
061    protected abstract RemoteRepositoryFilterSource getRemoteRepositoryFilterSource(
062            DefaultRepositorySystemSession session, RemoteRepository remoteRepository );
063
064    protected abstract void enableSource( DefaultRepositorySystemSession session );
065
066    protected abstract void allowArtifact(
067            DefaultRepositorySystemSession session, RemoteRepository remoteRepository, Artifact artifact );
068
069    @Test
070    public void notEnabled()
071    {
072        RemoteRepositoryFilter filter = subject.getRemoteRepositoryFilter( session );
073        assertThat( filter, nullValue() );
074    }
075
076    @Test
077    public void acceptedArtifact()
078    {
079        allowArtifact( session, remoteRepository, acceptedArtifact );
080        enableSource( session );
081
082        RemoteRepositoryFilter filter = subject.getRemoteRepositoryFilter( session );
083        assertThat( filter, notNullValue() );
084
085        RemoteRepositoryFilter.Result result = filter.acceptArtifact( remoteRepository, acceptedArtifact );
086
087        assertThat( result.isAccepted(), equalTo( true ) );
088        assertThat( result.reasoning(), containsString( "allowed from test" ) );
089    }
090
091    @Test
092    public void notAcceptedArtifact()
093    {
094        allowArtifact( session, remoteRepository, acceptedArtifact );
095        enableSource( session );
096
097        RemoteRepositoryFilter filter = subject.getRemoteRepositoryFilter( session );
098        assertThat( filter, notNullValue() );
099
100        RemoteRepositoryFilter.Result result = filter.acceptArtifact( remoteRepository, notAcceptedArtifact );
101
102        assertThat( result.isAccepted(), equalTo( false ) );
103        assertThat( result.reasoning(), containsString( "NOT allowed from test" ) );
104    }
105}