undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
BIN
test/jdk/java/beans/PropertyChangeSupport/1.5.0_10.ser
Normal file
BIN
test/jdk/java/beans/PropertyChangeSupport/1.5.0_10.ser
Normal file
Binary file not shown.
BIN
test/jdk/java/beans/PropertyChangeSupport/1.6.0.ser
Normal file
BIN
test/jdk/java/beans/PropertyChangeSupport/1.6.0.ser
Normal file
Binary file not shown.
88
test/jdk/java/beans/PropertyChangeSupport/Test4092905.java
Normal file
88
test/jdk/java/beans/PropertyChangeSupport/Test4092905.java
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4092905
|
||||
* @summary Tests that hasListeners() is not failed across serialization
|
||||
* @author Graham Hamilton
|
||||
*/
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Test4092905 {
|
||||
private static final String PUBLIC = "public";
|
||||
private static final String PRIVATE = "private";
|
||||
|
||||
public static void main(String[] args) {
|
||||
PropertyChangeSupport pcs = new PropertyChangeSupport(args);
|
||||
pcs.addPropertyChangeListener(PUBLIC, new PublicListener());
|
||||
pcs.addPropertyChangeListener(PRIVATE, new PrivateListener());
|
||||
|
||||
if (!pcs.hasListeners(PUBLIC)) {
|
||||
throw new Error("no public listener");
|
||||
}
|
||||
if (!pcs.hasListeners(PRIVATE)) {
|
||||
throw new Error("no private listener");
|
||||
}
|
||||
|
||||
try {
|
||||
// serialize into byte array
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream output = new ObjectOutputStream(baos);
|
||||
output.writeObject(pcs);
|
||||
output.flush();
|
||||
// deserialize from byte array
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
ObjectInputStream input = new ObjectInputStream(bais);
|
||||
pcs = (PropertyChangeSupport) input.readObject();
|
||||
} catch (Exception exception) {
|
||||
throw new Error("unexpected exception", exception);
|
||||
}
|
||||
|
||||
if (!pcs.hasListeners(PUBLIC)) {
|
||||
throw new Error("no public listener");
|
||||
}
|
||||
if (pcs.hasListeners(PRIVATE)) {
|
||||
throw new Error("unexpected private listener");
|
||||
}
|
||||
}
|
||||
|
||||
public static class PublicListener implements PropertyChangeListener, Serializable {
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
}
|
||||
}
|
||||
|
||||
private static class PrivateListener implements PropertyChangeListener {
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
}
|
||||
}
|
||||
}
|
||||
162
test/jdk/java/beans/PropertyChangeSupport/Test4353056.java
Normal file
162
test/jdk/java/beans/PropertyChangeSupport/Test4353056.java
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4353056
|
||||
* @summary Tests basic IndexPropertyChangeEvent functionality
|
||||
* @author Mark Davidson
|
||||
*/
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import java.beans.IndexedPropertyChangeEvent;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
|
||||
/**
|
||||
* Tests the basic functionality of IndexedPropertyChangeEvent and
|
||||
* the fireIndexed... methods on PropertyChangeSupport.
|
||||
*/
|
||||
public class Test4353056 implements PropertyChangeListener {
|
||||
private static final int COUNT = 100;
|
||||
private static final String COLOR = "color";
|
||||
private static final String BOOLEAN = "boolean";
|
||||
private static final String INTEGER = "integer";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Test4353056 test = new Test4353056();
|
||||
test.addPropertyChangeListener(test);
|
||||
for (int i = 0; i < COUNT; i++) {
|
||||
boolean even = i % 2 == 0;
|
||||
test.setColor(i, i % 3 == 0 ? Color.RED : Color.BLUE);
|
||||
test.setBoolean(i, even);
|
||||
test.setInteger(i, i);
|
||||
}
|
||||
}
|
||||
|
||||
private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
|
||||
|
||||
private Color color;
|
||||
private boolean flag;
|
||||
private int value;
|
||||
|
||||
private String name;
|
||||
private int index = -1;
|
||||
|
||||
public void addPropertyChangeListener(PropertyChangeListener listener) {
|
||||
this.pcs.addPropertyChangeListener(listener);
|
||||
}
|
||||
|
||||
public void removePropertyChangeListener(PropertyChangeListener listener) {
|
||||
this.pcs.removePropertyChangeListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for Object indexed property.
|
||||
*
|
||||
* @param index the property index
|
||||
* @param color new value
|
||||
*/
|
||||
public void setColor(int index, Color color) {
|
||||
Color oldColor = this.color;
|
||||
this.color = color;
|
||||
|
||||
this.index = index;
|
||||
this.name = COLOR;
|
||||
|
||||
this.pcs.fireIndexedPropertyChange(COLOR, index,
|
||||
oldColor, color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for boolean indexed property.
|
||||
*
|
||||
* @param index the property index
|
||||
* @param flag new value
|
||||
*/
|
||||
public void setBoolean(int index, boolean flag) {
|
||||
boolean oldBool = this.flag;
|
||||
this.flag = flag;
|
||||
|
||||
this.index = index;
|
||||
this.name = BOOLEAN;
|
||||
|
||||
this.pcs.fireIndexedPropertyChange(BOOLEAN, index,
|
||||
oldBool, flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for integer indexed property.
|
||||
*
|
||||
* @param index the property index
|
||||
* @param value new value
|
||||
*/
|
||||
public void setInteger(int index, int value) {
|
||||
int oldInt = this.value;
|
||||
this.value = value;
|
||||
|
||||
this.index = index;
|
||||
this.name = INTEGER;
|
||||
|
||||
this.pcs.fireIndexedPropertyChange(INTEGER, index,
|
||||
oldInt, value);
|
||||
}
|
||||
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
Object value = event.getNewValue();
|
||||
if (value.equals(event.getOldValue())) {
|
||||
throw new Error("new value is equal to old one");
|
||||
}
|
||||
if (!this.name.equals(event.getPropertyName())) {
|
||||
throw new Error("unexpected property name");
|
||||
} else if (this.name.equals(COLOR)) {
|
||||
if (!value.equals(this.color)) {
|
||||
throw new Error("unexpected object value");
|
||||
}
|
||||
} else if (this.name.equals(BOOLEAN)) {
|
||||
if (!value.equals(Boolean.valueOf(this.flag))) {
|
||||
throw new Error("unexpected boolean value");
|
||||
}
|
||||
} else if (this.name.equals(INTEGER)) {
|
||||
if (!value.equals(Integer.valueOf(this.value))) {
|
||||
throw new Error("unexpected integer value");
|
||||
}
|
||||
} else {
|
||||
throw new Error("unexpected property name");
|
||||
}
|
||||
if (event instanceof IndexedPropertyChangeEvent) {
|
||||
IndexedPropertyChangeEvent ipce = (IndexedPropertyChangeEvent) event;
|
||||
if (this.index != ipce.getIndex()) {
|
||||
throw new Error("unexpected property index");
|
||||
}
|
||||
} else {
|
||||
throw new Error("unexpected event type");
|
||||
}
|
||||
System.out.println(this.name + " at " + this.index + " is " + value);
|
||||
|
||||
this.name = null;
|
||||
this.index = -1;
|
||||
}
|
||||
}
|
||||
297
test/jdk/java/beans/PropertyChangeSupport/Test4682386.java
Normal file
297
test/jdk/java/beans/PropertyChangeSupport/Test4682386.java
Normal file
|
|
@ -0,0 +1,297 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4682386
|
||||
* @summary Tests for PropertyChangeSupport refactoring
|
||||
* @author Mark Davidson
|
||||
*/
|
||||
|
||||
import java.beans.Beans;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.beans.PropertyDescriptor;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JProgressBar;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextPane;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JToolBar;
|
||||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.JTree;
|
||||
import javax.swing.JTable;
|
||||
|
||||
/**
|
||||
* This class tests the multi-threaded access to PropertyChangeSupport and
|
||||
* will also use reflection to test propertyChanges on Swing components.
|
||||
* <p/>
|
||||
* There is no new functionality from the implementation of this RFE.
|
||||
* Semantically, it should be equivalent.
|
||||
*/
|
||||
public class Test4682386 {
|
||||
private static final String FOO = "foo";
|
||||
private static final String BAR = "bar";
|
||||
|
||||
private static final int NUM_LISTENERS = 100;
|
||||
private static final boolean DEBUG = true;
|
||||
|
||||
private static final Class[] TYPES = {
|
||||
JButton.class,
|
||||
JCheckBox.class,
|
||||
JComboBox.class,
|
||||
JLabel.class,
|
||||
JList.class,
|
||||
JMenuItem.class,
|
||||
JProgressBar.class,
|
||||
JTextArea.class,
|
||||
JTextPane.class,
|
||||
JTextField.class,
|
||||
JToolBar.class,
|
||||
JTabbedPane.class,
|
||||
JTree.class,
|
||||
JTable.class,
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
testSwingProperties();
|
||||
|
||||
// tests the multi-threaded access
|
||||
|
||||
TestBean bean = new TestBean();
|
||||
|
||||
Thread add = new Thread(new AddThread(bean));
|
||||
Thread remove = new Thread(new RemoveThread(bean));
|
||||
Thread prop = new Thread(new PropertyThread(bean));
|
||||
|
||||
add.start();
|
||||
prop.start();
|
||||
remove.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be exectuted with $JAVA_HOME/lib/dt.jar in the classpath
|
||||
* so that there will be a lot more bound properties.
|
||||
* <p/>
|
||||
* This test isn't really appropriate for automated testing.
|
||||
*/
|
||||
private static void testSwingProperties() {
|
||||
long start = System.currentTimeMillis();
|
||||
for (Class type : TYPES) {
|
||||
try {
|
||||
Object bean = Beans.instantiate(type.getClassLoader(), type.getName());
|
||||
|
||||
JComponent comp = (JComponent) bean;
|
||||
for (int k = 0; k < NUM_LISTENERS; k++) {
|
||||
comp.addPropertyChangeListener(new PropertyListener());
|
||||
}
|
||||
|
||||
for (PropertyDescriptor pd : getPropertyDescriptors(type)) {
|
||||
if (pd.isBound()) {
|
||||
if (DEBUG) {
|
||||
System.out.println("Bound property found: " + pd.getName());
|
||||
}
|
||||
|
||||
Method read = pd.getReadMethod();
|
||||
Method write = pd.getWriteMethod();
|
||||
try {
|
||||
write.invoke(
|
||||
bean,
|
||||
getValue(
|
||||
pd.getPropertyType(),
|
||||
read.invoke(bean)));
|
||||
} catch (Exception ex) {
|
||||
// do nothing - just move on.
|
||||
if (DEBUG) {
|
||||
System.out.println("Reflective method invocation Exception for " + type + " : " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
// do nothing - just move on.
|
||||
if (DEBUG) {
|
||||
System.out.println("Exception for " + type.getName() +
|
||||
" : " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Exec time (ms): " + (System.currentTimeMillis() - start));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a fake value from a type and old value;
|
||||
*/
|
||||
public static Object getValue(Class type, Object value) {
|
||||
if (String.class.equals(type)) {
|
||||
return "test string";
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
Integer i = (Integer) value;
|
||||
return Integer.valueOf(i + 1);
|
||||
}
|
||||
if (value instanceof Boolean) {
|
||||
Boolean b = (Boolean) value;
|
||||
return Boolean.valueOf(!b);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static PropertyDescriptor[] getPropertyDescriptors(Class type) {
|
||||
try {
|
||||
return Introspector.getBeanInfo(type).getPropertyDescriptors();
|
||||
} catch (IntrospectionException exception) {
|
||||
throw new Error("unexpected exception", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static void sleep(long ms) {
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (InterruptedException exception) {
|
||||
}
|
||||
}
|
||||
|
||||
private static class AddThread implements Runnable {
|
||||
private final TestBean bean;
|
||||
|
||||
AddThread(TestBean bean) {
|
||||
this.bean = bean;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for (int i = 0; i < NUM_LISTENERS; i++) {
|
||||
for (int j = 0; j < 10; j++) {
|
||||
this.bean.addPropertyChangeListener(new PropertyListener());
|
||||
}
|
||||
if (DEBUG) {
|
||||
System.out.println("10 listeners added");
|
||||
}
|
||||
sleep(25L);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class RemoveThread implements Runnable {
|
||||
private final TestBean bean;
|
||||
|
||||
RemoveThread(TestBean bean) {
|
||||
this.bean = bean;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for (int k = 0; k < NUM_LISTENERS; k++) {
|
||||
sleep(100L);
|
||||
PropertyChangeListener[] listeners = this.bean.getPropertyChangeListners();
|
||||
for (int i = listeners.length - 1; i >= 0; i--) {
|
||||
this.bean.removePropertyChangeListener(listeners[i]);
|
||||
}
|
||||
if (DEBUG) {
|
||||
System.out.println(listeners.length + " listeners removed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class PropertyThread implements Runnable {
|
||||
private final TestBean bean;
|
||||
|
||||
PropertyThread(TestBean bean) {
|
||||
this.bean = bean;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for (int i = 0; i < NUM_LISTENERS; i++) {
|
||||
boolean flag = this.bean.isFoo();
|
||||
this.bean.setFoo(!flag);
|
||||
this.bean.setBar(Boolean.toString(flag));
|
||||
if (DEBUG) {
|
||||
System.out.println("Executed property changes");
|
||||
}
|
||||
sleep(40L);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for the property change events.
|
||||
*/
|
||||
private static class PropertyListener implements PropertyChangeListener {
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
// blank since this should execute as fast as possible.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple bean to test multi-threaded acccess to the listeners.
|
||||
*/
|
||||
public static class TestBean {
|
||||
private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
|
||||
private boolean foo;
|
||||
private String bar;
|
||||
|
||||
public void addPropertyChangeListener(PropertyChangeListener listener) {
|
||||
this.pcs.addPropertyChangeListener(listener);
|
||||
}
|
||||
|
||||
public void removePropertyChangeListener(PropertyChangeListener listener) {
|
||||
this.pcs.removePropertyChangeListener(listener);
|
||||
}
|
||||
|
||||
public PropertyChangeListener[] getPropertyChangeListners() {
|
||||
return this.pcs.getPropertyChangeListeners();
|
||||
}
|
||||
|
||||
public boolean isFoo() {
|
||||
return this.foo;
|
||||
}
|
||||
|
||||
public void setFoo(boolean foo) {
|
||||
boolean old = this.foo;
|
||||
this.foo = foo;
|
||||
this.pcs.firePropertyChange(FOO, old, foo);
|
||||
}
|
||||
|
||||
public String getBar() {
|
||||
return this.bar;
|
||||
}
|
||||
|
||||
public void setBar(String bar) {
|
||||
String old = this.bar;
|
||||
this.bar = bar;
|
||||
this.pcs.firePropertyChange(BAR, old, bar);
|
||||
}
|
||||
}
|
||||
}
|
||||
208
test/jdk/java/beans/PropertyChangeSupport/Test4985020.java
Normal file
208
test/jdk/java/beans/PropertyChangeSupport/Test4985020.java
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4985020
|
||||
* @summary Tests passing of null as property name
|
||||
* @author Brent Christian
|
||||
*/
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.beans.VetoableChangeListener;
|
||||
import java.beans.VetoableChangeSupport;
|
||||
|
||||
public class Test4985020 implements PropertyChangeListener, VetoableChangeListener {
|
||||
private static final String NAME = new String("Property Name");
|
||||
private static boolean failed;
|
||||
|
||||
public static void main(String[] args) {
|
||||
PropertyChangeSupport pcs = new PropertyChangeSupport(args);
|
||||
VetoableChangeSupport vcs = new VetoableChangeSupport(args);
|
||||
Test4985020 listener = new Test4985020();
|
||||
|
||||
|
||||
//PropertyChangeSupport.addPropertyChangeListener(null, listener)
|
||||
System.out.println("PropertyChangeSupport.addPropertyChangeListener(null, listener)");
|
||||
try {
|
||||
pcs.addPropertyChangeListener(null, listener);
|
||||
pcs.firePropertyChange(NAME, null, null);
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
//PropertyChangeSupport.removePropertyChangeListener(null, listener)
|
||||
System.out.println("PropertyChangeSupport.removePropertyChangeListener(null, listener)");
|
||||
try {
|
||||
pcs.removePropertyChangeListener(null, listener);
|
||||
pcs.firePropertyChange(NAME, null, null);
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
//PropertyChangeSupport.getPropertyChangeListeners(null)
|
||||
System.out.println("PropertyChangeSupport.getPropertyChangeListeners(null)");
|
||||
try {
|
||||
PropertyChangeListener[] pcls = pcs.getPropertyChangeListeners(null);
|
||||
if (pcls == null) {
|
||||
throw new Error("getPropertyChangeListener() returned null");
|
||||
}
|
||||
if (pcls.length != 0) {
|
||||
throw new Error("getPropertyChangeListener() did not return an empty array");
|
||||
}
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
//PropertyChangeSupport.hasListeners(null)
|
||||
System.out.println("PropertyChangeSupport.hasListeners(null)");
|
||||
try {
|
||||
pcs.hasListeners(null);
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
//PropertyChangeSupport.hasListeners(null): with a generic listener
|
||||
System.out.println("PropertyChangeSupport.hasListeners(null) with a generic listener");
|
||||
try {
|
||||
pcs.addPropertyChangeListener(listener);
|
||||
if (!pcs.hasListeners(null)) {
|
||||
throw new Error("hasListeners(null) returned false, but there was a generic listener");
|
||||
}
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
pcs = new PropertyChangeSupport(args); // reset
|
||||
|
||||
//PropertyChangeSupport.hasListeners(null): with a specific listener
|
||||
System.out.println("PropertyChangeSupport.hasListeners(null) with a specific listener");
|
||||
try {
|
||||
pcs.addPropertyChangeListener(NAME, listener);
|
||||
if (pcs.hasListeners(null)) {
|
||||
throw new Error("hasListeners(null) returned true, but there were no generic listeners - only a specific listener");
|
||||
}
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
|
||||
//VetoableChangeSupport.addVetoableChangeListener(null, listener)
|
||||
System.out.println("VetoableChangeSupport.addVetoableChangeListener(null, listener)");
|
||||
try {
|
||||
vcs.addVetoableChangeListener(null, listener);
|
||||
vcs.fireVetoableChange(NAME, null, null);
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
//VetoableChangeSupport.removeVetoableChangeListener(null, listener)
|
||||
System.out.println("VetoableChangeSupport.removeVetoableChangeListener(null, listener)");
|
||||
try {
|
||||
vcs.removeVetoableChangeListener(null, listener);
|
||||
vcs.fireVetoableChange(NAME, null, null);
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
//VetoableChangeSupport.getVetoableChangeListeners(null)
|
||||
System.out.println("VetoableChangeSupport.getVetoableChangeListeners(null)");
|
||||
try {
|
||||
VetoableChangeListener[] pcls = vcs.getVetoableChangeListeners(null);
|
||||
if (pcls == null) {
|
||||
throw new Error("getVetoableChangeListener() returned null");
|
||||
}
|
||||
if (pcls.length != 0) {
|
||||
throw new Error("getVetoableChangeListener() did not return an empty array");
|
||||
}
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
//VetoableChangeSupport.hasListeners(null)
|
||||
System.out.println("VetoableChangeSupport.hasListeners(null)");
|
||||
try {
|
||||
boolean result = vcs.hasListeners(null);
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
//VetoableChangeSupport.hasListeners(null): with a generic listener
|
||||
System.out.println("VetoableChangeSupport.hasListeners(null) with a generic listener");
|
||||
try {
|
||||
vcs.addVetoableChangeListener(listener);
|
||||
if (!vcs.hasListeners(null)) {
|
||||
throw new Error("hasListeners(null) returned false, but there was a generic listener");
|
||||
}
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
vcs = new VetoableChangeSupport(args); // reset
|
||||
|
||||
//VetoableChangeSupport.hasListeners(null): with a specific listener
|
||||
System.out.println("VetoableChangeSupport.hasListeners(null) with a specific listener");
|
||||
try {
|
||||
vcs.addVetoableChangeListener(NAME, listener);
|
||||
if (vcs.hasListeners(null)) {
|
||||
throw new Error("hasListeners(null) returned true, but there were no generic listeners - only a specific listener");
|
||||
}
|
||||
}
|
||||
catch (Throwable error) {
|
||||
print(error);
|
||||
}
|
||||
|
||||
|
||||
if (failed) {
|
||||
throw new Error("TEST FAILED");
|
||||
}
|
||||
}
|
||||
|
||||
private static void print(Throwable error) {
|
||||
error.printStackTrace();
|
||||
failed = true;
|
||||
}
|
||||
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
System.out.println("* propertyChange(event) event is " + event.getPropertyName());
|
||||
throw new Error("shouldn't be any listeners");
|
||||
}
|
||||
|
||||
public void vetoableChange(PropertyChangeEvent event) {
|
||||
System.out.println("* vetoableChange(event) event is " + event.getPropertyName());
|
||||
throw new Error("shouldn't be any listeners");
|
||||
}
|
||||
}
|
||||
45
test/jdk/java/beans/PropertyChangeSupport/Test7087429.java
Normal file
45
test/jdk/java/beans/PropertyChangeSupport/Test7087429.java
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7087429
|
||||
* @summary Tests IllegalArgumentException from the PropertyChangeEvent constructor
|
||||
* @author Sergey Malenkov
|
||||
*/
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
|
||||
public final class Test7087429 {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
new PropertyChangeEvent(null, null, null, null);
|
||||
}
|
||||
catch (IllegalArgumentException exception) {
|
||||
if (exception.getMessage().equals("null source")) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new Error("IllegalArgumentException expected");
|
||||
}
|
||||
}
|
||||
58
test/jdk/java/beans/PropertyChangeSupport/Test7148143.java
Normal file
58
test/jdk/java/beans/PropertyChangeSupport/Test7148143.java
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7148143
|
||||
* @summary Tests ClassCastException for the PropertyChangeSupport class
|
||||
* @author Sergey Malenkov
|
||||
*/
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.util.EventListener;
|
||||
import java.util.EventListenerProxy;
|
||||
|
||||
public class Test7148143 {
|
||||
|
||||
private static class CustomProxy
|
||||
extends EventListenerProxy<EventListener>
|
||||
implements PropertyChangeListener {
|
||||
|
||||
public CustomProxy() {
|
||||
super(new EventListener() {
|
||||
});
|
||||
}
|
||||
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
PropertyChangeListener listener = new CustomProxy();
|
||||
PropertyChangeSupport support = new PropertyChangeSupport(listener);
|
||||
support.addPropertyChangeListener(listener);
|
||||
support.addPropertyChangeListener("foo", listener); // cast class exception
|
||||
}
|
||||
}
|
||||
84
test/jdk/java/beans/PropertyChangeSupport/TestEquals.java
Normal file
84
test/jdk/java/beans/PropertyChangeSupport/TestEquals.java
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 5004188
|
||||
* @summary Tests how often method equals() is called
|
||||
* @author Sergey Malenkov
|
||||
*/
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
|
||||
public final class TestEquals implements PropertyChangeListener {
|
||||
private static final String PROPERTY = "property";
|
||||
|
||||
public static void main(String[] args) {
|
||||
TestEquals one = new TestEquals(1);
|
||||
TestEquals two = new TestEquals(2);
|
||||
|
||||
Object source = TestEquals.class;
|
||||
PropertyChangeSupport pcs = new PropertyChangeSupport(source);
|
||||
pcs.addPropertyChangeListener(PROPERTY, one);
|
||||
pcs.addPropertyChangeListener(PROPERTY, two);
|
||||
|
||||
PropertyChangeEvent event = new PropertyChangeEvent(source, PROPERTY, one, two);
|
||||
pcs.firePropertyChange(event);
|
||||
test(one, two, 1); // only one check
|
||||
pcs.firePropertyChange(PROPERTY, one, two);
|
||||
test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)
|
||||
pcs.fireIndexedPropertyChange(PROPERTY, 1, one, two);
|
||||
test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)
|
||||
}
|
||||
|
||||
private static void test(TestEquals v1, TestEquals v2, int amount) {
|
||||
int count = v1.count + v2.count;
|
||||
if (amount < count)
|
||||
throw new Error("method equals() is called " + count + " times");
|
||||
|
||||
v1.count = 0;
|
||||
v2.count = 0;
|
||||
}
|
||||
|
||||
private final int value;
|
||||
private int count;
|
||||
|
||||
private TestEquals(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (object instanceof TestEquals) {
|
||||
this.count++;
|
||||
TestEquals that = (TestEquals)object;
|
||||
return that.value == this.value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
}
|
||||
}
|
||||
119
test/jdk/java/beans/PropertyChangeSupport/TestListeners.java
Normal file
119
test/jdk/java/beans/PropertyChangeSupport/TestListeners.java
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 5004188
|
||||
* @summary Tests sequence of listeners
|
||||
* @author Sergey Malenkov
|
||||
*/
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeListenerProxy;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
|
||||
public final class TestListeners implements PropertyChangeListener {
|
||||
private static final String NAME = "property";
|
||||
private static final String NONE = "broken";
|
||||
|
||||
private static int current;
|
||||
|
||||
public static void main(String[] args) {
|
||||
PropertyChangeSupport pcs = new PropertyChangeSupport(TestListeners.class);
|
||||
pcs.addPropertyChangeListener(new TestListeners(0));
|
||||
pcs.addPropertyChangeListener(NAME, new TestListeners(2));
|
||||
pcs.addPropertyChangeListener(new TestListeners(1));
|
||||
pcs.addPropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));
|
||||
|
||||
|
||||
current = 0;
|
||||
pcs.firePropertyChange(NAME, 0, 1);
|
||||
if (current != 4)
|
||||
throw new Error("Expected 4 listeners, but called " + current);
|
||||
|
||||
current = 0;
|
||||
pcs.firePropertyChange(NONE, 1, 0);
|
||||
if (current != 2)
|
||||
throw new Error("Expected 2 listeners, but called " + current);
|
||||
|
||||
|
||||
PropertyChangeListener[] all = pcs.getPropertyChangeListeners();
|
||||
if (all.length != 4)
|
||||
throw new Error("Expected 4 listeners, but contained " + all.length);
|
||||
|
||||
PropertyChangeListener[] named = pcs.getPropertyChangeListeners(NAME);
|
||||
if (named.length != 2)
|
||||
throw new Error("Expected 2 named listeners, but contained " + named.length);
|
||||
|
||||
PropertyChangeListener[] other = pcs.getPropertyChangeListeners(NONE);
|
||||
if (other.length != 0)
|
||||
throw new Error("Expected 0 other listeners, but contained " + other.length);
|
||||
|
||||
pcs.removePropertyChangeListener(new TestListeners(0));
|
||||
pcs.removePropertyChangeListener(new TestListeners(1));
|
||||
pcs.removePropertyChangeListener(NAME, new TestListeners(2));
|
||||
pcs.removePropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));
|
||||
|
||||
all = pcs.getPropertyChangeListeners();
|
||||
if (all.length != 0)
|
||||
throw new Error("Expected 4 listeners, but contained " + all.length);
|
||||
|
||||
named = pcs.getPropertyChangeListeners(NAME);
|
||||
if (named.length != 0)
|
||||
throw new Error("Expected 2 named listeners, but contained " + named.length);
|
||||
|
||||
other = pcs.getPropertyChangeListeners(NONE);
|
||||
if (other.length != 0)
|
||||
throw new Error("Expected 0 other listeners, but contained " + other.length);
|
||||
}
|
||||
|
||||
private final int index;
|
||||
|
||||
public TestListeners(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
if (this.index < 0)
|
||||
throw new Error("Unexpected listener: " + this.index);
|
||||
|
||||
System.out.println("index = " + this.index);
|
||||
if (this.index != current++)
|
||||
throw new Error("Unexpected listener: " + this.index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (object instanceof TestListeners) {
|
||||
TestListeners test = (TestListeners)object;
|
||||
return test.index == this.index;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.index;
|
||||
}
|
||||
}
|
||||
133
test/jdk/java/beans/PropertyChangeSupport/TestMethods.java
Normal file
133
test/jdk/java/beans/PropertyChangeSupport/TestMethods.java
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 5004188
|
||||
* @summary Tests sequence of methods
|
||||
* @author Sergey Malenkov
|
||||
*/
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
|
||||
enum Fire {
|
||||
PropertyChangeEvent,
|
||||
PropertyObject,
|
||||
PropertyInteger,
|
||||
PropertyBoolean,
|
||||
IndexedPropertyObject,
|
||||
IndexedPropertyInteger,
|
||||
IndexedPropertyBoolean,
|
||||
}
|
||||
|
||||
public class TestMethods extends PropertyChangeSupport implements PropertyChangeListener {
|
||||
private static final String NAME = "property";
|
||||
|
||||
public static void main(String[] args) {
|
||||
Object source = new Object();
|
||||
new TestMethods(source).firePropertyChange(new PropertyChangeEvent(source, NAME, null, null));
|
||||
new TestMethods(source).firePropertyChange(NAME, null, null);
|
||||
new TestMethods(source).firePropertyChange(NAME, 0, 1);
|
||||
new TestMethods(source).firePropertyChange(NAME, true, false);
|
||||
new TestMethods(source).fireIndexedPropertyChange(NAME, 0, null, null);
|
||||
new TestMethods(source).fireIndexedPropertyChange(NAME, 0, 0, 1);
|
||||
new TestMethods(source).fireIndexedPropertyChange(NAME, 0, true, false);
|
||||
}
|
||||
|
||||
private Fire state;
|
||||
|
||||
public TestMethods(Object source) {
|
||||
super(source);
|
||||
addPropertyChangeListener(this);
|
||||
}
|
||||
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
if (this.state != Fire.PropertyChangeEvent)
|
||||
throw new Error("Illegal state: " + this.state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void firePropertyChange(String property, Object oldValue, Object newValue) {
|
||||
if ((this.state != null) && (this.state != Fire.PropertyBoolean) && (this.state != Fire.PropertyInteger))
|
||||
throw new Error("Illegal state: " + this.state);
|
||||
|
||||
this.state = Fire.PropertyObject;
|
||||
super.firePropertyChange(property, oldValue, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void firePropertyChange(String property, int oldValue, int newValue) {
|
||||
if (this.state != null)
|
||||
throw new Error("Illegal state: " + this.state);
|
||||
|
||||
this.state = Fire.PropertyInteger;
|
||||
super.firePropertyChange(property, oldValue, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void firePropertyChange(String property, boolean oldValue, boolean newValue) {
|
||||
if (this.state != null)
|
||||
throw new Error("Illegal state: " + this.state);
|
||||
|
||||
this.state = Fire.PropertyBoolean;
|
||||
super.firePropertyChange(property, oldValue, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void firePropertyChange(PropertyChangeEvent event) {
|
||||
if ((this.state != null) && (this.state != Fire.PropertyObject) && (this.state != Fire.IndexedPropertyObject))
|
||||
throw new Error("Illegal state: " + this.state);
|
||||
|
||||
this.state = Fire.PropertyChangeEvent;
|
||||
super.firePropertyChange(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireIndexedPropertyChange(String property, int index, Object oldValue, Object newValue) {
|
||||
if ((this.state != null) && (this.state != Fire.IndexedPropertyBoolean) && (this.state != Fire.IndexedPropertyInteger))
|
||||
throw new Error("Illegal state: " + this.state);
|
||||
|
||||
this.state = Fire.IndexedPropertyObject;
|
||||
super.fireIndexedPropertyChange(property, index, oldValue, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireIndexedPropertyChange(String property, int index, int oldValue, int newValue) {
|
||||
if (this.state != null)
|
||||
throw new Error("Illegal state: " + this.state);
|
||||
|
||||
this.state = Fire.IndexedPropertyInteger;
|
||||
super.fireIndexedPropertyChange(property, index, oldValue, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireIndexedPropertyChange(String property, int index, boolean oldValue, boolean newValue) {
|
||||
if (this.state != null)
|
||||
throw new Error("Illegal state: " + this.state);
|
||||
|
||||
this.state = Fire.IndexedPropertyBoolean;
|
||||
super.fireIndexedPropertyChange(property, index, oldValue, newValue);
|
||||
}
|
||||
}
|
||||
182
test/jdk/java/beans/PropertyChangeSupport/TestSerialization.java
Normal file
182
test/jdk/java/beans/PropertyChangeSupport/TestSerialization.java
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 5004188
|
||||
* @run main TestSerialization 1.5.0_10.ser 1.6.0.ser
|
||||
* @summary Tests serialization of PropertyChangeSupport
|
||||
* @author Sergey Malenkov
|
||||
*/
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeListenerProxy;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
public final class TestSerialization implements PropertyChangeListener, Serializable {
|
||||
private static final String NAME = "property";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length == 0) {
|
||||
String file = System.getProperty("java.version") + ".ser";
|
||||
serialize(file, create());
|
||||
}
|
||||
else {
|
||||
byte[] array = serialize(create());
|
||||
for (String file : args) {
|
||||
check(deserialize(file));
|
||||
check(array, read(file));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static PropertyChangeSupport create() {
|
||||
PropertyChangeSupport pcs = new PropertyChangeSupport(TestSerialization.class);
|
||||
pcs.addPropertyChangeListener(new TestSerialization(0));
|
||||
pcs.addPropertyChangeListener(NAME, new TestSerialization(1));
|
||||
return pcs;
|
||||
}
|
||||
|
||||
private static void check(PropertyChangeSupport pcs) {
|
||||
PropertyChangeListener[] namedListeners = pcs.getPropertyChangeListeners(NAME);
|
||||
check(namedListeners, 1);
|
||||
check(namedListeners[0], 1);
|
||||
|
||||
PropertyChangeListener[] allListeners = pcs.getPropertyChangeListeners();
|
||||
check(allListeners, 2);
|
||||
check(allListeners[0], 0);
|
||||
check(allListeners[1], 1, NAME);
|
||||
}
|
||||
|
||||
private static void check(byte[] a1, byte[] a2) {
|
||||
int length = a1.length;
|
||||
if (length != a2.length)
|
||||
throw new Error("Different file sizes: " + length + " != " + a2.length);
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
if (a1[i] != a2[i])
|
||||
throw new Error("Different bytes at " + i + " position");
|
||||
}
|
||||
|
||||
private static void check(PropertyChangeListener[] array, int length) {
|
||||
if (length != array.length)
|
||||
throw new Error("Unexpected amount of listeners: " + array.length);
|
||||
}
|
||||
|
||||
private static void check(PropertyChangeListener listener, int index) {
|
||||
if (!(listener instanceof TestSerialization))
|
||||
throw new Error("Unexpected listener: " + listener);
|
||||
|
||||
TestSerialization object = (TestSerialization)listener;
|
||||
if (index != object.index)
|
||||
throw new Error("Unexpected index: " + index + " != " + object.index);
|
||||
}
|
||||
|
||||
private static void check(PropertyChangeListener listener, int index, String name) {
|
||||
if (!(listener instanceof PropertyChangeListenerProxy))
|
||||
throw new Error("Unexpected listener: " + listener);
|
||||
|
||||
PropertyChangeListenerProxy object = (PropertyChangeListenerProxy)listener;
|
||||
if (!name.equals(object.getPropertyName()))
|
||||
throw new Error("Unexpected name: " + name + " != " + object.getPropertyName());
|
||||
|
||||
check((PropertyChangeListener)object.getListener(), index);
|
||||
}
|
||||
|
||||
private static byte[] read(String file) throws Exception {
|
||||
FileInputStream stream = null;
|
||||
try {
|
||||
stream = new FileInputStream(new File(System.getProperty("test.src", "."), file));
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
for (int i = stream.read(); i != -1; i = stream.read())
|
||||
out.write(i);
|
||||
|
||||
return out.toByteArray();
|
||||
}
|
||||
finally {
|
||||
if (stream != null)
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static PropertyChangeSupport deserialize(String file) throws Exception {
|
||||
ObjectInputStream stream = null;
|
||||
try {
|
||||
stream = new ObjectInputStream(new FileInputStream(new File(System.getProperty("test.src", "."), file)));
|
||||
return (PropertyChangeSupport)stream.readObject();
|
||||
}
|
||||
finally {
|
||||
if (stream != null)
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void serialize(String file, PropertyChangeSupport pcs) throws Exception {
|
||||
ObjectOutputStream stream = null;
|
||||
try {
|
||||
stream = new ObjectOutputStream(new FileOutputStream(new File(System.getProperty("test.src", "."), file)));
|
||||
stream.writeObject(pcs);
|
||||
}
|
||||
finally {
|
||||
if (stream != null)
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] serialize(PropertyChangeSupport pcs) throws Exception {
|
||||
ObjectOutputStream stream = null;
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
stream = new ObjectOutputStream(out);
|
||||
stream.writeObject(pcs);
|
||||
return out.toByteArray();
|
||||
}
|
||||
finally {
|
||||
if (stream != null)
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private int index;
|
||||
|
||||
public TestSerialization(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return this.index;
|
||||
}
|
||||
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 5004188
|
||||
* @summary Tests synchronization
|
||||
* @author Sergey Malenkov
|
||||
*/
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import javax.swing.JButton;
|
||||
|
||||
public class TestSynchronization {
|
||||
private static boolean isCalled;
|
||||
|
||||
public static void main(String[] args) {
|
||||
final JButton button = new JButton();
|
||||
button.addPropertyChangeListener("name", new PropertyChangeListener() {
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
isCalled = true;
|
||||
}
|
||||
});
|
||||
button.addPropertyChangeListener(new PropertyChangeListener() {
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
for (PropertyChangeListener listener : button.getPropertyChangeListeners())
|
||||
button.removePropertyChangeListener(listener);
|
||||
}
|
||||
});
|
||||
button.setName(null);
|
||||
if (!isCalled)
|
||||
throw new Error("test failed");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue