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