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

📄 menufactory.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	}

	/**
	 * Create a menu from a collection of items.
	 * 
	 * @param menuInfo - collection of MenuInfo items
	 * @param actionCommand - predefined string, e.g. ActionNames.ADD
	 *     @see org.apache.jmeter.gui.action.ActionNames
	 * @param menuName
	 * @return the menu
	 */
	public static JMenu makeMenu(Collection menuInfo, String actionCommand, String menuName) {
		Iterator iter = menuInfo.iterator();
		JMenu menu = new JMenu(menuName);
		while (iter.hasNext()) {
			MenuInfo info = (MenuInfo) iter.next();
			menu.add(makeMenuItem(info.label, info.className, actionCommand));
		}
		return menu;
	}

	public static void setEnabled(JMenu menu) {
		if (menu.getSubElements().length == 0) {
			menu.setEnabled(false);
		}
	}

	/**
	 * Create a single menu item
	 * 
	 * @param label for the MenuItem
	 * @param name for the MenuItem
	 * @param actionCommand - predefined string, e.g. ActionNames.ADD
	 *     @see org.apache.jmeter.gui.action.ActionNames
	 * @return the menu item
	 */
	public static JMenuItem makeMenuItem(String label, String name, String actionCommand) {
		JMenuItem newMenuChoice = new JMenuItem(label);
		newMenuChoice.setName(name);
		newMenuChoice.addActionListener(ActionRouter.getInstance());
		if (actionCommand != null) {
			newMenuChoice.setActionCommand(actionCommand);
		}

		return newMenuChoice;
	}

	public static JMenuItem makeMenuItem(String label, String name, String actionCommand, KeyStroke accel) {
		JMenuItem item = makeMenuItem(label, name, actionCommand);
		item.setAccelerator(accel);
		return item;
	}

	private static void initializeMenus() {
		try {
			List guiClasses = ClassFinder.findClassesThatExtend(JMeterUtils.getSearchPaths(), new Class[] {
					JMeterGUIComponent.class, TestBean.class });
			timers = new LinkedList();
			controllers = new LinkedList();
			samplers = new LinkedList();
			configElements = new LinkedList();
			assertions = new LinkedList();
			listeners = new LinkedList();
			postProcessors = new LinkedList();
			preProcessors = new LinkedList();
			nonTestElements = new LinkedList();
			menuMap.put(TIMERS, timers);
			menuMap.put(ASSERTIONS, assertions);
			menuMap.put(CONFIG_ELEMENTS, configElements);
			menuMap.put(CONTROLLERS, controllers);
			menuMap.put(LISTENERS, listeners);
			menuMap.put(NON_TEST_ELEMENTS, nonTestElements);
			menuMap.put(SAMPLERS, samplers);
			menuMap.put(POST_PROCESSORS, postProcessors);
			menuMap.put(PRE_PROCESSORS, preProcessors);
			Collections.sort(guiClasses);
			Iterator iter = guiClasses.iterator();
			while (iter.hasNext()) {
				String name = (String) iter.next();

				/*
				 * JMeterTreeNode and TestBeanGUI are special GUI classes, and
				 * aren't intended to be added to menus
				 * 
				 * TODO: find a better way of checking this
				 */
				if (name.endsWith("JMeterTreeNode") // $NON-NLS-1$
                        || name.endsWith("TestBeanGUI")) {// $NON-NLS-1$
					continue;// Don't try to instantiate these
				}

				JMeterGUIComponent item;
				try {
					Class c = Class.forName(name);
					if (TestBean.class.isAssignableFrom(c)) {
						item = new TestBeanGUI(c);
					} else {
						item = (JMeterGUIComponent) c.newInstance();
					}
				} catch (NoClassDefFoundError e) {
					log.warn("Missing jar? Could not create " + name + ". " + e);
					continue;
				} catch (Throwable e) {
					log.warn("Could not instantiate " + name, e);
					continue;
				}
				if (elementsToSkip.contains(name) || elementsToSkip.contains(item.getStaticLabel())) {
					log.info("Skipping " + name);
					continue;
				} else {
					elementsToSkip.add(name);
				}
				Collection categories = item.getMenuCategories();
				if (categories == null) {
					log.debug(name + " participates in no menus.");
					continue;
				}
				if (categories.contains(TIMERS)) {
					timers.add(new MenuInfo(item.getStaticLabel(), name));
				}

				if (categories.contains(POST_PROCESSORS)) {
					postProcessors.add(new MenuInfo(item.getStaticLabel(), name));
				}

				if (categories.contains(PRE_PROCESSORS)) {
					preProcessors.add(new MenuInfo(item.getStaticLabel(), name));
				}

				if (categories.contains(CONTROLLERS)) {
					controllers.add(new MenuInfo(item.getStaticLabel(), name));
				}

				if (categories.contains(SAMPLERS)) {
					samplers.add(new MenuInfo(item.getStaticLabel(), name));
				}

				if (categories.contains(NON_TEST_ELEMENTS)) {
					nonTestElements.add(new MenuInfo(item.getStaticLabel(), name));
				}

				if (categories.contains(LISTENERS)) {
					listeners.add(new MenuInfo(item.getStaticLabel(), name));
				}

				if (categories.contains(CONFIG_ELEMENTS)) {
					configElements.add(new MenuInfo(item.getStaticLabel(), name));
				}
				if (categories.contains(ASSERTIONS)) {
					assertions.add(new MenuInfo(item.getStaticLabel(), name));
				}

			}
		} catch (IOException e) {
            log.error("", e);
        }
	}

	private static void addSeparator(JPopupMenu menu) {
		MenuElement[] elements = menu.getSubElements();
		if ((elements.length > 0) && !(elements[elements.length - 1] instanceof JPopupMenu.Separator)) {
			menu.addSeparator();
		}
	}

	/**
	 * Determine whether or not nodes can be added to this parent.
	 * 
	 * Used by Merge
	 * 
	 * @param parentNode
	 * @param element - top-level test element to be added
	 * 
	 * @return whether it is OK to add the element to this parent
	 */
	public static boolean canAddTo(JMeterTreeNode parentNode, TestElement element) {
		JMeterTreeNode node = new JMeterTreeNode(element, null);
		return canAddTo(parentNode, new JMeterTreeNode[]{node});
	}

	/**
	 * Determine whether or not nodes can be added to this parent.
	 * 
	 * Used by DragNDrop and Paste.
	 * 
	 * @param parentNode
	 * @param nodes - array of nodes that are to be added
	 * 
	 * @return whether it is OK to add the dragged nodes to this parent
	 */
	public static boolean canAddTo(JMeterTreeNode parentNode, JMeterTreeNode nodes[]) {
		if (null == parentNode) {
			return false;
		}
		if (foundClass(nodes, new Class[]{WorkBench.class})){// Can't add a Workbench anywhere
			return false;
		}
		if (foundClass(nodes, new Class[]{TestPlan.class})){// Can't add a TestPlan anywhere
			return false;
		}
		TestElement parent = parentNode.getTestElement();
		if (parent instanceof WorkBench) {// allow everything else
			return true;
		}
		if (parent instanceof TestPlan) {
			if (foundClass(nodes, 
					 new Class[]{Sampler.class, Controller.class}, // Samplers and Controllers need not apply ...
			         org.apache.jmeter.threads.ThreadGroup.class)  // but ThreadGroup (Controller) is OK
			    ){
				return false;
			}
			return true;
		}
		// ThreadGroup is only allowed under a TestPlan
		if (foundClass(nodes, new Class[]{org.apache.jmeter.threads.ThreadGroup.class})){
			return false;
		}
		if (parent instanceof Controller) {// Includes thread group; anything goes
			return true;
		}
		if (parent instanceof Sampler) {// Samplers and Controllers need not apply ...
			if (foundClass(nodes, new Class[]{Sampler.class, Controller.class})){
				return false;
			}
			return true;
		}
		// All other 
		return false;
	}

	// Is any node an instance of one of the classes?
	private static boolean foundClass(JMeterTreeNode nodes[],Class classes[]){
		for (int i = 0; i < nodes.length; i++) {
			JMeterTreeNode node = nodes[i];
			for (int j=0; j < classes.length; j++) {
				if (classes[j].isInstance(node.getUserObject())){
					return true;
				}				
			}
		}
		return false;
	}

	// Is any node an instance of one of the classes, but not an exception?
	private static boolean foundClass(JMeterTreeNode nodes[],Class classes[], Class except){
		for (int i = 0; i < nodes.length; i++) {
			JMeterTreeNode node = nodes[i];
			Object userObject = node.getUserObject();
			if (!except.isInstance(userObject)) {
				for (int j=0; j < classes.length; j++) {
					if (classes[j].isInstance(userObject)){
						return true;
					}				
				}
			}
		}
		return false;
	}

	// Methods used for Test cases
	static int menuMap_size() {
		return menuMap.size();
	}
	static int assertions_size() {
		return assertions.size();
	}
	static int configElements_size() {
		return configElements.size();
	}
	static int controllers_size() {
		return controllers.size();
	}
	static int listeners_size() {
		return listeners.size();
	}
	static int nonTestElements_size() {
		return nonTestElements.size();
	}
	static int postProcessors_size() {
		return postProcessors.size();
	}
	static int preProcessors_size() {
		return preProcessors.size();
	}
	static int samplers_size() {
		return samplers.size();
	}
	static int timers_size() {
		return timers.size();
	}
	static int elementsToSkip_size() {
		return elementsToSkip.size();
	}
}

⌨️ 快捷键说明

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