2020
2121import java .io .File ;
2222import java .io .IOException ;
23+ import java .nio .file .Path ;
2324import java .util .Collection ;
2425import java .util .Iterator ;
2526
2627import org .eclipse .aether .artifact .DefaultArtifact ;
2728import org .eclipse .aether .metadata .DefaultMetadata ;
2829import org .eclipse .aether .metadata .Metadata ;
2930import org .eclipse .aether .named .NamedLockKey ;
31+ import org .eclipse .aether .util .DirectoryUtils ;
3032import org .junit .jupiter .api .Test ;
3133
3234import static java .util .Collections .emptyList ;
3335import static java .util .Collections .singletonList ;
3436import static org .junit .jupiter .api .Assertions .*;
3537
3638public 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 ()));
39+ private final BasedirNameMapper mapper = new BasedirNameMapper (GAVNameMapper .fileGav ());
40+
41+ private String getPrefix () throws IOException {
42+ Path basedir = DirectoryUtils .resolveDirectory (
43+ session , BasedirNameMapper .DEFAULT_LOCKS_DIR , BasedirNameMapper .CONFIG_PROP_LOCKS_DIR , false );
44+ String basedirPath = basedir .toAbsolutePath ().toUri ().toASCIIString ();
45+ if (!basedirPath .endsWith ("/" )) {
46+ basedirPath = basedirPath + "/" ;
47+ }
48+ return basedirPath ;
49+ }
4050
4151 @ Test
4252 void nullsAndEmptyInputs () {
@@ -56,27 +66,27 @@ void nullsAndEmptyInputs() {
5666 }
5767
5868 @ Test
59- void defaultLocksDir () {
69+ void defaultLocksDir () throws IOException {
6070 configProperties .put ("aether.syncContext.named.hashing.depth" , "0" );
6171 configProperties .put ("aether.syncContext.named.basedir.locksDir" , null );
6272 DefaultArtifact artifact = new DefaultArtifact ("group:artifact:1.0" );
6373 Collection <NamedLockKey > names = mapper .nameLocks (session , singletonList (artifact ), null );
6474 assertEquals (1 , names .size ());
6575 assertEquals (
66- names . iterator (). next (). name () ,
67- basedir . toUri () + PS + ".locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10" );
76+ getPrefix () + "artifact~group~artifact~1.0.lock" ,
77+ names . iterator (). next (). name () );
6878 }
6979
7080 @ Test
71- void relativeLocksDir () {
81+ void relativeLocksDir () throws IOException {
7282 configProperties .put ("aether.syncContext.named.hashing.depth" , "0" );
7383 configProperties .put ("aether.syncContext.named.basedir.locksDir" , "my/locks" );
7484 DefaultArtifact artifact = new DefaultArtifact ("group:artifact:1.0" );
7585 Collection <NamedLockKey > names = mapper .nameLocks (session , singletonList (artifact ), null );
7686 assertEquals (1 , names .size ());
7787 assertEquals (
78- names . iterator (). next (). name () ,
79- basedir . toUri () + PS + "my" + PS + "locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10" );
88+ getPrefix () + "artifact~group~artifact~1.0.lock" ,
89+ names . iterator (). next (). name () );
8090 }
8191
8292 @ Test
@@ -90,52 +100,92 @@ void absoluteLocksDir() throws IOException {
90100 DefaultArtifact artifact = new DefaultArtifact ("group:artifact:1.0" );
91101 Collection <NamedLockKey > names = mapper .nameLocks (session , singletonList (artifact ), null );
92102 assertEquals (1 , names .size ());
93- assertEquals (names .iterator ().next ().name (), customBaseDir + PS + "46e98183d232f1e16f863025080c7f2b9797fd10" );
103+ assertEquals (
104+ getPrefix () + "artifact~group~artifact~1.0.lock" ,
105+ names .iterator ().next ().name ());
94106 }
95107
96108 @ Test
97- void singleArtifact () {
109+ void singleArtifact () throws IOException {
98110 configProperties .put ("aether.syncContext.named.hashing.depth" , "0" );
99111
100112 DefaultArtifact artifact = new DefaultArtifact ("group:artifact:1.0" );
101113 Collection <NamedLockKey > names = mapper .nameLocks (session , singletonList (artifact ), null );
102114 assertEquals (1 , names .size ());
103115 assertEquals (
104- names . iterator (). next (). name () ,
105- basedir . toUri () + PS + ".locks" + PS + "46e98183d232f1e16f863025080c7f2b9797fd10" );
116+ getPrefix () + "artifact~group~artifact~1.0.lock" ,
117+ names . iterator (). next (). name () );
106118 }
107119
108120 @ Test
109- void singleMetadata () {
121+ void singleMetadata () throws IOException {
110122 configProperties .put ("aether.syncContext.named.hashing.depth" , "0" );
111123
112124 DefaultMetadata metadata =
113125 new DefaultMetadata ("group" , "artifact" , "maven-metadata.xml" , Metadata .Nature .RELEASE_OR_SNAPSHOT );
114126 Collection <NamedLockKey > names = mapper .nameLocks (session , null , singletonList (metadata ));
115127 assertEquals (1 , names .size ());
116128 assertEquals (
117- names .iterator ().next ().name (),
118- basedir .toUri () + PS + ".locks" + PS + "293b3990971f4b4b02b220620d2538eaac5f221b" );
129+ getPrefix () + "metadata~group~artifact.lock" ,
130+ names .iterator ().next ().name ());
131+ }
132+
133+ @ Test
134+ void prefixMetadata () throws IOException {
135+ configProperties .put ("aether.syncContext.named.hashing.depth" , "0" );
136+
137+ DefaultMetadata metadata =
138+ new DefaultMetadata ("" , "" , ".meta/prefixes-central.txt" , Metadata .Nature .RELEASE_OR_SNAPSHOT );
139+ Collection <NamedLockKey > names = mapper .nameLocks (session , null , singletonList (metadata ));
140+ assertEquals (1 , names .size ());
141+ assertEquals (
142+ getPrefix () + "metadata~.meta-SLASH-prefixes-central.txt.lock" ,
143+ names .iterator ().next ().name ());
144+ }
145+
146+ @ Test
147+ void rootSomeMetadata () throws IOException {
148+ configProperties .put ("aether.syncContext.named.hashing.depth" , "0" );
149+
150+ DefaultMetadata metadata = new DefaultMetadata ("" , "" , "something.xml" , Metadata .Nature .RELEASE_OR_SNAPSHOT );
151+ Collection <NamedLockKey > names = mapper .nameLocks (session , null , singletonList (metadata ));
152+ assertEquals (1 , names .size ());
153+ assertEquals (
154+ getPrefix () + "metadata~something.xml.lock" ,
155+ names .iterator ().next ().name ());
156+ }
157+
158+ @ Test
159+ void nonRootSomeMetadata () throws IOException {
160+ configProperties .put ("aether.syncContext.named.hashing.depth" , "0" );
161+
162+ DefaultMetadata metadata =
163+ new DefaultMetadata ("groupId" , "artifactId" , "something.xml" , Metadata .Nature .RELEASE_OR_SNAPSHOT );
164+ Collection <NamedLockKey > names = mapper .nameLocks (session , null , singletonList (metadata ));
165+ assertEquals (1 , names .size ());
166+ assertEquals (
167+ getPrefix () + "metadata~groupId~artifactId~something.xml.lock" ,
168+ names .iterator ().next ().name ());
119169 }
120170
121171 @ Test
122- void oneAndOne () {
172+ void oneAndOne () throws IOException {
123173 configProperties .put ("aether.syncContext.named.hashing.depth" , "0" );
124174
125175 DefaultArtifact artifact = new DefaultArtifact ("agroup:artifact:1.0" );
126176 DefaultMetadata metadata =
127177 new DefaultMetadata ("bgroup" , "artifact" , "maven-metadata.xml" , Metadata .Nature .RELEASE_OR_SNAPSHOT );
128178 Collection <NamedLockKey > names = mapper .nameLocks (session , singletonList (artifact ), singletonList (metadata ));
129179
130- assertEquals (names .size (), 2 );
180+ assertEquals (2 , names .size ());
131181 Iterator <NamedLockKey > namesIterator = names .iterator ();
132182
133183 // they are sorted as well
134184 assertEquals (
135- namesIterator . next (). name () ,
136- basedir . toUri () + PS + ".locks" + PS + "d36504431d00d1c6e4d1c34258f2bf0a004de085" );
185+ getPrefix () + "artifact~agroup~artifact~1.0.lock" ,
186+ namesIterator . next (). name () );
137187 assertEquals (
138- namesIterator . next (). name () ,
139- basedir . toUri () + PS + ".locks" + PS + "fbcebba60d7eb931eca634f6ca494a8a1701b638" );
188+ getPrefix () + "metadata~bgroup~artifact.lock" ,
189+ namesIterator . next (). name () );
140190 }
141191}
0 commit comments