From: Brett Porter Date: Fri, 22 Feb 2008 05:13:13 +0000 (+0000) Subject: [MRM-674] correct problems with test-sources classifier and alternate version approaches X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8802ed3b8e11ede47c89f7613c5b6b4080732991;p=archiva.git [MRM-674] correct problems with test-sources classifier and alternate version approaches git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@630092 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-1.0.x/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java b/archiva-1.0.x/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java index 7ac85e2ac..2e3831d4a 100644 --- a/archiva-1.0.x/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java +++ b/archiva-1.0.x/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java @@ -44,7 +44,7 @@ public class VersionUtil "(alpha[_.0-9]*)", "(beta[_.0-9]*)", "(rc[_.0-9]*)", - "(test[_.0-9]*)", +// "(test[_.0-9]*)", -- omitted for MRM-681, can be reinstated as part of MRM-712 "(debug[_.0-9]*)", "(unofficial[_.0-9]*)", "(current)", @@ -57,8 +57,6 @@ public class VersionUtil "(incubator)", "([ab][_.0-9]+)" }; - private static final String VersionMegaPattern = StringUtils.join( versionPatterns, '|' ); - public static final String SNAPSHOT = "SNAPSHOT"; public static final Pattern UNIQUE_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-([0-9]{8}\\.[0-9]{6})-([0-9]+)$" ); @@ -66,7 +64,9 @@ public class VersionUtil public static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "^([0-9]{8})\\.([0-9]{6})$" ); public static final Pattern GENERIC_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-" + SNAPSHOT ); - + + private static final Pattern VERSION_MEGA_PATTERN = Pattern.compile( StringUtils.join( versionPatterns, '|' ), Pattern.CASE_INSENSITIVE ); + /** *

