Author: peterreilly
Date: Wed Jul 18 13:10:59 2007
New Revision: 557376

URL: http://svn.apache.org/viewvc?view=rev&rev=557376
Log:
merge jar strict attribute from trunk

Modified:
    ant/core/branches/ANT_17_BRANCH/WHATSNEW
    ant/core/branches/ANT_17_BRANCH/docs/manual/CoreTasks/jar.html
    ant/core/branches/ANT_17_BRANCH/src/etc/testcases/taskdefs/jar.xml
    
ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/taskdefs/Jar.java
    
ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java

Modified: ant/core/branches/ANT_17_BRANCH/WHATSNEW
URL: 
http://svn.apache.org/viewvc/ant/core/branches/ANT_17_BRANCH/WHATSNEW?view=diff&rev=557376&r1=557375&r2=557376
==============================================================================
--- ant/core/branches/ANT_17_BRANCH/WHATSNEW (original)
+++ ant/core/branches/ANT_17_BRANCH/WHATSNEW Wed Jul 18 13:10:59 2007
@@ -172,6 +172,8 @@
 
 * Add new retry task container.
 
+* <jar> has a new strict attribute that checks if the jar complies with
+  the jar packaging version specification.
 
 Changes from Ant 1.6.5 to Ant 1.7.0
 ===================================

Modified: ant/core/branches/ANT_17_BRANCH/docs/manual/CoreTasks/jar.html
URL: 
http://svn.apache.org/viewvc/ant/core/branches/ANT_17_BRANCH/docs/manual/CoreTasks/jar.html?view=diff&rev=557376&r1=557375&r2=557376
==============================================================================
--- ant/core/branches/ANT_17_BRANCH/docs/manual/CoreTasks/jar.html (original)
+++ ant/core/branches/ANT_17_BRANCH/docs/manual/CoreTasks/jar.html Wed Jul 18 
13:10:59 2007
@@ -228,6 +228,17 @@
     (maximum compression/slowest). <em>Since Ant 1.7</em></td>
     <td valign="top" align="center">No</td>
   </tr>
+  <tr>
+    <td valign="top">strict</td>
+    <td valign="top">Configures how to handle breaks of the packaging version
+    specification: <ul>
+    <li><b>fail</b> = throws a BuildException</li>
+    <li><b>warn</b> = logs a message on warn level</li>
+    <li><b>ignore</b> = logs a message on verbose level (default)</li>
+    </ul>
+    <em>Since Ant 1.7.1</em></td>
+    <td valign="top" align="center">No, defaults to <tt>ignore</tt>. </td>
+  </tr>
 </table>
 
 <h3>Nested elements</h3>

Modified: ant/core/branches/ANT_17_BRANCH/src/etc/testcases/taskdefs/jar.xml
URL: 
http://svn.apache.org/viewvc/ant/core/branches/ANT_17_BRANCH/src/etc/testcases/taskdefs/jar.xml?view=diff&rev=557376&r1=557375&r2=557376
==============================================================================
--- ant/core/branches/ANT_17_BRANCH/src/etc/testcases/taskdefs/jar.xml 
(original)
+++ ant/core/branches/ANT_17_BRANCH/src/etc/testcases/taskdefs/jar.xml Wed Jul 
18 13:10:59 2007
@@ -237,15 +237,30 @@
     </jar>
   </target>
     
-  <target name="testNoVersionInfo">
+  <target name="testNoVersionInfoNoStrict">
     <mkdir dir="${tmp.dir}"/>
     <jar destfile="${tmp.jar}" basedir="${tmp.dir}"/>
   </target>  
 
+  <target name="testNoVersionInfoFail">
+    <mkdir dir="${tmp.dir}"/>
+    <jar destfile="${tmp.jar}" basedir="${tmp.dir}" strict="fail"/>
+  </target>  
+
+  <target name="testNoVersionInfoIgnore">
+    <mkdir dir="${tmp.dir}"/>
+    <jar destfile="${tmp.jar}" basedir="${tmp.dir}" strict="ignore"/>
+  </target>  
+
+  <target name="testNoVersionInfoWarn">
+    <mkdir dir="${tmp.dir}"/>
+    <jar destfile="${tmp.jar}" basedir="${tmp.dir}" strict="warn"/>
+  </target>  
+    
   <!-- see 
