01: package org.uispec4j.extension;
02:
03: import junit.framework.Assert;
04: import org.uispec4j.Panel;
05: import org.uispec4j.finder.ComponentMatcher;
06: import org.uispec4j.finder.ComponentMatchers;
07:
08: import javax.swing.*;
09: import java.awt.*;
10: import java.lang.reflect.Constructor;
11: import java.lang.reflect.InvocationTargetException;
12: import java.lang.reflect.Method;
13:
14: public class GeneratedJarChecker {
15: public static void main(String[] args) throws Exception {
16: Class componentClass = getComponentClass(args);
17:
18: JCountingButton jCountingButton = new JCountingButton("counter");
19: Object newPanel = createUISpecPanel(Panel.class,
20: jCountingButton);
21:
22: checkGetCoutingButtonMethods(newPanel, jCountingButton,
23: componentClass);
24:
25: System.out.println("OK");
26: }
27:
28: private static void checkGetCoutingButtonMethods(Object newPanel,
29: JCountingButton jCountingButton, Class componentClass)
30: throws NoSuchMethodException, IllegalAccessException,
31: InvocationTargetException {
32: Method methodWithStringArg = Panel.class.getMethod(
33: "getCountingButton", new Class[] { String.class });
34: checkGetCountingButton(methodWithStringArg, newPanel,
35: jCountingButton, new Object[] { "counter" },
36: componentClass);
37:
38: Method methodWithMatcherArg = Panel.class.getMethod(
39: "getCountingButton",
40: new Class[] { ComponentMatcher.class });
41: checkGetCountingButton(methodWithMatcherArg, newPanel,
42: jCountingButton, new Object[] { ComponentMatchers
43: .displayedNameIdentity("counter") },
44: componentClass);
45:
46: Method methodWithNoArgs = Panel.class.getMethod(
47: "getCountingButton", new Class[] {});
48: checkGetCountingButton(methodWithNoArgs, newPanel,
49: jCountingButton, new Object[] {}, componentClass);
50: }
51:
52: private static Class getComponentClass(String[] args)
53: throws ClassNotFoundException {
54: Assert.assertEquals(1, args.length);
55: return GeneratedJarChecker.class.getClassLoader().loadClass(
56: args[0]);
57: }
58:
59: private static void checkGetCountingButton(Method method,
60: Object newPanel, JCountingButton jCountingButton,
61: Object[] args, Class componentClass)
62: throws IllegalAccessException, InvocationTargetException,
63: NoSuchMethodException {
64: Assert.assertNotNull(method);
65: Object found = method.invoke(newPanel, args);
66: Assert.assertNotNull(found);
67: Assert.assertTrue(componentClass.isInstance(found));
68: jCountingButton.reset();
69: componentClass.getMethod("click", new Class[0]).invoke(found,
70: new Object[0]);
71: Assert.assertEquals(1, jCountingButton.getCount());
72: }
73:
74: private static Object createUISpecPanel(Class newPanelClass,
75: JComponent component) throws NoSuchMethodException,
76: InstantiationException, IllegalAccessException,
77: InvocationTargetException {
78: JPanel jPanel = new JPanel();
79: jPanel.add(component);
80: Constructor constructor = newPanelClass
81: .getConstructor(new Class[] { Container.class });
82: return constructor.newInstance(new Object[] { jPanel });
83: }
84:
85: }
|