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     @SuppressWarnings("DeadException")
     41     @Override
     42     public void run() {
     43         logWriter.println("-> CombinedExceptionEventsDebuggee: Starting...");
     44 
     45         // Preload exception classes
     46         new SubDebuggeeException("dummy");
     47 
     48         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     49 
     50         String signalMessage = synchronizer.receiveMessage();
     51         if (signalMessage.equals(TEST_CAUGHT_EXCEPTION_SIGNAL)) {
     52             testCaughtException();
     53         } else {
     54             testUncaughtException();
     55         }
     56 
     57         logWriter.println("-> CombinedExceptionEventsDebuggee: Finishing...");
     58     }
     59 
     60     private void testCaughtException() {
     61         Thread t = new Thread(new Runnable() {
     62             @Override
     63             public void run() {
     64                 // Catch a different exception to test catch location is
     65                 // properly reported.
     66                 try {
     67                     throwDebuggeeException("Caught debuggee exception");
     68                 } catch (DebuggeeException e) {
     69                     // Expected exception
     70                 }
     71             }
     72         });
     73         t.start();
     74 
     75         try {
     76             t.join();
     77         } catch (InterruptedException e) {
     78             logWriter.printError(e);
     79         }
     80     }
     81 
     82     private void testUncaughtException() {
     83         Thread t = new Thread(new Runnable() {
     84             @Override
     85             public void run() {
     86                 // Catch a different exception to test catch location is
     87                 // properly reported.
     88                 try {
     89                     throwDebuggeeException("Uncaught debuggee exception");
     90                 } catch (NullPointerException e) {
     91                     // Unexpect exception
     92                     logWriter.printError("Unexpected exception", e);
     93                 }
     94             }
     95         });
     96         t.start();
     97 
     98         try {
     99             t.join();
    100         } catch (InterruptedException e) {
    101             logWriter.printError(e);
    102         }
    103     }
    104 
    105     private static void throwDebuggeeException(String msg) {
    106         throw new SubDebuggeeException(msg);
    107     }
    108 }
    109