* Tests if the unknown string contains elements that identify it as a version string (or not). @@ -85,7 +85,6 @@ public class VersionUtil { String versionParts[] = StringUtils.split( unknown, '-' ); - Pattern pat = Pattern.compile( VersionMegaPattern, Pattern.CASE_INSENSITIVE ); Matcher mat; int countValidParts = 0; @@ -93,7 +92,7 @@ public class VersionUtil for ( int i = 0; i < versionParts.length; i++ ) { String part = versionParts[i]; - mat = pat.matcher( part ); + mat = VERSION_MEGA_PATTERN.matcher( part ); if ( mat.matches() ) { @@ -124,8 +123,7 @@ public class VersionUtil */ public static boolean isSimpleVersionKeyword( String identifier ) { - Pattern pat = Pattern.compile( VersionMegaPattern, Pattern.CASE_INSENSITIVE ); - Matcher mat = pat.matcher( identifier ); + Matcher mat = VERSION_MEGA_PATTERN.matcher( identifier ); return mat.matches(); } diff --git a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/ArtifactClassifierMapping.java b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/ArtifactClassifierMapping.java index e975f030c..3e2de1812 100644 --- a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/ArtifactClassifierMapping.java +++ b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/ArtifactClassifierMapping.java @@ -19,12 +19,8 @@ package org.apache.maven.archiva.repository.content; * under the License. */ -import org.apache.commons.lang.StringUtils; - -import java.io.File; import java.util.HashMap; import java.util.Map; -import java.util.regex.Pattern; /** * ArtifactExtensionMapping @@ -38,9 +34,9 @@ public class ArtifactClassifierMapping static { typeToClassifierMap = new HashMap(); - typeToClassifierMap.put( "java-source", "sources" ); - typeToClassifierMap.put( "javadoc.jar", "javadoc" ); - typeToClassifierMap.put( "javadoc", "javadoc" ); + typeToClassifierMap.put( "java-sources", "sources" ); + typeToClassifierMap.put( "javadoc.jars", "javadoc" ); + typeToClassifierMap.put( "javadocs", "javadoc" ); } public static String getClassifier( String type ) diff --git a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/ArtifactExtensionMapping.java b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/ArtifactExtensionMapping.java index 9edf48891..699f91352 100644 --- a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/ArtifactExtensionMapping.java +++ b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/ArtifactExtensionMapping.java @@ -19,9 +19,6 @@ package org.apache.maven.archiva.repository.content; * under the License. */ -import org.apache.commons.lang.StringUtils; - -import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; @@ -70,60 +67,44 @@ public class ArtifactExtensionMapping return type.replace( '-', '.' ); } - public static String guessTypeFromFilename( File file ) + /** + * Determine if a given artifact Id conforms to the naming scheme for a maven plugin. + * + * @param artifactId the artifactId to test. + * @return true if this artifactId conforms to the naming scheme for a maven plugin. + */ + public static boolean isMavenPlugin( String artifactId ) { - return guessTypeFromFilename( file.getName() ); + return mavenPluginPattern.matcher( artifactId ).matches(); } - public static String guessTypeFromFilename( String filename ) + public static String mapExtensionAndClassifierToType( String classifier, String extension ) { - if ( StringUtils.isBlank( filename ) ) + if ( "sources".equals( classifier ) ) { - return null; + return "java-source"; } - - String normalizedName = filename.toLowerCase().trim(); - int idx = normalizedName.lastIndexOf( '.' ); - - if ( idx == ( -1 ) ) + else if ( "javadoc".equals( classifier ) ) { - return null; + return "javadoc"; } + return mapExtensionToType( extension ); + } - if ( normalizedName.endsWith( ".tar.gz" ) ) + public static String mapExtensionToType( String extension ) + { + if ( extension.equals( "tar.gz" ) ) { return "distribution-tgz"; } - if ( normalizedName.endsWith( ".tar.bz2" ) ) + else if ( extension.equals( "tar.bz2" ) ) { return "distribution-bzip"; } - else if ( normalizedName.endsWith( ".zip" ) ) + else if ( extension.equals( "zip" ) ) { return "distribution-zip"; } - else if ( normalizedName.endsWith( "-sources.jar" ) ) - { - return "java-source"; - } - else if ( normalizedName.endsWith( "-javadoc.jar" ) ) - { - return "javadoc"; - } - else - { - return normalizedName.substring( idx + 1 ); - } - } - - /** - * Determine if a given artifact Id conforms to the naming scheme for a maven plugin. - * - * @param artifactId the artifactId to test. - * @return true if this artifactId conforms to the naming scheme for a maven plugin. - */ - public static boolean isMavenPlugin( String artifactId ) - { - return mavenPluginPattern.matcher( artifactId ).matches(); + return extension; } } diff --git a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/DefaultPathParser.java b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/DefaultPathParser.java index 88d6dd244..c123f5734 100644 --- a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/DefaultPathParser.java +++ b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/DefaultPathParser.java @@ -147,7 +147,7 @@ public class DefaultPathParser implements PathParser artifact.setClassifier( parser.remaining() ); // Set the type. - artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) ); + artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( artifact.getClassifier(), parser.getExtension() ) ); break; case '.': // We have an dual extension possibility. @@ -156,7 +156,7 @@ public class DefaultPathParser implements PathParser break; case 0: // End of the filename, only a simple extension left. - Set the type. - artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) ); + artifact.setType( ArtifactExtensionMapping.mapExtensionToType( parser.getExtension() ) ); break; } diff --git a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/FilenameParser.java b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/FilenameParser.java index 173716523..18a2b8577 100644 --- a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/FilenameParser.java +++ b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/FilenameParser.java @@ -40,8 +40,10 @@ public class FilenameParser private static final Pattern mavenPluginPattern = Pattern.compile( "(maven-.*-plugin)|(.*-maven-plugin)" ); - private static final Pattern extensionPattern = Pattern.compile( "(.tar.gz$)|(.tar.bz2$)|(.[a-z0-9]*$)", - Pattern.CASE_INSENSITIVE ); + private static final Pattern extensionPattern = + Pattern.compile( "(.tar.gz$)|(.tar.bz2$)|(.[a-z0-9]*$)", Pattern.CASE_INSENSITIVE ); + + private static final Pattern SNAPSHOT_PATTERN = Pattern.compile( "^([0-9]{8}\\.[0-9]{6}-[0-9]+)(.*)$" ); private static final Pattern section = Pattern.compile( "([^-]*)" ); @@ -90,16 +92,39 @@ public class FilenameParser protected String expect( String expected ) { + String value = null; + if ( name.startsWith( expected, offset ) ) + { + value = expected; + } + else if ( VersionUtil.isGenericSnapshot( expected ) ) + { + String version = name.substring( offset ); + + // check it starts with the same version up to the snapshot part + int leadingLength = expected.length() - 9; + if ( version.startsWith( expected.substring( 0, leadingLength ) ) && version.length() > leadingLength ) + { + // If we expect a non-generic snapshot - look for the timestamp + Matcher m = SNAPSHOT_PATTERN.matcher( version.substring( leadingLength + 1 ) ); + if ( m.matches() ) + { + value = version.substring( 0, leadingLength + 1 ) + m.group( 1 ); + } + } + } + + if ( value != null ) { // Potential hit. check for '.' or '-' at end of expected. - int seperatorOffset = offset + expected.length(); + int seperatorOffset = offset + value.length(); // Test for "out of bounds" first. if ( seperatorOffset >= name.length() ) { offset = name.length(); - return expected; + return value; } // Test for seperator char. @@ -107,16 +132,16 @@ public class FilenameParser if ( ( seperatorChar == '-' ) || ( seperatorChar == '.' ) ) { offset = seperatorOffset + 1; - return expected; + return value; } } return null; } - + /** * Get the current seperator character. - * + * * @return the seperator character (either '.' or '-'), or 0 if no seperator character available. */ protected char seperator() @@ -230,5 +255,5 @@ public class FilenameParser return ver.toString(); } - + } diff --git a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/LegacyPathParser.java b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/LegacyPathParser.java index 8861b892e..e1d5eaba3 100644 --- a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/LegacyPathParser.java +++ b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/LegacyPathParser.java @@ -19,16 +19,15 @@ package org.apache.maven.archiva.repository.content; * under the License. */ -import java.util.Collection; -import java.util.Iterator; -import java.util.Properties; - import org.apache.commons.lang.StringUtils; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.LegacyArtifactPath; import org.apache.maven.archiva.model.ArtifactReference; import org.apache.maven.archiva.repository.layout.LayoutException; +import java.util.Collection; +import java.util.Iterator; + /** * LegacyPathParser is a parser for maven 1 (legacy layout) paths to * ArtifactReference. @@ -163,8 +162,23 @@ public class LegacyPathParser } } + String classifier = ArtifactClassifierMapping.getClassifier( expectedType ); + if ( classifier != null ) + { + String version = artifact.getVersion(); + if ( ! version.endsWith( "-" + classifier ) ) + { + throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier ); + } + version = version.substring( 0, version.length() - classifier.length() - 1 ); + artifact.setVersion( version ); + artifact.setClassifier( classifier ); + } + + String extension = parser.getExtension(); + // Set Type - artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) ); + artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension ) ); // Sanity Check: does it have an extension? if ( StringUtils.isEmpty( artifact.getType() ) ) @@ -183,27 +197,20 @@ public class LegacyPathParser String trimPathType = expectedType.substring( 0, expectedType.length() - 1 ); String expectedExtension = ArtifactExtensionMapping.getExtension( trimPathType ); - String actualExtension = parser.getExtension(); - if ( !expectedExtension.equals( actualExtension ) ) + if ( !expectedExtension.equals( extension ) ) { - throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + actualExtension + throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + extension + "] and layout specified type [" + expectedType + "] (which maps to extension: [" + expectedExtension + "]) on path [" + path + "]" ); } - } - String classifier = ArtifactClassifierMapping.getClassifier( artifact.getType() ); - if ( classifier != null ) - { - String version = artifact.getVersion(); - if ( ! version.endsWith( "-" + classifier ) ) - { - throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier ); - } - version = version.substring( 0, version.length() - classifier.length() - 1 ); - artifact.setVersion( version ); - artifact.setClassifier( classifier ); +// if ( classifier != null && !filename.endsWith( "-" + classifier + "." + extension ) ) +// { +// throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on filename [" + filename +// + "] and layout specified type [" + expectedType + "] (which maps to classifier: [" +// + classifier + "]) on path [" + path + "]" ); +// } } return artifact; diff --git a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/DefaultPathParserTest.java b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/DefaultPathParserTest.java index 9d13bd104..5b0e103fe 100644 --- a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/DefaultPathParserTest.java +++ b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/DefaultPathParserTest.java @@ -259,6 +259,40 @@ public class DefaultPathParserTest assertLayout( path, groupId, artifactId, version, classifier, type ); } + /** + * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory. + * @throws LayoutException + */ + public void testGoodLongSnapshotMavenTest() + throws LayoutException + { + String groupId = "a.group.id"; + String artifactId = "artifact-id"; + String version = "1.0-abc-1.1-20080221.062205-9"; + String classifier = null; + String type = "pom"; + String path = "a/group/id/artifact-id/1.0-abc-1.1-SNAPSHOT/artifact-id-1.0-abc-1.1-20080221.062205-9.pom"; + + assertLayout( path, groupId, artifactId, version, classifier, type ); + } + + /** + * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory. + * @throws LayoutException + */ + public void testClassifiedSnapshotMavenTest() + throws LayoutException + { + String groupId = "a.group.id"; + String artifactId = "artifact-id"; + String version = "1.0-20070219.171202-34"; + String classifier = "test-sources"; + String type = "jar"; + String path = "a/group/id/artifact-id/1.0-SNAPSHOT/artifact-id-1.0-20070219.171202-34-test-sources.jar"; + + assertLayout( path, groupId, artifactId, version, classifier, type ); + } + /** * [MRM-519] version identifiers within filename cause misidentification of version. * Example uses "test" in artifact Id, which is also part of the versionKeyword list. diff --git a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/FilenameParserTest.java b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/FilenameParserTest.java index 23924aa55..f95f367b3 100644 --- a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/FilenameParserTest.java +++ b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/FilenameParserTest.java @@ -149,4 +149,34 @@ public class FilenameParserTest assertEquals( "libfobs4jmf-0.4.1.4-20080217.211715-4", parser.getName() ); assertEquals( "jnilib", parser.getExtension() ); } + + public void testInterveningVersion() + { + FilenameParser parser = new FilenameParser( "artifact-id-1.0-abc-1.1-20080221.062205-9.pom" ); + + assertEquals( "artifact-id", parser.nextNonVersion() ); + assertEquals( "1.0-abc-1.1-20080221.062205-9", parser.expect( "1.0-abc-1.1-SNAPSHOT" ) ); + assertNull( null, parser.remaining() ); + assertEquals( "artifact-id-1.0-abc-1.1-20080221.062205-9", parser.getName() ); + assertEquals( "pom", parser.getExtension() ); + } + + public void testExpectWrongSnapshot() + { + FilenameParser parser = new FilenameParser( "artifact-id-1.0-20080221.062205-9.pom" ); + + assertEquals( "artifact-id", parser.nextNonVersion() ); + assertNull( parser.expect( "2.0-SNAPSHOT" ) ); + } + + public void testClassifier() + { + FilenameParser parser = new FilenameParser( "artifact-id-1.0-20070219.171202-34-test-sources.jar" ); + + assertEquals( "artifact-id", parser.nextNonVersion() ); + assertEquals( "1.0-20070219.171202-34", parser.nextVersion() ); + assertEquals( "test-sources", parser.remaining() ); + assertEquals( "artifact-id-1.0-20070219.171202-34-test-sources", parser.getName() ); + assertEquals( "jar", parser.getExtension() ); + } } diff --git a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/LegacyPathParserTest.java b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/LegacyPathParserTest.java index b21bcacb5..0267e2aba 100644 --- a/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/LegacyPathParserTest.java +++ b/archiva-1.0.x/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/LegacyPathParserTest.java @@ -21,7 +21,6 @@ package org.apache.maven.archiva.repository.content; import org.apache.maven.archiva.configuration.ArchivaConfiguration; -import org.apache.maven.archiva.configuration.DefaultArchivaConfiguration; import org.apache.maven.archiva.configuration.LegacyArtifactPath; import org.apache.maven.archiva.model.ArtifactReference; import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase; @@ -212,6 +211,8 @@ public class LegacyPathParserTest String path = "com.foo.lib/javadoc.jars/foo-lib-2.1-alpha-1-javadoc.jar"; assertLayout( path, groupId, artifactId, version, classifier, type ); + + assertLayout( "com.foo.lib/javadocs/foo-lib-2.1-alpha-1-javadoc.jar", "com.foo.lib", "foo-lib", "2.1-alpha-1", "javadoc", "javadoc" ); } /** @@ -231,6 +232,43 @@ public class LegacyPathParserTest assertLayout( path, groupId, artifactId, version, classifier, type ); } + /** + * Test the classifier, and java-source type spec. + * @throws LayoutException + */ + public void testBadClassifierFooLibSources() + throws LayoutException + { + String path = "com.foo.lib/java-sources/foo-lib-2.1-alpha-1.jar"; + + try + { + parser.toArtifactReference( path ); + fail( "Expected an exception" ); + } + catch ( LayoutException e ) + { + assertTrue( true ); + } + } + + /** + * Test the classifier, and java-source type spec. + * @throws LayoutException + */ + public void testGoodFooLibTestSources() + throws LayoutException + { + String groupId = "com.foo.lib"; + String artifactId = "foo-lib"; + String version = "2.1-alpha-1-test-sources"; + String type = "jar"; + String classifier = null; // we can't parse this type of classifier in legacy format + String path = "com.foo.lib/jars/foo-lib-2.1-alpha-1-test-sources.jar"; + + assertLayout( path, groupId, artifactId, version, classifier, type ); + } + public void testGoodFooTool() throws LayoutException {