jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject svn commit: r1767048 - in /jmeter/trunk: src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java test/src/org/apache/jmeter/protocol/http/proxy/NonGuiProxySample.java xdocs/changes.xml
Date Fri, 28 Oct 2016 17:56:26 GMT
Author: fschumacher
Date: Fri Oct 28 17:56:25 2016
New Revision: 1767048

URL: http://svn.apache.org/viewvc?rev=1767048&view=rev
Log:
Remove dependency of ProxyControl on GuiPackage.
Based on patches by jarek102 at gmail.com and Wyatt Epp (wyatt.epp at gmail.com)

Bugzilla Id: 57305

Added:
    jmeter/trunk/test/src/org/apache/jmeter/protocol/http/proxy/NonGuiProxySample.java   (with
props)
Modified:
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
    jmeter/trunk/xdocs/changes.xml

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java?rev=1767048&r1=1767047&r2=1767048&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
(original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
Fri Oct 28 17:56:25 2016
@@ -301,6 +301,8 @@ public class ProxyControl extends Generi
 
     private String keyPassword;
 
+    private JMeterTreeModel nonGuiTreeModel;
+
     public ProxyControl() {
         setPort(DEFAULT_PORT);
         setExcludeList(new HashSet<String>());
@@ -308,6 +310,19 @@ public class ProxyControl extends Generi
         setCaptureHttpHeaders(true); // maintain original behaviour
     }
 
+    /**
+     * Set a {@link JMeterTreeModel} to be used by the ProxyControl, when used
+     * in a non-GUI environment, where the {@link JMeterTreeModel} can't be
+     * acquired through {@link GuiPackage#getTreeModel()}
+     *
+     * @param treeModel
+     *            the {@link JMeterTreeModel} to be used, or {@code null} when
+     *            the GUI model should be used
+     */
+    public void setNonGuiTreeModel(JMeterTreeModel treeModel) {
+        this.nonGuiTreeModel = treeModel;
+    }
+
     public void setPort(int port) {
         this.setProperty(new IntegerProperty(PORT, port));
     }
@@ -475,6 +490,13 @@ public class ProxyControl extends Generi
         return getPropertyAsString(CONTENT_TYPE_INCLUDE);
     }
 
+    /**
+     * @return the {@link JMeterTreeModel} used when run in non-GUI mode, or {@code null}
when run in GUI mode
+     */
+    public JMeterTreeModel getNonGuiTreeModel() {
+        return nonGuiTreeModel;
+    }
+
     public void addConfigElement(ConfigElement config) {
         // NOOP
     }
