You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by un...@apache.org on 2015/06/03 15:38:24 UTC

svn commit: r1683328 - in /jmeter/trunk: src/core/org/apache/jmeter/services/ src/protocol/http/org/apache/jmeter/protocol/http/sampler/ test/src/org/apache/jmeter/services/ xdocs/

Author: undera
Date: Wed Jun  3 13:38:24 2015
New Revision: 1683328

URL: http://svn.apache.org/r1683328
Log:
Bug 57995 - Use FileServer in HTTP Request files

Modified:
    jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
    jmeter/trunk/test/src/org/apache/jmeter/services/TestFileServer.java
    jmeter/trunk/xdocs/changes.xml

Modified: jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java?rev=1683328&r1=1683327&r2=1683328&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/services/FileServer.java Wed Jun  3 13:38:24 2015
@@ -267,9 +267,7 @@ public class FileServer {
         }
         FileEntry fileEntry = files.get(alias);
         if (fileEntry == null) {
-            File f = new File(filename);
-            fileEntry =
-                new FileEntry(f.isAbsolute() ? f : new File(base, filename),null,charsetName);
+            fileEntry = new FileEntry(resolveFileFromPath(filename), null, charsetName);
             if (filename.equals(alias)){
                 log.info("Stored: "+filename);
             } else {
@@ -294,7 +292,23 @@ public class FileServer {
         return fileEntry.headerLine;
     }
 
-   /**
+    /**
+     * Resolves file name into {@link File} instance.
+     * When filename is not absolute and not found from current workind dir,
+     * it tries to find it under current base directory
+     * @param filename original file name
+     * @return {@link File} instance
+     */
+    private File resolveFileFromPath(String filename) {
+        File f = new File(filename);
+        if (f.isAbsolute() || f.exists()) {
+            return f;
+        } else {
+            return new File(base, filename);
+        }
+    }
+
+    /**
      * Get the next line of the named file, recycle by default.
      *
      * @param filename the filename or alias that was used to reserve the file
@@ -506,6 +520,17 @@ public class FileServer {
         return input;
     }
 
+    /**
+     * Get {@link File} instance for provided file path,
+     * resolve file location relative to base dir or script dir when needed
+     * @param path original path to file, maybe relative
+     * @return {@link File} instance 
+     */
+    public File getResolvedFile(String path) {
+        reserveFile(path);
+        return files.get(path).file;
+    }
+
     private static class FileEntry{
         private String headerLine;
         private Throwable exception;

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java?rev=1683328&r1=1683327&r2=1683328&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java (original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java Wed Jun  3 13:38:24 2015
@@ -75,6 +75,7 @@ import org.apache.jmeter.protocol.http.u
 import org.apache.jmeter.protocol.http.util.HTTPFileArg;
 import org.apache.jmeter.protocol.http.util.LoopbackHttpClientSocketFactory;
 import org.apache.jmeter.protocol.http.util.SlowHttpClientSocketFactory;
+import org.apache.jmeter.services.FileServer;
 import org.apache.jmeter.testelement.property.CollectionProperty;
 import org.apache.jmeter.testelement.property.PropertyIterator;
 import org.apache.jmeter.util.JMeterUtils;
@@ -770,7 +771,7 @@ public class HTTPHC3Impl extends HTTPHCA
             // Add any files
             for (int i=0; i < files.length; i++) {
                 HTTPFileArg file = files[i];
-                File inputFile = new File(file.getPath());
+                File inputFile = FileServer.getFileServer().getResolvedFile(file.getPath());
                 // We do not know the char set of the file to be uploaded, so we set it to null
                 ViewableFilePart filePart = new ViewableFilePart(file.getParamName(), inputFile, file.getMimeType(), null);
                 filePart.setCharSet(null); // We do not know what the char set of the file is
@@ -986,7 +987,8 @@ public class HTTPHC3Impl extends HTTPHCA
             hasPutBody = true;
 
             // If getSendFileAsPostBody returned true, it's sure that file is not null
-            FileRequestEntity fileRequestEntity = new FileRequestEntity(new File(files[0].getPath()),null);
+            File reservedFile = FileServer.getFileServer().getResolvedFile(files[0].getPath());
+            FileRequestEntity fileRequestEntity = new FileRequestEntity(reservedFile,null);
             put.setRequestEntity(fileRequestEntity);
         }
         // If none of the arguments have a name specified, we

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java?rev=1683328&r1=1683327&r2=1683328&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java (original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java Wed Jun  3 13:38:24 2015
@@ -117,6 +117,7 @@ import org.apache.jmeter.protocol.http.u
 import org.apache.jmeter.protocol.http.util.SlowHC4SSLSocketFactory;
 import org.apache.jmeter.protocol.http.util.SlowHC4SocketFactory;
 import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.services.FileServer;
 import org.apache.jmeter.testelement.property.CollectionProperty;
 import org.apache.jmeter.testelement.property.PropertyIterator;
 import org.apache.jmeter.util.JMeterUtils;
@@ -1026,7 +1027,9 @@ public class HTTPHC4Impl extends HTTPHCA
             ViewableFileBody[] fileBodies = new ViewableFileBody[files.length];
             for (int i=0; i < files.length; i++) {
                 HTTPFileArg file = files[i];
-                fileBodies[i] = new ViewableFileBody(new File(file.getPath()), file.getMimeType());
+                
+                File reservedFile = FileServer.getFileServer().getResolvedFile(file.getPath());
+                fileBodies[i] = new ViewableFileBody(reservedFile, file.getMimeType());
                 multiPart.addPart(file.getParamName(),fileBodies[i]);
             }
 
@@ -1229,7 +1232,8 @@ public class HTTPHC4Impl extends HTTPHCA
             hasEntityBody = true;
 
             // If getSendFileAsPostBody returned true, it's sure that file is not null
-            FileEntity fileRequestEntity = new FileEntity(new File(files[0].getPath())); // no need for content-type here
+            File reservedFile = FileServer.getFileServer().getResolvedFile(files[0].getPath());
+            FileEntity fileRequestEntity = new FileEntity(reservedFile); // no need for content-type here
             entity.setEntity(fileRequestEntity);
         }
         // If none of the arguments have a name specified, we

Modified: jmeter/trunk/test/src/org/apache/jmeter/services/TestFileServer.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/services/TestFileServer.java?rev=1683328&r1=1683327&r2=1683328&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/services/TestFileServer.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/services/TestFileServer.java Wed Jun  3 13:38:24 2015
@@ -156,4 +156,17 @@ public class TestFileServer extends JMet
             assertTrue("Expected EOF", e.getCause() instanceof java.io.EOFException);
         }
     }
+
+    public void testResolvingPaths() {
+        final File anchor = new File(findTestPath("testfiles/empty.csv"));
+
+        // absolute
+        assertTrue(FS.getResolvedFile(anchor.getAbsolutePath()).exists());
+
+        // relative
+        assertTrue(FS.getResolvedFile(anchor.getParentFile().getPath() + "/../testfiles/empty.csv").exists());
+        // test-plan-relative
+        FS.setBaseForScript(anchor);
+        assertTrue(FS.getResolvedFile(anchor.getName()).exists());
+    }
 }

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1683328&r1=1683327&r2=1683328&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml (original)
+++ jmeter/trunk/xdocs/changes.xml Wed Jun  3 13:38:24 2015
@@ -78,6 +78,7 @@ Summary
 <h3>HTTP Samplers and Test Script Recorder</h3>
 <ul>
     <li><bug>57696</bug>HTTP Request : Improve responseMessage when resource download fails. Contributed by Ubik Load Pack (support at ubikloadpack.com)</li>
+    <li><bug>57995</bug>Use FileServer for HTTP Request files. Implemented by Andrey Pokhilko (andrey at blazemeter.com) and contributed by BlazeMeter Ltd.</li>
 </ul>
 
 <h3>Other samplers</h3>