http://java.sun.com/j2se/1.3/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersioning
 -->  
   <target name="testHasVersionInfo">
     <mkdir dir="${tmp.dir}"/>
-    <jar destfile="${tmp.jar}" basedir="${tmp.dir}">
+    <jar destfile="${tmp.jar}" basedir="${tmp.dir}" strict="fail">
       <manifest>
         <attribute name="Implementation-Title"   value="Packaging Version 
Test"/>
         <attribute name="Implementation-Version" value="1.0"/>

Modified: 
ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/taskdefs/Jar.java
URL: 
http://svn.apache.org/viewvc/ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/taskdefs/Jar.java?view=diff&rev=557376&r1=557375&r2=557376
==============================================================================
--- 
ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/taskdefs/Jar.java 
(original)
+++ 
ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/taskdefs/Jar.java 
Wed Jul 18 13:10:59 2007
@@ -143,6 +143,14 @@
      */
     private Path indexJars;
 
+    // CheckStyle:LineLength OFF - Link is too long.
+    /**
+     * Strict mode for checking rules of the JAR-Specification.
+     * @see 
http://java.sun.com/j2se/1.3/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersioning
+     */
+    private StrictMode strict;
+    // CheckStyle:LineLength ON
+
     /**
      * Extra fields needed to make Solaris recognize the archive as a jar file.
      *
@@ -159,8 +167,9 @@
         emptyBehavior = "create";
         setEncoding("UTF8");
         rootEntries = new Vector();
+        strict = new StrictMode("ignore");
     }
-    
+
     /**
      * Not used for jar files.
      * @param we not used
@@ -186,6 +195,15 @@
     }
 
     /**
+     * Activate the strict mode. When set to <i>true</i> a BuildException
+     * will be thrown if the Jar-Packaging specification was broken.
+     * @param strict New value of the strict mode.
+     */
+    public void setStrict(String strict) {
+        this.strict = new StrictMode(strict);
+    }
+
+    /**
      * Set the destination file.
      * @param jarFile the destination file
      * @deprecated since 1.5.x.
@@ -775,34 +793,57 @@
      */
     protected void cleanUp() {
         super.cleanUp();
-        
-        // check against packaging spec
-        // 
http://java.sun.com/j2se/1.3/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersioning
-        Section mainSection = (configuredManifest==null) ? null : 
configuredManifest.getMainSection();
-        if (mainSection==null) {
-            log("No Implementation-Title set. (" + getLocation() + ")");
-            log("No Implementation-Version set. (" + getLocation() + ")");
-            log("No Implementation-Vendor set. (" + getLocation() + ")");
+        checkJarSpec();
+
+        // we want to save this info if we are going to make another pass
+        if (!doubleFilePass || !skipWriting) {
+            manifest = null;
+            configuredManifest = savedConfiguredManifest;
+            filesetManifest = null;
+            originalManifest = null;
+        }
+        rootEntries.removeAllElements();
+    }
+
+    // CheckStyle:LineLength OFF - Link is too long.
+    /**
+     * Check against packaging spec
+     * @see 
http://java.sun.com/j2se/1.3/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersioning
+     */
+    // CheckStyle:LineLength ON
+    private void checkJarSpec() {
+        String br = System.getProperty("line.separator");
+        StringBuffer message = new StringBuffer();
+        Section mainSection = (configuredManifest == null)
+                            ? null
+                            : configuredManifest.getMainSection();
+
+        if (mainSection == null) {
+            message.append("No Implementation-Title set.");
+            message.append("No Implementation-Version set.");
+            message.append("No Implementation-Vendor set.");
         } else {
             if (mainSection.getAttribute("Implementation-Title") == null) {
-                log("No Implementation-Title set. (" + getLocation() + ")");
+                message.append("No Implementation-Title set.");
             }
             if (mainSection.getAttribute("Implementation-Version") == null) {
-                log("No Implementation-Version set. (" + getLocation() + ")");
+                message.append("No Implementation-Version set.");
             }
             if (mainSection.getAttribute("Implementation-Vendor") == null) {
-                log("No Implementation-Vendor set. (" + getLocation() + ")");
+                message.append("No Implementation-Vendor set.");
             }
         }
 
-        // we want to save this info if we are going to make another pass
-        if (!doubleFilePass || !skipWriting) {
-            manifest = null;
-            configuredManifest = savedConfiguredManifest;
-            filesetManifest = null;
-            originalManifest = null;
+        if (message.length() > 0) {
+            message.append(br);
+            message.append("Location: ").append(getLocation());
+            message.append(br);
+            if (strict.getValue().equalsIgnoreCase("fail")) {
+                throw new BuildException(message.toString(), getLocation());
+            } else {
+                log(message.toString(), strict.getLogLevel());
+            }
         }
-        rootEntries.removeAllElements();
     }
 
     /**
@@ -989,4 +1030,25 @@
             }
         }
     }
+
+    // CheckStyle:JavadocType OFF - simple enum
+    public class StrictMode extends EnumeratedAttribute {
+        // CheckStyle:JavadocMethod OFF - simple enum
+        public StrictMode() {
+        }
+        public StrictMode(String value) {
+            setValue(value);
+        }
+        public String[] getValues() {
+            return new String[]{"fail", "warn", "ignore"};
+        }
+        /**
+         * @return The log level according to the strict mode.
+         */
+        public int getLogLevel() {
+            return (getValue().equals("ignore")) ? Project.MSG_VERBOSE : 
Project.MSG_WARN;
+        }
+        // CheckStyle:JavadocMethod ON
+    }
+    // CheckStyle:JavadocType ON
 }

