MonitorWaitTest.java revision 5f0a23683aa603d8c50b6dd071a565821b76067b
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 */
18
19package org.apache.harmony.jpda.tests.jdwp.Events;
20
21import org.apache.harmony.jpda.tests.framework.TestErrorException;
22import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
23import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
24import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
25import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent.Event_MONITOR_WAIT;
26import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
27import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
28
29public class MonitorWaitTest extends JDWPSyncTestCase {
30    String monitorSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/MonitorWaitMockMonitor;";
31
32    protected String getDebuggeeClassName() {
33        return "org.apache.harmony.jpda.tests.jdwp.Events.MonitorWaitAndWaitedDebuggee";
34    }
35
36    public void testMonitorWaitForClassOnly() {
37        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
38
39        // Attain Object class id
40        long monitorRefTypeID = getClassIDBySignature(monitorSignature);
41        logWriter.println("==> Object ReferenceType ID = " + monitorRefTypeID);
42
43        // Set MonitorWait request for MockMonitorClass
44        debuggeeWrapper.vmMirror.setMonitorWaitForClassOnly(monitorRefTypeID);
45
46        // Verify received event
47        verifyEvent();
48    }
49
50    public void testMonitorWaitForClassMatchExact() {
51        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
52
53        String classPattern = "org.apache.harmony.jpda.tests.jdwp.Events.MonitorWaitMockMonitor";
54        logWriter.println("==> tested class match pattern: " + classPattern);
55
56        // Set MonitorWait request for MockMonitorClass
57        debuggeeWrapper.vmMirror.setMonitorWaitForClassMatch(classPattern);
58
59        // Verify received event
60        verifyEvent();
61    }
62
63    public void testMonitorWaitForClassMatchFirst() {
64        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
65
66        String classPattern = "org.apache.harmony.jpda.tests.jdwp.Events.*";
67        logWriter.println("==> tested class match pattern: " + classPattern);
68
69        // Set MonitorWait request for MockMonitorClass
70        debuggeeWrapper.vmMirror.setMonitorWaitForClassMatch(classPattern);
71
72        // Verify received event
73        verifyEvent();
74    }
75
76    public void testMonitorWaitForClassMatchSecond() {
77        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
78
79        String classPattern = "*MonitorWaitMockMonitor";
80        logWriter.println("==> tested class match pattern: " + classPattern);
81
82        // Set MonitorWait request for MockMonitorClass
83        debuggeeWrapper.vmMirror.setMonitorWaitForClassMatch(classPattern);
84
85        // Verify received event
86        verifyEvent();
87    }
88
89    public void testMonitorWaitForClassExclude() {
90        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
91
92        String classPattern = "MockPatter*";
93        logWriter.println("==> tested class exclude pattern: " + classPattern);
94
95        // Set MonitorWait request for MockMonitorClass
96        debuggeeWrapper.vmMirror.setMonitorWaitForClassExclude(classPattern);
97
98        // Verify received event
99        verifyEvent();
100    }
101
102    private void verifyEvent(){
103        // Inform debuggee that the request has been set
104        logWriter.println("==> Request has been set.");
105        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
106
107        // Receive event of MonitorWait
108        logWriter.println("==> Receive Event.");
109        CommandPacket receiveEvent = null;
110        try {
111            receiveEvent = debuggeeWrapper.vmMirror.receiveEvent();
112        } catch (TestErrorException e) {
113            printErrorAndFail("There is no event received.");
114        }
115        ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(receiveEvent);
116        Event_MONITOR_WAIT event = (ParsedEvent.Event_MONITOR_WAIT)parsedEvents[0];
117
118        // Check event basic message
119        assertEquals("Invalid number of events,", 1, parsedEvents.length);
120        assertEquals("Invalid event kind,", JDWPConstants.EventKind.MONITOR_WAIT, parsedEvents[0].getEventKind()
121                , JDWPConstants.EventKind.getName(JDWPConstants.EventKind.MONITOR_WAIT)
122                , JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
123        logWriter.println("==> CHECK: Event Kind: " + JDWPConstants.EventKind.getName(JDWPConstants.EventKind.MONITOR_WAIT));
124
125        // Check tested thread message
126        logWriter.println("==> Get testedThreadID...");
127        long testedThreadID = debuggeeWrapper.vmMirror.getThreadID("main");
128        assertEquals("Invalid tested thread id: ", testedThreadID, event.getThreadID());
129        logWriter.println("==> CHECK: tested blocked thread id: " + testedThreadID );
130
131        // Check the ReferenceType of monitor object
132        long objID = event.getTaggedObject().objectID;
133        long refID = debuggeeWrapper.vmMirror.getReferenceType(objID);
134        String actualSignature =  debuggeeWrapper.vmMirror.getReferenceTypeSignature(refID);
135        assertEquals("Invalid monitor class signature: ", monitorSignature
136                , actualSignature);
137        logWriter.println("==> CHECK: monitor class signature: " + actualSignature);
138
139        // Check wait's timeout
140        assertEquals("Invalid reference type of prepared class,", MonitorWaitAndWaitedDebuggee.TIMEOUT
141                , ((ParsedEvent.Event_MONITOR_WAIT)parsedEvents[0]).getTimeout());
142        logWriter.println("==> CHECK: Wait time out: " + MonitorWaitAndWaitedDebuggee.TIMEOUT);
143    }
144
145}
146