Home | History | Annotate | Download | only in Events
      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  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.jpda.tests.jdwp.Events;
     19 
     20 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     21 import org.apache.harmony.jpda.tests.share.SyncDebuggee;
     22 
     23 /**
     24  * Debuggee for JDWP unit tests for CombinedExceptionEventsTest.
     25  */
     26 public class CombinedExceptionEventsDebuggee extends SyncDebuggee {
     27     public static final String TEST_CAUGHT_EXCEPTION_SIGNAL = "CAUGHT";
     28     public static final String TEST_UNCAUGHT_EXCEPTION_SIGNAL = "UNCAUGHT";
     29 
     30     public static class SubDebuggeeException extends DebuggeeException {
     31         public SubDebuggeeException(String msg) {
     32             super(msg);
     33         }
     34     }
     35 
     36     public static void main(String[] args) {
     37         runDebuggee(CombinedExceptionEventsDebuggee.class);
     38     }
     39 
     40     public void run() {
     41         logWriter.println("-> CombinedExceptionEventsDebuggee: Starting...");
     42 
     43         // Preload exception classes
     44         new SubDebuggeeException("dummy");
     45 
     46         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     47 
     48         String signalMessage = synchronizer.receiveMessage();
     49         if (signalMessage.equals(TEST_CAUGHT_EXCEPTION_SIGNAL)) {
     50             testCaughtException();
     51         } else {
     52             testUncaughtException();
     53         }
     54 
     55         logWriter.println("-> CombinedExceptionEventsDebuggee: Finishing...");
     56     }
     57 
     58     private void testCaughtException() {
     59         Thread t = new Thread(new Runnable() {
     60             public void run() {
     61                 // Catch a different exception to test catch location is
     62                 // properly reported.
     63                 try {
     64                     throwDebuggeeException("Caught debuggee exception");
     65                 } catch (DebuggeeException e) {
     66                     // Expected exception
     67                 }
     68             }
     69         });
     70         t.start();
     71 
     72         try {
     73             t.join();
     74         } catch (InterruptedException e) {
     75             logWriter.printError(e);
     76         }
     77     }
     78 
     79     private void testUncaughtException() {
     80         Thread t = new Thread(new Runnable() {
     81             public void run() {
     82                 // Catch a different exception to test catch location is
     83                 // properly reported.
     84                 try {
     85                     throwDebuggeeException("Uncaught debuggee exception");
     86                 } catch (NullPointerException e) {
     87                     // Unexpect exception
     88                     logWriter.printError("Unexpected exception", e);
     89                 }
     90             }
     91         });
     92         t.start();
     93 
     94         try {
     95             t.join();
     96         } catch (InterruptedException e) {
     97             logWriter.printError(e);
     98         }
     99     }
    100 
    101     private static void throwDebuggeeException(String msg) {
    102         throw new SubDebuggeeException(msg);
    103     }
    104 }
    105