tests/libjava-mauve/src/gnu/testlet/java/lang/Thread/stop.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.0
       
     2 
       
     3 // Copyright (C) 2003 Free Software Foundation, Inc.
       
     4 // Written by C. Brian Jones (cbj@gnu.org)
       
     5 
       
     6 // This file is part of Mauve.
       
     7 
       
     8 // Mauve is free software; you can redistribute it and/or modify
       
     9 // it under the terms of the GNU General Public License as published by
       
    10 // the Free Software Foundation; either version 2, or (at your option)
       
    11 // any later version.
       
    12 
       
    13 // Mauve is distributed in the hope that it will be useful,
       
    14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16 // GNU General Public License for more details.
       
    17 
       
    18 // You should have received a copy of the GNU General Public License
       
    19 // along with Mauve; see the file COPYING.  If not, write to
       
    20 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    21 // Boston, MA 02111-1307, USA.  */
       
    22 
       
    23 package gnu.testlet.java.lang.Thread;
       
    24 
       
    25 import gnu.testlet.Testlet;
       
    26 import gnu.testlet.TestHarness;
       
    27 
       
    28 public class stop extends Thread implements Testlet
       
    29 {
       
    30   static TestHarness harness;
       
    31   // accessed outside synchronized block by multiple threads
       
    32   static volatile boolean death = false;
       
    33 
       
    34   static Object lock = new Object();
       
    35   static boolean running = false;
       
    36 
       
    37   public void run()
       
    38   {
       
    39     try
       
    40       {
       
    41 	synchronized(lock)
       
    42 	  {
       
    43 	    running = true;
       
    44 	    lock.notifyAll();
       
    45 	    while (true)
       
    46 	      lock.wait();
       
    47 	  }
       
    48       }
       
    49     catch (Exception e)
       
    50       {
       
    51 	harness.fail("Unexpected exception during run()");
       
    52       }
       
    53     catch (ThreadDeath d)
       
    54       {
       
    55 	death = true;
       
    56 	Thread thread = Thread.currentThread();
       
    57 	ThreadGroup group = thread.getThreadGroup();
       
    58 	harness.check(group != null, "Stop should not remove thread from ThreadGroup");
       
    59 	throw d;
       
    60       }
       
    61   }
       
    62 
       
    63   public void test (TestHarness h)
       
    64   {
       
    65     harness = h;
       
    66     int initial_thread_count = 0;
       
    67     int running_thread_count = 0;
       
    68     int stopped_thread_count = 0;
       
    69     Thread[] thread_list = null;
       
    70 
       
    71     try
       
    72       {
       
    73 	int x = 0;
       
    74 	Thread current = Thread.currentThread();
       
    75 	ThreadGroup group = current.getThreadGroup();
       
    76 	x = group.activeCount() + 10;
       
    77 	thread_list = new Thread[x];
       
    78 	initial_thread_count = group.enumerate(thread_list, true);
       
    79 
       
    80 	stop t = new stop();
       
    81 	ThreadGroup tgroup = t.getThreadGroup();
       
    82 	harness.check (tgroup != null, "Unstarted thread has non-null thread group");
       
    83 	t.start();
       
    84 	synchronized(lock)
       
    85 	  {
       
    86 	    while (!running) 
       
    87 	      {
       
    88 		lock.wait();
       
    89 	      }
       
    90 	    x = group.activeCount() + 10;
       
    91 	    thread_list = new Thread[x];
       
    92 	    running_thread_count = group.enumerate(thread_list, true);
       
    93 	    tgroup = t.getThreadGroup();
       
    94 	    harness.check(tgroup != null, "Running thread has non-null thread group");
       
    95 	  }
       
    96 	t.stop();
       
    97 	t.join(2000, 0);
       
    98 	x = group.activeCount() + 10;
       
    99 	thread_list = new Thread[x];
       
   100 	stopped_thread_count = group.enumerate(thread_list, true);
       
   101 
       
   102 	harness.check(death, "ThreadDeath properly thrown and caught");
       
   103 	harness.check(initial_thread_count == stopped_thread_count, 
       
   104 		      "Initial thread count matches stopped thread count");
       
   105 	harness.check(running_thread_count-1 == initial_thread_count,
       
   106 		      "Running thread properly increased thread count");
       
   107 
       
   108 	tgroup = t.getThreadGroup();
       
   109 	harness.check(tgroup == null, "Stopped thread has null thread group");
       
   110 
       
   111       }
       
   112     catch (InterruptedException e) 
       
   113       {
       
   114 	harness.fail("Thread not joined - Thread.stop() unimplemented?");
       
   115       }
       
   116     catch (Exception e)
       
   117       {
       
   118         harness.debug(e);
       
   119 	harness.fail("Unexpected exception during test()");
       
   120       }
       
   121   }
       
   122 }
       
   123