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,8 @@ private String baseVersion; + private ArtifactHandler artifactHandler; + public String getGroupId() { return groupId; @@ -162,8 +164,7 @@ public ArtifactHandler getArtifactHandler() { - // TODO - return null; //To change body of implemented methods use File | Settings | File Templates. + return artifactHandler; } public List getDependencyTrail() @@ -222,8 +223,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) @@ -16,162 +16,294 @@ * 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" ); + /** + * 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( artifact1 ); + 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( null, - manifest.getMainSection().getAttributeValue( "Extension-List" ) ); + Manifest manifest = objectUnderTest.getManifest( project, archiveConfiguration ); - MockArtifact artifact2 = new MockArtifact(); - artifact2.setGroupId( "org.apache.dummy" ); - artifact2.setArtifactId( "dummy2" ); - artifact2.setVersion( "1.0" ); - artifact2.setType( "jar" ); - artifact2.setScope( "compile" ); + assertManifestClasspathEquals( + new String[] { "help/", artifact.getArtifactId() + "-" + artifact.getVersion() }, + manifest ); + } - artifacts.add( artifact2 ); + /** + * 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(); - manifest = archiver.getManifest( project, config ); + MavenProject project = createProject(); - assertEquals( "dummy2", - manifest.getMainSection().getAttributeValue( "Extension-List" ) ); + MavenArchiveConfiguration archiveConfiguration = new MavenArchiveConfiguration(); + archiveConfiguration.setManifest( manifestConfigWithClasspath ); - MockArtifact artifact3 = new MockArtifact(); - artifact3.setGroupId( "org.apache.dummy" ); - artifact3.setArtifactId( "dummy3" ); - artifact3.setVersion( "1.0" ); - artifact3.setScope( "test" ); - artifact3.setType( "jar" ); + Manifest manifest = objectUnderTest.getManifest( project, archiveConfiguration ); + assertManifestClasspathIsEmpty( manifest ); + } - artifacts.add( artifact3 ); + /** + * 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(); - manifest = archiver.getManifest( project, config ); + MavenProject project = createProject(); + project.getArtifacts().add( createArtifactJarWithCompileScope( "ignored" ) ); - assertEquals( "dummy2", - manifest.getMainSection().getAttributeValue( "Extension-List" ) ); + MavenArchiveConfiguration archiveConfiguration = new MavenArchiveConfiguration(); + archiveConfiguration.setManifest( manifestConfigWithoutClasspath ); + Manifest manifest = objectUnderTest.getManifest( project, archiveConfiguration ); + assertManifestClasspathIsEmpty( manifest ); + } - MockArtifact artifact4 = new MockArtifact(); - artifact4.setGroupId( "org.apache.dummy" ); - artifact4.setArtifactId( "dummy4" ); - artifact4.setVersion( "1.0" ); - artifact4.setType( "jar" ); - artifact4.setScope( "compile" ); + private void assertManifestClasspathEquals( String[] expectedClasspath, Manifest manifest ) + { + String classpath = getClasspathFromManifest( manifest ); + assertNotNull( "Manifest Class-Path must not be null", classpath ); + String[] classpathEntries = classpath.split( " " ); - artifacts.add( artifact4 ); + List classpathEntriesAsList = Arrays.asList( classpathEntries ); + Collections.sort( classpathEntriesAsList ); + List expectedClasspathEntriesAsList = Arrays.asList( expectedClasspath ); + Collections.sort( expectedClasspathEntriesAsList ); - manifest = archiver.getManifest( project, config ); + assertEquals( "Manifest Class-Path does not match expected classpath", expectedClasspathEntriesAsList, + classpathEntriesAsList ); + } - assertEquals( "dummy2 dummy4", - manifest.getMainSection().getAttributeValue( "Extension-List" ) ); + 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 ); } - public void testMultiClassPath() throws Exception { - final File tempFile = File.createTempFile( "maven-archiver-test-", ".jar" ); + private void assertManifestClasspathIsEmpty( Manifest manifest ) + { + String classpath = getClasspathFromManifest( manifest ); + assertNull( "Manifest Class-Path is not null", classpath ); + } - try { - MavenArchiver archiver = new MavenArchiver(); + private void assertManifestExtensionListIsEmpty( Manifest manifest ) + { + assertNull( "Manifest Extension-List is not null", getManifestExtensionList( manifest ) ); + } - Model model = new Model(); - model.setArtifactId( "dummy" ); + 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" ); + } - MavenProject project = new MavenProject( model ) { - public List getRuntimeClasspathElements() { - return Collections.singletonList( tempFile.getAbsolutePath() ); - } - }; + private MavenProject createProject() + { + Model model = new Model(); + model.setArtifactId( "dummy" ); - // there should be a mock or a setter for this field. - ManifestConfiguration manifestConfig = new ManifestConfiguration() + 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 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 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 +