Modified: 
ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java
URL: 
http://svn.apache.org/viewvc/ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java?view=diff&rev=557376&r1=557375&r2=557376
==============================================================================
--- 
ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java
 (original)
+++ 
ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java
 Wed Jul 18 13:10:59 2007
@@ -268,13 +268,31 @@
         executeTarget("testIndexJarsPlusJarMarker");
     }
     
-    public void testNoVersionInfo() {
-        executeTarget("testNoVersionInfo");
-        assertLogContaining("No Implementation-Title set.");
-        assertLogContaining("No Implementation-Version set.");
-        assertLogContaining("No Implementation-Vendor set.");
+    public void testNoVersionInfoFail() {
+        expectBuildExceptionContaining("testNoVersionInfoFail", "Manifest 
Implemention information missing.", "No Implementation-Title set.");
+    }
+    
+    public void testNoVersionInfoIgnore() {
+        executeTarget("testNoVersionInfoIgnore");
+        assertTrue( getFullLog().contains("No Implementation-Title set.") );
+        assertTrue( getFullLog().contains("No Implementation-Version set.") );
+        assertTrue( getFullLog().contains("No Implementation-Vendor set.") );
+    }
+
+    public void testNoVersionInfoWarn() {
+        executeTarget("testNoVersionInfoWarn");
+        assertTrue( getLog().contains("No Implementation-Title set.") );
+        assertTrue( getLog().contains("No Implementation-Version set.") );
+        assertTrue( getLog().contains("No Implementation-Vendor set.") );
     }
 
+    public void testNoVersionInfoNoStrict() {
+        executeTarget("testNoVersionInfoNoStrict");
+        assertFalse( getLog().contains("No Implementation-Title set.") );
+        assertFalse( getLog().contains("No Implementation-Version set.") );
+        assertFalse( getLog().contains("No Implementation-Vendor set.") );
+    }
+    
     public void testHasVersionInfo() {
         executeTarget("testHasVersionInfo");
         assertFalse( getLog().contains("No Implementation-Title set.") );



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to