commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestPartsNoHost.java
Date Tue, 11 Mar 2003 19:54:48 GMT
olegk       2003/03/11 11:54:48

  Modified:    httpclient/src/java/org/apache/commons/httpclient/methods/multipart
                        FilePartSource.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestPartsNoHost.java
  Log:
  NullPointerException in FilePartSource fix
  
  Reported by Thomas Mathis <mathis@vtg.at>
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.8       +29 -28    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePartSource.java
  
  Index: FilePartSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePartSource.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FilePartSource.java	30 Jan 2003 05:01:56 -0000	1.7
  +++ FilePartSource.java	11 Mar 2003 19:54:48 -0000	1.8
  @@ -63,6 +63,7 @@
    
   package org.apache.commons.httpclient.methods.multipart;
   
  +import java.io.ByteArrayInputStream;
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileNotFoundException;
  @@ -81,7 +82,7 @@
   public class FilePartSource implements PartSource {
   
       /** File part file. */
  -    private File file;
  +    private File file = null;
   
       /** File part file name. */
       private String fileName = null;
  @@ -95,17 +96,16 @@
        * cannot be read
        */
       public FilePartSource(File file) throws FileNotFoundException {
  -        
  -        if (!file.isFile()) {
  -            throw new FileNotFoundException("File is not a normal file.");
  -        }
  -    
  -        if (!file.canRead()) {
  -            throw new FileNotFoundException("File is not readable.");
  -        }
  -        
           this.file = file;
  -        
  +        if (file != null) {
  +            if (!file.isFile()) {
  +                throw new FileNotFoundException("File is not a normal file.");
  +            }
  +            if (!file.canRead()) {
  +                throw new FileNotFoundException("File is not readable.");
  +            }
  +            this.fileName = file.getName();       
  +        }
       }
   
       /**
  @@ -118,18 +118,11 @@
        * cannot be read
        */
       public FilePartSource(String fileName, File file) 
  -    throws FileNotFoundException {
  -        
  -        if (!file.isFile()) {
  -            throw new FileNotFoundException("File is not a normal file.");
  +      throws FileNotFoundException {
  +        this(file);
  +        if (fileName != null) {
  +            this.fileName = fileName;
           }
  -    
  -        if (!file.canRead()) {
  -            throw new FileNotFoundException("File is not readable.");
  -        }
  -        
  -        this.file = file;
  -        this.fileName = fileName;
       }
       
       /**
  @@ -138,7 +131,11 @@
        * @see PartSource#getLength()
        */
       public long getLength() {
  -        return file.length();
  +        if (this.file != null) {
  +            return this.file.length();
  +        } else {
  +            return 0;
  +        }
       }
   
       /**
  @@ -147,7 +144,7 @@
        * @see PartSource#getFileName()
        */
       public String getFileName() {
  -        return (fileName == null) ? file.getName() : fileName;
  +        return (fileName == null) ? "noname" : fileName;
       }
   
       /**
  @@ -157,7 +154,11 @@
        * @see PartSource#createInputStream()
        */
       public InputStream createInputStream() throws IOException {
  -        return new FileInputStream(file);
  +        if (this.file != null) {
  +            return new FileInputStream(this.file);
  +        } else {
  +            return new ByteArrayInputStream(new byte[] {});
  +        }
       }
   
   }
  
  
  
  1.6       +16 -4     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPartsNoHost.java
  
  Index: TestPartsNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPartsNoHost.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestPartsNoHost.java	23 Jan 2003 22:48:27 -0000	1.5
  +++ TestPartsNoHost.java	11 Mar 2003 19:54:48 -0000	1.6
  @@ -122,6 +122,18 @@
           assertEquals(resp1, resp2);
       }
   
  +    public void testFilePartNullFileResendsData() throws Exception {
  +        FilePart part = new FilePart(NAME, "emptyfile.ext", null);
  +        ByteArrayOutputStream stream = new ByteArrayOutputStream();
  +        part.send(stream);
  +        String resp1 = stream.toString();
  +        stream = new ByteArrayOutputStream();
  +        part.send(stream);
  +        String resp2 = stream.toString();
  +        assertEquals(resp1, resp2);
  +    }
  +
  +
       /** Writes PART_DATA out to a temporary file and returns the file it
        * was written to.
        * @return the File object representing the file the data was
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message