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()); }
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()); }
/** * 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(); } }
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]); }
/** * 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()); }
/** * 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 } }
/** * 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 }); }
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) { } }
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) { } }
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) { } }
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()); }
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); }
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) { } }
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) { } }
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()); }
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(); } }
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); }
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")); }
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]); }
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); } }
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); }
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); }
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()); }
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()); }
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); }
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()); }
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()); }
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); }
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()); }
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"); } }
private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); if (listeners != null) { Object[] list = listeners.getListenerList(); for (int i = 1; i < list.length; i += 2) { if (Proxy.isProxyClass(list[i].getClass())) { InvocationHandler h = Proxy.getInvocationHandler(list[i]); if (h instanceof EventHandler && ((EventHandler) h).getTarget() instanceof Serializable) { EventHandler eh = (EventHandler) h; s.writeObject("callback"); s.writeObject(eh.getTarget()); s.writeObject(eh.getAction()); } } else if (list[i] instanceof BooleanInvocationHandler) { BooleanInvocationHandler bih = (BooleanInvocationHandler) list[i]; Object target = bih.trueStatement.getTarget(); if (target instanceof Serializable) { s.writeObject(BooleanInvocationHandler.class.getName()); s.writeObject(target); s.writeObject(bih.trueStatement.getMethodName()); } } else if (list[i] instanceof Serializable) { s.writeObject(((Class<?>) list[i - 1]).getName()); s.writeObject(list[i]); } } } s.writeObject(null); }
private void bindImpl(Object target, String targetProperty, AbstractModel source, String itemProperty) { PropertyChangeListener pcl = (PropertyChangeListener) EventHandler.create( PropertyChangeListener.class, target, targetProperty, "source." + itemProperty, "propertyChange"); source.addPropertyChangeListener(itemProperty, pcl); }
private void bindImpl(Object target, String targetProperty, Component source, String itemProperty) { PropertyChangeListener pcl = (PropertyChangeListener) EventHandler.create( PropertyChangeListener.class, target, targetProperty, "source." + itemProperty, "propertyChange"); source.addPropertyChangeListener(pcl); }
private void bindImpl(Object target, String targetProperty, ItemSelectable is, String itemProperty) { is.addItemListener( (ItemListener) EventHandler.create( ItemListener.class, target, targetProperty, "source." + itemProperty ) ); }
private void bindImpl(Object target, String targetProperty, ComboBoxModel model, String itemProperty) { model.addListDataListener( (ListDataListener) EventHandler.create( ListDataListener.class, target, targetProperty, "source." + itemProperty ) ); }
private void bindImpl(Object target, String targetProperty, JTextField view, String itemProperty) { view.addFocusListener( (FocusListener) EventHandler.create( FocusListener.class, target, targetProperty, "source." + itemProperty ) ); }