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 22.02.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
27 
28 import org.apache.harmony.jpda.tests.framework.DebuggeeSynchronizer;
29 import org.apache.harmony.jpda.tests.framework.LogWriter;
30 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
31 import org.apache.harmony.jpda.tests.share.SyncDebuggee;
32 
33 
34 /**
35  * The class specifies debuggee for <code>org.apache.harmony.jpda.tests.jdwp.ThreadReference.InterruptTest</code>.
36  * This debuggee starts the tested thread <code>TESTED_THREAD</code> and blocks it
37  * in  an invocation of the <code>wait()</code> method.
38  * If it receives an <code>InterruptedException</code>, it notifies debugger via
39  * the synchronization channel.
40  */
41 public class InterruptDebuggee extends SyncDebuggee {
42 
43     public static final String TESTED_THREAD = "TestedThread";
44 
45     static Object waitForStart = new Object();
46     static Object waitForInterrupt = new Object();
47     static Object waitForFinish = new Object();
48 
run()49     public void run() {
50         DebuggeeThread thrd = new DebuggeeThread(TESTED_THREAD,
51                 logWriter, synchronizer);
52 
53         synchronized(waitForStart){
54             thrd.start();
55             try {
56                 waitForStart.wait();
57             } catch (InterruptedException e) {
58 
59             }
60         }
61 
62         synchronized(waitForFinish){
63             logWriter.println("thread is finished");
64         }
65     }
66 
67     class DebuggeeThread extends Thread {
68 
69         LogWriter logWriter;
70         DebuggeeSynchronizer synchronizer;
71 
DebuggeeThread(String name, LogWriter logWriter, DebuggeeSynchronizer synchronizer)72         public DebuggeeThread(String name, LogWriter logWriter,
73                 DebuggeeSynchronizer synchronizer) {
74             super(name);
75             this.logWriter = logWriter;
76             this.synchronizer = synchronizer;
77         }
78 
run()79         public void run() {
80 
81             synchronized(InterruptDebuggee.waitForFinish){
82 
83                 synchronized(InterruptDebuggee.waitForStart){
84 
85                     InterruptDebuggee.waitForStart.notifyAll();
86                 }
87 
88                 synchronized(InterruptDebuggee.waitForInterrupt){
89 
90                     logWriter.println(getName() +  ": started");
91                     synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
92 
93                     try {
94                         InterruptDebuggee.waitForInterrupt.wait();
95                     } catch (InterruptedException e) {
96                         logWriter.println("Expected " + e);
97                         synchronizer.sendMessage(e.toString());
98                     }
99 
100                     logWriter.println(getName() +  ": wait for SGNL_CONTINUE");
101                     synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
102                     logWriter.println(getName() +  ": finished");
103                 }
104             }
105         }
106     }
107 
main(String [] args)108     public static void main(String [] args) {
109         runDebuggee(InterruptDebuggee.class);
110     }
111 }
112