15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed to the Apache Software Foundation (ASF) under one or more
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * contributor license agreements.  See the NOTICE file distributed with
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * this work for additional information regarding copyright ownership.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The ASF licenses this file to You under the Apache License, Version 2.0
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (the "License"); you may not use this file except in compliance with
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the License.  You may obtain a copy of the License at
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) *
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *     http://www.apache.org/licenses/LICENSE-2.0
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *  Unless required by applicable law or agreed to in writing, software
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) *  distributed under the License is distributed on an "AS IS" BASIS,
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *  See the License for the specific language governing permissions and
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *  limitations under the License.
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) */
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)package org.apache.harmony.jpda.tests.jdwp.EventModifiers;
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import org.apache.harmony.jpda.tests.share.SyncDebuggee;
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/**
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * Debuggee for ThreadOnlyModifierTest.
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)public class ThreadOnlyModifierDebuggee extends SyncDebuggee {
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    static class TestException extends Exception {
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static class TestClass {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        public void eventTestMethod() {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            System.out.println(
34                    "ThreadOnlyModifierDebuggee.TestClass.eventTestMethod()");
35        }
36    }
37
38    static class TestThread implements Runnable {
39        private final TestClass obj;
40
41        public TestThread(TestClass obj) {
42            this.obj = obj;
43        }
44
45        @Override
46        public void run() {
47            obj.eventTestMethod();
48            throwAndCatchException();
49            readAndWriteField();
50        }
51
52        void readAndWriteField() {
53            System.out
54            .println(
55                    "ThreadOnlyModifierDebuggee.TestThread.readAndWriteField()");
56            watchedField = watchedField + 1;
57        }
58
59        private void throwAndCatchException() {
60            try {
61                throwException();
62            } catch (TestException e) {
63                // This should trigger a caught exception event here.
64            }
65        }
66
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
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
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
121    public static void main(String[] args) {
122        runDebuggee(ThreadOnlyModifierDebuggee.class);
123    }
124
125}
126