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