001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.drlvm.tests.regression.h3894;
019:
020: import junit.framework.TestCase;
021: import java.util.HashSet;
022:
023: /**
024: * Test case for GetThreadGroupChildren() jvmti function.
025: * Checks that the function may report more than 10 child theads.
026: * Checks that the function reports the same thread group children that were
027: * added by java application.
028: * Checks that not started threads are not reported.
029: * Checks that grand children are not reported.
030: */
031: public class ThreadGroupChildren extends TestCase {
032:
033: static final int CHILD_THREADS = 15;
034: static final int NOT_STARTED_CHILD_THREADS = 2;
035: static final int CHILD_GROUPS = 15;
036: static final int EMPTY_CHILD_GROUPS = 2;
037: static final int GRAND_CHILD_THREADS = 2;
038: static final int GRAND_CHILD_GROUPS = 2;
039:
040: public static void main(String args[]) {
041: (new ThreadGroupChildren()).test();
042: }
043:
044: public void test() {
045: // this collection should hold references to created Thead and
046: // ThreadGroup objects to avoid their elimination by garbage collector
047: HashSet<Object> set = new HashSet<Object>();
048:
049: ThreadGroup testGroup = new ThreadGroup("test group");
050:
051: Thread[] childThreads = new Thread[CHILD_THREADS];
052:
053: for (int i = 0; i < CHILD_THREADS; i++) {
054: Thread thread = new TestThread(testGroup, "child thread "
055: + i);
056: childThreads[i] = thread;
057: set.add(thread);
058: thread.start();
059: }
060:
061: for (int i = 0; i < NOT_STARTED_CHILD_THREADS; i++) {
062: Thread thread = new TestThread(testGroup,
063: "not started child thread " + i);
064: set.add(thread);
065: }
066:
067: ThreadGroup[] childGroups = new ThreadGroup[CHILD_GROUPS
068: + EMPTY_CHILD_GROUPS];
069: int childGroupsIndex = 0;
070:
071: for (int i = 0; i < EMPTY_CHILD_GROUPS; i++) {
072: ThreadGroup group = new ThreadGroup(testGroup,
073: "empty child group " + i);
074: childGroups[childGroupsIndex++] = group;
075: set.add(group);
076: }
077:
078: for (int i = 0; i < CHILD_GROUPS; i++) {
079: ThreadGroup group = new ThreadGroup(testGroup,
080: "child group " + i);
081: childGroups[childGroupsIndex++] = group;
082: set.add(group);
083:
084: for (int j = 0; j < GRAND_CHILD_THREADS; j++) {
085: Thread thread = new TestThread(group,
086: "grand child thread " + j);
087: set.add(thread);
088: thread.start();
089: }
090:
091: for (int j = 0; j < GRAND_CHILD_GROUPS; j++) {
092: ThreadGroup subGroup = new ThreadGroup(group,
093: "empty grand child group " + j);
094: set.add(subGroup);
095: }
096: }
097:
098: try {
099: System.err.println("[Java]: Throwing an exception");
100: // pass execution to the agent
101: throw new InvokeAgentException(testGroup, childThreads,
102: childGroups);
103: } catch (Exception e) {
104: System.err.println("[Java]: Exception caught");
105: }
106:
107: synchronized (TestThread.class) {
108: try {
109: TestThread.class.notifyAll();
110: } catch (Throwable exc) {
111: exc.printStackTrace();
112: }
113: }
114:
115: System.err.println("[Java]: test done");
116: assertTrue(Status.status);
117: }
118: }
119:
120: class TestThread extends Thread {
121:
122: public TestThread(ThreadGroup group, String name) {
123: super (group, name);
124: }
125:
126: public void run() {
127: System.err.println("Thread started: " + getName());
128:
129: synchronized (TestThread.class) {
130: try {
131: TestThread.class.wait();
132: } catch (Throwable exc) {
133: exc.printStackTrace();
134: }
135: }
136:
137: System.err.println("Thread finished: " + getName());
138: }
139: }
140:
141: class InvokeAgentException extends Exception {
142:
143: ThreadGroup testGroup;
144: Thread[] childThreads;
145: ThreadGroup[] childGroups;
146:
147: InvokeAgentException(ThreadGroup group, Thread[] threads,
148: ThreadGroup[] groups) {
149: testGroup = group;
150: childThreads = threads;
151: childGroups = groups;
152: }
153: }
154:
155: class Status {
156: /** the field should be modified by jvmti agent to determine test result. */
157: public static boolean status = false;
158: }
|