needs docs ...

Jan

> -----Ursprüngliche Nachricht-----
> Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Gesendet am: Mittwoch, 8. September 2004 16:29
> An: [EMAIL PROTECTED]
> Betreff: cvs commit: ant/src/testcases/org/apache/tools/ant/taskdefs
> ChecksumTest.java
> 
> bodewig     2004/09/08 07:29:17
> 
>   Modified:    src/etc/testcases/taskdefs checksum.xml
>                src/etc/testcases/taskdefs/expected asf-logo.gif.md5
>                src/main/org/apache/tools/ant/taskdefs Checksum.java
>                src/testcases/org/apache/tools/ant/taskdefs
>                         ChecksumTest.java
>   Added:       src/etc/testcases/taskdefs/expected asf-logo.gif.md5sum
>                         asf-logo.gif.pattern asf-logo.gif.svf
>   Log:
>   Support different output formats in checksum, PR 16539, 
> based on a patch by Mark R. Diggory
>   
>   Revision  Changes    Path
>   1.7       +28 -1     ant/src/etc/testcases/taskdefs/checksum.xml
>   
>   Index: checksum.xml
>   ===================================================================
>   RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/checksum.xml,v
>   retrieving revision 1.6
>   retrieving revision 1.7
>   diff -u -r1.6 -r1.7
>   --- checksum.xml    18 Dec 2003 08:39:06 -0000      1.6
>   +++ checksum.xml    8 Sep 2004 14:29:16 -0000       1.7
>   @@ -4,7 +4,11 @@
>      <target name="cleanup">
>        <delete file="../asf-logo.gif.MD5" />
>        <delete file="../asf-logo.gif.md5" />
>   -    <delete>
>   +    <delete file="../asf-logo.gif.MD5SUM" />
>   +    <delete file="../asf-logo.gif.md5sum" />
>   +    <delete file="../asf-logo.gif.SVF" />
>   +    <delete file="../asf-logo.gif.svf" />
>   +   <delete>
>          <fileset dir="checksum">
>            <include name="**/*.MD5"/>
>          </fileset>
>   @@ -16,6 +20,18 @@
>        <checksum file="../asf-logo.gif" fileext=".MD5" />
>      </target>
>    
>   +  <target name="createMD5SUMformat">
>   +    <checksum file="../asf-logo.gif" format="MD5SUM" 
> fileext=".MD5SUM" />
>   +  </target>
>   +
>   +  <target name="createSVFformat">
>   +    <checksum file="../asf-logo.gif" format="SVF" fileext=".SVF" />
>   +  </target>
>   +
>   +  <target name="createPattern">
>   +    <checksum file="../asf-logo.gif" pattern="foo{0}bar" 
> fileext=".PATTERN" />
>   +  </target>
>   +
>      <target name="setProperty">
>        <checksum file="../asf-logo.gif" property="logo.MD5" />
>      </target>
>   @@ -29,6 +45,17 @@
>              overwrite="true" />
>        <checksum file="../asf-logo.gif" fileext=".MD5"
>                  verifyproperty="no.logo.MD5" />
>   +  </target>
>   +
>   +  <target name="verifyMD5SUMAsTask">
>   +    <copy file="expected/asf-logo.gif.md5sum" todir=".." />
>   +    <checksum file="../asf-logo.gif" fileext=".md5sum"
>   +              verifyproperty="logo.MD5" format="MD5SUM"/>
>   +
>   +    <copy file="checksum.xml" tofile="../asf-logo.gif.MD5SUM"
>   +          overwrite="true" />
>   +    <checksum file="../asf-logo.gif" fileext=".MD5SUM"
>   +              verifyproperty="no.logo.MD5" format="MD5SUM"/>
>      </target>
>    
>      <target name="verifyAsCondition">
>   
>   
>   
>   1.2       +1 -1      
> ant/src/etc/testcases/taskdefs/expected/asf-logo.gif.md5
>   
>   Index: asf-logo.gif.md5
>   ===================================================================
>   RCS file: 
> /home/cvs/ant/src/etc/testcases/taskdefs/expected/asf-logo.gif.md5,v
>   retrieving revision 1.1
>   retrieving revision 1.2
>   diff -u -r1.1 -r1.2
>   --- asf-logo.gif.md5        19 Nov 2001 15:18:46 -0000      1.1
>   +++ asf-logo.gif.md5        8 Sep 2004 14:29:16 -0000       1.2
>   @@ -1 +1 @@
>   -0541d3df42520911f268abc730f3afe0
>   \ No newline at end of file
>   +0541d3df42520911f268abc730f3afe0
>   
>   
>   
>   1.1                  
> ant/src/etc/testcases/taskdefs/expected/asf-logo.gif.md5sum
>   
>   Index: asf-logo.gif.md5sum
>   ===================================================================
>   0541d3df42520911f268abc730f3afe0 *asf-logo.gif
>   
>   
>   
>   1.1                  
> ant/src/etc/testcases/taskdefs/expected/asf-logo.gif.pattern
>   
>   Index: asf-logo.gif.pattern
>   ===================================================================
>   foo0541d3df42520911f268abc730f3afe0bar
>   
>   
>   
>   1.1                  
> ant/src/etc/testcases/taskdefs/expected/asf-logo.gif.svf
>   
>   Index: asf-logo.gif.svf
>   ===================================================================
>   MD5 (asf-logo.gif) = 0541d3df42520911f268abc730f3afe0
>   
>   
>   
>   1.40      +122 -33   
> ant/src/main/org/apache/tools/ant/taskdefs/Checksum.java
>   
>   Index: Checksum.java
>   ===================================================================
>   RCS file: 
> /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Checksum.java,v
>   retrieving revision 1.39
>   retrieving revision 1.40
>   diff -u -r1.39 -r1.40
>   --- Checksum.java   9 Mar 2004 16:48:04 -0000       1.39
>   +++ Checksum.java   8 Sep 2004 14:29:16 -0000       1.40
>   @@ -34,12 +34,16 @@
>    import java.util.Enumeration;
>    import java.util.Set;
>    import java.util.Arrays;
>   +import java.text.MessageFormat;
>   +import java.text.ParseException;
>    
>    import org.apache.tools.ant.BuildException;
>    import org.apache.tools.ant.DirectoryScanner;
>    import org.apache.tools.ant.Project;
>    import org.apache.tools.ant.taskdefs.condition.Condition;
>   +import org.apache.tools.ant.types.EnumeratedAttribute;
>    import org.apache.tools.ant.types.FileSet;
>   +import org.apache.tools.ant.util.StringUtils;
>    
>    /**
>     * Used to create or verify file checksums.
>   @@ -127,6 +131,11 @@
>        private int readBufferSize = 8 * 1024;
>    
>        /**
>   +     * Formater for the checksum file.
>   +     */
>   +    private MessageFormat format = 
> FormatElement.getDefault().getFormat();
>   +
>   +    /**
>         * Sets the file for which the checksum is to be calculated.
>         */
>        public void setFile(File file) {
>   @@ -209,6 +218,26 @@
>        }
>    
>        /**
>   +     * Select the in/output pattern via a well know format name.
>   +     *
>   +     * @since 1.7.0
>   +     */
>   +    public void setFormat(FormatElement e) {
>   +        format = e.getFormat();
>   +    }
>   +
>   +    /**
>   +     * Specify the pattern to use as a MessageFormat pattern.
>   +     *
>   +     * <p>{0} gets replaced by the checksum, {1} by the 
> filename.</p>
>   +     *
>   +     * @since 1.7.0
>   +     */
>   +    public void setPattern(String p) {
>   +        format = new MessageFormat(p);
>   +    }
>   +
>   +    /**
>         * Files to generate checksums for.
>         */
>        public void addFileset(FileSet set) {
>   @@ -223,7 +252,7 @@
>            boolean value = validateAndExecute();
>            if (verifyProperty != null) {
>                getProject().setNewProperty(verifyProperty,
>   -                                new Boolean(value).toString());
>   +                                        new 
> Boolean(value).toString());
>            }
>        }
>    
>   @@ -246,39 +275,39 @@
>    
>            if (file == null && filesets.size() == 0) {
>                throw new BuildException(
>   -                "Specify at least one source - a file or a 
> fileset.");
>   +                                     "Specify at least one 
> source - a file or a fileset.");
>            }
>    
>            if (file != null && file.exists() && file.isDirectory()) {
>                throw new BuildException(
>   -                "Checksum cannot be generated for directories");
>   +                                     "Checksum cannot be 
> generated for directories");
>            }
>    
>            if (file != null && totalproperty != null) {
>                throw new BuildException(
>   -                "File and Totalproperty cannot co-exist.");
>   +                                     "File and 
> Totalproperty cannot co-exist.");
>            }
>    
>            if (property != null && fileext != null) {
>                throw new BuildException(
>   -                "Property and FileExt cannot co-exist.");
>   +                                     "Property and FileExt 
> cannot co-exist.");
>            }
>    
>            if (property != null) {
>                if (forceOverwrite) {
>                    throw new BuildException(
>   -                    "ForceOverwrite cannot be used when 
> Property is specified");
>   +                                         "ForceOverwrite 
> cannot be used when Property is specified");
>                }
>    
>                if (file != null) {
>                    if (filesets.size() > 0) {
>                        throw new BuildException("Multiple 
> files cannot be used "
>   -                        + "when Property is specified");
>   +                                             + "when 
> Property is specified");
>                    }
>                } else {
>                    if (filesets.size() > 1) {
>                        throw new BuildException("Multiple 
> files cannot be used "
>   -                        + "when Property is specified");
>   +                                             + "when 
> Property is specified");
>                    }
>                }
>            }
>   @@ -289,12 +318,12 @@
>    
>            if (verifyProperty != null && forceOverwrite) {
>                throw new BuildException(
>   -                "VerifyProperty and ForceOverwrite cannot 
> co-exist.");
>   +                                     "VerifyProperty and 
> ForceOverwrite cannot co-exist.");
>            }
>    
>            if (isCondition && forceOverwrite) {
>                throw new BuildException("ForceOverwrite 
> cannot be used when "
>   -                + "conditions are being used.");
>   +                                     + "conditions are 
> being used.");
>            }
>    
>            messageDigest = null;
>   @@ -323,7 +352,7 @@
>                fileext = "." + algorithm;
>            } else if (fileext.trim().length() == 0) {
>                throw new BuildException(
>   -                "File extension when specified must not be 
> an empty string");
>   +                                     "File extension when 
> specified must not be an empty string");
>            }
>    
>            try {
>   @@ -371,15 +400,7 @@
>                                Project.MSG_VERBOSE);
>                            if (totalproperty != null) {
>                                // Read the checksum from disk.
>   -                            String checksum = null;
>   -                            try {
>   -                                BufferedReader diskChecksumReader
>   -                                    = new 
> BufferedReader(new FileReader(checksumFile));
>   -                                checksum = 
> diskChecksumReader.readLine();
>   -                            } catch (IOException e) {
>   -                                throw new 
> BuildException("Couldn't read checksum file "
>   -                                    + checksumFile, e);
>   -                            }
>   +                            String checksum = 
> readChecksum(checksumFile);
>                                byte[] digest = 
> decodeHex(checksum.toCharArray());
>                                allDigests.put(file, digest);
>                            }
>   @@ -389,8 +410,8 @@
>                    }
>                } else {
>                    String message = "Could not find file "
>   -                                 + file.getAbsolutePath()
>   -                                 + " to generate checksum for.";
>   +                    + file.getAbsolutePath()
>   +                    + " to generate checksum for.";
>                    log(message);
>                    throw new BuildException(message, getLocation());
>                }
>   @@ -457,23 +478,26 @@
>                        if (isCondition) {
>                            File existingFile = (File) destination;
>                            if (existingFile.exists()) {
>   -                            fis = new 
> FileInputStream(existingFile);
>   -                            InputStreamReader isr = new 
> InputStreamReader(fis);
>   -                            BufferedReader br = new 
> BufferedReader(isr);
>   -                            String suppliedChecksum = 
> br.readLine();
>   -                            fis.close();
>   -                            fis = null;
>   -                            br.close();
>   -                            isr.close();
>   -                            checksumMatches = checksumMatches
>   -                                && 
> checksum.equals(suppliedChecksum);
>   +                            try {
>   +                                String suppliedChecksum = 
>   +                                    readChecksum(existingFile);
>   +                                checksumMatches = checksumMatches
>   +                                    && 
> checksum.equals(suppliedChecksum);
>   +                            } catch (BuildException be) {
>   +                                // file is on wrong format, swallow
>   +                                checksumMatches = false;
>   +                            }
>                            } else {
>                                checksumMatches = false;
>                            }
>                        } else {
>                            File dest = (File) destination;
>                            fos = new FileOutputStream(dest);
>   -                        fos.write(checksum.getBytes());
>   +                        fos.write(format.format(new Object[] {
>   +                                                    checksum,
>   +                                                    src.getName(),
>   +                                                }).getBytes());
>   +                        fos.write(StringUtils.LINE_SEP.getBytes());
>                            fos.close();
>                            fos = null;
>                        }
>   @@ -561,5 +585,70 @@
>            }
>    
>            return out;
>   +    }
>   +
>   +    /**
>   +     * reads the checksum from a file using the specified format.
>   +     *
>   +     * @since 1.7
>   +     */
>   +    private String readChecksum(File f) {
>   +        BufferedReader diskChecksumReader = null;
>   +        try {
>   +            diskChecksumReader = new BufferedReader(new 
> FileReader(f));
>   +            Object[] result = 
> format.parse(diskChecksumReader.readLine());
>   +            if (result == null || result.length == 0 || 
> result[0] == null) {
>   +                throw new BuildException("failed to find a 
> checksum");
>   +            }
>   +            return (String) result[0];
>   +        } catch (IOException e) {
>   +            throw new BuildException("Couldn't read 
> checksum file " + f, e);
>   +        } catch (ParseException e) {
>   +            throw new BuildException("Couldn't read 
> checksum file " + f, e);
>   +        } finally {
>   +            if (diskChecksumReader != null) {
>   +                try {
>   +                    diskChecksumReader.close();
>   +                } catch (IOException e) {
>   +                    // ignore
>   +                }
>   +            }
>   +        }
>   +    }
>   +
>   +    /**
>   +     * Helper class for the format attribute.
>   +     *
>   +     * @since 1.7
>   +     */
>   +    public static class FormatElement extends EnumeratedAttribute {
>   +        private static HashMap formatMap = new HashMap();
>   +        private static final String CHECKSUM = "CHECKSUM";
>   +        private static final String MD5SUM = "MD5SUM";
>   +        private static final String SVF = "SVF";
>   +
>   +        static {
>   +            formatMap.put(CHECKSUM, new MessageFormat("{0}"));
>   +            formatMap.put(MD5SUM, new MessageFormat("{0} *{1}")); 
>   +            formatMap.put(SVF, new MessageFormat("MD5 
> ({1}) = {0}"));
>   +        }
>   +
>   +        public FormatElement() {
>   +            super();
>   +        }
>   +
>   +        public static FormatElement getDefault() {
>   +            FormatElement e = new FormatElement();
>   +            e.setValue(CHECKSUM);
>   +            return e;
>   +        }
>   +        
>   +        public MessageFormat getFormat() {
>   +            return (MessageFormat) formatMap.get(getValue());
>   +        }
>   +
>   +        public String[] getValues() {
>   +            return new String[] {CHECKSUM, MD5SUM, SVF};
>   +        }
>        }
>    }
>   
>   
>   
>   1.12      +28 -1     
> ant/src/testcases/org/apache/tools/ant/taskdefs/ChecksumTest.java
>   
>   Index: ChecksumTest.java
>   ===================================================================
>   RCS file: 
> /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/Chec
> ksumTest.java,v
>   retrieving revision 1.11
>   retrieving revision 1.12
>   diff -u -r1.11 -r1.12
>   --- ChecksumTest.java       9 Mar 2004 16:48:57 -0000       1.11
>   +++ ChecksumTest.java       8 Sep 2004 14:29:16 -0000       1.12
>   @@ -47,6 +47,27 @@
>                                               
> project.resolveFile("../asf-logo.gif.MD5")));
>        }
>    
>   +    public void testCreateMD5SUMformat() throws IOException {
>   +        FileUtils fileUtils = FileUtils.newFileUtils();
>   +        executeTarget("createMD5SUMformat");
>   +        
> assertTrue(fileUtils.contentEquals(project.resolveFile("expect
> ed/asf-logo.gif.md5sum"),
>   +                                           
> project.resolveFile("../asf-logo.gif.MD5SUM")));
>   +    }
>   +    
>   +    public void testCreateSVFformat() throws IOException {
>   +        FileUtils fileUtils = FileUtils.newFileUtils();
>   +        executeTarget("createSVFformat");
>   +        
> assertTrue(fileUtils.contentEquals(project.resolveFile("expect
> ed/asf-logo.gif.svf"),
>   +                                           
> project.resolveFile("../asf-logo.gif.SVF")));
>   +    }
>   +    
>   +    public void testCreatePattern() throws IOException {
>   +        FileUtils fileUtils = FileUtils.newFileUtils();
>   +        executeTarget("createPattern");
>   +        
> assertTrue(fileUtils.contentEquals(project.resolveFile("expect
> ed/asf-logo.gif.pattern"),
>   +                                           
> project.resolveFile("../asf-logo.gif.PATTERN")));
>   +    }
>   +
>        public void testSetProperty() {
>            executeTarget("setProperty");
>            assertEquals("0541d3df42520911f268abc730f3afe0",
>   @@ -72,6 +93,12 @@
>    
>        public void testVerifyAsTask() {
>            testVerify("verifyAsTask");
>   +        assertNotNull(project.getProperty("no.logo.MD5"));
>   +        assertEquals("false", project.getProperty("no.logo.MD5"));
>   +    }
>   +
>   +    public void testVerifyMD5SUMAsTask() {
>   +        testVerify("verifyMD5SUMAsTask");
>            assertNotNull(project.getProperty("no.logo.MD5"));
>            assertEquals("false", project.getProperty("no.logo.MD5"));
>        }
>   
>   
>   
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

Reply via email to