1 package org.apache.archiva.repository.maven.content;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.filelock.DefaultFileLockManager;
22 import org.apache.archiva.maven.metadata.MavenMetadataReader;
23 import org.apache.archiva.repository.content.ItemSelector;
24 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
25 import org.apache.archiva.repository.storage.StorageAsset;
26 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
27 import org.junit.jupiter.api.AfterAll;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
31 import java.io.IOException;
32 import java.net.URISyntaxException;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
37 import static org.junit.jupiter.api.Assertions.assertEquals;
38 import static org.junit.jupiter.api.Assertions.assertNotNull;
41 * @author Martin Stockhammer <martin_s@apache.org>
43 class MavenContentHelperTest
46 private static FilesystemStorage storage;
47 private static Path tempDir;
50 static void setUp() throws IOException
52 tempDir = Files.createTempDirectory( "archivamaventest" );
53 storage = new FilesystemStorage( tempDir, new DefaultFileLockManager() );
57 static void tearDown() {
60 Files.deleteIfExists( tempDir );
62 catch ( IOException e )
64 System.err.println( "Could not delete " + tempDir );
69 void getNamespaceFromNamespacePath( )
71 StorageAsset asset = storage.getAsset( "org/apache/archiva" );
72 String ns = MavenContentHelper.getNamespaceFromNamespacePath( asset );
74 assertEquals( "org.apache.archiva", ns );
76 asset = storage.getRoot();
77 ns = MavenContentHelper.getNamespaceFromNamespacePath( asset );
79 assertEquals( "", ns );
83 void getArtifactVersion( ) throws IOException, URISyntaxException
85 MavenContentHelper mavenContentHelper = new MavenContentHelper( );
86 MavenMetadataReader reader = new MavenMetadataReader( );
87 mavenContentHelper.setMetadataReader( reader );
88 Path testRepoPath = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/metadata-repository" ).toURI() );
89 FilesystemStorage storage = new FilesystemStorage( testRepoPath, new DefaultFileLockManager( ) );
90 assertArtifactVersion( mavenContentHelper, "1.0-alpha-11-SNAPSHOT", storage.getAsset( "org/apache/archiva/metadata/tests/snap_shots_1/1.0-alpha-11-SNAPSHOT" )
91 , "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-SNAPSHOT");
93 assertArtifactVersion( mavenContentHelper, "1.0-alpha-11-20070316.175232-11", storage.getAsset( "org/apache/archiva/metadata/tests/snap_shots_a/1.0-alpha-11-SNAPSHOT" )
94 , "", "1.0-alpha-11-SNAPSHOT");
96 assertArtifactVersion( mavenContentHelper, "2.2-20070316.153953-10", storage.getAsset( "org/apache/archiva/metadata/tests/snap_shots_b/2.2-SNAPSHOT" )
97 , "", "2.2-SNAPSHOT");
101 private void assertArtifactVersion(MavenContentHelper mavenContentHelper, String expectedVersion, StorageAsset dir, String selectorArtifactVersion, String selectorVersion) {
102 ItemSelector selector = ArchivaItemSelector.builder( )
103 .withVersion( selectorVersion )
104 .withArtifactVersion( selectorArtifactVersion )
106 assertEquals( expectedVersion, mavenContentHelper.getArtifactVersion( dir, selector ) );
110 void getLatestArtifactSnapshotVersion( ) throws URISyntaxException, IOException
112 MavenContentHelper mavenContentHelper = new MavenContentHelper( );
113 MavenMetadataReader reader = new MavenMetadataReader( );
114 mavenContentHelper.setMetadataReader( reader );
115 Path testRepoPath = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/default-repository" ).toURI() );
116 FilesystemStorage storage = new FilesystemStorage( testRepoPath, new DefaultFileLockManager( ) );
117 // Directory without metadata file
118 assertEquals( "2.1-20090808.085535-2", mavenContentHelper.getLatestArtifactSnapshotVersion( storage.getAsset( "org/apache/archiva/sample-parent/2.1-SNAPSHOT" ), "2.1-SNAPSHOT" ) );
119 // Directory with metadata file
120 assertEquals( "1.3-20070802.113139-29", mavenContentHelper.getLatestArtifactSnapshotVersion( storage.getAsset( "org/apache/axis2/axis2/1.3-SNAPSHOT" ), "1.3-SNAPSHOT" ) );
124 void getArtifactFileName( )
126 assertFileName( "test-1.0.jar", "test", "", "1.0", "jar" );
127 assertFileName( "test-1.1-client.jar", "test", "client", "1.1", "jar" );
128 assertFileName( "te445st-2.1-sources.jar", "te445st", "sources", "2.1", "jar" );
129 assertFileName( "abcde-8888.994894.48484-10.jar", "abcde", "", "8888.994894.48484-10", "jar" );
130 assertFileName( "testarchive-5.0.war", "testarchive", "", "5.0", "war" );
133 private void assertFileName(String expectedFilename, String artifactId, String classifier, String version, String extension) {
134 assertEquals( expectedFilename, MavenContentHelper.getArtifactFileName( artifactId, version, classifier, extension ) );
138 void getClassifier( )
140 assertClassifier( "sources", "","java-source" );
141 assertClassifier( "tests", "", "test-jar" );
142 assertClassifier( "client", "","ejb-client" );
143 assertClassifier( "javadoc", "","javadoc" );
144 assertClassifier( "", "","test" );
145 assertClassifier( "test1", "test1","java-source" );
146 assertClassifier( "test2", "test2", "test-jar" );
147 assertClassifier( "test3", "test3","ejb-client" );
148 assertClassifier( "test4", "test4","javadoc" );
149 assertClassifier( "test5", "test5","test" );
151 private void assertClassifier(String expectedClassifier, String classifier, String type) {
152 ItemSelector selector = ArchivaItemSelector.builder( )
153 .withClassifier( classifier )
154 .withType( type ).build();
155 assertEquals( expectedClassifier, MavenContentHelper.getClassifier( selector ) );
159 void getClassifierFromType( )
161 assertClassifier( "sources", "java-source" );
162 assertClassifier( "tests", "test-jar" );
163 assertClassifier( "client", "ejb-client" );
164 assertClassifier( "javadoc", "javadoc" );
165 assertClassifier( "", "test" );
168 private void assertClassifier(String expectedClassifier, String type) {
169 assertEquals( expectedClassifier, MavenContentHelper.getClassifierFromType( type ) );
173 void getTypeFromClassifierAndExtension( )
175 assertType( "javadoc", "javadoc", "jar" );
176 assertType( "war", "", "war" );
177 assertType( "ear", "", "ear" );
178 assertType( "rar", "", "rar" );
179 assertType( "java-source", "sources", "jar" );
180 assertType( "ejb-client", "client", "jar" );
181 assertType( "pom", "", "pom" );
182 assertType( "test-jar", "tests", "jar" );
186 private void assertType(String expectedType, String classifier, String extension) {
187 assertEquals( expectedType, MavenContentHelper.getTypeFromClassifierAndExtension( classifier, extension ) );
193 void getArtifactExtension( )
195 assertExtension( "test", "", "test" );
196 assertExtension( "jar", "javadoc", "" );
197 assertExtension( "war", "war", "" );
198 assertExtension( "ear", "ear", "" );
199 assertExtension( "rar", "rar", "" );
200 assertExtension( "jar", "", "" );
203 private void assertExtension( String expectedExtension, String type, String extension )
205 ItemSelector selector = ArchivaItemSelector.builder( )
206 .withType( type ).withExtension( extension ).build();
207 assertEquals( expectedExtension, MavenContentHelper.getArtifactExtension( selector ) );