From b96a6851aa8571121ab5b34301aad6b3b880aeaf Mon Sep 17 00:00:00 2001 From: "Edwin L. Punzalan" Date: Mon, 13 Mar 2006 11:17:31 +0000 Subject: [PATCH] unloaded ArtifactUtils.buildArtifact() not to handle m1 paths and created buildArtifactFromLegacyPath to do it instead. Added unit tests for the legacy path git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@385513 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/repository/ArtifactUtils.java | 238 ++++++++++++++++-- .../repository/ArtifactUtilsLegacyTest.java | 110 ++++++++ 2 files changed, 330 insertions(+), 18 deletions(-) create mode 100644 maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java diff --git a/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java b/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java index 85683bcdc..e302854f7 100644 --- a/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java +++ b/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java @@ -10,6 +10,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; +import java.util.LinkedList; +import java.util.Iterator; /* * Copyright 2005-2006 The Apache Software Foundation. @@ -191,35 +193,235 @@ public class ArtifactUtils } } } - else if ( pathParts.size() == 3 ) + + return artifact; + } + + public static Artifact buildArtifactFromLegacyPath( String path, ArtifactFactory artifactFactory ) + { + StringTokenizer tokens = new StringTokenizer( path, "/\\" ); + + Artifact result = null; + + int numberOfTokens = tokens.countTokens(); + + if ( numberOfTokens == 3 ) { - //maven 1.x path + String groupId = tokens.nextToken(); - String filename = (String) pathParts.remove( 0 ); + String type = tokens.nextToken(); - int idx = filename.lastIndexOf( '-' ); - if ( idx > 0 ) + if ( type.endsWith( "s" ) ) { - String extension = filename.substring( filename.lastIndexOf( '.' ) + 1 ); + type = type.substring( 0, type.length() - 1 ); - String version = filename.substring( idx + 1, filename.lastIndexOf( '.' ) ); + // contains artifactId, version, classifier, and extension. + String avceGlob = tokens.nextToken(); - String artifactId = filename.substring( 0, idx ); + LinkedList avceTokenList = new LinkedList(); - String types = (String) pathParts.remove( 0 ); - - // remove the "s" in types - String type = types.substring( 0, types.length() -1 ); - - if ( type.equals( extension ) ) + StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" ); + while ( avceTokenizer.hasMoreTokens() ) { - String groupId = (String) pathParts.remove( 0 ); + avceTokenList.addLast( avceTokenizer.nextToken() ); + } - artifact = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type ); + String lastAvceToken = (String) avceTokenList.removeLast(); + + boolean valid = true; + + // TODO: share with other discoverer, use artifact handlers instead + if ( lastAvceToken.endsWith( ".tar.gz" ) ) + { + type = "distribution-tgz"; + + lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() ); + + avceTokenList.addLast( lastAvceToken ); + } + else if ( lastAvceToken.endsWith( "sources.jar" ) ) + { + type = "java-source"; + + lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() ); + + avceTokenList.addLast( lastAvceToken ); + } + else if ( lastAvceToken.endsWith( ".zip" ) ) + { + type = "distribution-zip"; + + lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() ); + + avceTokenList.addLast( lastAvceToken ); + } + else + { + int extPos = lastAvceToken.lastIndexOf( '.' ); + + if ( extPos > 0 ) + { + String ext = lastAvceToken.substring( extPos + 1 ); + if ( type.equals( ext ) ) + { + lastAvceToken = lastAvceToken.substring( 0, extPos ); + + avceTokenList.addLast( lastAvceToken ); + } + else + { + //type does not match extension + valid = false; + } + } + else + { + // no extension + valid = false; + } + } + + if ( valid ) + { + // let's discover the version, and whatever's leftover will be either + // a classifier, or part of the artifactId, depending on position. + // Since version is at the end, we have to move in from the back. + Collections.reverse( avceTokenList ); + + // TODO: this is obscene - surely a better way? + String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" + + "([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" + + "([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" + + "([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" + + "([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" + + "([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" + + "([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "([AaBb][_.0-9]*)"; + + StringBuffer classifierBuffer = new StringBuffer(); + StringBuffer versionBuffer = new StringBuffer(); + + boolean firstVersionTokenEncountered = false; + boolean firstToken = true; + + int tokensIterated = 0; + for ( Iterator it = avceTokenList.iterator(); it.hasNext(); ) + { + String token = (String) it.next(); + + boolean tokenIsVersionPart = token.matches( validVersionParts ); + + StringBuffer bufferToUpdate; + + // NOTE: logic in code is reversed, since we're peeling off the back + // Any token after the last versionPart will be in the classifier. + // Any token UP TO first non-versionPart is part of the version. + if ( !tokenIsVersionPart ) + { + if ( firstVersionTokenEncountered ) + { + //noinspection BreakStatement + break; + } + else + { + bufferToUpdate = classifierBuffer; + } + } + else + { + firstVersionTokenEncountered = true; + + bufferToUpdate = versionBuffer; + } + + if ( firstToken ) + { + firstToken = false; + } + else + { + bufferToUpdate.insert( 0, '-' ); + } + + bufferToUpdate.insert( 0, token ); + + tokensIterated++; + } + + // Now, restore the proper ordering so we can build the artifactId. + Collections.reverse( avceTokenList ); + + // if we didn't find a version, then punt. Use the last token + // as the version, and set the classifier empty. + if ( versionBuffer.length() < 1 ) + { + if ( avceTokenList.size() > 1 ) + { + int lastIdx = avceTokenList.size() - 1; + + versionBuffer.append( avceTokenList.get( lastIdx ) ); + avceTokenList.remove( lastIdx ); + } + + classifierBuffer.setLength( 0 ); + } + else + { + // if everything is kosher, then pop off all the classifier and + // version tokens, leaving the naked artifact id in the list. + avceTokenList = + new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) ); + } + + StringBuffer artifactIdBuffer = new StringBuffer(); + + firstToken = true; + for ( Iterator it = avceTokenList.iterator(); it.hasNext(); ) + { + String token = (String) it.next(); + + if ( firstToken ) + { + firstToken = false; + } + else + { + artifactIdBuffer.append( '-' ); + } + + artifactIdBuffer.append( token ); + } + + String artifactId = artifactIdBuffer.toString(); + + if ( artifactId.length() > 0 ) + { + int lastVersionCharIdx = versionBuffer.length() - 1; + if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' ) + { + versionBuffer.setLength( lastVersionCharIdx ); + } + + String version = versionBuffer.toString(); + + if ( version.length() >= 1 ) + { + if ( classifierBuffer.length() > 0 ) + { + result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, + classifierBuffer.toString() ); + } + else + { + result = artifactFactory.createArtifact( groupId, artifactId, version, + Artifact.SCOPE_RUNTIME, type ); + } + } + } } } } - return artifact; + return result; } -} +} \ No newline at end of file diff --git a/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java b/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java new file mode 100644 index 000000000..cf7ae25d6 --- /dev/null +++ b/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java @@ -0,0 +1,110 @@ +package org.apache.maven.repository; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; + +/** + * @author Edwin Punzalan + */ +public class ArtifactUtilsLegacyTest + extends PlexusTestCase +{ + private ArtifactFactory factory; + + protected void setUp() + throws Exception + { + super.setUp(); + + factory = (ArtifactFactory) container.lookup( ArtifactFactory.ROLE ); + } + + protected void tearDown() + throws Exception + { + container.release( factory ); + + super.tearDown(); + } + + public void testWrongArtifactPackaging() + throws ComponentLookupException + { + String testPath = "org.apache.maven.test/jars/artifactId-1.0.jar.md5"; + + Artifact artifact = getArtifactFromPath( testPath ); + + assertNull( "Artifact should be null for wrong package extension", artifact ); + } + + public void testNoArtifactid() + { + String testPath = "groupId/jars/-1.0.jar"; + + Artifact artifact = getArtifactFromPath( testPath ); + + assertNull( "Artifact should be null when artifactId is missing", artifact ); + + testPath = "groupId/jars/1.0.jar"; + + artifact = getArtifactFromPath( testPath ); + + assertNull( "Artifact should be null when artifactId is missing", artifact ); + } + + public void testNoType() + throws ComponentLookupException + { + String testPath = "invalid/invalid/1/invalid-1"; + + Artifact artifact = getArtifactFromPath( testPath ); + + assertNull( "Artifact should be null for no type", artifact ); + } + + public void testSnapshot() + throws ComponentLookupException + { + String testPath = "org.apache.maven.test/jars/maven-model-1.0-SNAPSHOT.jar"; + + Artifact artifact = getArtifactFromPath( testPath ); + + assertNotNull( "Artifact path with invalid snapshot error", artifact ); + + assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-SNAPSHOT" ), artifact ); + } + + public void testNormal() + throws ComponentLookupException + { + String testPath = "javax.sql/jars/jdbc-2.0.jar"; + + Artifact artifact = getArtifactFromPath( testPath ); + + assertNotNull( "Normal artifact path error", artifact ); + + assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact ); + } + + private Artifact getArtifactFromPath( String path ) + { + return ArtifactUtils.buildArtifactFromLegacyPath( path, factory ); + } + + private Artifact createArtifact( String groupId, String artifactId, String version ) + { + return factory.createArtifact( groupId, artifactId, version, null, "jar" ); + } + + private Artifact createArtifact( String groupId, String artifactId, String version, String type ) + { + return factory.createArtifact( groupId, artifactId, version, null, type ); + } + + private Artifact createArtifact( String groupId, String artifactId, String version, String type, String classifier ) + { + return factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier ); + } +}