java类java.beans.EventHandler的实例源码

SecurityVulnerabilityTest.java 文件源码 项目:xstream 阅读 15 收藏 0 点赞 0 评论 0
public void testCannotInjectEventHandler() {
    final String xml = ""
        + "<string class='runnable-array'>\n"
        + "  <dynamic-proxy>\n"
        + "    <interface>java.lang.Runnable</interface>\n"
        + "    <handler class='java.beans.EventHandler'>\n"
        + "      <target class='com.thoughtworks.acceptance.SecurityVulnerabilityTest$Exec'/>\n"
        + "      <action>exec</action>\n"
        + "    </handler>\n"
        + "  </dynamic-proxy>\n"
        + "</string>";

    try {
        xstream.fromXML(xml);
        fail("Thrown " + XStreamException.class.getName() + " expected");
    } catch (final XStreamException e) {
        assertTrue(e.getMessage().contains(EventHandler.class.getName()));
    }
    assertEquals(0, BUFFER.length());
}
SecurityVulnerabilityTest.java 文件源码 项目:xstream 阅读 14 收藏 0 点赞 0 评论 0
public void testExplicitlyConvertEventHandler() {
    final String xml = ""
        + "<string class='runnable-array'>\n"
        + "  <dynamic-proxy>\n"
        + "    <interface>java.lang.Runnable</interface>\n"
        + "    <handler class='java.beans.EventHandler'>\n"
        + "      <target class='com.thoughtworks.acceptance.SecurityVulnerabilityTest$Exec'/>\n"
        + "      <action>exec</action>\n"
        + "    </handler>\n"
        + "  </dynamic-proxy>\n"
        + "</string>";

    xstream.allowTypes(EventHandler.class);

    final Runnable[] array = (Runnable[])xstream.fromXML(xml);
    assertEquals(0, BUFFER.length());
    array[0].run();
    assertEquals("Executed!", BUFFER.toString());
}
Controller.java 文件源码 项目:hipster 阅读 22 收藏 0 点赞 0 评论 0
/**
 *  If object source generates an action-event, then call the given method in
 *  the target object. NB: source must contain an "addActionListener" method
 *  for this to work.
 */
public void bindMethod(Object target, String methodName, Object source) {
    Class clazz = source.getClass();
    try {
        Method method = clazz.getMethod("addActionListener",
                new Class[]{ActionListener.class});
        ActionListener actionListener = (ActionListener) EventHandler.create(
                ActionListener.class,
                target,
                methodName
                );
        method.invoke(source, new Object[]{actionListener});
    } catch(Exception e) {
        e.printStackTrace();
    }
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 21 收藏 0 点赞 0 评论 0
public void testRemoveVetoableChangeListenerStringVetoableChangeListener_property_diff() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");

    VetoableChangeListener proxy2 = EventHandler.create(
            VetoableChangeListener.class, source, "getText");

    String propertyName = "label";
    support.addVetoableChangeListener(propertyName, proxy);
    support.addVetoableChangeListener(propertyName, proxy2);

    assertTrue(support.hasListeners(propertyName));
    assertEquals(2, support.getVetoableChangeListeners(propertyName).length);

    support.removeVetoableChangeListener(propertyName, proxy);
    assertTrue(support.hasListeners(propertyName));
    assertEquals(1, support.getVetoableChangeListeners(propertyName).length);
    assertEquals(1, support.getVetoableChangeListeners().length);
    assertSame(proxy2, support.getVetoableChangeListeners(propertyName)[0]);
}
EventHandlerTest.java 文件源码 项目:cn1 阅读 20 收藏 0 点赞 0 评论 0
/**
 * The test checks the method invoke() with null listener value
 */
public void testNullListenerMethodName() throws Exception {
    InvocationObject invocationObject = new InvocationObject();

    EventHandler handler = new EventHandler(invocationObject, "someText",
            "source.text", null);

    Object proxy = EventHandler.create(ActionListener.class,
            invocationObject, "someText", "source.text");

    Method m = ActionListener.class.getMethod("actionPerformed",
            new Class[] { ActionEvent.class });
    handler.invoke(proxy, m, new Object[] { new ActionEvent(this, 0, "") });

    assertEquals(invocationObject.getSomeText(), getText());
}
EventHandlerTest.java 文件源码 项目:cn1 阅读 20 收藏 0 点赞 0 评论 0
/**
 * The test checks the method invoke()
 */
