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.util.Collection;
22  import java.util.Iterator;
23  
24  import org.eclipse.aether.artifact.DefaultArtifact;
25  import org.eclipse.aether.metadata.DefaultMetadata;
26  import org.eclipse.aether.metadata.Metadata;
27  import org.hamcrest.Matchers;
28  import org.junit.Test;
29  
30  import static java.util.Collections.emptyList;
31  import static java.util.Collections.singletonList;
32  import static org.hamcrest.MatcherAssert.assertThat;
33  import static org.hamcrest.Matchers.equalTo;
34  import static org.hamcrest.Matchers.hasSize;
35  
36  public class HashingNameMapperTest extends NameMapperTestSupport {
37      HashingNameMapper mapper = new HashingNameMapper(GAVNameMapper.gav());
38  
39      @Test
40      public void nullsAndEmptyInputs() {
41          Collection<String> names;
42  
43          names = mapper.nameLocks(session, null, null);
44          assertThat(names, Matchers.empty());
45  
46          names = mapper.nameLocks(session, null, emptyList());
47          assertThat(names, Matchers.empty());
48  
49          names = mapper.nameLocks(session, emptyList(), null);
50          assertThat(names, Matchers.empty());
51  
52          names = mapper.nameLocks(session, emptyList(), emptyList());
53          assertThat(names, Matchers.empty());
54      }
55  
56      @Test
57      public void singleArtifact_depth0() {
58          configProperties.put("aether.syncContext.named.hashing.depth", "0");
59          DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
60          Collection<String> names = mapper.nameLocks(session, singletonList(artifact), null);
61  
62          assertThat(names, hasSize(1));
63          assertThat(names.iterator().next(), equalTo("46e98183d232f1e16f863025080c7f2b9797fd10"));
64      }
65  
66      @Test
67      public void singleMetadata_depth0() {
68          configProperties.put("aether.syncContext.named.hashing.depth", "0");
69          DefaultMetadata metadata =
70                  new DefaultMetadata("group", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
71          Collection<String> names = mapper.nameLocks(session, null, singletonList(metadata));
72  
73          assertThat(names, hasSize(1));
74          assertThat(names.iterator().next(), equalTo("293b3990971f4b4b02b220620d2538eaac5f221b"));
75      }
76  
77      @Test
78      public void oneAndOne_depth0() {
79          configProperties.put("aether.syncContext.named.hashing.depth", "0");
80          DefaultArtifact artifact = new DefaultArtifact("agroup:artifact:1.0");
81          DefaultMetadata metadata =
82                  new DefaultMetadata("bgroup", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
83          Collection<String> names = mapper.nameLocks(session, singletonList(artifact), singletonList(metadata));
84  
85          assertThat(names, hasSize(2));
86          Iterator<String> namesIterator = names.iterator();
87  
88          // they are sorted as well
89          assertThat(namesIterator.next(), equalTo("d36504431d00d1c6e4d1c34258f2bf0a004de085"));
90          assertThat(namesIterator.next(), equalTo("fbcebba60d7eb931eca634f6ca494a8a1701b638"));
91      }
92  
93      @Test
94      public void singleArtifact_depth2() {
95          configProperties.put("aether.syncContext.named.hashing.depth", "2");
96          DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
97          Collection<String> names = mapper.nameLocks(session, singletonList(artifact), null);
98  
99          assertThat(names, hasSize(1));
100         assertThat(names.iterator().next(), equalTo("46/e9/46e98183d232f1e16f863025080c7f2b9797fd10"));
101     }
102 
103     @Test
104     public void singleMetadata_depth2() {
105         configProperties.put("aether.syncContext.named.hashing.depth", "2");
106         DefaultMetadata metadata =
107                 new DefaultMetadata("group", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
108         Collection<String> names = mapper.nameLocks(session, null, singletonList(metadata));
109 
110         assertThat(names, hasSize(1));
111         assertThat(names.iterator().next(), equalTo("29/3b/293b3990971f4b4b02b220620d2538eaac5f221b"));
112     }
113 
114     @Test
115     public void oneAndOne_depth2() {
116         configProperties.put("aether.syncContext.named.hashing.depth", "2");
117         DefaultArtifact artifact = new DefaultArtifact("agroup:artifact:1.0");
118         DefaultMetadata metadata =
119                 new DefaultMetadata("bgroup", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
120         Collection<String> names = mapper.nameLocks(session, singletonList(artifact), singletonList(metadata));
121 
122         assertThat(names, hasSize(2));
123         Iterator<String> namesIterator = names.iterator();
124 
125         // they are sorted as well
126         assertThat(namesIterator.next(), equalTo("d3/65/d36504431d00d1c6e4d1c34258f2bf0a004de085"));
127         assertThat(namesIterator.next(), equalTo("fb/ce/fbcebba60d7eb931eca634f6ca494a8a1701b638"));
128     }
129 }