From b466d78a47a50387bba32c2b253e4e153675404f Mon Sep 17 00:00:00 2001 From: ltheussl Date: Wed, 12 Apr 2006 06:10:38 +0000 Subject: [PATCH] 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 --- jar/project.xml | 9 + .../main/org/apache/maven/jar/JarUtils.java | 211 ++++++++++++++++++ .../main/org/apache/maven/jar/package.html | 11 + jar/src/plugin-test/test-manifest/maven.xml | 152 +++++++++++++ .../test-manifest/project.properties | 16 ++ jar/src/plugin-test/test-manifest/project.xml | 51 +++++ .../org/apache/maven/jar/JarUtilsTest.java | 71 ++++++ .../test/org/apache/maven/jar/package.html | 11 + jar/src/test/resources/MANIFEST.MF | 15 ++ jar/xdocs/changes.xml | 1 + 10 files changed, 548 insertions(+) create mode 100644 jar/src/main/org/apache/maven/jar/JarUtils.java create mode 100644 jar/src/main/org/apache/maven/jar/package.html create mode 100644 jar/src/plugin-test/test-manifest/maven.xml create mode 100644 jar/src/plugin-test/test-manifest/project.properties create mode 100644 jar/src/plugin-test/test-manifest/project.xml create mode 100644 jar/src/test/org/apache/maven/jar/JarUtilsTest.java create mode 100644 jar/src/test/org/apache/maven/jar/package.html create mode 100644 jar/src/test/resources/MANIFEST.MF diff --git a/jar/project.xml b/jar/project.xml index 44b97063..902d9a80 100644 --- a/jar/project.xml +++ b/jar/project.xml @@ -74,4 +74,13 @@ 1.4 + + + + + src/test/resources + + + + diff --git a/jar/src/main/org/apache/maven/jar/JarUtils.java b/jar/src/main/org/apache/maven/jar/JarUtils.java new file mode 100644 index 00000000..90c43b0c --- /dev/null +++ b/jar/src/main/org/apache/maven/jar/JarUtils.java @@ -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 Lukas Theussl + * @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 ); + } + } + +} diff --git a/jar/src/main/org/apache/maven/jar/package.html b/jar/src/main/org/apache/maven/jar/package.html new file mode 100644 index 00000000..75b40b0a --- /dev/null +++ b/jar/src/main/org/apache/maven/jar/package.html @@ -0,0 +1,11 @@ + + + + org.apache.maven.jar + + +

+ Contains utility classes for the jar plugin. +

+ + diff --git a/jar/src/plugin-test/test-manifest/maven.xml b/jar/src/plugin-test/test-manifest/maven.xml new file mode 100644 index 00000000..c7d388ae --- /dev/null +++ b/jar/src/plugin-test/test-manifest/maven.xml @@ -0,0 +1,152 @@ + + + + + + + + + + + + + + + ${exc} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jar/src/plugin-test/test-manifest/project.properties b/jar/src/plugin-test/test-manifest/project.properties new file mode 100644 index 00000000..45214d7f --- /dev/null +++ b/jar/src/plugin-test/test-manifest/project.properties @@ -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. +# ------------------------------------------------------------------- + diff --git a/jar/src/plugin-test/test-manifest/project.xml b/jar/src/plugin-test/test-manifest/project.xml new file mode 100644 index 00000000..c3ecc4a2 --- /dev/null +++ b/jar/src/plugin-test/test-manifest/project.xml @@ -0,0 +1,51 @@ + + + + + + 3 + test-manifest + maven + Maven Jar Plugin Manifest Test + 1.0 + + Apache Software Foundation + http://www.apache.org/ + http://maven.apache.org/images/apache-maven-project.png + + + 2006 + org.apache.maven + http://maven.apache.org/images/maven.jpg + A project to test manifest files created by the jar:jar goal + Testing manifests created by the jar plugin + http://maven.apache.org/maven-1.x/plugins/jar/ + + + scm:svn:http://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk/jar/ + http://svn.apache.org/viewcvs.cgi/maven/maven-1/plugins/trunk/jar/ + + + + testPlugin + src/main/java + + + diff --git a/jar/src/test/org/apache/maven/jar/JarUtilsTest.java b/jar/src/test/org/apache/maven/jar/JarUtilsTest.java new file mode 100644 index 00000000..9aedd17a --- /dev/null +++ b/jar/src/test/org/apache/maven/jar/JarUtilsTest.java @@ -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 Lukas Theussl + * @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" ) ); + + } + +} diff --git a/jar/src/test/org/apache/maven/jar/package.html b/jar/src/test/org/apache/maven/jar/package.html new file mode 100644 index 00000000..afeeb804 --- /dev/null +++ b/jar/src/test/org/apache/maven/jar/package.html @@ -0,0 +1,11 @@ + + + + org.apache.maven.jar + + +

+ Test classes for org.apache.maven.jar. +

+ + diff --git a/jar/src/test/resources/MANIFEST.MF b/jar/src/test/resources/MANIFEST.MF new file mode 100644 index 00000000..13491199 --- /dev/null +++ b/jar/src/test/resources/MANIFEST.MF @@ -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 + diff --git a/jar/xdocs/changes.xml b/jar/xdocs/changes.xml index db0c2a4d..473afb6a 100644 --- a/jar/xdocs/changes.xml +++ b/jar/xdocs/changes.xml @@ -25,6 +25,7 @@ + Add tests for jar manifest creation. Using maven.jar.manifest.classpath.add=true from maven console gives huge Class-Path. Include Implementation-Vendor-Id attribute for dependencies in manifest. Support specificationVersion in manifest.