public void testInvoke() throws Exception {
    InvocationObject invocationObject = new InvocationObject();

    EventHandler handler = new EventHandler(invocationObject, "someText",
            "source.text", "actionPerformed");

    Object proxy = EventHandler.create(ActionListener.class,
            invocationObject, "someText", "source.text");

    Method m = ActionListener.class.getMethod("actionPerformed",
            new Class[] { ActionEvent.class });
    handler.invoke(proxy, m, new Object[] { new ActionEvent(this, 0, "") });

    assertEquals(invocationObject, handler.getTarget());
    assertEquals(invocationObject.getSomeText(), getText());
    try {
        handler.invoke(proxy, null, new Object[] { new ActionEvent(this, 0,
                "") });
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
        // Expected
    }
}
EventHandlerTest.java 文件源码 项目:cn1 阅读 20 收藏 0 点赞 0 评论 0
/**
 * The test checks the method invoke() with null property name
 */
public void testInvokeWithNullPropertyName() throws Exception {
    InvocationObject invocationObject = new InvocationObject();

    EventHandler handler = new EventHandler(invocationObject,
            "doSomething", null, null);

    Object proxy = EventHandler.create(SampleListener.class,
            invocationObject, "doSomething");

    Method m = SampleListener.class.getMethod("fireSampleEvent",
            new Class[] { SampleEvent.class });
    handler.invoke(proxy, m, new Object[] { new SampleEvent("") });

    assertEquals(invocationObject, handler.getTarget());
    assertEquals("doSomething", getMethodName());

    // Regression test for HARMONY-4033
    m = FredListener.class.getMethod("fireFredEvent",
            new Class[] { FredEvent.class });
    proxy = EventHandler.create(FredListener.class, invocationObject,
            "doSomething", null, "fireFredEvent");
    m.invoke(proxy, new Object[] { null });
}
EventHandlerTest.java 文件源码 项目:cn1 阅读 17 收藏 0 点赞 0 评论 0
public void testCreateClassObjectStringString_ActionInvalid() {
    MockTarget target = new MockTarget();
    MockButton button = new MockButton();
    PropertyChangeListener proxy = EventHandler.create(
            PropertyChangeListener.class, target, "action_invalid",
            "source.label");
    assertTrue(Proxy.isProxyClass(proxy.getClass()));

    button.addPropertyChangeListener(proxy);
    String newLabel = "New Value: set text.";
    try {
        button.setLabel(newLabel);
        fail("Should throw RuntimeException.");
    } catch (RuntimeException e) {
    }
}
EventHandlerTest.java 文件源码 项目:cn1 阅读 20 收藏 0 点赞 0 评论 0
public void testCreateClassObjectStringString_PropertyNameInvalid() {
    MockTarget target = new MockTarget();
    MockButton button = new MockButton();
    PropertyChangeListener proxy = EventHandler.create(
            PropertyChangeListener.class, target, "text",
            "source.label_invalid");

    assertTrue(Proxy.isProxyClass(proxy.getClass()));

    button.addPropertyChangeListener(proxy);
    String newLabel = "New Value: set text.";

    try {
        button.setLabel(newLabel);
        fail("Should throw NullPointerException.");
    } catch (RuntimeException e) {
    }
}
EventHandlerTest.java 文件源码 项目:cn1 阅读 19 收藏 0 点赞 0 评论 0
public void testCreateClassObjectStringStringString_ActionInvalid() {
    MockTarget target = new MockTarget();
    MockButton button = new MockButton();
    PropertyChangeListener proxy = EventHandler.create(
            PropertyChangeListener.class, target, "text_invalid",
            "source.label", "propertyChange");
    assertTrue(Proxy.isProxyClass(proxy.getClass()));

    button.addPropertyChangeListener(proxy);
    String newLabel = "New Value: set text.";
    try {
        button.setLabel(newLabel);
        fail("Should throw RuntimeException.");
    } catch (RuntimeException e) {
    }
}
PropertyEditorSupportTest.java 文件源码 项目:cn1 阅读 14 收藏 0 点赞 0 评论 0
public void testRemovePropertyChangeListener_diff() {
    MockTarget target = new MockTarget();
    PropertyEditorSupport support = new PropertyEditorSupport();
    PropertyChangeListener proxy = EventHandler.create(
            PropertyChangeListener.class, target, "eventSource", "source");
    support.addPropertyChangeListener(proxy);
    support.firePropertyChange();
    assertSame(support, target.getEventSource());

    target.setEventSource(null);
    PropertyChangeListener proxy2 = EventHandler.create(
            PropertyChangeListener.class, target, "eventSource", "source");
    support.removePropertyChangeListener(proxy2);
    support.firePropertyChange();
    assertSame(support, target.getEventSource());
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 23 收藏 0 点赞 0 评论 0
public void testRemoveVetoableChangeListenerStringVetoableChangeListener_all() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");

    String propertyName = "label";
    support.addVetoableChangeListener(proxy);
    assertTrue(support.hasListeners(propertyName));

    support.removeVetoableChangeListener(propertyName, proxy);
    assertTrue(support.hasListeners(propertyName));
    assertEquals(0, support.getVetoableChangeListeners(propertyName).length);
    assertEquals(1, support.getVetoableChangeListeners().length);
}
EventHandlerTest.java 文件源码 项目:cn1 阅读 25 收藏 0 点赞 0 评论 0
public void testIncompatibleMethod() {
    MockTarget target = new MockTarget();
    MockButton button = new MockButton();
    PropertyChangeListener proxy = EventHandler.create(
            PropertyChangeListener.class, target, "Text", "source");
    assertTrue(Proxy.isProxyClass(proxy.getClass()));

    button.addPropertyChangeListener(proxy);
    String newLabel = "New Value: set text.";
    try {
        button.setLabel(newLabel);
        fail("Should throw RuntimeException.");
    } catch (RuntimeException e) {

    }
}
EventHandlerTest.java 文件源码 项目:cn1 阅读 23 收藏 0 点赞 0 评论 0
public void testCoverage_1() {
    MockTarget target = new MockTarget();
    MockButton button = new MockButton();
    PropertyChangeListener proxy = EventHandler.create(
            PropertyChangeListener.class, target, "Text", "");
    assertTrue(Proxy.isProxyClass(proxy.getClass()));

    button.addPropertyChangeListener(proxy);
    String newLabel = "New Value: set text.";
    try {
        button.setLabel(newLabel);
        fail("Should throw RuntimeException.");
    } catch (RuntimeException e) {
    }

}
EventHandlerTest.java 文件源码 项目:cn1 阅读 19 收藏 0 点赞 0 评论 0
public void testInvoke_extend1_1() {
    MockFish fish = new MockFish();
    MockFishTarget target = new MockFishTarget();
    PropertyChangeSupport support = new PropertyChangeSupport(fish);
    Object proxy = EventHandler.create(PropertyChangeListener.class,
            target, "action4");
    support.addPropertyChangeListener((PropertyChangeListener) proxy);
    PropertyChangeEvent event = new PropertyChangeEvent(fish, "name", "1",
            "5");
    try {
        support.firePropertyChange(event);
    } catch (Exception e) {

    }
    assertEquals("action4", target.getActionRecord());
}
EventHandlerTest.java 文件源码 项目:cn1 阅读 17 收藏 0 点赞 0 评论 0
public void testInvoke_extend2() {
    MockFish fish = new MockFish();
    MockFishTarget target = new MockFishTarget();
    PropertyChangeSupport support = new PropertyChangeSupport(fish);
    Object proxy = EventHandler.create(PropertyChangeListener.class,
            target, "action2");
    support.addPropertyChangeListener((PropertyChangeListener) proxy);
    PropertyChangeEvent event = new PropertyChangeEvent(fish, "name", "1",
            "5");
    try {
        support.firePropertyChange(event);
        fail("Should throw exception");
    } catch (Exception e) {
        // e.printStackTrace();
    }

}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 22 收藏 0 点赞 0 评论 0
public void testRemoveVetoableChangeListenerVetoableChangeListener_all() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");

    String propertyName = "label";
    support.addVetoableChangeListener(proxy);
    assertTrue(support.hasListeners(propertyName));

    support.removeVetoableChangeListener(proxy);
    assertFalse(support.hasListeners(propertyName));
    assertEquals(0, support.getVetoableChangeListeners(propertyName).length);
    assertEquals(0, support.getVetoableChangeListeners().length);
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 17 收藏 0 点赞 0 评论 0
public void testAddVetoableChangeListenerStringVetoableChangeListener_property_invalid() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");
    String propertyName = "label_invalid";
    support.addVetoableChangeListener(propertyName, proxy);

    VetoableChangeListener[] listeners1 = support
            .getVetoableChangeListeners();
    VetoableChangeListener[] listeners2 = support
            .getVetoableChangeListeners("label_invalid");
    assertEquals(1, listeners1.length);
    assertEquals(1, listeners2.length);
    assertEquals("label_invalid",
            ((VetoableChangeListenerProxy) listeners1[0]).getPropertyName());
    assertSame(proxy, listeners2[0]);
    assertFalse(support.hasListeners("label"));
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 24 收藏 0 点赞 0 评论 0
public void testAddVetoableChangeListenerVetoableChangeListener() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");
    support.addVetoableChangeListener(proxy);

    assertTrue(support.hasListeners("label"));
    assertTrue(support.hasListeners("text"));

    VetoableChangeListener[] listeners1 = support
            .getVetoableChangeListeners();
    VetoableChangeListener[] listeners2 = support
            .getVetoableChangeListeners("label");
    VetoableChangeListener[] listeners3 = support
            .getVetoableChangeListeners("text");

    assertEquals(1, listeners1.length);
    assertEquals(0, listeners2.length);
    assertEquals(0, listeners3.length);

    assertSame(proxy, listeners1[0]);
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 25 收藏 0 点赞 0 评论 0
public void testAddVetoableChangeListenerVetoableChangeListener_duplicate() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");
    support.addVetoableChangeListener(proxy);
    support.addVetoableChangeListener(proxy);

    assertTrue(support.hasListeners("label"));
    assertTrue(support.hasListeners("text"));

    VetoableChangeListener[] listeners1 = support
            .getVetoableChangeListeners();
    VetoableChangeListener[] listeners2 = support
            .getVetoableChangeListeners("label");
    VetoableChangeListener[] listeners3 = support
            .getVetoableChangeListeners("text");

    assertEquals(2, listeners1.length);
    assertEquals(0, listeners2.length);
    assertEquals(0, listeners3.length);
    for (VetoableChangeListener element : listeners1) {
        assertSame(proxy, element);
    }
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 20 收藏 0 点赞 0 评论 0
public void testRemoveVetoableChangeListenerStringVetoableChangeListener_property_more() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");

    String propertyName = "label";
    support.addVetoableChangeListener(propertyName, proxy);
    support.addVetoableChangeListener(propertyName, proxy);

    assertTrue(support.hasListeners(propertyName));
    assertEquals(2, support.getVetoableChangeListeners(propertyName).length);

    support.removeVetoableChangeListener(propertyName, proxy);
    assertTrue(support.hasListeners(propertyName));
    assertEquals(1, support.getVetoableChangeListeners(propertyName).length);
    assertEquals(1, support.getVetoableChangeListeners().length);
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 22 收藏 0 点赞 0 评论 0
public void testRemoveVetoableChangeListenerVetoableChangeListener_all_more() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");

    String propertyName = "label";
    support.addVetoableChangeListener(proxy);
    support.addVetoableChangeListener(proxy);

    assertTrue(support.hasListeners(propertyName));
    assertEquals(2, support.getVetoableChangeListeners().length);

    support.removeVetoableChangeListener(proxy);

    assertTrue(support.hasListeners(propertyName));
    assertEquals(0, support.getVetoableChangeListeners(propertyName).length);
    assertEquals(1, support.getVetoableChangeListeners().length);
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 28 收藏 0 点赞 0 评论 0
public void testFireVetoableChangePropertyChangeEvent_property_invalid()
        throws PropertyVetoException {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");

    String propertyName = "label_invalid";
    support.addVetoableChangeListener(propertyName, proxy);
    PropertyChangeEvent event = new PropertyChangeEvent(source, "label",
            "Label: old", "Label: new");
    support.fireVetoableChange(event);

    assertEquals("text.default", source.getText());
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 20 收藏 0 点赞 0 评论 0
public void testFireVetoableChangePropertyChangeEvent_DuplicateListener()
        throws PropertyVetoException {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "increaseTop");
    VetoableChangeListener proxy2 = EventHandler.create(
            VetoableChangeListener.class, source, "setText");

    support.addVetoableChangeListener(proxy);
    support.addVetoableChangeListener(proxy);
    support.addVetoableChangeListener(proxy2);

    PropertyChangeEvent event = new PropertyChangeEvent(source, "label",
            "Label: old", "Label: new");
    support.fireVetoableChange(event);
    assertEquals(2, source.getTop());
    assertEquals("called", source.getText());
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 27 收藏 0 点赞 0 评论 0
public void testRemoveVetoableChangeListenerVetoableChangeListener_null_null() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    EventHandler.create(VetoableChangeListener.class, source, "setText");

    String propertyName = "label";
    support.addVetoableChangeListener(null);

    assertFalse(support.hasListeners(propertyName));
    assertEquals(0, support.getVetoableChangeListeners().length);

    support.removeVetoableChangeListener(null);

    assertFalse(support.hasListeners(propertyName));
    assertEquals(0, support.getVetoableChangeListeners(propertyName).length);
    assertEquals(0, support.getVetoableChangeListeners().length);
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 19 收藏 0 点赞 0 评论 0
public void testFireVetoableChangeStringbooleanboolean_twoListeners()
        throws PropertyVetoException {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");
    VetoableChangeListener proxy2 = EventHandler.create(
            VetoableChangeListener.class, source, "increaseTop");
    support.addVetoableChangeListener(proxy);
    support.addVetoableChangeListener(proxy2);

    support.fireVetoableChange("label", true, false);

    assertEquals("called", source.getText());
    assertEquals(1, source.getTop());
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 19 收藏 0 点赞 0 评论 0
public void testFireVetoableChangeStringbooleanboolean_property_twoListeners()
        throws PropertyVetoException {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");
    VetoableChangeListener proxy2 = EventHandler.create(
            VetoableChangeListener.class, source, "increaseTop");
    support.addVetoableChangeListener("label", proxy);
    support.addVetoableChangeListener("label", proxy2);

    support.fireVetoableChange("label", true, false);

    assertEquals("called", source.getText());
    assertEquals(1, source.getTop());
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 20 收藏 0 点赞 0 评论 0
public void testRemoveVetoableChangeListenerVetoableChangeListener_invalid() {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "setText");

    String propertyName = "label";
    assertFalse(support.hasListeners(propertyName));
    assertEquals(0, support.getVetoableChangeListeners().length);

    support.removeVetoableChangeListener(proxy);

    assertFalse(support.hasListeners(propertyName));
    assertEquals(0, support.getVetoableChangeListeners(propertyName).length);
    assertEquals(0, support.getVetoableChangeListeners().length);
}
VetoableChangeSupportTest.java 文件源码 项目:cn1 阅读 20 收藏 0 点赞 0 评论 0
public void testFireVetoableChangeStringbooleanboolean_listener_Property_invalid()
        throws PropertyVetoException {
    MockSource source = new MockSource();
    VetoableChangeSupport support = new VetoableChangeSupport(source);

    VetoableChangeListener proxy = EventHandler.create(
            VetoableChangeListener.class, source, "label", "source.text");
    support.addVetoableChangeListener("label", proxy);
    String newText = "new Text value";
    source.setText(newText);

    support.fireVetoableChange("text", true, false);
    assertEquals("label.default", source.getLabel());

    support.fireVetoableChange("label_invalid", true, false);
    assertEquals("label.default", source.getLabel());

    support.fireVetoableChange("label", true, false);
    assertEquals(newText, source.getLabel());
}
Test6179222.java 文件源码 项目:jdk8u-jdk 阅读 14 收藏 0 点赞 0 评论 0
public static void main(String[] args) {
    Test6179222 test = new Test6179222();
    // test 6179222
    test(EventHandler.create(ActionListener.class, test, "foo", "source.icon"));
    // test 6265540
    test(EventHandler.create(ActionListener.class, test, "bar.doit"));
    if (!test.bar.invoked) {
        throw new Error("Bar was not set");
    }
}


问题


面经


文章

微信
公众号

扫码关注公众号