📄 generatedjarchecker.java
字号:
package org.uispec4j.extension;
import junit.framework.Assert;
import org.uispec4j.Panel;
import org.uispec4j.finder.ComponentMatcher;
import org.uispec4j.finder.ComponentMatchers;
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class GeneratedJarChecker {
public static void main(String[] args) throws Exception {
Class componentClass = getComponentClass(args);
JCountingButton jCountingButton = new JCountingButton("counter");
Object newPanel = createUISpecPanel(Panel.class, jCountingButton);
checkGetCoutingButtonMethods(newPanel, jCountingButton, componentClass);
System.out.println("OK");
}
private static void checkGetCoutingButtonMethods(Object newPanel, JCountingButton jCountingButton, Class componentClass)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method methodWithStringArg = Panel.class.getMethod("getCountingButton", new Class[]{String.class});
checkGetCountingButton(methodWithStringArg, newPanel, jCountingButton, new Object[]{"counter"}, componentClass);
Method methodWithMatcherArg = Panel.class.getMethod("getCountingButton", new Class[]{ComponentMatcher.class});
checkGetCountingButton(methodWithMatcherArg, newPanel, jCountingButton,
new Object[]{ComponentMatchers.displayedNameIdentity("counter")}, componentClass);
Method methodWithNoArgs = Panel.class.getMethod("getCountingButton", new Class[]{});
checkGetCountingButton(methodWithNoArgs, newPanel, jCountingButton, new Object[]{}, componentClass);
}
private static Class getComponentClass(String[] args) throws ClassNotFoundException {
Assert.assertEquals(1, args.length);
return GeneratedJarChecker.class.getClassLoader().loadClass(args[0]);
}
private static void checkGetCountingButton(Method method, Object newPanel, JCountingButton jCountingButton, Object[] args, Class componentClass) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Assert.assertNotNull(method);
Object found = method.invoke(newPanel, args);
Assert.assertNotNull(found);
Assert.assertTrue(componentClass.isInstance(found));
jCountingButton.reset();
componentClass.getMethod("click", new Class[0]).invoke(found, new Object[0]);
Assert.assertEquals(1, jCountingButton.getCount());
}
private static Object createUISpecPanel(Class newPanelClass, JComponent component) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
JPanel jPanel = new JPanel();
jPanel.add(component);
Constructor constructor = newPanelClass.getConstructor(new Class[]{Container.class});
return constructor.newInstance(new Object[]{jPanel});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -