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
19/**
20 * @author Vitaly A. Provodin
21 */
22
23/**
24 * Created on 18.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
27
28import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
31import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
33import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
34
35
36/**
37 * JDWP Unit test for ThreadReference.Interrupt command.
38 */
39public class InterruptTest extends JDWPSyncTestCase {
40
41    static String waitForString = "java.lang.InterruptedException";
42    boolean isReceived = false;
43
44    protected String getDebuggeeClassName() {
45        return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.InterruptDebuggee";
46    }
47
48    /**
49     * This testcase exercises ThreadReference.Interrupt command.
50     * <BR>At first the test starts InterruptDebuggee which runs
51     * the tested thread 'TESTED_THREAD' and blocks it in invocation of
52     * the 'wait()' method.
53     * <BR> Then the tests performs the ThreadReference.Interrupt command
54     * for tested thread.
55     * <BR>After sending Interrupt command, the test waits signals via synchronization
56     * channel that 'InterruptedException' was thrown.
57     */
58    public void testInterrupt001() {
59        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
60
61        // getting ID of the tested thread
62        logWriter.println("get thread ID");
63        long threadID =
64            debuggeeWrapper.vmMirror.getThreadID(InterruptDebuggee.TESTED_THREAD);
65
66        // getting the thread group ID
67        CommandPacket packet = new CommandPacket(
68                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
69                JDWPCommands.ThreadReferenceCommandSet.InterruptCommand);
70        packet.setNextValueAsThreadID(threadID);
71        logWriter.println("send \"Interrupt\" command");
72
73        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
74
75        short err = reply.getErrorCode();
76        logWriter.println("error = " + err);
77
78        if (err == JDWPConstants.Error.NONE) {
79            Thread thrd = new RecvThread();
80            thrd.start();
81            try {
82                thrd.join(settings.getTimeout());
83            } catch(InterruptedException e) {
84
85            }
86        }
87
88        if (!isReceived) {
89            printErrorAndFail(waitForString + " is not received");
90        } else {
91            logWriter.println(waitForString + " is received");
92        }
93        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
94    }
95
96    class RecvThread extends Thread {
97
98        public void run() {
99            logWriter.println("wait for " + waitForString);
100            isReceived = synchronizer.receiveMessage(waitForString);
101        }
102
103    }
104}
105