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 package org.apache.harmony.jpda.tests.jdwp.EventModifiers;
20 
21 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
22 import org.apache.harmony.jpda.tests.share.SyncDebuggee;
23 
24 /**
25  * Debuggee for ThreadOnlyModifierTest.
26  */
27 public class ThreadOnlyModifierDebuggee extends SyncDebuggee {
28     static class TestException extends Exception {
29     }
30 
31     static class TestClass {
eventTestMethod()32         public void eventTestMethod() {
33             System.out.println(
34                     "ThreadOnlyModifierDebuggee.TestClass.eventTestMethod()");
35         }
36     }
37 
38     static class TestThread implements Runnable {
39         private final TestClass obj;
40 
TestThread(TestClass obj)41         public TestThread(TestClass obj) {
42             this.obj = obj;
43         }
44 
45         @Override
run()46         public void run() {
47             obj.eventTestMethod();
48             throwAndCatchException();
49             readAndWriteField();
50         }
51 
readAndWriteField()52         void readAndWriteField() {
53             System.out
54             .println(
55                     "ThreadOnlyModifierDebuggee.TestThread.readAndWriteField()");
56             watchedField = watchedField + 1;
57         }
58 
throwAndCatchException()59         private void throwAndCatchException() {
60             try {
61                 throwException();
62             } catch (TestException e) {
63                 // This should trigger a caught exception event here.
64             }
65         }
66 
throwException()67         void throwException() throws TestException {
68             System.out.println(
69                     "ThreadOnlyModifierDebuggee.TestThread.throwException()");
70             throw new TestException();
71         }
72     }
73 
74     static Thread THREAD_ONLY;
75 
76     static int watchedField = 0;
77 
78     @Override
run()79     public void run() {
80         TestClass obj = new TestClass();
81         new TestException();  // force class loading.
82 
83         logWriter.println("Create threads");
84         Thread[] threads = new Thread[10];
85         for (int i = 0; i < threads.length; ++i) {
86             threads[i] = new Thread(new TestThread(obj));
87             threads[i].setName("TestThread#" + i);
88         }
89 
90         // Let's report events for the last thread only.
91         THREAD_ONLY = threads[threads.length - 1];
92 
93         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
94 
95         logWriter.println("ThreadOnlyModifierDebuggee started");
96 
97         // Wait for test setup.
98         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
99 
100         // Run all threads.
101         for (int i = 0; i < threads.length; ++i) {
102             runThread(threads[i]);
103         }
104 
105         logWriter.println("ThreadOnlyModifierDebuggee finished");
106     }
107 
runThread(Thread t)108     void runThread(Thread t) {
109         String threadName = t.getName();
110         logWriter.println("Thread " + threadName + " starts");
111         t.start();
112         try {
113             logWriter.println("Wait for end of thread " + threadName);
114             t.join();
115         } catch (InterruptedException e) {
116             e.printStackTrace();
117         }
118         logWriter.println("Thread " + threadName + " ends");
119     }
120 
main(String[] args)121     public static void main(String[] args) {
122         runDebuggee(ThreadOnlyModifierDebuggee.class);
123     }
124 
125 }
126