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 */
26package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
27
28import org.apache.harmony.jpda.tests.framework.DebuggeeSynchronizer;
29import org.apache.harmony.jpda.tests.framework.LogWriter;
30import org.apache.harmony.jpda.tests.framework.TestOptions;
31import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
32import org.apache.harmony.jpda.tests.share.SyncDebuggee;
33
34
35/**
36 * The class specifies debuggee for <code>org.apache.harmony.jpda.tests.jdwp.ThreadReference.StopTest</code>.
37 * This debuggee starts the tested thread <code>TESTED_THREAD</code> which waits for
38 * 'Stop' command with NullPointerException exception.
39 */
40public class StopDebuggee extends SyncDebuggee {
41
42    static Object waitTimeObject = new Object();
43    static void waitMlsecsTime(long mlsecsTime) {
44        synchronized(waitTimeObject) {
45            try {
46                waitTimeObject.wait(mlsecsTime);
47            } catch (Throwable throwable) {
48                 // ignore
49            }
50        }
51    }
52
53    static Object waitTimeObjectWithException = new Object();
54    static void waitMlsecsTimeWithException(long mlsecsTime) throws Throwable {
55        synchronized(waitTimeObject) {
56            try {
57                waitTimeObject.wait(mlsecsTime);
58            } catch (Throwable throwable) {
59                 throw throwable;
60            }
61        }
62    }
63
64    public static String testStatus = "PASSED";
65    public static final String TESTED_THREAD = "TestedThread";
66    public static final String FIELD_NAME = "exception";
67//    public static final String EXCEPTION_SIGNATURE = "Ljava/lang/NullPointerException;";
68    public static NullPointerException exception = new NullPointerException();
69
70    static Object waitForStart = new Object();
71
72    public void run() {
73        logWriter.println("StopDebuggee: started");
74        DebuggeeThread thrd = new DebuggeeThread(TESTED_THREAD,
75                logWriter, synchronizer);
76
77        synchronized(waitForStart){
78            thrd.start();
79            try {
80                waitForStart.wait();
81            } catch (InterruptedException e) {
82                logWriter.println("StopDebuggee:" + e + " is caught while waitForStart.wait()");
83            }
84        }
85
86        logWriter.println("StopDebuggee: Wait for TestedThread to finish...");
87        while ( thrd.isAlive() ) {
88            waitMlsecsTime(1000);
89        }
90        logWriter.println("StopDebuggee: TestedThread finished");
91
92        synchronizer.sendMessage(testStatus);
93        logWriter.println("StopDebuggee: finishing...");
94    }
95
96    class DebuggeeThread extends Thread {
97
98        LogWriter logWriter;
99        DebuggeeSynchronizer synchronizer;
100
101        public DebuggeeThread(String name, LogWriter logWriter,
102                DebuggeeSynchronizer synchronizer) {
103            super(name);
104            this.logWriter = logWriter;
105            this.synchronizer = synchronizer;
106        }
107
108        public void run() {
109            logWriter.println(getName() +  ": started");
110            synchronized(waitForStart){
111                waitForStart.notifyAll();
112            }
113
114            logWriter.println(getName() +  ": Wait for 'Stop' command with NullPointerException...");
115            try {
116                synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
117                waitMlsecsTimeWithException(TestOptions.DEFAULT_TIMEOUT);
118                logWriter.println(getName() +  ": FAILED: TIMEOUT is run out - No any exception is caught");
119                testStatus = "FAILED";
120            } catch (Throwable thrown) {
121                logWriter.println(getName() +  ": Exception is caught: " + thrown);
122                if ( thrown.equals(exception) ) {
123                    logWriter.println(getName() +  ": PASSED: It is expected Exception");
124                } else {
125                    logWriter.println(getName() +  ": FAILED: It is unexpected Exception");
126                    testStatus = "FAILED";
127                }
128            }
129        }
130    }
131
132    public static void main(String [] args) {
133        runDebuggee(StopDebuggee.class);
134    }
135}
136