View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl.synccontext.named;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Collection;
24  import java.util.Iterator;
25  
26  import org.eclipse.aether.artifact.DefaultArtifact;
27  import org.eclipse.aether.metadata.DefaultMetadata;
28  import org.eclipse.aether.metadata.Metadata;
29  import org.eclipse.aether.named.NamedLockKey;
30  import org.junit.jupiter.api.Test;
31  
32  import static java.util.Collections.emptyList;
33  import static java.util.Collections.singletonList;
34  import static org.junit.jupiter.api.Assertions.*;
35  
36  public class BasedirNameMapperTest extends NameMapperTestSupport {
37      private final String PS = "/"; // we work with URIs now, not OS file paths
38  
39      BasedirNameMapper mapper = new BasedirNameMapper(new HashingNameMapper(GAVNameMapper.gav()));
40  
41      @Test
42      void nullsAndEmptyInputs() {
43          Collection<NamedLockKey> names;
44  
45          names = mapper.nameLocks(session, null, null);
46          assertTrue(names.isEmpty());
47  
48          names = mapper.nameLocks(session, null, emptyList());
49          assertTrue(names.isEmpty());
50  
51          names = mapper.nameLocks(session, emptyList(), null);
52          assertTrue(names.isEmpty());
53  
54          names = mapper.nameLocks(session, emptyList(), emptyList());
55          assertTrue(names.isEmpty());
56      }
57  
58      @Test
59      void defaultLocksDir() {
60          configProperties.put("aether.syncContext.named.hashing.depth", "0");
61          configProperties.put("aether.syncContext.named.basedir.locksDir", null);
62          DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
63          Collection<NamedLockKey> names = mapper.nameLocks(session, singletonList(artifact), null);
64          assertEquals(names.size(), 1);
65          assertEquals(
66                  names.iterator().next().name(),
67                  basedir.toUri() + PS + ".locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10");
68      }
69  
70      @Test
71      void relativeLocksDir() {
72          configProperties.put("aether.syncContext.named.hashing.depth", "0");
73          configProperties.put("aether.syncContext.named.basedir.locksDir", "my/locks");
74          DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
75          Collection<NamedLockKey> names = mapper.nameLocks(session, singletonList(artifact), null);
76          assertEquals(names.size(), 1);
77          assertEquals(
78                  names.iterator().next().name(),
79                  basedir.toUri() + PS + "my" + PS + "locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10");
80      }
81  
82      @Test
83      void absoluteLocksDir() throws IOException {
84          String absoluteLocksDir = "/my/locks";
85          String customBaseDir =
86                  new File(absoluteLocksDir).getCanonicalFile().toPath().toUri().toASCIIString();
87  
88          configProperties.put("aether.syncContext.named.hashing.depth", "0");
89          configProperties.put("aether.syncContext.named.basedir.locksDir", absoluteLocksDir);
90          DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
91          Collection<NamedLockKey> names = mapper.nameLocks(session, singletonList(artifact), null);
92          assertEquals(names.size(), 1);
93          assertEquals(names.iterator().next().name(), customBaseDir + PS + "46e98183d232f1e16f863025080c7f2b9797fd10");
94      }
95  
96      @Test
97      void singleArtifact() {
98          configProperties.put("aether.syncContext.named.hashing.depth", "0");
99  
100         DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
101         Collection<NamedLockKey> names = mapper.nameLocks(session, singletonList(artifact), null);
102         assertEquals(names.size(), 1);
103         assertEquals(
104                 names.iterator().next().name(),
105                 basedir.toUri() + PS + ".locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10");
106     }
107 
108     @Test
109     void singleMetadata() {
110         configProperties.put("aether.syncContext.named.hashing.depth", "0");
111 
112         DefaultMetadata metadata =
113                 new DefaultMetadata("group", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
114         Collection<NamedLockKey> names = mapper.nameLocks(session, null, singletonList(metadata));
115         assertEquals(names.size(), 1);
116         assertEquals(
117                 names.iterator().next().name(),
118                 basedir.toUri() + PS + ".locks" + PS + "293b3990971f4b4b02b220620d2538eaac5f221b");
119     }
120 
121     @Test
122     void oneAndOne() {
123         configProperties.put("aether.syncContext.named.hashing.depth", "0");
124 
125         DefaultArtifact artifact = new DefaultArtifact("agroup:artifact:1.0");
126         DefaultMetadata metadata =
127                 new DefaultMetadata("bgroup", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
128         Collection<NamedLockKey> names = mapper.nameLocks(session, singletonList(artifact), singletonList(metadata));
129 
130         assertEquals(names.size(), 2);
131         Iterator<NamedLockKey> namesIterator = names.iterator();
132 
133         // they are sorted as well
134         assertEquals(
135                 namesIterator.next().name(),
136                 basedir.toUri() + PS + ".locks" + PS + "d36504431d00d1c6e4d1c34258f2bf0a004de085");
137         assertEquals(
138                 namesIterator.next().name(),
139                 basedir.toUri() + PS + ".locks" + PS + "fbcebba60d7eb931eca634f6ca494a8a1701b638");
140     }
141 }