⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proxycontrol.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			return myTarget;
		myTarget = findFirstNodeOfType(ThreadGroup.class);
		if (myTarget != null)
			return myTarget;
		myTarget = findFirstNodeOfType(WorkBench.class);
		if (myTarget != null)
			return myTarget;
		log.error("Program error: proxy recording target not found.");
		return null;
	}

	/**
	 * Finds all configuration objects of the given class applicable to the
	 * recorded samplers, that is:
	 * <ul>
	 * <li>All such elements directly within the HTTP Proxy Server (these have
	 * the highest priority).
	 * <li>All such elements directly within the target controller (higher
	 * priority) or directly within any containing controller (lower priority),
	 * including the Test Plan itself (lowest priority).
	 * </ul>
	 * 
	 * @param myTarget
	 *            tree node for the recording target controller.
	 * @param myClass
	 *            Class of the elements to be found.
	 * @param ascending
	 *            true if returned elements should be ordered in ascending
	 *            priority, false if they should be in descending priority.
	 * 
	 * @return a collection of applicable objects of the given class.
	 */
	private Collection findApplicableElements(JMeterTreeNode myTarget, Class myClass, boolean ascending) {
		JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
		LinkedList elements = new LinkedList();

		// Look for elements directly within the HTTP proxy:
		Enumeration kids = treeModel.getNodeOf(this).children();
		while (kids.hasMoreElements()) {
			JMeterTreeNode subNode = (JMeterTreeNode) kids.nextElement();
			if (subNode.isEnabled()) {
				TestElement element = (TestElement) subNode.getUserObject();
				if (myClass.isInstance(element)) {
					if (ascending)
						elements.addFirst(element);
					else
						elements.add(element);
				}
			}
		}

		// Look for arguments elements in the target controller or higher up:
		for (JMeterTreeNode controller = myTarget; controller != null; controller = (JMeterTreeNode) controller
				.getParent()) {
			kids = controller.children();
			while (kids.hasMoreElements()) {
				JMeterTreeNode subNode = (JMeterTreeNode) kids.nextElement();
				if (subNode.isEnabled()) {
					TestElement element = (TestElement) subNode.getUserObject();
					if (myClass.isInstance(element)) {
						log.debug("Applicable: " + element.getPropertyAsString(TestElement.NAME));
						if (ascending)
							elements.addFirst(element);
						else
							elements.add(element);
					}

					// Special case for the TestPlan's Arguments sub-element:
					if (element instanceof TestPlan) {
						TestPlan tp = (TestPlan) element;
						Arguments args = tp.getArguments();
						if (myClass.isInstance(args)) {
							if (ascending)
								elements.addFirst(args);
							else
								elements.add(args);
						}
					}
				}
			}
		}

		return elements;
	}

	private void placeSampler(HTTPSamplerBase sampler, TestElement[] subConfigs, JMeterTreeNode myTarget) {
		try {
			JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();

			boolean firstInBatch = false;
			long now = System.currentTimeMillis();
			long deltaT = now - lastTime;
			if (deltaT > sampleGap) {
				if (!myTarget.isLeaf() && groupingMode == GROUPING_ADD_SEPARATORS) {
					addDivider(treeModel, myTarget);
				}
				if (groupingMode == GROUPING_IN_CONTROLLERS) {
					addSimpleController(treeModel, myTarget, sampler.getName());
				}
				firstInBatch = true;// Remember this was first in its batch
			}
			if (lastTime == 0)
				deltaT = 0; // Decent value for timers
			lastTime = now;

			if (groupingMode == GROUPING_STORE_FIRST_ONLY) {
				if (!firstInBatch)
					return; // Huh! don't store this one!

				// If we're not storing subsequent samplers, we'll need the
				// first sampler to do all the work...:
				sampler.setFollowRedirects(true);
				sampler.setImageParser(true);
			}

			if (groupingMode == GROUPING_IN_CONTROLLERS) {
				// Find the last controller in the target to store the
				// sampler there:
				for (int i = myTarget.getChildCount() - 1; i >= 0; i--) {
					JMeterTreeNode c = (JMeterTreeNode) myTarget.getChildAt(i);
					if (c.getTestElement() instanceof GenericController) {
						myTarget = c;
						break;
					}
				}
			}

			JMeterTreeNode newNode = treeModel.addComponent(sampler, myTarget);

			if (firstInBatch) {
				if (addAssertions) {
					addAssertion(treeModel, newNode);
				}
				addTimers(treeModel, newNode, deltaT);
				firstInBatch = false;
			}

			for (int i = 0; subConfigs != null && i < subConfigs.length; i++) {
				if (subConfigs[i] instanceof HeaderManager) {
					subConfigs[i].setProperty(TestElement.GUI_CLASS, HEADER_PANEL);
					treeModel.addComponent(subConfigs[i], newNode);
				}
			}
		} catch (IllegalUserActionException e) {
			JMeterUtils.reportErrorToUser(e.getMessage());
		}
	}

	/**
	 * Remove from the sampler all values which match the one provided by the
	 * first configuration in the given collection which provides a value for
	 * that property.
	 * 
	 * @param sampler
	 *            Sampler to remove values from.
	 * @param configurations
	 *            ConfigTestElements in descending priority.
	 */
	private void removeValuesFromSampler(HTTPSamplerBase sampler, Collection configurations) {
		for (PropertyIterator props = sampler.propertyIterator(); props.hasNext();) {
			JMeterProperty prop = props.next();
			String name = prop.getName();
			String value = prop.getStringValue();

			// There's a few properties which are excluded from this processing:
			if (name.equals(TestElement.ENABLED) || name.equals(TestElement.GUI_CLASS) || name.equals(TestElement.NAME)
					|| name.equals(TestElement.TEST_CLASS)) {
				continue; // go on with next property.
			}

			for (Iterator configs = configurations.iterator(); configs.hasNext();) {
				ConfigTestElement config = (ConfigTestElement) configs.next();

				String configValue = config.getPropertyAsString(name);

				if (configValue != null && configValue.length() > 0) {
					if (configValue.equals(value))
						sampler.setProperty(name, ""); // $NON-NLS-1$
					// Property was found in a config element. Whether or not
					// it matched the value in the sampler, we're done with
					// this property -- don't look at lower-priority configs:
					break;
				}
			}
		}
	}

	private String generateMatchUrl(HTTPSamplerBase sampler) {
		StringBuffer buf = new StringBuffer(sampler.getDomain());
		buf.append(':'); // $NON-NLS-1$
		buf.append(sampler.getPort());
		buf.append(sampler.getPath());
		if (sampler.getQueryString().length() > 0) {
			buf.append('?'); // $NON-NLS-1$
			buf.append(sampler.getQueryString());
		}
		return buf.toString();
	}

	private boolean matchesPatterns(String url, CollectionProperty patterns) {
		PropertyIterator iter = patterns.iterator();
		while (iter.hasNext()) {
			String item = iter.next().getStringValue();
			Pattern pattern = null;
			try {
				pattern = JMeterUtils.getPatternCache().getPattern(item, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
				if (JMeterUtils.getMatcher().matches(url, pattern)) {
					return true;
				}
			} catch (MalformedCachePatternException e) {
				log.warn("Skipped invalid pattern: " + item, e);
			}
		}
		return false;
	}

	/**
	 * Scan all test elements passed in for values matching the value of any of
	 * the variables in any of the variable-holding elements in the collection.
	 * 
	 * @param sampler
	 *            A TestElement to replace values on
	 * @param configs
	 *            More TestElements to replace values on
	 * @param variables
	 *            Collection of Arguments to use to do the replacement, ordered
	 *            by ascending priority.
	 */
	private void replaceValues(TestElement sampler, TestElement[] configs, Collection variables) {
		// Build the replacer from all the variables in the collection:
		ValueReplacer replacer = new ValueReplacer();
		for (Iterator vars = variables.iterator(); vars.hasNext();) {
			replacer.addVariables(((Arguments) vars.next()).getArgumentsAsMap());
		}

		try {
			replacer.reverseReplace(sampler, regexMatch);
			for (int i = 0; i < configs.length; i++) {
				if (configs[i] != null) {
					replacer.reverseReplace(configs[i], regexMatch);
				}

			}
		} catch (InvalidVariableException e) {
			log.warn("Invalid variables included for replacement into recorded " + "sample", e);
		}
	}

	/**
	 * This will notify sample listeners directly within the Proxy of the
	 * sampling that just occured -- so that we have a means to record the
	 * server's responses as we go.
	 * 
	 * @param event
	 *            sampling event to be delivered
	 */
	private void notifySampleListeners(SampleEvent event) {
		JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
		JMeterTreeNode myNode = treeModel.getNodeOf(this);
		Enumeration kids = myNode.children();
		while (kids.hasMoreElements()) {
			JMeterTreeNode subNode = (JMeterTreeNode) kids.nextElement();
			if (subNode.isEnabled()) {
				TestElement testElement = subNode.getTestElement();
				if (testElement instanceof SampleListener) {
					((SampleListener) testElement).sampleOccurred(event);
				}
			}
		}
	}

	/**
	 * This will notify test listeners directly within the Proxy that the 'test'
	 * (here meaning the proxy recording) has started.
	 */
	private void notifyTestListenersOfStart() {
		JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
		JMeterTreeNode myNode = treeModel.getNodeOf(this);
		Enumeration kids = myNode.children();
		while (kids.hasMoreElements()) {
			JMeterTreeNode subNode = (JMeterTreeNode) kids.nextElement();
			if (subNode.isEnabled()) {
				TestElement testElement = subNode.getTestElement();
				if (testElement instanceof TestListener) {
					((TestListener) testElement).testStarted();
				}
			}
		}
	}

	/**
	 * This will notify test listeners directly within the Proxy that the 'test'
	 * (here meaning the proxy recording) has ended.
	 */
	private void notifyTestListenersOfEnd() {
		JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
		JMeterTreeNode myNode = treeModel.getNodeOf(this);
		Enumeration kids = myNode.children();
		while (kids.hasMoreElements()) {
			JMeterTreeNode subNode = (JMeterTreeNode) kids.nextElement();
			if (subNode.isEnabled()) {
				TestElement testElement = subNode.getTestElement();
				if (testElement instanceof TestListener) {
					((TestListener) testElement).testEnded();
				}
			}
		}
	}

	public boolean canRemove() {
		return null == server;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -