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:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 1997, 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 4076065
* @summary Tests that constructor check for null source
* @author Graham Hamilton
*/
import java.beans.VetoableChangeSupport;
public class Test4076065 {
public static void main(String[] args) {
try {
new VetoableChangeSupport(null);
} catch (NullPointerException exception) {
return;
}
throw new Error("didn't get expected NullPointerException");
}
}

View file

@ -0,0 +1,89 @@
/*
* 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 4092906
* @summary Tests that hasListeners() is not failed across serialization
* @author Graham Hamilton
*/
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Test4092906 {
private static final String PUBLIC = "public";
private static final String PRIVATE = "private";
public static void main(String[] args) {
VetoableChangeSupport pcs = new VetoableChangeSupport(args);
pcs.addVetoableChangeListener(PUBLIC, new PublicListener());
pcs.addVetoableChangeListener(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 = (VetoableChangeSupport) 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 VetoableChangeListener, Serializable {
public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException {
}
}
private static class PrivateListener implements VetoableChangeListener {
public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException {
}
}
}

View file

@ -0,0 +1,75 @@
/*
* 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 4425885
* @summary Tests VetoableChangeListener notification
* @author Sergey Malenkov
*/
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
public class Test4425885 {
private static final String PROPERTY = "property"; // NON-NLS: predefined property name
public static void main(String[] args) {
CheckListener first = new CheckListener();
VetoListener second = new VetoListener();
CheckListener third = new CheckListener();
VetoableChangeSupport vcs = new VetoableChangeSupport(Test4425885.class);
vcs.addVetoableChangeListener(PROPERTY, first);
vcs.addVetoableChangeListener(PROPERTY, second);
vcs.addVetoableChangeListener(PROPERTY, third);
try {
vcs.fireVetoableChange(PROPERTY, 0, 1);
} catch (PropertyVetoException exception) {
if (first.odd)
throw new Error("no undo for the first listener", exception);
if (third.odd)
throw new Error("no undo for the third listener", exception);
return; // expected exception
}
throw new Error("exception should be thrown");
}
private static class CheckListener implements VetoableChangeListener {
private boolean odd; // even/odd check for notification
public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException {
this.odd = !this.odd;
}
}
private static class VetoListener implements VetoableChangeListener {
public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException {
throw new PropertyVetoException("disable all changes", event);
}
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2006, 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 4994635
* @summary Tests VetoableChangeListenerProxy adding
* @author Sergey Malenkov
*/
import java.beans.PropertyChangeEvent;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeListenerProxy;
import java.beans.VetoableChangeSupport;
public class Test4994635 implements VetoableChangeListener {
public static void main(String[] args) {
Test4994635 test = new Test4994635();
test.vcs.addVetoableChangeListener(
new VetoableChangeListenerProxy("property", test));
}
private final VetoableChangeSupport vcs = new VetoableChangeSupport(this);
public void vetoableChange(PropertyChangeEvent event) {
}
}

View file

@ -0,0 +1,81 @@
/*
* Copyright (c) 2008, 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 6630275
* @summary Tests VetoableChangeSupport specification
* @author Sergey Malenkov
*/
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
public class Test6630275 {
private static final String PROPERTY = "property"; // NON-NLS: predefined property name
public static void main(String[] args) {
CheckListener first = new CheckListener(false);
CheckListener second = new CheckListener(true);
CheckListener third = new CheckListener(false);
VetoableChangeSupport vcs = new VetoableChangeSupport(Test6630275.class);
vcs.addVetoableChangeListener(first);
vcs.addVetoableChangeListener(PROPERTY, first);
vcs.addVetoableChangeListener(PROPERTY, second);
vcs.addVetoableChangeListener(PROPERTY, third);
try {
vcs.fireVetoableChange(PROPERTY, true, false);
} catch (PropertyVetoException exception) {
first.validate();
second.validate();
third.validate();
return; // expected exception
}
throw new Error("exception should be thrown");
}
private static class CheckListener implements VetoableChangeListener {
private final boolean veto;
private boolean odd; // even/odd check for notification
private CheckListener(boolean veto) {
this.veto = veto;
}
private void validate() {
if (this.veto != this.odd)
throw new Error(this.odd
? "undo event expected"
: "unexpected undo event");
}
public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException {
this.odd = !this.odd;
if (this.veto)
throw new PropertyVetoException("disable all changes", event);
}
}
}

View 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 VetoableChangeSupport class
* @author Sergey Malenkov
*/
import java.beans.PropertyChangeEvent;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
import java.util.EventListener;
import java.util.EventListenerProxy;
public class Test7148143 {
private static class CustomProxy
extends EventListenerProxy<EventListener>
implements VetoableChangeListener {
public CustomProxy() {
super(new EventListener() {
});
}
public void vetoableChange(PropertyChangeEvent event) {
}
}
public static void main(String[] args) {
VetoableChangeListener listener = new CustomProxy();
VetoableChangeSupport support = new VetoableChangeSupport(listener);
support.addVetoableChangeListener(listener);
support.addVetoableChangeListener("foo", listener); // cast class exception
}
}

View file

@ -0,0 +1,83 @@
/*
* 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.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
public final class TestEquals implements VetoableChangeListener {
private static final String PROPERTY = "property";
public static void main(String[] args) throws PropertyVetoException {
TestEquals one = new TestEquals(1);
TestEquals two = new TestEquals(2);
Object source = TestEquals.class;
VetoableChangeSupport vcs = new VetoableChangeSupport(source);
vcs.addVetoableChangeListener(PROPERTY, one);
vcs.addVetoableChangeListener(PROPERTY, two);
PropertyChangeEvent event = new PropertyChangeEvent(source, PROPERTY, one, two);
vcs.fireVetoableChange(event);
test(one, two, 1); // only one check
vcs.fireVetoableChange(PROPERTY, one, two);
test(one, two, 2); // because it invokes fireVetoableChange(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 vetoableChange(PropertyChangeEvent event) {
}
}

View file

@ -0,0 +1,120 @@
/*
* 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.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeListenerProxy;
import java.beans.VetoableChangeSupport;
public final class TestListeners implements VetoableChangeListener {
private static final String NAME = "property";
private static final String NONE = "broken";
private static int current;
public static void main(String[] args) throws PropertyVetoException {
VetoableChangeSupport vcs = new VetoableChangeSupport(TestListeners.class);
vcs.addVetoableChangeListener(new TestListeners(0));
vcs.addVetoableChangeListener(NAME, new TestListeners(2));
vcs.addVetoableChangeListener(new TestListeners(1));
vcs.addVetoableChangeListener(NAME, new VetoableChangeListenerProxy(NAME, new TestListeners(3)));
current = 0;
vcs.fireVetoableChange(NAME, 0, 1);
if (current != 4)
throw new Error("Expected 4 listeners, but called " + current);
current = 0;
vcs.fireVetoableChange(NONE, 1, 0);
if (current != 2)
throw new Error("Expected 2 listeners, but called " + current);
VetoableChangeListener[] all = vcs.getVetoableChangeListeners();
if (all.length != 4)
throw new Error("Expected 4 listeners, but contained " + all.length);
VetoableChangeListener[] named = vcs.getVetoableChangeListeners(NAME);
if (named.length != 2)
throw new Error("Expected 2 named listeners, but contained " + named.length);
VetoableChangeListener[] other = vcs.getVetoableChangeListeners(NONE);
if (other.length != 0)
throw new Error("Expected 0 other listeners, but contained " + other.length);
vcs.removeVetoableChangeListener(new TestListeners(0));
vcs.removeVetoableChangeListener(new TestListeners(1));
vcs.removeVetoableChangeListener(NAME, new TestListeners(2));
vcs.removeVetoableChangeListener(NAME, new VetoableChangeListenerProxy(NAME, new TestListeners(3)));
all = vcs.getVetoableChangeListeners();
if (all.length != 0)
throw new Error("Expected 4 listeners, but contained " + all.length);
named = vcs.getVetoableChangeListeners(NAME);
if (named.length != 0)
throw new Error("Expected 2 named listeners, but contained " + named.length);
other = vcs.getVetoableChangeListeners(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 vetoableChange(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;
}
}

View file

@ -0,0 +1,101 @@
/*
* 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.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
enum Fire {
PropertyChangeEvent,
PropertyObject,
PropertyInteger,
PropertyBoolean,
}
public class TestMethods extends VetoableChangeSupport implements VetoableChangeListener {
private static final String NAME = "property";
public static void main(String[] args) throws PropertyVetoException {
Object source = new Object();
new TestMethods(source).fireVetoableChange(new PropertyChangeEvent(source, NAME, null, null));
new TestMethods(source).fireVetoableChange(NAME, null, null);
new TestMethods(source).fireVetoableChange(NAME, 0, 1);
new TestMethods(source).fireVetoableChange(NAME, true, false);
}
private Fire state;
public TestMethods(Object source) {
super(source);
addVetoableChangeListener(this);
}
public void vetoableChange(PropertyChangeEvent event) {
if (this.state != Fire.PropertyChangeEvent)
throw new Error("Illegal state: " + this.state);
}
@Override
public void fireVetoableChange(String property, Object oldValue, Object newValue) throws PropertyVetoException {
if ((this.state != null) && (this.state != Fire.PropertyBoolean) && (this.state != Fire.PropertyInteger))
throw new Error("Illegal state: " + this.state);
this.state = Fire.PropertyObject;
super.fireVetoableChange(property, oldValue, newValue);
}
@Override
public void fireVetoableChange(String property, int oldValue, int newValue) throws PropertyVetoException {
if (this.state != null)
throw new Error("Illegal state: " + this.state);
this.state = Fire.PropertyInteger;
super.fireVetoableChange(property, oldValue, newValue);
}
@Override
public void fireVetoableChange(String property, boolean oldValue, boolean newValue) throws PropertyVetoException {
if (this.state != null)
throw new Error("Illegal state: " + this.state);
this.state = Fire.PropertyBoolean;
super.fireVetoableChange(property, oldValue, newValue);
}
@Override
public void fireVetoableChange(PropertyChangeEvent event) throws PropertyVetoException {
if ((this.state != null) && (this.state != Fire.PropertyObject))
throw new Error("Illegal state: " + this.state);
this.state = Fire.PropertyChangeEvent;
super.fireVetoableChange(event);
}
}

View 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 VetoableChangeSupport
* @author Sergey Malenkov
*/
import java.beans.PropertyChangeEvent;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeListenerProxy;
import java.beans.VetoableChangeSupport;
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 VetoableChangeListener, 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 VetoableChangeSupport create() {
VetoableChangeSupport vcs = new VetoableChangeSupport(TestSerialization.class);
vcs.addVetoableChangeListener(new TestSerialization(0));
vcs.addVetoableChangeListener(NAME, new TestSerialization(1));
return vcs;
}
private static void check(VetoableChangeSupport vcs) {
VetoableChangeListener[] namedListeners = vcs.getVetoableChangeListeners(NAME);
check(namedListeners, 1);
check(namedListeners[0], 1);
VetoableChangeListener[] allListeners = vcs.getVetoableChangeListeners();
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(VetoableChangeListener[] array, int length) {
if (length != array.length)
throw new Error("Unexpected amount of listeners: " + array.length);
}
private static void check(VetoableChangeListener 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(VetoableChangeListener listener, int index, String name) {
if (!(listener instanceof VetoableChangeListenerProxy))
throw new Error("Unexpected listener: " + listener);
VetoableChangeListenerProxy object = (VetoableChangeListenerProxy)listener;
if (!name.equals(object.getPropertyName()))
throw new Error("Unexpected name: " + name + " != " + object.getPropertyName());
check((VetoableChangeListener)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 VetoableChangeSupport deserialize(String file) throws Exception {
ObjectInputStream stream = null;
try {
stream = new ObjectInputStream(new FileInputStream(new File(System.getProperty("test.src", "."), file)));
return (VetoableChangeSupport)stream.readObject();
}
finally {
if (stream != null)
stream.close();
}
}
private static void serialize(String file, VetoableChangeSupport vcs) throws Exception {
ObjectOutputStream stream = null;
try {
stream = new ObjectOutputStream(new FileOutputStream(new File(System.getProperty("test.src", "."), file)));
stream.writeObject(vcs);
}
finally {
if (stream != null)
stream.close();
}
}
private static byte[] serialize(VetoableChangeSupport vcs) throws Exception {
ObjectOutputStream stream = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
stream = new ObjectOutputStream(out);
stream.writeObject(vcs);
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 vetoableChange(PropertyChangeEvent event) {
}
}

View file

@ -0,0 +1,56 @@
/*
* 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.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
public class TestSynchronization {
private static boolean isCalled;
public static void main(String[] args) throws PropertyVetoException {
final VetoableChangeSupport vcs = new VetoableChangeSupport(TestSynchronization.class);
vcs.addVetoableChangeListener("name", new VetoableChangeListener() {
public void vetoableChange(PropertyChangeEvent event) {
isCalled = true;
}
});
vcs.addVetoableChangeListener(new VetoableChangeListener() {
public void vetoableChange(PropertyChangeEvent event) {
for (VetoableChangeListener listener : vcs.getVetoableChangeListeners())
vcs.removeVetoableChangeListener(listener);
}
});
vcs.fireVetoableChange("name", null, null);
if (!isCalled)
throw new Error("test failed");
}
}