001package org.eclipse.aether.internal.impl.synccontext.named;
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 java.util.Collection;
023import java.util.Iterator;
024
025import org.eclipse.aether.artifact.DefaultArtifact;
026import org.eclipse.aether.metadata.DefaultMetadata;
027import org.eclipse.aether.metadata.Metadata;
028import org.hamcrest.Matchers;
029import org.junit.Test;
030
031import static java.util.Collections.emptyList;
032import static java.util.Collections.singletonList;
033import static org.hamcrest.MatcherAssert.assertThat;
034import static org.hamcrest.Matchers.equalTo;
035import static org.hamcrest.Matchers.hasSize;
036
037public class GAVNameMapperTest extends NameMapperTestSupport
038{
039    NameMapper mapper = GAVNameMapper.fileGav();
040
041    @Test
042    public void nullsAndEmptyInputs()
043    {
044        Collection<String> names;
045
046        names = mapper.nameLocks( session, null, null );
047        assertThat( names, Matchers.empty() );
048
049        names = mapper.nameLocks( session, null, emptyList() );
050        assertThat( names, Matchers.empty() );
051
052        names = mapper.nameLocks( session, emptyList(), null );
053        assertThat( names, Matchers.empty() );
054
055        names = mapper.nameLocks( session, emptyList(), emptyList() );
056        assertThat( names, Matchers.empty() );
057    }
058
059    @Test
060    public void singleArtifact()
061    {
062        DefaultArtifact artifact = new DefaultArtifact( "group:artifact:1.0" );
063        Collection<String> names = mapper.nameLocks( session, singletonList( artifact ), null );
064
065        assertThat( names, hasSize( 1 ) );
066        assertThat( names.iterator().next(), equalTo( "group~artifact~1.0.lock" ) );
067    }
068
069    @Test
070    public void singleMetadata()
071    {
072        DefaultMetadata metadata =
073                new DefaultMetadata( "group", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT );
074        Collection<String> names = mapper.nameLocks( session, null, singletonList( metadata ) );
075
076        assertThat( names, hasSize( 1 ) );
077        assertThat( names.iterator().next(), equalTo( "group~artifact.lock" ) );
078    }
079
080    @Test
081    public void oneAndOne()
082    {
083        DefaultArtifact artifact = new DefaultArtifact( "agroup:artifact:1.0" );
084        DefaultMetadata metadata =
085                new DefaultMetadata( "bgroup", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT );
086        Collection<String> names = mapper.nameLocks( session, singletonList( artifact ), singletonList( metadata ) );
087
088        assertThat( names, hasSize( 2 ) );
089        Iterator<String> namesIterator = names.iterator();
090
091        // they are sorted as well
092        assertThat( namesIterator.next(), equalTo( "agroup~artifact~1.0.lock" ) );
093        assertThat( namesIterator.next(), equalTo( "bgroup~artifact.lock" ) );
094    }
095}