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

📄 jmetertest.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				suite.addTest(ts);
			} catch (IllegalArgumentException e) {
				System.out.println("Cannot create test for " + c.getName() + " " + e);
				e.printStackTrace(System.out);
			}
		}
		return suite;
	}

	/*
	 * Test GUI elements - run the test
	 */
	public void runGUITitle() throws Exception {
		if (guiTitles.size() > 0) {
			String title = guiItem.getDocAnchor();
			boolean ct = guiTitles.containsKey(title);
			if (ct) {
				guiTitles.put(title, Boolean.TRUE);// So we can detect extra entries
            }
            String name = guiItem.getClass().getName();
			if (// Is this a work in progress or an internal GUI component?
			    (title != null && title.length() > 0) // Will be "" for internal components
				&& (title.toUpperCase().indexOf("(ALPHA") == -1) 
                && (title.toUpperCase().indexOf("(BETA") == -1)
				&& (!title.equals("Example1")) // Skip the example samplers ...
				&& (!title.equals("Example2"))
                && (!name.startsWith("org.apache.jmeter.examples."))
                )
            {// No, not a work in progress ...
                String s = "component_reference.xml needs '" + title + "' anchor for " + name;
				if (!ct) {
					log.warn(s); // Record in log as well
                }
				assertTrue(s, ct);
			}
		}
	}

	/*
	 * run the function test
	 */
	public void runFunction() throws Exception {
		if (funcTitles.size() > 0) {
			String title = funcItem.getReferenceKey();
			boolean ct = funcTitles.containsKey(title);
			if (ct)
				funcTitles.put(title, Boolean.TRUE);// For detecting extra
													// entries
			if (// Is this a work in progress ?
			title.indexOf("(ALPHA") == -1 && title.indexOf("(EXPERIMENTAL") == -1) {// No,
																					// not
																					// a
																					// work
																					// in
																					// progress
																					// ...
				String s = "function.xml needs '" + title + "' entry for " + funcItem.getClass().getName();
				if (!ct)
					log.warn(s); // Record in log as well
				assertTrue(s, ct);
			}
		}
	}

	/*
	 * Check that function descriptions are OK
	 */
	public void runFunction2() throws Exception {
		Iterator i = funcItem.getArgumentDesc().iterator();
		while (i.hasNext()) {
			Object o = i.next();
			assertTrue("Description must be a String", o instanceof String);
			assertFalse("Description must not start with [refkey", ((String) o).startsWith("[refkey"));
		}
	}

	/*
	 * Test GUI elements - run for all components
	 */
	public void GUIComponents1() throws Exception {
        // We must set the Locale to the same value that was used when the
        // GUI objects was instantiated in this class
        JMeterUtils.setLocale(TEST_LOCALE);
		String name = guiItem.getClass().getName();

		assertEquals("Name should be same as static label for " + name, guiItem.getStaticLabel(), guiItem.getName());
        if (name.startsWith("org.apache.jmeter.examples.")){
            return;
        }
		if (!name.endsWith("TestBeanGUI")) {
			try {
				String label = guiItem.getLabelResource();
				assertTrue(label.length() > 0);
				if (!label.equals("unused")) { // TODO use constant
					assertFalse("'" + label + "' should be in resource file for " + name, JMeterUtils.getResString(
							label).startsWith(JMeterUtils.RES_KEY_PFX));
				}
			} catch (UnsupportedOperationException uoe) {
				log.warn("Class has not yet implemented getLabelResource " + name);
			}
		}
        checkElementAlias(guiItem);
	}

	/*
	 * Test GUI elements - not run for TestBeanGui items
	 */
	public void GUIComponents2() throws Exception {
		String name = guiItem.getClass().getName();

		// TODO these assertions should be separate tests

		TestElement el = guiItem.createTestElement();
		assertNotNull(name + ".createTestElement should be non-null ", el);
		assertEquals("GUI-CLASS: Failed on " + name, name, el.getPropertyAsString(TestElement.GUI_CLASS));

		assertEquals("NAME: Failed on " + name, guiItem.getName(), el.getPropertyAsString(TestElement.NAME));
		assertEquals("TEST-CLASS: Failed on " + name, el.getClass().getName(), el
				.getPropertyAsString(TestElement.TEST_CLASS));
		TestElement el2 = guiItem.createTestElement();
		el.setProperty(TestElement.NAME, "hey, new name!:");
		el.setProperty("NOT", "Shouldn't be here");
		if (!(guiItem instanceof UnsharedComponent)) {
			assertEquals("SHARED: Failed on " + name, "", el2.getPropertyAsString("NOT"));
		}
		log.debug("Saving element: " + el.getClass());
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		SaveService.saveElement(el, bos);
		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
		bos.close();
		el = (TestElement) SaveService.loadElement(bis);
		bis.close();
        assertNotNull("Load element failed on: "+name,el);
		guiItem.configure(el);
		assertEquals("CONFIGURE-TEST: Failed on " + name, el.getPropertyAsString(TestElement.NAME), guiItem.getName());
		guiItem.modifyTestElement(el2);
		assertEquals("Modify Test: Failed on " + name, "hey, new name!:", el2.getPropertyAsString(TestElement.NAME));
	}

	/*
	 * Test serializable elements - create the suite of tests
	 */
	private static Test suiteSerializableElements() throws Exception {
		TestSuite suite = new TestSuite("SerializableElements");
		Iterator iter = getObjects(Serializable.class).iterator();
		while (iter.hasNext()) {
			Serializable serObj = (Serializable) iter.next();
			if (serObj.getClass().getName().endsWith("_Stub")) {
				continue;
			}
			TestSuite ts = new TestSuite(serObj.getClass().getName());
			ts.addTest(new JMeterTest("runSerialTest", serObj));
			suite.addTest(ts);
		}
		return suite;
	}

	/*
	 * Test serializable elements - test the object
	 */
	public void runSerialTest() throws Exception {
		if (!(serObj instanceof JComponent)) {
			try {
				ByteArrayOutputStream bytes = new ByteArrayOutputStream();
				ObjectOutputStream out = new ObjectOutputStream(bytes);
				out.writeObject(serObj);
				out.close();
				ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
				Object readObject = in.readObject();
				in.close();
				assertEquals("deserializing class: " + serObj.getClass().getName(), serObj.getClass(), readObject
						.getClass());
			} catch (Throwable e) {
				fail("serialization of " + serObj.getClass().getName() + " failed: " + e);
			}
		}
	}

	/*
	 * Test TestElements - create the suite
	 */
	private static Test suiteTestElements() throws Exception {
		TestSuite suite = new TestSuite("TestElements");
		Iterator iter = getObjects(TestElement.class).iterator();
		while (iter.hasNext()) {
			TestElement item = (TestElement) iter.next();
			TestSuite ts = new TestSuite(item.getClass().getName());
			ts.addTest(new JMeterTest("runTestElement", item));
			suite.addTest(ts);
		}
		return suite;
	}

	/*
	 * Test TestElements - implement the test case
	 */
	public void runTestElement() throws Exception {
		checkElementCloning(testItem);
		String name = testItem.getClass().getName();
        assertTrue(name + " must implement Serializable", testItem instanceof Serializable);
        if (name.startsWith("org.apache.jmeter.examples.")){
            return;
        }
        if (name.equals("org.apache.jmeter.control.TransactionSampler")){
        	return; // Not a real sampler
        }
        	
        checkElementAlias(testItem);
	}

    public void readAliases() throws Exception {
        nameMap = SaveService.loadProperties();        
        assertNotNull("SaveService nameMap should not be null",nameMap);
    }
    
	private void checkElementAlias(Object item) {
        String name=item.getClass().getName();
        boolean contains = nameMap.values().contains(name);
        if (!contains){
            //System.out.println(name.substring(name.lastIndexOf('.')+1)+"="+name);
            fail("SaveService nameMap should contain "+name);
        }
    }

    private static Collection getObjects(Class extendsClass) throws Exception {
		String exName = extendsClass.getName();
		Object myThis = "";
		Iterator classes = ClassFinder
				.findClassesThatExtend(JMeterUtils.getSearchPaths(), new Class[] { extendsClass }).iterator();
		List objects = new LinkedList();
		String n = "";
		boolean caughtError = true;
		Throwable caught = null;
		try {
			while (classes.hasNext()) {
				n = (String) classes.next();
				// TODO - improve this check
				if (n.endsWith("RemoteJMeterEngineImpl")) {
					continue; // Don't try to instantiate remote server
				}
				Class c = null;
				try {
					c = Class.forName(n);
					try {
						// Try with a parameter-less constructor first
						objects.add(c.newInstance());
					} catch (InstantiationException e) {
						caught = e;
						// System.out.println(e.toString());
						try {
							// Events often have this constructor
							objects.add(c.getConstructor(new Class[] { Object.class }).newInstance(
									new Object[] { myThis }));
						} catch (NoSuchMethodException f) {
							// no luck. Ignore this class
							System.out.println("WARN: " + exName + ": NoSuchMethodException  " + n);
						}
					}
				} catch (NoClassDefFoundError e) {
					// no luck. Ignore this class
					System.out.println("WARN: " + exName + ": NoClassDefFoundError " + n);
				} catch (IllegalAccessException e) {
					caught = e;
					System.out.println("WARN: " + exName + ": IllegalAccessException " + n);
					// We won't test restricted-access classes.
				}
				// JDK1.4: catch (java.awt.HeadlessException e)
				// JDK1.4: {
				// JDK1.4: System.out.println("Error creating "+n+"
				// "+e.toString());
				// JDK1.4: }
				catch (Exception e) {
					caught = e;
					if ((e instanceof RemoteException) || e.getClass().getName().equals("java.awt.HeadlessException")// for
																														// JDK1.3
					) {
						System.out.println("WARN: " + "Error creating " + n + " " + e.toString());
					} else {
						throw new Exception("Error creating " + n + " " + e.toString());
					}
				}
			}
			caughtError = false;
		} finally {
			if (caughtError) {
				System.out.println("Last class=" + n);
				System.out.println("objects.size=" + objects.size());
				System.out.println("Last error=" + caught);
			}
		}

		if (objects.size() == 0) {
			System.out.println("No classes found that extend " + exName + ". Check the following:");
			System.out.println("Search paths are:");
			String ss[] = JMeterUtils.getSearchPaths();
			for (int i = 0; i < ss.length; i++) {
				System.out.println(ss[i]);
			}
			if (!classPathShown) {// Only dump it once
				System.out.println("Class path is:");
				String cp = System.getProperty("java.class.path");
				String cpe[] = JOrphanUtils.split(cp, java.io.File.pathSeparator);
				for (int i = 0; i < cpe.length; i++) {
					System.out.println(cpe[i]);
				}
				classPathShown = true;
			}
		}
		return objects;
	}

	private static void cloneTesting(TestElement item, TestElement clonedItem) {
		assertTrue(item != clonedItem);
		assertEquals("CLONE-SAME-CLASS: testing " + item.getClass().getName(), item.getClass().getName(), clonedItem
				.getClass().getName());
	}

	private static void checkElementCloning(TestElement item) {
		TestElement clonedItem = (TestElement) item.clone();
		cloneTesting(item, clonedItem);
		PropertyIterator iter2 = item.propertyIterator();
		while (iter2.hasNext()) {
			JMeterProperty item2 = iter2.next();
			// [sebb] assertEquals(item2,
			// clonedItem.getProperty(item2.getName()));
			assertEquals(item2.getStringValue(), clonedItem.getProperty(item2.getName()).getStringValue());
			assertTrue(item2 != clonedItem.getProperty(item2.getName()));
		}
	}
}

⌨️ 快捷键说明

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