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.junit.jupiter.api.Test;
30  
31  import static java.util.Collections.emptyList;
32  import static java.util.Collections.singletonList;
33  import static org.junit.jupiter.api.Assertions.*;
34  
35  public class BasedirNameMapperTest extends NameMapperTestSupport {
36      private final String PS = File.separator;
37  
38      BasedirNameMapper mapper = new BasedirNameMapper(new HashingNameMapper(GAVNameMapper.gav()));
39  
40      @Test
41      void nullsAndEmptyInputs() {
42          Collection<String> names;
43  
44          names = mapper.nameLocks(session, null, null);
45          assertTrue(names.isEmpty());
46  
47          names = mapper.nameLocks(session, null, emptyList());
48          assertTrue(names.isEmpty());
49  
50          names = mapper.nameLocks(session, emptyList(), null);
51          assertTrue(names.isEmpty());
52  
53          names = mapper.nameLocks(session, emptyList(), emptyList());
54          assertTrue(names.isEmpty());
55      }
56  
57      @Test
58      void defaultLocksDir() {
59          configProperties.put("aether.syncContext.named.hashing.depth", "0");
60          configProperties.put("aether.syncContext.named.basedir.locksDir", null);
61          DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
62          Collection<String> names = mapper.nameLocks(session, singletonList(artifact), null);
63          assertEquals(names.size(), 1);
64          assertEquals(
65                  names.iterator().next(), basedir + PS + ".locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10");
66      }
67  
68      @Test
69      void relativeLocksDir() {
70          configProperties.put("aether.syncContext.named.hashing.depth", "0");
71          configProperties.put("aether.syncContext.named.basedir.locksDir", "my/locks");
72          DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
73          Collection<String> names = mapper.nameLocks(session, singletonList(artifact), null);
74          assertEquals(names.size(), 1);
75          assertEquals(
76                  names.iterator().next(),
77                  basedir + PS + "my" + PS + "locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10");
78      }
79  
80      @Test
81      void absoluteLocksDir() throws IOException {
82          String absoluteLocksDir = "/my/locks";
83          String customBaseDir = new File(absoluteLocksDir).getCanonicalPath();
84  
85          configProperties.put("aether.syncContext.named.hashing.depth", "0");
86          configProperties.put("aether.syncContext.named.basedir.locksDir", absoluteLocksDir);
87          DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
88          Collection<String> names = mapper.nameLocks(session, singletonList(artifact), null);
89          assertEquals(names.size(), 1);
90          assertEquals(names.iterator().next(), customBaseDir + PS + "46e98183d232f1e16f863025080c7f2b9797fd10");
91      }
92  
93      @Test
94      void singleArtifact() {
95          configProperties.put("aether.syncContext.named.hashing.depth", "0");
96  
97          DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
98          Collection<String> names = mapper.nameLocks(session, singletonList(artifact), null);
99          assertEquals(names.size(), 1);
100         assertEquals(
101                 names.iterator().next(), basedir + PS + ".locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10");
102     }
103 
104     @Test
105     void singleMetadata() {
106         configProperties.put("aether.syncContext.named.hashing.depth", "0");
107 
108         DefaultMetadata metadata =
109                 new DefaultMetadata("group", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
110         Collection<String> names = mapper.nameLocks(session, null, singletonList(metadata));
111         assertEquals(names.size(), 1);
112         assertEquals(
113                 names.iterator().next(), basedir + PS + ".locks" + PS + "293b3990971f4b4b02b220620d2538eaac5f221b");
114     }
115 
116     @Test
117     void oneAndOne() {
118         configProperties.put("aether.syncContext.named.hashing.depth", "0");
119 
120         DefaultArtifact artifact = new DefaultArtifact("agroup:artifact:1.0");
121         DefaultMetadata metadata =
122                 new DefaultMetadata("bgroup", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
123         Collection<String> names = mapper.nameLocks(session, singletonList(artifact), singletonList(metadata));
124 
125         assertEquals(names.size(), 2);
126         Iterator<String> namesIterator = names.iterator();
127 
128         // they are sorted as well
129         assertEquals(namesIterator.next(), basedir + PS + ".locks" + PS + "d36504431d00d1c6e4d1c34258f2bf0a004de085");
130         assertEquals(namesIterator.next(), basedir + PS + ".locks" + PS + "fbcebba60d7eb931eca634f6ca494a8a1701b638");
131     }
132 }