PR: MPJAR-19

Add tests for jar manifest creation.


git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@393396 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
ltheussl 2006-04-12 06:10:38 +00:00
parent 7c06c7cadd
commit b466d78a47
10 changed files with 548 additions and 0 deletions

View File

@ -74,4 +74,13 @@
<version>1.4</version>
</dependency>
</dependencies>
<build>
<unitTest>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
</unitTest>
</build>
</project>

View File

@ -0,0 +1,211 @@
package org.apache.maven.jar;
/* ====================================================================
* Copyright 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
*
* 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.
* ====================================================================
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
/**
* Utility class for maven-jar-plugin. Currently only used for
* testing the manifest created by Maven's jar:jar goal.
*
* @author <a href="mailto:ltheussl@apache.org">Lukas Theussl</a>
* @version $Id$
*/
public class JarUtils
{
/** The manifest. */
private Manifest manifest;
/** The Map of manifest entries. */
private Map manifestEntries;
/** The main Attributes of the manifest. */
private Attributes mainAttributes;
/** The section Attributes of the manifest. */
private Attributes sectionAttributes;
/**
* Extracts the manifest from the given jar file.
* The manifest entries can then be retrieved via
* getManifestEntries() and getMainAttributes().
*
* @param theJarFile The jar file to extract the manifest from.
* @throws IOException If there is an error opening the jar file.
*/
public void extractManifestFromJar( File theJarFile ) throws IOException
{
JarFile jarfile = new JarFile( theJarFile );
this.manifest = jarfile.getManifest();
extractManifestEntries();
}
/**
* Extracts the manifest from a given file.
* The manifest entries can then be retrieved via
* getManifestEntries() and getMainAttributes().
*
* @param manifestFile The manifest file.
* @throws IOException If there is an error opening the file.
*/
public void extractManifestFromFile( File manifestFile ) throws IOException
{
InputStream fis = new FileInputStream( manifestFile );
this.manifest = new Manifest( fis );
extractManifestEntries();
}
/**
* Sets the manifest.
*
* @param mf The new manifest.
*/
public void setManifest( Manifest mf )
{
this.manifest = mf;
}
/**
* Gets the manifest.
*
* @return The manifest.
*/
public Manifest getManifest()
{
return manifest;
}
/**
* Returns a Map of manifest entries.
*
* @return Map of manifest entries.
*/
public Map getManifestEntries()
{
return manifestEntries;
}
/**
* Returns the main attributes of the manifest as Attributes.
*
* @return The main attributes.
*/
public Attributes getMainAttributes()
{
return mainAttributes;
}
/**
* Checks if the current manifest contains a section with the given key.
*
* @param key The section name.
* @return true if the manifest contains a section with the given key.
*/
public boolean containsSection( String key )
{
return manifestEntries.containsKey( key );
}
/**
* Returns the value of the main attribute key.
*
* @param key The attribute name.
* @return The attribute value, null if the attribute is not found.
*/
public String getMainAttribute( String key )
{
return mainAttributes.getValue( key );
}
/**
* Checks if the current manifest contains a main attribute
* with the given key.
*
* @param key The main attribute name.
* @return true if the manifest contains a main attribute
* with the given key.
*/
public boolean containsMainAttribute( String key )
{
boolean contains = false;
if ( getMainAttribute( key ) != null )
{
contains = true;
}
return contains;
}
/**
* Returns the value of the section attribute key.
*
* @param key The attribute name.
* @return The attribute value, null if the attribute is not found.
*/
public String getSectionAttribute( String key )
{
return sectionAttributes.getValue( key );
}
/**
* Checks if the current manifest contains a section attribute
* with the given key.
*
* @param key The section attribute name.
* @return true if the manifest contains a section attribute
* with the given key.
*/
public boolean containsSectionAttribute( String key )
{
boolean contains = false;
if ( getSectionAttribute( key ) != null )
{
contains = true;
}
return contains;
}
/**
* Extracts the manifest entries, main attributes and section attributes.
*/
private void extractManifestEntries()
{
this.manifestEntries = manifest.getEntries();
this.mainAttributes = manifest.getMainAttributes();
for (Iterator it = manifestEntries.keySet().iterator(); it.hasNext(); )
{
String entryName = (String) it.next();
this.sectionAttributes =
(Attributes) manifestEntries.get( entryName );
}
}
}

