Index: D:/Work-3.2/maven-archiver/src/test/java/org/apache/maven/archiver/MockArtifact.java =================================================================== --- D:/Work-3.2/maven-archiver/src/test/java/org/apache/maven/archiver/MockArtifact.java (revision 422999) +++ D:/Work-3.2/maven-archiver/src/test/java/org/apache/maven/archiver/MockArtifact.java (working copy) @@ -35,6 +35,10 @@ private String baseVersion; + private ArtifactHandler artifactHandler; + + private boolean isSnapshot; + public String getGroupId() { return groupId; @@ -162,8 +166,7 @@ public ArtifactHandler getArtifactHandler() { - // TODO - return null; //To change body of implemented methods use File | Settings | File Templates. + return artifactHandler; } public List getDependencyTrail() @@ -196,10 +199,14 @@ //To change body of implemented methods use File | Settings | File Templates. } + public void setSnapshot( boolean b ) + { + isSnapshot = b; + } + public boolean isSnapshot() { - // TODO - return false; //To change body of implemented methods use File | Settings | File Templates. + return isSnapshot; } public void setResolved( boolean b ) @@ -222,8 +229,7 @@ public void setArtifactHandler( ArtifactHandler artifactHandler ) { - // TODO - //To change body of implemented methods use File | Settings | File Templates. + this.artifactHandler = artifactHandler; } public boolean isRelease() Index: D:/Work-3.2/maven-archiver/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java =================================================================== --- D:/Work-3.2/maven-archiver/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java (revision 422999) +++ D:/Work-3.2/maven-archiver/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java (working copy) @@ -3,175 +3,385 @@ /* * Copyright 2001-2006 The Apache Software Foundation. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. */ +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import junit.framework.TestCase; import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.model.Model; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.archiver.jar.Manifest; -import java.io.File; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - public class MavenArchiverTest extends TestCase { - static class ArtifactComparator implements Comparator { - public int compare( Object o1, Object o2 ) - { - return ((Artifact) o1).getArtifactId().compareTo(((Artifact) o2).getArtifactId()); - } - public boolean equals(Object o) { return false; } + + /** + * Test the extension list is empty if there are no runtime artifacts. + * + * @throws Exception + * + */ + public void testEmptyManifestExensionListWhenNoArtifacts() + throws Exception + { + MavenArchiver objectUnderTest = new MavenArchiver(); + + MavenProject project = createProject(); + + assertManifestExtensionListIsEmpty( objectUnderTest.getManifest( project, manifestConfigWithExtensions ) ); } - public void testGetManifestExtensionList() throws Exception + /** + * Test the extension list does not include non-jar type artifacts + * + * @throws Exception + * + */ + public void testEmptyManifestExensionListWhenOnlyNonJarArtifacts() + throws Exception { - MavenArchiver archiver = new MavenArchiver(); + MavenArchiver objectUnderTest = new MavenArchiver(); - Model model = new Model(); - model.setArtifactId( "dummy" ); + MavenProject project = createProject(); - MavenProject project = new MavenProject( model ); - // we need to sort the artifacts for test purposes - Set artifacts = new TreeSet( new ArtifactComparator() ); - project.setArtifacts( artifacts ); + Artifact dllArtifact = createArtifactDll(); + project.getArtifacts().add( dllArtifact ); - // there should be a mock or a setter for this field. - ManifestConfiguration config = new ManifestConfiguration() - { - public boolean isAddExtensions() - { - return true; - } - }; + assertManifestExtensionListIsEmpty( objectUnderTest.getManifest( project, manifestConfigWithExtensions ) ); + } - Manifest manifest; + /** + * Test that the extension list is populated correctly. + * + * @throws Exception + */ + public void testGetManifestExtensionList() + throws Exception + { + MavenArchiver objectUnderTest = new MavenArchiver(); - manifest = archiver.getManifest( project, config ); + MavenProject project = createProject(); - assertNotNull( manifest.getMainSection() ); + project.getArtifacts().add( createArtifactDll() ); + project.getArtifacts().add( createArtifactJarWithCompileScope( "dummy2" ) ); + project.getArtifacts().add( createArtifactJarWithTestScope() ); + project.getArtifacts().add( createArtifactJarWithCompileScope( "dummy4" ) ); - java.util.Enumeration enume = manifest.getSectionNames(); - while (enume.hasMoreElements()) { - Manifest.Section section = manifest.getSection(enume.nextElement().toString()); - System.out.println( section + " " + section.getAttributeValue( "Extension-List" ) ); - } + Manifest manifest = objectUnderTest.getManifest( project, manifestConfigWithExtensions ); - assertEquals( null, - manifest.getMainSection().getAttributeValue( "Extension-List" ) ); + assertManifestExtensionListEquals( new String[] { "dummy4", "dummy2" }, manifest ); + } - MockArtifact artifact1 = new MockArtifact(); - artifact1.setGroupId( "org.apache.dummy" ); - artifact1.setArtifactId( "dummy1" ); - artifact1.setVersion( "1.0" ); - artifact1.setType( "dll" ); - artifact1.setScope( "compile" ); + /** + * A snapshot artifact that is installed should be just ${artifactId}-${versionId} on the + * classpath. + * + * @throws Exception + * + */ + public void testMNG_2456_InstalledSnapshot() + throws Exception + { + MavenArchiver objectUnderTest = new MavenArchiver(); - artifacts.add( artifact1 ); + MavenProject project = createProject(); + Artifact artifact = createInstalledSnapshotArtifact(); + project.getArtifacts().add( artifact ); - manifest = archiver.getManifest( project, config ); + Manifest manifest = objectUnderTest.getManifest( project, manifestConfigWithClasspath ); - assertEquals( null, - manifest.getMainSection().getAttributeValue( "Extension-List" ) ); + assertManifestClasspathEquals( new String[] { artifact.getArtifactId() + "-" + artifact.getVersion() }, + manifest ); + } - MockArtifact artifact2 = new MockArtifact(); - artifact2.setGroupId( "org.apache.dummy" ); - artifact2.setArtifactId( "dummy2" ); - artifact2.setVersion( "1.0" ); - artifact2.setType( "jar" ); - artifact2.setScope( "compile" ); + /** + * A snapshot artifact that has been deployed should not be the same as an installed artifact, + * i.e. ${artifactId}-${versionId} but the versionId should be the timestamped identifier. + * + * @throws Exception + */ + public void testMNG_2456_DeployedSnapshot() + throws Exception + { + MavenArchiver objectUnderTest = new MavenArchiver(); - artifacts.add( artifact2 ); + MavenProject project = createProject(); + Artifact artifact = createDeployedSnapshotArtifact(); + project.getArtifacts().add( artifact ); - manifest = archiver.getManifest( project, config ); + Manifest manifest = objectUnderTest.getManifest( project, manifestConfigWithClasspath ); - assertEquals( "dummy2", - manifest.getMainSection().getAttributeValue( "Extension-List" ) ); + assertManifestClasspathEquals( new String[] { artifact.getArtifactId() + "-" + artifact.getVersion() }, + manifest ); + } - MockArtifact artifact3 = new MockArtifact(); - artifact3.setGroupId( "org.apache.dummy" ); - artifact3.setArtifactId( "dummy3" ); - artifact3.setVersion( "1.0" ); - artifact3.setScope( "test" ); - artifact3.setType( "jar" ); + /** + * Test that the artifact's used at runtime and any user specified classpath values are included + * in the Manifest. + * + * @throws Exception + */ + public void testSupportsUserDefinedClasspath() + throws Exception + { + MavenArchiver objectUnderTest = new MavenArchiver(); - artifacts.add( artifact3 ); + MavenProject project = createProject(); + Artifact artifact = createArtifactJarWithCompileScope( "testArtifact" ); + project.getArtifacts().add( artifact ); - manifest = archiver.getManifest( project, config ); + MavenArchiveConfiguration archiveConfiguration = new MavenArchiveConfiguration(); + archiveConfiguration.setManifest( manifestConfigWithClasspath ); + archiveConfiguration.addManifestEntry( "Class-Path", "help/" ); - assertEquals( "dummy2", - manifest.getMainSection().getAttributeValue( "Extension-List" ) ); + Manifest manifest = objectUnderTest.getManifest( project, archiveConfiguration ); + assertManifestClasspathEquals( + new String[] { "help/", artifact.getArtifactId() + "-" + artifact.getVersion() }, + manifest ); + } - MockArtifact artifact4 = new MockArtifact(); - artifact4.setGroupId( "org.apache.dummy" ); - artifact4.setArtifactId( "dummy4" ); - artifact4.setVersion( "1.0" ); - artifact4.setType( "jar" ); - artifact4.setScope( "compile" ); + /** + * Test that when isAddClasspath is true that the classpath is not set in the Manifest when + * there are no runtime artifacts linked to the project. + * + * @throws Exception + */ + public void testEmptyClasspathWhenNoRuntimeArtifactsInUse() + throws Exception + { + MavenArchiver objectUnderTest = new MavenArchiver(); - artifacts.add( artifact4 ); + MavenProject project = createProject(); - manifest = archiver.getManifest( project, config ); + MavenArchiveConfiguration archiveConfiguration = new MavenArchiveConfiguration(); + archiveConfiguration.setManifest( manifestConfigWithClasspath ); - assertEquals( "dummy2 dummy4", - manifest.getMainSection().getAttributeValue( "Extension-List" ) ); + Manifest manifest = objectUnderTest.getManifest( project, archiveConfiguration ); + assertManifestClasspathIsEmpty( manifest ); } - public void testMultiClassPath() throws Exception { - final File tempFile = File.createTempFile( "maven-archiver-test-", ".jar" ); + /** + * Test that when isAddClasspath is false that the classpath is not set in the Manifest even + * though there are runtime artifacts linked to the project. + * + * @throws Exception + */ + public void testEmptyClasspathWhenRuntimeArtifactsInUseAndAddClasspathNotEnabled() + throws Exception + { + MavenArchiver objectUnderTest = new MavenArchiver(); - try { - MavenArchiver archiver = new MavenArchiver(); + MavenProject project = createProject(); + project.getArtifacts().add( createArtifactJarWithCompileScope( "ignored" ) ); - Model model = new Model(); - model.setArtifactId( "dummy" ); + MavenArchiveConfiguration archiveConfiguration = new MavenArchiveConfiguration(); + archiveConfiguration.setManifest( manifestConfigWithoutClasspath ); + Manifest manifest = objectUnderTest.getManifest( project, archiveConfiguration ); + assertManifestClasspathIsEmpty( manifest ); + } - MavenProject project = new MavenProject( model ) { - public List getRuntimeClasspathElements() { - return Collections.singletonList( tempFile.getAbsolutePath() ); - } - }; + private void assertManifestClasspathEquals( String[] expectedClasspath, Manifest manifest ) + { + String classpath = getClasspathFromManifest( manifest ); + assertNotNull( "Manifest Class-Path must not be null", classpath ); + String[] classpathEntries = classpath.split( " " ); - // there should be a mock or a setter for this field. - ManifestConfiguration manifestConfig = new ManifestConfiguration() + List classpathEntriesAsList = Arrays.asList( classpathEntries ); + Collections.sort( classpathEntriesAsList ); + List expectedClasspathEntriesAsList = Arrays.asList( expectedClasspath ); + Collections.sort( expectedClasspathEntriesAsList ); + + assertEquals( "Manifest Class-Path does not match expected classpath", expectedClasspathEntriesAsList, + classpathEntriesAsList ); + } + + private void assertManifestExtensionListEquals( String[] expectedExtensionList, Manifest manifest ) + { + String extensionList = getManifestExtensionList( manifest ); + assertNotNull( "Manifest Extension-List must not be null", extensionList ); + String[] extensionListEntries = extensionList.split( " " ); + + List extensionListEntriesAsList = Arrays.asList( extensionListEntries ); + Collections.sort( extensionListEntriesAsList ); + List expectedClasspathEntriesAsList = Arrays.asList( expectedExtensionList ); + Collections.sort( expectedClasspathEntriesAsList ); + + assertEquals( "Manifest Extension-List does not match expected extensionList", expectedClasspathEntriesAsList, + extensionListEntriesAsList ); + } + + private void assertManifestClasspathIsEmpty( Manifest manifest ) + { + String classpath = getClasspathFromManifest( manifest ); + assertNull( "Manifest Class-Path is not null", classpath ); + } + + private void assertManifestExtensionListIsEmpty( Manifest manifest ) + { + assertNull( "Manifest Extension-List is not null", getManifestExtensionList( manifest ) ); + } + + private String getManifestExtensionList( Manifest manifest ) + { + assertNotNull( manifest.getMainSection() ); + return manifest.getMainSection().getAttributeValue( "Extension-List" ); + } + + private String getClasspathFromManifest( Manifest manifest ) + { + assertNotNull( manifest.getMainSection() ); + return manifest.getMainSection().getAttributeValue( "Class-Path" ); + } + + private MavenProject createProject() + { + Model model = new Model(); + model.setArtifactId( "dummy" ); + + MavenProject project = new MavenProject( model ); + Set artifacts = new HashSet(); + project.setArtifacts( artifacts ); + + return project; + } + + private MockArtifact createMockArtifact() + { + MockArtifact artifact = new MockArtifact(); + ArtifactHandler artifactHandler = new DefaultArtifactHandler() + { + + public boolean isAddedToClasspath() { - public boolean isAddClasspath() - { - return true; - } - }; + return true; + } - MavenArchiveConfiguration archiveConfiguration = new MavenArchiveConfiguration(); - archiveConfiguration.setManifest( manifestConfig ); - archiveConfiguration.addManifestEntry( "Class-Path", "help/" ); + }; + artifact.setArtifactHandler( artifactHandler ); + return artifact; + } - Manifest manifest = archiver.getManifest( project, archiveConfiguration ); - String classPath = manifest.getMainSection().getAttribute( "Class-Path" ).getValue(); - assertTrue( "User specified Class-Path entry was not added to manifest", classPath.indexOf( "help/" ) != -1 ); - assertTrue( "Class-Path generated by addClasspath was not added to manifest", classPath.indexOf( tempFile.getName() ) != -1 ); - } finally { - tempFile.delete(); - } + private Artifact createArtifactDll() + { + MockArtifact artifact = createMockArtifact(); + artifact.setGroupId( "org.apache.dummy" ); + artifact.setArtifactId( "dummy1" ); + artifact.setVersion( "1.0" ); + artifact.setType( "dll" ); + artifact.setScope( "compile" ); + initialiseFile( artifact ); + return artifact; + } + private Artifact createArtifactJarWithTestScope() + { + MockArtifact artifact = createMockArtifact(); + artifact.setGroupId( "org.apache.dummy" ); + artifact.setArtifactId( "dummy3" ); + artifact.setVersion( "1.0" ); + artifact.setScope( "test" ); + artifact.setType( "jar" ); + initialiseFile( artifact ); + return artifact; } + private Artifact createArtifactJarWithCompileScope( String artifactId ) + { + MockArtifact artifact = createMockArtifact(); + artifact.setGroupId( "org.apache.dummy" ); + artifact.setArtifactId( artifactId ); + artifact.setVersion( "1.0" ); + artifact.setType( "jar" ); + artifact.setScope( "compile" ); + initialiseFile( artifact ); + return artifact; + } + + private Artifact createInstalledSnapshotArtifact() + { + MockArtifact artifact = createMockArtifact(); + artifact.setGroupId( "org.apache.dummy" ); + artifact.setArtifactId( "installedSnapshot" ); + artifact.setVersion( "1.0-SNAPSHOT" ); + artifact.setSnapshot( true ); + artifact.setType( "jar" ); + artifact.setScope( "compile" ); + initialiseFile( artifact ); + return artifact; + } + + private Artifact createDeployedSnapshotArtifact() + { + MockArtifact artifact = createMockArtifact(); + artifact.setGroupId( "org.apache.dummy" ); + artifact.setArtifactId( "deployedSnapshot" ); + artifact.setVersion( "1.0-20060705.230724-7" ); + artifact.setSnapshot( true ); + artifact.setType( "jar" ); + artifact.setScope( "compile" ); + initialiseDeployedSnapshotFile( artifact, "1.0-SNAPSHOT" ); + return artifact; + } + + private void initialiseFile( MockArtifact artifact ) + { + String pathname = artifact.getGroupId().replaceAll( ".", "/" ) + "/" + artifact.getArtifactId() + "/" + + artifact.getBaseVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion(); + File file = new File( pathname ); + artifact.setFile( file ); + } + + private void initialiseDeployedSnapshotFile( MockArtifact artifact, String versionPartOfFilename ) + { + String pathname = artifact.getGroupId().replaceAll( ".", "/" ) + "/" + artifact.getArtifactId() + "/" + + artifact.getBaseVersion() + "/" + artifact.getArtifactId() + "-" + versionPartOfFilename; + File file = new File( pathname ); + artifact.setFile( file ); + } + + private final ManifestConfiguration manifestConfigWithExtensions = new ManifestConfiguration() + { + public boolean isAddExtensions() + { + return true; + } + }; + + private final ManifestConfiguration manifestConfigWithClasspath = new ManifestConfiguration() + { + public boolean isAddClasspath() + { + return true; + } + }; + + private final ManifestConfiguration manifestConfigWithoutClasspath = new ManifestConfiguration() + { + public boolean isAddClasspath() + { + return false; + } + }; + } Index: D:/Work-3.2/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java =================================================================== --- D:/Work-3.2/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java (revision 422999) +++ D:/Work-3.2/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java (working copy) @@ -23,6 +23,7 @@ import org.codehaus.plexus.archiver.jar.JarArchiver; import org.codehaus.plexus.archiver.jar.Manifest; import org.codehaus.plexus.archiver.jar.ManifestException; +import org.codehaus.plexus.util.StringUtils; import java.io.File; import java.io.FileOutputStream; @@ -129,25 +130,19 @@ if ( config.isAddClasspath() ) { StringBuffer classpath = new StringBuffer(); - List artifacts = project.getRuntimeClasspathElements(); + List artifacts = project.getRuntimeArtifacts(); String classpathPrefix = config.getClasspathPrefix(); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - File f = new File( (String) iter.next() ); - if ( f.isFile() ) - { - if ( classpath.length() > 0 ) - { - classpath.append( " " ); - } - - classpath.append( classpathPrefix ); - classpath.append( f.getName() ); - } + Artifact artifact = (Artifact) iter.next(); + File f = artifact.getFile(); + classpath.append( classpathPrefix ); + classpath.append( f.getName() ); + classpath.append( " " ); } - if ( classpath.length() > 0 ) + if ( StringUtils.isNotEmpty( classpath.toString() ) ) { // Class-Path is special and should be added to manifest even if // it is specified in the manifestEntries section Index: D:/Work-3.2/maven-archiver/pom.xml =================================================================== --- D:/Work-3.2/maven-archiver/pom.xml (revision 422999) +++ D:/Work-3.2/maven-archiver/pom.xml (working copy) @@ -1,4 +1,7 @@ - + + maven-shared-components org.apache.maven.shared @@ -25,5 +28,11 @@ maven-artifact 2.0 + + junit + junit + 3.8.1 + test +