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.framework.TestOptions; 31 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer; 32 import 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 */ 40 public 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 @Override 73 public void run() { 74 logWriter.println("StopDebuggee: started"); 75 DebuggeeThread thrd = new DebuggeeThread(TESTED_THREAD, 76 logWriter, synchronizer); 77 78 synchronized(waitForStart){ 79 thrd.start(); 80 try { 81 waitForStart.wait(); 82 } catch (InterruptedException e) { 83 logWriter.println("StopDebuggee:" + e + " is caught while waitForStart.wait()"); 84 } 85 } 86 87 logWriter.println("StopDebuggee: Wait for TestedThread to finish..."); 88 while ( thrd.isAlive() ) { 89 waitMlsecsTime(1000); 90 } 91 logWriter.println("StopDebuggee: TestedThread finished"); 92 93 synchronizer.sendMessage(testStatus); 94 logWriter.println("StopDebuggee: finishing..."); 95 } 96 97 static class DebuggeeThread extends Thread { 98 99 LogWriter logWriter; 100 DebuggeeSynchronizer synchronizer; 101 102 public DebuggeeThread(String name, LogWriter logWriter, 103 DebuggeeSynchronizer synchronizer) { 104 super(name); 105 this.logWriter = logWriter; 106 this.synchronizer = synchronizer; 107 } 108 109 @Override 110 public void run() { 111 logWriter.println(getName() + ": started"); 112 synchronized(waitForStart){ 113 waitForStart.notifyAll(); 114 } 115 116 logWriter.println(getName() + ": Wait for 'Stop' command with NullPointerException..."); 117 try { 118 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY); 119 waitMlsecsTimeWithException(TestOptions.DEFAULT_TIMEOUT); 120 logWriter.println(getName() + ": FAILED: TIMEOUT is run out - No any exception is caught"); 121 testStatus = "FAILED"; 122 } catch (Throwable thrown) { 123 logWriter.println(getName() + ": Exception is caught: " + thrown); 124 if ( thrown.equals(exception) ) { 125 logWriter.println(getName() + ": PASSED: It is expected Exception"); 126 } else { 127 logWriter.println(getName() + ": FAILED: It is unexpected Exception"); 128 testStatus = "FAILED"; 129 } 130 } 131 } 132 } 133 134 public static void main(String [] args) { 135 runDebuggee(StopDebuggee.class); 136 } 137 } 138