@@ -493,7 +515,9 @@ public class ProxyControl extends Generi
         try {
             server = new Daemon(getPort(), this);
             server.start();
-            GuiPackage.getInstance().register(server);
+            if (GuiPackage.getInstance() != null) {
+                GuiPackage.getInstance().register(server);
+            }
         } catch (IOException e) {
             log.error("Could not create Proxy daemon", e);
             throw e;
@@ -682,7 +706,9 @@ public class ProxyControl extends Generi
     public void stopProxy() {
         if (server != null) {
             server.stopServer();
-            GuiPackage.getInstance().unregister(server);
+            if (GuiPackage.getInstance() != null) {
+                GuiPackage.getInstance().unregister(server);
+            }
             try {
                 server.join(1000); // wait for server to stop
             } catch (InterruptedException e) {
@@ -819,7 +845,7 @@ public class ProxyControl extends Generi
      * @param target {@link JMeterTreeNode}
      */
     private void setAuthorization(Authorization authorization, JMeterTreeNode target) {
-        JMeterTreeModel jmeterTreeModel = GuiPackage.getInstance().getTreeModel();
+        JMeterTreeModel jmeterTreeModel = getJmeterTreeModel();
         List<JMeterTreeNode> authManagerNodes = jmeterTreeModel.getNodesOfType(AuthManager.class);
         if (authManagerNodes.size() == 0) {
             try {
@@ -835,6 +861,13 @@ public class ProxyControl extends Generi
         }
     }
 
+    private JMeterTreeModel getJmeterTreeModel() {
+        if (this.nonGuiTreeModel == null) {
+            return GuiPackage.getInstance().getTreeModel();
+        }
+        return this.nonGuiTreeModel;
+    }
+
     /**
      * Helper method to add a Response Assertion
      * Called from AWT Event thread
@@ -980,7 +1013,7 @@ public class ProxyControl extends Generi
      *         <code>null</code> if none was found.
      */
     private JMeterTreeNode findFirstNodeOfType(Class<?> type) {
-        JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
+        JMeterTreeModel treeModel = getJmeterTreeModel();
         List<JMeterTreeNode> nodes = treeModel.getNodesOfType(type);
         for (JMeterTreeNode node : nodes) {
             if (node.isEnabled()) {
@@ -1046,7 +1079,7 @@ public class ProxyControl extends Generi
      */
     // TODO - could be converted to generic class?
     private Collection<?> findApplicableElements(JMeterTreeNode myTarget, Class<?
extends TestElement> myClass, boolean ascending) {
-        JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
+        JMeterTreeModel treeModel = getJmeterTreeModel();
         LinkedList<TestElement> elements = new LinkedList<>();
 
         // Look for elements directly within the HTTP proxy:
@@ -1104,7 +1137,7 @@ public class ProxyControl extends Generi
     private void placeSampler(final HTTPSamplerBase sampler, final TestElement[] testElements,
             JMeterTreeNode myTarget) {
         try {
-            final JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
+            final JMeterTreeModel treeModel = getJmeterTreeModel();
 
             boolean firstInBatch = false;
             long now = System.currentTimeMillis();
@@ -1326,7 +1359,7 @@ public class ProxyControl extends Generi
      *            sampling event to be delivered
      */
     private void notifySampleListeners(SampleEvent event) {
-        JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
+        JMeterTreeModel treeModel = getJmeterTreeModel();
         JMeterTreeNode myNode = treeModel.getNodeOf(this);
         Enumeration<JMeterTreeNode> kids = myNode.children();
         while (kids.hasMoreElements()) {
@@ -1345,7 +1378,7 @@ public class ProxyControl extends Generi
      * (here meaning the proxy recording) has started.
      */
     private void notifyTestListenersOfStart() {
-        JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
+        JMeterTreeModel treeModel = getJmeterTreeModel();
         JMeterTreeNode myNode = treeModel.getNodeOf(this);
         Enumeration<JMeterTreeNode> kids = myNode.children();
         while (kids.hasMoreElements()) {
@@ -1364,7 +1397,7 @@ public class ProxyControl extends Generi
      * (here meaning the proxy recording) has ended.
      */
     private void notifyTestListenersOfEnd() {
-        JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
+        JMeterTreeModel treeModel = getJmeterTreeModel();
         JMeterTreeNode myNode = treeModel.getNodeOf(this);
         Enumeration<JMeterTreeNode> kids = myNode.children();
         while (kids.hasMoreElements()) {

Added: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/proxy/NonGuiProxySample.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/proxy/NonGuiProxySample.java?rev=1767048&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/protocol/http/proxy/NonGuiProxySample.java (added)
+++ jmeter/trunk/test/src/org/apache/jmeter/protocol/http/proxy/NonGuiProxySample.java Fri
Oct 28 17:56:25 2016
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ *
+ */
+
+package org.apache.jmeter.protocol.http.proxy;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.http.HttpHost;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
+import org.apache.jmeter.exceptions.IllegalUserActionException;
+import org.apache.jmeter.gui.tree.JMeterTreeModel;
+import org.apache.jmeter.gui.tree.JMeterTreeNode;
+import org.apache.jmeter.save.SaveService;
+import org.apache.jmeter.testelement.TestPlan;
+import org.apache.jmeter.threads.ThreadGroup;
+import org.apache.jmeter.util.JMeterUtils;
+import org.apache.jorphan.collections.ListedHashTree;
+
+public class NonGuiProxySample {
+    public static void main(String[] args) throws IllegalUserActionException,
+            IOException {
+        JMeterUtils.setJMeterHome("./"); // Or wherever you put it.
+        JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir()
+                + "/jmeter.properties");
+        JMeterUtils.initLocale();
+
+        TestPlan testPlan = new TestPlan();
+        ThreadGroup threadGroup = new ThreadGroup();
+        ListedHashTree testPlanTree = new ListedHashTree();
+        testPlanTree.add(testPlan);
+        testPlanTree.add(threadGroup, testPlan);
+
+        JMeterTreeModel treeModel = new JMeterTreeModel(new Object());
+
+        JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
+        treeModel.addSubTree(testPlanTree, root);
+
+        ProxyControl proxy = new ProxyControl();
+        proxy.setNonGuiTreeModel(treeModel);
+        proxy.setTarget(treeModel.getNodeOf(threadGroup));
+        proxy.setPort(8282);
+
+        treeModel.addComponent(proxy, (JMeterTreeNode) root.getChildAt(1));
+
+        proxy.startProxy();
+        HttpHost proxyHost = new HttpHost("localhost", 8282);
+        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(
+                proxyHost);
+        CloseableHttpClient httpclient = HttpClients.custom()
+                .setRoutePlanner(routePlanner).build();
+
+        try {
+            httpclient.execute(new HttpGet("http://example.invalid"));
+        } catch (Exception e) {
+            //
+        }
+        proxy.stopProxy();
+
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            SaveService.saveTree(treeModel.getTestPlan(), out);
+            out.close();
+            System.out.println(out.toString());
+        }
+
+    }
+}
+

Propchange: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/proxy/NonGuiProxySample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1767048&r1=1767047&r2=1767048&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml [utf-8] (original)
+++ jmeter/trunk/xdocs/changes.xml [utf-8] Fri Oct 28 17:56:25 2016
@@ -191,6 +191,7 @@ Summary
     <li><pr>229</pr> tiny memory allocation improvements. Contributed by
Benoit Wiart (b.wiart at ubik-ingenierie.com)</li>
     <li><bug>59945</bug>For all JSR223 elements, if script language has
not been chosen on the UI, the script will be interpreted as a groovy script.</li>
     <li><bug>60266</bug>Usability/ UX : It should not be possible to close/exit/Revert/Load/Load
a recent project or create from template a JMeter plan or open a new one if a test is running</li>
+    <li><bug>57305</bug>Remove dependency of <code>ProxyControl</code>
on <code>GuiPackage</code>. Based on patches by jarek102 at gmail.com and Wyatt
Epp (wyatt.epp at gmail.com)</li>
 </ul>
 
 <ch_section>Non-functional changes</ch_section>
@@ -298,6 +299,8 @@ Summary
 <li><a href="http://ubikloadpack.com">Ubik Load Pack</a></li>
 <li>Tamas Szabadi (tamas.szabadi at rightside.co)</li>
 <li>Roberto Braga (roberto.braga at soziale.it)</li>
+<li>jarek102 at gmail.com</li>
+<li>Wyatt Epp (wyatt.epp at gmail.com)</li>
 </ul>
 <p>We also thank bug reporters who helped us improve JMeter. <br/>
 For this release we want to give special thanks to the following reporters for the clear
reports and tests made after our fixes:</p>



Mime
View raw message