You are viewing a plain text version of this content. The canonical link for it is here.
Posted to kato-commits@incubator.apache.org by cc...@apache.org on 2009/03/16 16:42:41 UTC
svn commit: r754945 [3/8] - in
/incubator/kato/trunk/import/org.apache.kato.tck.tests: ./ .settings/ src/
src/com/ src/com/ibm/ src/com/ibm/dtfj/ src/com/ibm/dtfj/tck/
src/com/ibm/dtfj/tck/tests/ src/com/ibm/dtfj/tck/tests/javaruntime/
src/com/ibm/dtfj...
Added: incubator/kato/trunk/import/org.apache.kato.tck.tests/src/com/ibm/dtfj/tck/tests/javaruntime/TestJavaMonitor_ObjectMonitors.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/import/org.apache.kato.tck.tests/src/com/ibm/dtfj/tck/tests/javaruntime/TestJavaMonitor_ObjectMonitors.java?rev=754945&view=auto
==============================================================================
--- incubator/kato/trunk/import/org.apache.kato.tck.tests/src/com/ibm/dtfj/tck/tests/javaruntime/TestJavaMonitor_ObjectMonitors.java (added)
+++ incubator/kato/trunk/import/org.apache.kato.tck.tests/src/com/ibm/dtfj/tck/tests/javaruntime/TestJavaMonitor_ObjectMonitors.java Mon Mar 16 16:42:39 2009
@@ -0,0 +1,1259 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package com.ibm.dtfj.tck.tests.javaruntime;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import com.ibm.dtfj.image.CorruptData;
+import com.ibm.dtfj.image.CorruptDataException;
+import com.ibm.dtfj.image.ImagePointer;
+import com.ibm.dtfj.java.JavaClass;
+import com.ibm.dtfj.java.JavaField;
+import com.ibm.dtfj.java.JavaMonitor;
+import com.ibm.dtfj.java.JavaObject;
+import com.ibm.dtfj.java.JavaRuntime;
+import com.ibm.dtfj.java.JavaThread;
+import com.ibm.dtfj.tck.harness.TCKJavaRuntimeTestcase;
+
+/**
+ * Tests that monitors work correctly.
+ *
+ * Tests with variations on the number of notify waiters, enter waiters and owners,
+ * up to a maximum of 2 waiters of each type (there can be only one owner, of course).
+ *
+ * TODO Add tests where threads own one monitor and wait on another.
+ *
+ */
+public class TestJavaMonitor_ObjectMonitors extends TCKJavaRuntimeTestcase {
+ static final String monitorNotifyThreadName = "NotifyThread";
+
+ static final String monitorOwnerThreadName = "OwnerThread";
+
+ static final String monitorEnterThreadName = "EnterThread";
+ static final String namePrefix = "TestMonitors-";
+
+ /**
+ * A unique class to allow DTFJ to find the correct object.
+ *
+ */
+ public class MonitorClass {
+ private String id;
+
+ public MonitorClass(String id) {
+ this.id = id;
+ }
+ }
+
+ static final String monitorClassName = MonitorClass.class.getName()
+ .replace('.', '/');
+
+ public static class MonitorThreadConfig {
+ public MonitorThreadConfig(int waiters, int enters, int owners) {
+ this.waiters = waiters;
+ this.enters = enters;
+ this.owners = owners;
+ this.id = generatePrefix(waiters, enters, owners);
+ }
+
+ void setMonitor(JavaMonitor monitor) {
+ this.monitor = monitor;
+ }
+ JavaMonitor getMonitor() {
+ return monitor;
+ }
+
+ String id;
+ int waiters, enters, owners;
+ private JavaMonitor monitor;
+
+ }
+
+ private static int NUM_MONITORS = 9;
+
+ static MonitorThreadConfig[] monitorConfigs;
+ {
+ int i=0;
+ monitorConfigs = new MonitorThreadConfig[NUM_MONITORS];
+
+ monitorConfigs[i++] = new MonitorThreadConfig(2, 2, 1);
+ monitorConfigs[i++] = new MonitorThreadConfig(0, 2, 1);
+ monitorConfigs[i++] = new MonitorThreadConfig(2, 0, 1);
+ monitorConfigs[i++] = new MonitorThreadConfig(2, 0, 0);
+ monitorConfigs[i++] = new MonitorThreadConfig(1, 1, 1);
+ monitorConfigs[i++] = new MonitorThreadConfig(1, 0, 1);
+ monitorConfigs[i++] = new MonitorThreadConfig(1, 0, 0);
+ monitorConfigs[i++] = new MonitorThreadConfig(0, 0, 1);
+ monitorConfigs[i++] = new MonitorThreadConfig(0, 1, 1);
+
+ }
+
+ private MonitorThreadConfig findMonitorThreadConfig(int waiters,
+ int enters, int owners) {
+ for (int i = 0; i < monitorConfigs.length; i++) {
+ MonitorThreadConfig current = monitorConfigs[i];
+
+ if ((current.waiters == waiters) && (current.enters == enters)
+ && (current.owners == owners)) {
+ return current;
+ }
+ }
+
+ return null;
+ }
+
+ private MonitorThreadConfig findMonitorThreadConfig(String id) {
+ for (int i = 0; i < monitorConfigs.length; i++) {
+ MonitorThreadConfig current = monitorConfigs[i];
+
+ if (current.id.equals(id)) {
+ return current;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Not executed anywhere - for debugging
+ */
+ public void printMonitors() {
+ System.out.println("JVM "+ System.getProperty("java.vm.name")+ ", "+System.getProperty("java.vm.vendor")+
+ System.getProperty("java.vm.version"));
+
+ for (int i = 0; i < monitorConfigs.length; i++) {
+ System.out.println("Printing out status of "+ monitorConfigs[i].id);
+ System.out.println("==================================");
+ JavaMonitor monitor = monitorConfigs[i].getMonitor();
+
+ if (monitor == null) {
+ System.out.println("monitor = null");
+ continue;
+ }
+
+ try {
+ System.out.println("monitor.getName()"+ monitor.getName());
+ } catch (CorruptDataException e) {
+ assertNotNull(e.getCorruptData());
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ System.out.println("\nmonitor.getEntersWaiters():");
+
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ Object next = enters.next();
+
+ if (next instanceof CorruptData) {
+ System.out.println("enter waiter corrupt: "+ next);
+ continue;
+ }
+
+ JavaThread thread = (JavaThread) next;
+
+ try {
+ System.out.println("EnterWaiter: "+thread.getName());
+ } catch (CorruptDataException e) {
+ assertNotNull(e.getCorruptData());
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+
+ }
+
+ System.out.println("\nmonitor.getNotifyWaiters():");
+
+ Iterator notifiers = monitor.getNotifyWaiters();
+
+ while (notifiers.hasNext()) {
+ Object next = notifiers.next();
+
+ if (next instanceof CorruptData) {
+ System.out.println("notify waiter corrupt: "+ next);
+ continue;
+ }
+
+ JavaThread thread = (JavaThread) next;
+
+ try {
+ System.out.println("NotifyWaiter: "+thread.getName());
+ } catch (CorruptDataException e) {
+ assertNotNull(e.getCorruptData());
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ JavaThread thread;
+ try {
+ thread = monitor.getOwner();
+ if (thread == null) {
+ System.out.println("Owner thread = null");
+ } else {
+ try {
+ System.out.println("Owner thread = "+thread.getName());
+ } catch (CorruptDataException e) {
+ assertNotNull(e.getCorruptData());
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ } catch (CorruptDataException e1) {
+ assertNotNull(e1.getCorruptData());
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+
+ System.out.println("\n\n");
+ }
+ }
+
+ public void configureMonitors() {
+ for (int i = 0; i < monitorConfigs.length; i++) {
+ createMonitor(monitorConfigs[i].waiters, monitorConfigs[i].enters,
+ monitorConfigs[i].owners);
+ }
+ }
+
+ private static String generatePrefix(int waiters, int enters, int owners) {
+ return namePrefix + waiters + "-" + owners + "-" + enters;
+ }
+
+ public void createMonitor(int waiters, int enters, int owners) {
+ String prefix = generatePrefix(waiters, enters, owners);
+
+ Object monitor = new MonitorClass(prefix);
+
+ // These rules are very important.
+ if (waiters == 0 && enters == 0 && owners == 0) {
+ throw new IllegalArgumentException(
+ "Can't create monitor with no threads");
+ }
+
+ if (waiters < 0 || enters < 0 || owners < 0) {
+ throw new IllegalArgumentException(
+ "Numbers of threads must be 0 or above.");
+ }
+
+ if (enters > 0 && owners == 0) {
+ throw new IllegalArgumentException(
+ "Can't create enters threads with no owner");
+ }
+
+ if (owners > 1) {
+ throw new IllegalArgumentException(
+ "Can't create monitor with more than one owner");
+ }
+
+ // The ordering is significant.
+ for (int i = 0; i < waiters; i++) {
+ new MonitorNotifyThread(prefix + "-" + monitorNotifyThreadName
+ + "-" + i, monitor).start();
+ }
+
+ for (int i = 0; i < owners; i++) {
+ new MonitorOwnerThread(prefix + "-" + monitorOwnerThreadName + "-"
+ + i, monitor).start();
+ }
+
+ for (int i = 0; i < enters; i++) {
+ new MonitorEnterThread(prefix + "-" + monitorEnterThreadName + "-"
+ + i, monitor).start();
+ }
+ }
+
+ public class MonitorNotifyThread extends Thread {
+ private Object monitor;
+ private volatile boolean almostReady = false;
+
+ public MonitorNotifyThread(String name, Object monitor) {
+ super(name);
+ this.setDaemon(true); // Allow JVM to exit.
+ this.monitor = monitor;
+ }
+
+ public void run() {
+ synchronized (monitor) {
+ synchronized (this) {
+ almostReady = true;
+ this.notify();
+ }
+ while (true) {
+ try {
+ monitor.wait();
+ } catch (InterruptedException e) {
+ // Keep waiting...
+ }
+ }
+ }
+ }
+
+ /**
+ * Start thread, and waits until the thread is about to release the
+ * monitor.
+ */
+ public void start() {
+ super.start();
+ synchronized (this) {
+ try {
+ while (!almostReady) {
+ this.wait();
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+ }
+
+ /**
+ * A thread that tries to own a monitor.
+ *
+ */
+ public class MonitorOwnerThread extends Thread {
+ private Object monitor;
+ private volatile boolean owned = false;
+
+ public MonitorOwnerThread(String name, Object monitor) {
+ super(name);
+ this.setDaemon(true); // Allow JVM to exit.
+ this.monitor = monitor;
+ }
+
+ public void run() {
+ synchronized (monitor) {
+ synchronized (this) {
+ owned = true;
+ this.notify();
+ }
+ while (true) {
+ // We have either entered, or are entering.
+
+ try {
+ sleep(10000);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ public void start() {
+ super.start();
+ synchronized (this) {
+ try {
+ while (!owned) {
+ this.wait();
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+ }
+
+ /**
+ * A thread that tries to enter a monitor.
+ *
+ */
+ public class MonitorEnterThread extends Thread {
+ private Object monitor;
+ private volatile boolean owned = false;
+
+ public MonitorEnterThread(String name, Object monitor) {
+ super(name);
+ this.setDaemon(true); // Allow JVM to exit.
+ this.monitor = monitor;
+ }
+
+ public void run() {
+ synchronized (this) {
+ owned = true;
+ this.notify();
+ }
+ synchronized (monitor) {
+ // This code should never be reached.
+ }
+ }
+
+ public void start() {
+ super.start();
+
+ synchronized (this) {
+
+ try {
+ while (!owned) {
+ this.wait();
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ try {
+ sleep(1000);
+ } catch (InterruptedException e1) {
+ e1.printStackTrace();
+ }
+ }
+ }
+
+ static private JavaRuntime runtime;
+
+ public void setUp() throws Exception {
+ JavaRuntime currentRuntime = getJavaRuntime();
+
+ if (runtime != null && currentRuntime.equals(runtime)) {
+ return;
+ }
+
+ runtime = getJavaRuntime();
+
+ findMonitors();
+ }
+
+ public void findMonitors() throws Exception {
+ JavaRuntime runtime = getJavaRuntime();
+
+ Iterator monitorIterator = runtime.getMonitors();
+
+ // Search through all monitors for this test's monitor.
+ while (monitorIterator.hasNext()) {
+ Object next = monitorIterator.next();
+
+ // Ignore corrupt data
+ if (next instanceof CorruptData) {
+ continue;
+ }
+
+ JavaMonitor monitorCandidate = (JavaMonitor) next;
+
+ //System.out.println("Monitor candidate->"+monitorCandidate.getName());
+ JavaObject monitorObjectCandidate = monitorCandidate.getObject();
+
+ if (monitorObjectCandidate != null) {
+ JavaClass monitorClass = monitorObjectCandidate.getJavaClass();
+ if (!monitorClassName.equals(monitorClass.getName())) {
+ continue;
+ }
+
+ Iterator fields = monitorClass.getDeclaredFields();
+
+ while (fields.hasNext()) {
+ Object next2 = fields.next();
+
+ if (next2 instanceof CorruptData) {
+ continue;
+ }
+
+ JavaField field = (JavaField) next2;
+ String id = null;
+
+ if ("id".equals(field.getName())) {
+ id = field.getString(monitorObjectCandidate);
+ }
+
+ if (id != null) {
+
+ MonitorThreadConfig config = findMonitorThreadConfig(id);
+
+ if (config != null) {
+ config.setMonitor(monitorCandidate);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ public void testFindMonitor_0_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 1, 1).getMonitor();
+
+ assertNotNull("Couldn't find 0-1-1", monitor);
+ }
+
+ public void testGetID_0_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 1, 1).getMonitor();
+
+ assertNotNull("JavaMonitor.getID() for 0-1-1 is null.", monitor.getID());
+ }
+
+ public void testGetName_0_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 1, 1).getMonitor();
+ assertNotNull("JavaMonitor.getName() for 0-1-1 is null.", monitor
+ .getName());
+ }
+
+ public void testCountWaiters_0_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 1, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ Object next = waiters.next();
+ count++;
+ }
+
+ assertEquals("Waiters count incorrect :" + count, 0, count);
+ }
+
+ public void testCountEnters_0_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 1, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ Object next = enters.next();
+ count++;
+ }
+
+ assertEquals("Enters count incorrect :" + count, 1, count);
+ }
+
+ public void testCountOwners_0_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 1, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertNotNull("Owner count incorrect", owner);
+ }
+
+ public void testNameWaiters_0_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 1, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ JavaThread next = (JavaThread) waiters.next();
+ assertEquals("Waiters name incorrect", generatePrefix(0, 1, 1)
+ + "-" + monitorNotifyThreadName + "-" + count, next
+ .getName());
+ count++;
+ }
+
+ assertEquals("Waiters count incorrect :" + count, 0, count);
+ }
+
+ public void testNameEnters_0_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 1, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ JavaThread next = (JavaThread) enters.next();
+ assertEquals("Enters name incorrect", generatePrefix(0, 1, 1) + "-"
+ + monitorEnterThreadName + "-" + count, next.getName());
+ count++;
+ }
+ }
+
+ public void testNameOwners_0_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 1, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertEquals("Owner name incorrect", generatePrefix(0, 1, 1) + "-"
+ + monitorOwnerThreadName + "-0", owner.getName());
+ }
+
+ ///////////////////////////////////////////
+
+ public void testFindMonitor_0_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 0, 1).getMonitor();
+
+ assertNotNull("Couldn't find 0-1-1", monitor);
+ }
+
+ public void testGetID_0_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 0, 1).getMonitor();
+
+ assertNotNull("JavaMonitor.getID() for 0-1-1 is null.", monitor.getID());
+ }
+
+ public void testGetName_0_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 0, 1).getMonitor();
+ assertNotNull("JavaMonitor.getName() for 0-1-1 is null.", monitor
+ .getName());
+ }
+
+ public void testCountWaiters_0_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 0, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ Object next = waiters.next();
+ count++;
+ }
+
+ assertEquals("Waiters count incorrect :" + count, 0, count);
+ }
+
+ public void testCountEnters_0_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 0, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ Object next = enters.next();
+ count++;
+ }
+
+ assertEquals("Enters count incorrect :" + count, 0, count);
+ }
+
+ public void testCountOwners_0_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 0, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertNotNull("Owner count incorrect", owner);
+ }
+
+ public void testNameWaiters_0_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 0, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ JavaThread next = (JavaThread) waiters.next();
+ assertEquals("Waiters name incorrect", generatePrefix(0, 0, 1)
+ + "-" + monitorNotifyThreadName + "-" + count, next
+ .getName());
+ count++;
+ }
+
+ }
+
+ public void testNameEnters_0_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 0, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ JavaThread next = (JavaThread) enters.next();
+ assertEquals("Enters name incorrect", generatePrefix(0, 0, 1) + "-"
+ + monitorEnterThreadName + "-" + count, next.getName());
+ count++;
+ }
+ }
+
+ public void testNameOwners_0_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 0, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertEquals("Owner name incorrect", generatePrefix(0, 0, 1) + "-"
+ + monitorOwnerThreadName + "-0", owner.getName());
+ }
+
+ ///////////////////////////////////////////
+
+ public void testFindMonitor_1_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 0).getMonitor();
+
+ assertNotNull("Couldn't find monitor 1-0-0", monitor);
+ }
+
+ public void testGetID_1_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 0).getMonitor();
+
+ assertNotNull("JavaMonitor.getID() for 1-0-0 is null.", monitor.getID());
+ }
+
+ public void testGetName_1_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 0).getMonitor();
+ assertNotNull("JavaMonitor.getName() for 1-0-0 is null.", monitor
+ .getName());
+ }
+
+ public void testCountWaiters_1_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 0).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ Object next = waiters.next();
+ count++;
+ }
+
+ assertEquals("Waiters count incorrect :" + count, 1, count);
+ }
+
+ public void testCountEnters_1_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 0).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ Object next = enters.next();
+ count++;
+ }
+
+ assertEquals("Enters count incorrect :" + count, 0, count);
+ }
+
+ public void testCountOwners_1_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 0).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertNull("Owner count incorrect", owner);
+ }
+
+ public void testNameWaiters_1_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 0).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ JavaThread next = (JavaThread) waiters.next();
+ assertEquals("Waiters name incorrect", generatePrefix(1, 0, 0)
+ + "-" + monitorNotifyThreadName + "-" + count, next
+ .getName());
+ count++;
+ }
+ }
+
+ public void testNameEnters_1_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 0).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ JavaThread next = (JavaThread) enters.next();
+ assertEquals("Enters name incorrect", generatePrefix(1, 0, 0) + "-"
+ + monitorEnterThreadName + "-" + count, next.getName());
+ count++;
+ }
+ }
+
+ ///////////////////////////////////////////
+
+ public void testFindMonitor_1_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 1, 1).getMonitor();
+
+ assertNotNull("Couldn't find monitor 1-1-1", monitor);
+ }
+
+ public void testGetID_1_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 1, 1).getMonitor();
+
+ assertNotNull("JavaMonitor.getID() for 1-1-1 is null.", monitor.getID());
+ }
+
+ public void testGetName_1_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 1, 1).getMonitor();
+ assertNotNull("JavaMonitor.getName() for 1-1-1 is null.", monitor
+ .getName());
+ }
+
+ public void testCountWaiters_1_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 1, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ Object next = waiters.next();
+ count++;
+ }
+
+ assertEquals("Waiters count incorrect :" + count, 1, count);
+ }
+
+ public void testCountEnters_1_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 1, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+ enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ Object next = enters.next();
+ count++;
+ }
+
+ assertEquals("Enters count incorrect :" + count, 1, count);
+ }
+
+ public void testCountOwners_1_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 1, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertNotNull("Owner count incorrect", owner);
+ }
+
+ public void testNameWaiters_1_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 1, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ Iterator monitors = getJavaRuntime().getMonitors();
+ monitors = getJavaRuntime().getMonitors();
+
+
+ while (waiters.hasNext()) {
+ JavaThread next = (JavaThread) waiters.next();
+ assertEquals("Waiters name incorrect", generatePrefix(1, 1, 1)
+ + "-" + monitorNotifyThreadName + "-" + count, next
+ .getName());
+ count++;
+ }
+ }
+
+ public void testNameEnters_1_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 1, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ JavaThread next = (JavaThread) enters.next();
+ assertEquals("Enters name incorrect", generatePrefix(1, 1, 1) + "-"
+ + monitorEnterThreadName + "-" + count, next.getName());
+ count++;
+ }
+ }
+
+ public void testNameOwners_1_1_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 1, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertEquals("Owner name incorrect", generatePrefix(1, 1, 1) + "-"
+ + monitorOwnerThreadName + "-0", owner.getName());
+ }
+
+ //////////////////////////////////////////////
+
+ public void testFindMonitor_2_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 0, 0).getMonitor();
+
+ assertNotNull("Couldn't find monitor 2-0-0", monitor);
+ }
+
+ public void testGetID_2_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 0, 0).getMonitor();
+
+ assertNotNull("JavaMonitor.getID() for 2-0-0 is null.", monitor.getID());
+ }
+
+ public void testGetName_2_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 0, 0).getMonitor();
+ assertNotNull("JavaMonitor.getName() for 2-0-0 is null.", monitor
+ .getName());
+ }
+
+ public void testCountWaiters_2_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 0, 0).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ Object next = waiters.next();
+ count++;
+ }
+
+ assertEquals("Waiters count incorrect :" + count, 2, count);
+ }
+
+ public void testCountEnters_2_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 0, 0).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ Object next = enters.next();
+ count++;
+ }
+
+ assertEquals("Enters count incorrect :" + count, 0, count);
+ }
+
+ public void testCountOwners_2_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 0, 0).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertNull("Owner count incorrect", owner);
+ }
+
+ public void testNameWaiters_2_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 0, 0).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ JavaThread next = (JavaThread) waiters.next();
+ assertEquals("Waiters name incorrect", generatePrefix(2, 0, 0)
+ + "-" + monitorNotifyThreadName + "-" + count, next
+ .getName());
+ count++;
+ }
+ }
+
+ public void testNameEnters_2_0_0() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 0, 0).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ JavaThread next = (JavaThread) enters.next();
+ assertEquals("Enters name incorrect", generatePrefix(2, 0, 0) + "-"
+ + monitorEnterThreadName + "-" + count, next.getName());
+ count++;
+ }
+ }
+ ///////////////////////////////////////////
+
+ public void testFindMonitor_1_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 1).getMonitor();
+
+ assertNotNull("Couldn't find monitor 1-0-1", monitor);
+ }
+
+ public void testGetID_1_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 1).getMonitor();
+
+ assertNotNull("JavaMonitor.getID() for 1-0-1 is null.", monitor.getID());
+ }
+
+ public void testGetName_1_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 1).getMonitor();
+ assertNotNull("JavaMonitor.getName() for 1-0-1 is null.", monitor
+ .getName());
+ }
+
+ public void testCountWaiters_1_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ Object next = waiters.next();
+ count++;
+ }
+
+ assertEquals("Waiters count incorrect :" + count, 1, count);
+ }
+
+ public void testCountEnters_1_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ Object next = enters.next();
+ count++;
+ }
+
+ assertEquals("Enters count incorrect :" + count, 0, count);
+ }
+
+ public void testCountOwners_1_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertNotNull("Owner count incorrect", owner);
+ }
+
+ public void testNameWaiters_1_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ JavaThread next = (JavaThread) waiters.next();
+ assertEquals("Waiters name incorrect", generatePrefix(1, 0, 1)
+ + "-" + monitorNotifyThreadName + "-" + count, next
+ .getName());
+ count++;
+ }
+ }
+
+ public void testNameEnters_1_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ JavaThread next = (JavaThread) enters.next();
+ assertEquals("Enters name incorrect", generatePrefix(1, 0, 1) + "-"
+ + monitorEnterThreadName + "-" + count, next.getName());
+ count++;
+ }
+ }
+
+ public void testNameOwners_1_0_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(1, 0, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertEquals("Owner name incorrect", generatePrefix(1, 0, 1) + "-"
+ + monitorOwnerThreadName + "-0", owner.getName());
+ }
+ ///////////////////////////////////////////
+
+ public void testFindMonitor_0_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 2, 1).getMonitor();
+
+ assertNotNull("Couldn't find monitor 0-2-1", monitor);
+ }
+
+ public void testGetID_0_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 2, 1).getMonitor();
+
+ assertNotNull("JavaMonitor.getID() for 0-2-1 is null.", monitor.getID());
+ }
+
+ public void testGetName_0_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 2, 1).getMonitor();
+ assertNotNull("JavaMonitor.getName() for 0-2-1 is null.", monitor
+ .getName());
+ }
+
+ public void testCountWaiters_0_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 2, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ Object next = waiters.next();
+ count++;
+ }
+
+ assertEquals("Waiters count incorrect :" + count, 0, count);
+ }
+
+ public void testCountEnters_0_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 2, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ Object next = enters.next();
+ count++;
+ }
+
+ assertEquals("Enters count incorrect :" + count, 2, count);
+ }
+
+ public void testCountOwners_0_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 2, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertNotNull("Owner count incorrect", owner);
+ }
+
+ public void testNameWaiters_0_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 2, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ JavaThread next = (JavaThread) waiters.next();
+ assertEquals("Waiters name incorrect", generatePrefix(0, 2, 1)
+ + "-" + monitorNotifyThreadName + "-" + count, next
+ .getName());
+ count++;
+ }
+ }
+
+ public void testNameEnters_0_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 2, 1).getMonitor();
+ Iterator enters = monitor.getEnterWaiters();
+
+ String error = checkNames(enters, generatePrefix(0, 2, 1) + "-"
+ + monitorEnterThreadName, 2);
+
+ assertNull(error, error);
+ }
+
+ public void testNameOwners_0_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(0, 2, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertEquals("Owner name incorrect", generatePrefix(0, 2, 1) + "-"
+ + monitorOwnerThreadName + "-0", owner.getName());
+ }
+
+ ///////////////////////////////////////////
+
+ public void testFindMonitor_2_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 2, 1).getMonitor();
+
+ assertNotNull("Couldn't find monitor 2-2-1", monitor);
+ }
+
+ public void testGetID_2_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 2, 1).getMonitor();
+
+ assertNotNull("JavaMonitor.getID() for 2-2-1 is null.", monitor.getID());
+ }
+
+ public void testGetName_2_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 2, 1).getMonitor();
+ assertNotNull("JavaMonitor.getName() for 2-2-1 is null.", monitor
+ .getName());
+ }
+
+ public void testCountWaiters_2_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 2, 1).getMonitor();
+ int count = 0;
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ while (waiters.hasNext()) {
+ Object next = waiters.next();
+ count++;
+ }
+
+ assertEquals("Waiters count incorrect :" + count, 2, count);
+ }
+
+ public void testCountEnters_2_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 2, 1).getMonitor();
+ int count = 0;
+ Iterator enters = monitor.getEnterWaiters();
+
+ while (enters.hasNext()) {
+ Object next = enters.next();
+ count++;
+ }
+
+ assertEquals("Enters count incorrect :" + count, 2, count);
+ }
+
+ public void testCountOwners_2_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 2, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertNotNull("Owner count incorrect", owner);
+ }
+
+ public void testNameWaiters_2_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 2, 1).getMonitor();
+ Iterator waiters = monitor.getNotifyWaiters();
+
+ String error = checkNames(waiters, generatePrefix(2, 2, 1)
+ + "-" + monitorNotifyThreadName , 2);
+ assertNull(error, error);
+ }
+
+ public void testNameEnters_2_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 2, 1).getMonitor();
+ Iterator enters = monitor.getEnterWaiters();
+
+ String error = checkNames(enters, generatePrefix(2, 2, 1) + "-"
+ + monitorEnterThreadName, 2);
+
+ assertNull(error, error);
+ }
+
+ public void testNameOwners_2_2_1() throws Exception {
+ JavaMonitor monitor = findMonitorThreadConfig(2, 2, 1).getMonitor();
+ JavaThread owner = monitor.getOwner();
+
+ assertEquals("Owner name incorrect", generatePrefix(2, 2, 1) + "-"
+ + monitorOwnerThreadName + "-0", owner.getName());
+ }
+
+ ////////////////////////////////////
+
+ /**
+ * Check uniqueness of all the monitor ID's.
+ *
+ */
+ public void testGetID() throws Exception {
+ Set foundSet = new HashSet(NUM_MONITORS);
+
+ for (int i=0; i < NUM_MONITORS; i++){
+ ImagePointer id = monitorConfigs[i].monitor.getID();
+
+ assertNotNull("Found null monitor id for " + monitorConfigs[i].id, id);
+
+ assertFalse("Found duplicate monitor id. " + monitorConfigs[i].id, foundSet.contains(id));
+
+ foundSet.add(id);
+ }
+ }
+
+ ////////////////////////////////////
+
+ public void testGetName() throws Exception {
+ for (int i=0; i < NUM_MONITORS; i++){
+ String name = monitorConfigs[i].monitor.getName();
+
+ assertNotNull("Found null monitor name for " + monitorConfigs[i].id, name);
+ }
+ }
+
+ public void testGetObject() throws Exception {
+ for (int i=0; i < NUM_MONITORS; i++){
+ JavaObject object = monitorConfigs[i].monitor.getObject();
+
+ assertEquals("Monitor object class unexpected name.",monitorClassName, object.getJavaClass().getName());
+
+ JavaField idField = getJavaField(object, "id");
+ String idString = idField.getString(object);
+
+ assertEquals("MonitorClass id strings didn't match.", monitorConfigs[i].id, idString);
+ }
+ }
+
+ public void testEqualsSelf() throws Exception {
+ assertTrue(monitorConfigs[0].monitor.equals(monitorConfigs[0].monitor));
+ }
+
+ public void testEqualsNull() throws Exception {
+ assertFalse(monitorConfigs[0].monitor.equals(null));
+ }
+
+ public void testNotEqualsOther() throws Exception {
+ assertFalse(monitorConfigs[0].monitor.equals(monitorConfigs[1].monitor));
+ }
+
+ /**
+ * Checks that the expected JavaThreads are within the iterator
+ * of waiters passed, in no particular order.
+ * Number of JavaThreads should be checked elsewhere.
+ *
+ * @param waiters a set of a monitor's waiters.
+ * @param prefix start of the thread name
+ * @param count number of threads to look for. Used as suffix of thread name
+ * @return null if ok or an error string if it is not ok
+ * @throws CorruptDataException
+ *
+ */
+ public String checkNames(Iterator waiters, String prefix, int count) throws CorruptDataException {
+ Set foundSet = new HashSet(count);
+
+ while (waiters.hasNext()) {
+ Object next = waiters.next();
+
+ if (next instanceof JavaThread) {
+ JavaThread thread = (JavaThread) next;
+ foundSet.add(thread.getName());
+ }
+ }
+
+ for(int i=0; i < count; i++) {
+ String threadName = prefix+"-"+i;
+
+ if (!foundSet.contains(threadName)) {
+ return "Missing thread "+threadName;
+ }
+ }
+
+ return null;
+ }
+
+
+}