View File

@ -0,0 +1,11 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>org.apache.maven.jar</title>
</head>
<body>
<p>
Contains utility classes for the jar plugin.
</p>
</body>
</html>

View File

@ -0,0 +1,152 @@
<!--
/*
* Copyright 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
*
* 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.
*/
-->
<project xmlns:ant="jelly:ant"
xmlns:u="jelly:util"
xmlns:j="jelly:core"
xmlns:assert="assert">
<goal name="testPlugin" prereqs="clean,jar:jar">
<u:file name="${maven.build.dir}/${maven.jar.final.name}" var="jarfile"/>
<j:new var="testBean" className="org.apache.maven.jar.TestManifest"/>
<j:set var="true" value="true"/>
<j:invoke var="success" on="${testBean}" method="extractManifestFromJar" exceptionVar="exc">
<j:arg value="${jarfile}" type="java.io.File"/>
</j:invoke>
<j:if test="${exc != null}">
<ant:fail>${exc}</ant:fail>
</j:if>
<j:invoke var="success" on="${testBean}" method="containsSection">
<j:arg value="org/apache/maven" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find section 'org/apache/maven' in manifest!"/>
<j:invoke var="value" on="${testBean}" method="getMainAttribute">
<j:arg value="Maven-Version" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${maven.application.version}"
value="${value}"
msg=" for Maven-Version attribute."/>
<j:invoke var="value" on="${testBean}" method="getMainAttribute">
<j:arg value="Built-By" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${user.name}"
value="${value}"
msg=" for Built-By attribute."/>
<j:invoke var="success" on="${testBean}" method="containsMainAttribute">
<j:arg value="Built-By" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Built-By' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsMainAttribute">
<j:arg value="Created-By" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Created-By' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsMainAttribute">
<j:arg value="Ant-Version" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Ant-Version' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsMainAttribute">
<j:arg value="Manifest-Version" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Manifest-Version' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsSectionAttribute">
<j:arg value="Specification-Title" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Specification-Title' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsSectionAttribute">
<j:arg value="Implementation-Title" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Implementation-Title' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsSectionAttribute">
<j:arg value="Specification-Version" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Specification-Version' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsSectionAttribute">
<j:arg value="Specification-Vendor" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Specification-Vendor' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsSectionAttribute">
<j:arg value="Extension-name" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Extension-name' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsSectionAttribute">
<j:arg value="Implementation-Version" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Implementation-Version' attribute in manifest!"/>
<j:invoke var="success" on="${testBean}" method="containsSectionAttribute">
<j:arg value="Implementation-Vendor" type="java.lang.String"/>
</j:invoke>
<assert:assertEquals
expected="${true}"
value="${success}"
msg=" Didn't find 'Implementation-Vendor' attribute in manifest!"/>
</goal>
</project>

View File

@ -0,0 +1,16 @@
# -------------------------------------------------------------------
# Copyright 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
#
# 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.
# -------------------------------------------------------------------

View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
* Copyright 2006The 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
*
* 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.
*/
-->
<project xmlns="http://maven.apache.org/POM/3.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/3.0.0 http://maven.apache.org/maven-v3_0_0.xsd">
<pomVersion>3</pomVersion>
<id>test-manifest</id>
<groupId>maven</groupId>
<name>Maven Jar Plugin Manifest Test</name>
<currentVersion>1.0</currentVersion>
<organization>
<name>Apache Software Foundation</name>
<url>http://www.apache.org/</url>
<logo>http://maven.apache.org/images/apache-maven-project.png</logo>
</organization>
<inceptionYear>2006</inceptionYear>
<package>org.apache.maven</package>
<logo>http://maven.apache.org/images/maven.jpg</logo>
<description>A project to test manifest files created by the jar:jar goal</description>
<shortDescription>Testing manifests created by the jar plugin</shortDescription>
<url>http://maven.apache.org/maven-1.x/plugins/jar/</url>
<repository>
<connection>scm:svn:http://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk/jar/</connection>
<url>http://svn.apache.org/viewcvs.cgi/maven/maven-1/plugins/trunk/jar/</url>
</repository>
<build>
<defaultGoal>testPlugin</defaultGoal>
<sourceDirectory>src/main/java</sourceDirectory>
</build>
</project>

