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.io.File;
023import java.io.IOException;
024import java.util.Collection;
025import java.util.Iterator;
026
027import org.eclipse.aether.artifact.DefaultArtifact;
028import org.eclipse.aether.metadata.DefaultMetadata;
029import org.eclipse.aether.metadata.Metadata;
030import org.hamcrest.Matchers;
031import org.junit.Test;
032
033import static java.util.Collections.emptyList;
034import static java.util.Collections.singletonList;
035import static org.hamcrest.MatcherAssert.assertThat;
036import static org.hamcrest.Matchers.equalTo;
037import static org.hamcrest.Matchers.hasSize;
038
039public class BasedirNameMapperTest extends NameMapperTestSupport
040{
041    private final String PS = File.separator;
042
043    BasedirNameMapper mapper = new BasedirNameMapper( new HashingNameMapper( GAVNameMapper.gav() ) );
044
045    @Test
046    public void nullsAndEmptyInputs()
047    {
048        Collection<String> names;
049
050        names = mapper.nameLocks( session, null, null );
051        assertThat( names, Matchers.empty() );
052
053        names = mapper.nameLocks( session, null, emptyList() );
054        assertThat( names, Matchers.empty() );
055
056        names = mapper.nameLocks( session, emptyList(), null );
057        assertThat( names, Matchers.empty() );
058
059        names = mapper.nameLocks( session, emptyList(), emptyList() );
060        assertThat( names, Matchers.empty() );
061    }
062
063    @Test
064    public void defaultLocksDir()
065    {
066        configProperties.put( "aether.syncContext.named.hashing.depth", "0" );
067        configProperties.put( "aether.syncContext.named.basedir.locksDir", null );
068        DefaultArtifact artifact = new DefaultArtifact( "group:artifact:1.0" );
069        Collection<String> names = mapper.nameLocks( session, singletonList( artifact ), null );
070        assertThat( names, hasSize( 1 ) );
071        assertThat( names.iterator().next(),
072                equalTo( basedir + PS + ".locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10" ) );
073    }
074
075    @Test
076    public void relativeLocksDir()
077    {
078        configProperties.put( "aether.syncContext.named.hashing.depth", "0" );
079        configProperties.put( "aether.syncContext.named.basedir.locksDir", "my/locks" );
080        DefaultArtifact artifact = new DefaultArtifact( "group:artifact:1.0" );
081        Collection<String> names = mapper.nameLocks( session, singletonList( artifact ), null );
082        assertThat( names, hasSize( 1 ) );
083        assertThat( names.iterator().next(),
084                equalTo( basedir + PS + "my" + PS + "locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10" ) );
085    }
086
087    @Test
088    public void absoluteLocksDir() throws IOException
089    {
090        String absoluteLocksDir = "/my/locks";
091        String customBaseDir = new File( absoluteLocksDir ).getCanonicalPath();
092
093        configProperties.put( "aether.syncContext.named.hashing.depth", "0" );
094        configProperties.put( "aether.syncContext.named.basedir.locksDir", absoluteLocksDir );
095        DefaultArtifact artifact = new DefaultArtifact( "group:artifact:1.0" );
096        Collection<String> names = mapper.nameLocks( session, singletonList( artifact ), null );
097        assertThat( names, hasSize( 1 ) );
098        assertThat( names.iterator().next(), // ends with as we do not test drive letter on non-Win plaf
099                equalTo( customBaseDir + PS + "46e98183d232f1e16f863025080c7f2b9797fd10" ) );
100    }
101
102    @Test
103    public void singleArtifact()
104    {
105        configProperties.put( "aether.syncContext.named.hashing.depth", "0" );
106
107        DefaultArtifact artifact = new DefaultArtifact( "group:artifact:1.0" );
108        Collection<String> names = mapper.nameLocks( session, singletonList( artifact ), null );
109
110        assertThat( names, hasSize( 1 ) );
111        assertThat( names.iterator().next(),
112                equalTo( basedir + PS + ".locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10" ) );
113    }
114
115    @Test
116    public void singleMetadata()
117    {
118        configProperties.put( "aether.syncContext.named.hashing.depth", "0" );
119
120        DefaultMetadata metadata =
121                new DefaultMetadata( "group", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT );
122        Collection<String> names = mapper.nameLocks( session, null, singletonList( metadata ) );
123
124        assertThat( names, hasSize( 1 ) );
125        assertThat( names.iterator().next(),
126                equalTo( basedir + PS + ".locks" + PS + "293b3990971f4b4b02b220620d2538eaac5f221b" ) );
127    }
128
129    @Test
130    public void oneAndOne()
131    {
132        configProperties.put( "aether.syncContext.named.hashing.depth", "0" );
133
134        DefaultArtifact artifact = new DefaultArtifact( "agroup:artifact:1.0" );
135        DefaultMetadata metadata =
136                new DefaultMetadata( "bgroup", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT );
137        Collection<String> names = mapper.nameLocks( session, singletonList( artifact ), singletonList( metadata ) );
138
139        assertThat( names, hasSize( 2 ) );
140        Iterator<String> namesIterator = names.iterator();
141
142        // they are sorted as well
143        assertThat( namesIterator.next(),
144                equalTo( basedir + PS + ".locks" + PS + "d36504431d00d1c6e4d1c34258f2bf0a004de085" ) );
145        assertThat( namesIterator.next(),
146                equalTo( basedir + PS + ".locks" + PS + "fbcebba60d7eb931eca634f6ca494a8a1701b638" ) );
147    }
148}