View File

@ -0,0 +1,71 @@
package org.apache.maven.jar;
/* ====================================================================
* Copyright 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
*
* 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.
* ====================================================================
*/
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
/**
* JarUtils Test class.
*
* @author <a href="mailto:ltheussl@apache.org">Lukas Theussl</a>
* @version $Id$
*/
public class JarUtilsTest extends TestCase
{
/** Tests the manifest entries. */
public void testManifestEntries()
{
JarUtils jarUtils = new JarUtils();
File manifestFile =
new File( System.getProperty( "basedir"), "src/test/resources/MANIFEST.MF" );
try
{
jarUtils.extractManifestFromFile( manifestFile );
}
catch (IOException e)
{
e.printStackTrace();
fail( "Could not open manifest file!" );
}
assertEquals( jarUtils.getMainAttribute( "Manifest-Version" ), "1.0" );
assertEquals( jarUtils.getMainAttribute( "Ant-Version" ), "Apache Ant 1.6.5" );
assertEquals( jarUtils.getMainAttribute( "Created-By" ), "1.4.2_10-b03 (Sun Microsystems Inc.)" );
assertEquals( jarUtils.getMainAttribute( "Built-By" ), "me" );
assertEquals( jarUtils.getMainAttribute( "Maven-Version" ), "1.1-beta-3-SNAPSHOT" );
assertEquals( jarUtils.getSectionAttribute( "Extension-name" ), "org.apache.maven" );
assertEquals( jarUtils.getSectionAttribute( "Specification-Title" ), "Create jar files" );
assertEquals( jarUtils.getSectionAttribute( "Specification-Vendor" ), "Apache Software Foundation" );
assertEquals( jarUtils.getSectionAttribute( "Specification-Version" ), "1.8" );
assertEquals( jarUtils.getSectionAttribute( "Implementation-Title" ), "org.apache.maven" );
assertEquals( jarUtils.getSectionAttribute( "Implementation-Vendor" ), "Apache Software Foundation" );
assertEquals( jarUtils.getSectionAttribute( "Implementation-Version" ), "1.8-SNAPSHOT" );
assertTrue( jarUtils.containsSection( "org/apache/maven" ) );
assertTrue( jarUtils.containsMainAttribute( "Maven-Version" ) );
assertTrue( jarUtils.containsSectionAttribute( "Specification-Version" ) );
}
}

View File

@ -0,0 +1,11 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>org.apache.maven.jar</title>
</head>
<body>
<p>
Test classes for org.apache.maven.jar.
</p>
</body>
</html>

View File

@ -0,0 +1,15 @@
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.4.2_10-b03 (Sun Microsystems Inc.)
Built-By: me
Maven-Version: 1.1-beta-3-SNAPSHOT
Name: org/apache/maven
Extension-name: org.apache.maven
Specification-Title: Create jar files
Specification-Vendor: Apache Software Foundation
Specification-Version: 1.8
Implementation-Title: org.apache.maven
Implementation-Vendor: Apache Software Foundation
Implementation-Version: 1.8-SNAPSHOT

View File

@ -25,6 +25,7 @@
</properties>
<body>
<release version="1.8-SNAPSHOT" date="In SVN">
<action dev="ltheussl" type="add" issue="MPJAR-19">Add tests for jar manifest creation.</action>
<action dev="ltheussl" type="fix" issue="MPJAR-46">Using <code>maven.jar.manifest.classpath.add=true</code> from maven console gives huge Class-Path.</action>
<action dev="ltheussl" type="add" issue="MPJAR-15" due-to="Matt Finn">Include <code>Implementation-Vendor-Id</code> attribute for dependencies in manifest.</action>
<action dev="ltheussl" type="add" issue="MPJAR-39">Support specificationVersion in manifest.</action>