< prev index next >

test/jdk/java/lang/Runtime/shutdown/Basic.java

Print this page




  32  */
  33 
  34 import java.lang.reflect.Method;
  35 import java.lang.reflect.InvocationTargetException;
  36 import java.io.PrintStream;
  37 import java.io.FileOutputStream;
  38 import java.awt.Frame;
  39 import java.awt.TextArea;
  40 import java.awt.event.WindowEvent;
  41 import java.awt.event.WindowAdapter;
  42 
  43 import org.testng.annotations.DataProvider;
  44 import org.testng.annotations.Test;
  45 import jdk.test.lib.process.ProcessTools;
  46 
  47 public class Basic {
  48 
  49     static Runtime rt = Runtime.getRuntime();
  50     static PrintStream out = System.out;
  51 

  52     @DataProvider(name = "testcase")
  53     public Object[][] getTestCase() {
  54         return new Object[][] {
  55             { "fallThrough", 0, "h1", "f1" },
  56             { "exit0",       0, "h1", "f1" },
  57             { "exit0NoHook", 0, "",   "f1" },
  58             { "exit1",       1, "h1", "" },
  59             { "exit1NoHook", 1, "",   "" },
  60             { "halt",        0, "",   "" },
  61             { "haltNoHook",  0, "",   "" },
  62             { "haltInHook",  0, "h1", "" },
  63             { "addLate",     0, "h1",
  64                 "Caught as expected: java.lang.IllegalStateException: Shutdown in progress" },
  65             { "removeLate",  0, "h2",
  66                 "Caught as expected: java.lang.IllegalStateException: Shutdown in progress" }
  67         };
  68     }
  69 
  70     @Test(dataProvider = "testcase")
  71     public void test(String testcase, int exitValue, String hook, String finalizer)
  72             throws Exception {
  73         System.out.println("Test " + testcase);
  74         ProcessTools.executeTestJava("Basic", testcase)
  75                 .shouldHaveExitValue(exitValue)
  76                 .stdoutShouldMatch(
  77                     hook + (hook.isEmpty() ? "" : System.lineSeparator()) + finalizer);


  95 
  96 
  97     public static class Hook extends Thread {
  98         String name;
  99 
 100         public Hook(String name) {
 101             this.name = name;
 102         }
 103 
 104         public void go() { }
 105 
 106         public void run() {
 107             out.println(name);
 108             go();
 109         }
 110     }
 111 
 112     public static void fallThrough() throws Exception {
 113         rt.addShutdownHook(new Hook("h1"));
 114         Fin f = new Fin("f1");
 115         rt.runFinalizersOnExit(true);
 116     }
 117 
 118     public static void exit0() throws Exception {
 119         rt.addShutdownHook(new Hook("h1"));
 120         Fin f = new Fin("f1");
 121         rt.runFinalizersOnExit(true);
 122         rt.exit(0);
 123     }
 124 
 125     public static void exit0NoHook() throws Exception {
 126         Fin f = new Fin("f1");
 127         rt.runFinalizersOnExit(true);
 128         rt.exit(0);
 129     }
 130 
 131     /* Finalizer should not run */
 132     public static void exit1() throws Exception {
 133         rt.addShutdownHook(new Hook("h1"));
 134         Fin f = new Fin("f1");
 135         rt.runFinalizersOnExit(true);
 136         rt.exit(1);
 137     }
 138 
 139     public static void exit1NoHook() throws Exception {
 140         Fin f = new Fin("f1");
 141         rt.runFinalizersOnExit(true);
 142         rt.exit(1);
 143     }
 144 
 145     public static void halt() throws Exception {
 146         rt.addShutdownHook(new Hook("h1") {
 147             public void go() { rt.halt(1); }});
 148         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};
 149         rt.runFinalizersOnExit(true);
 150         rt.halt(0);
 151     }
 152 
 153     public static void haltNoHook() throws Exception {
 154         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};
 155         rt.runFinalizersOnExit(true);
 156         rt.halt(0);
 157     }
 158 
 159     public static void haltInHook() throws Exception {
 160         rt.addShutdownHook(new Hook("h1") {
 161             public void go() { rt.halt(0); }});
 162         Fin f = new Fin("f1");
 163         rt.runFinalizersOnExit(true);
 164         rt.exit(1);
 165     }
 166 
 167     public static void addLate() throws Exception {
 168         rt.addShutdownHook(new Hook("h1") {
 169             public void go() {
 170                 try {
 171                     rt.addShutdownHook(new Hook("h2"));
 172                 } catch (IllegalStateException x) {
 173                     out.println("Caught as expected: " + x);
 174                     rt.halt(0);
 175                 }
 176             }});
 177         rt.exit(1);
 178     }
 179 
 180     public static void removeLate() throws Exception {
 181         final Hook h1 = new Hook("h1");
 182         rt.addShutdownHook(new Hook("h2") {
 183             public void go() {
 184                 try {
 185                     rt.removeShutdownHook(h1);
 186                 } catch (IllegalStateException x) {
 187                     out.println("Caught as expected: " + x);
 188                     rt.halt(0);
 189                 }
 190             }});
 191         rt.exit(1);
 192     }
 193 
 194 
 195     /* The following two methods are provided for manual testing only.
 196      * Neither test is suitable for being run from within the harness.
 197      */
 198 
 199     public static void awt() throws Exception {
 200         final Frame f = new Frame();
 201         final TextArea ta = new TextArea();
 202         Fin fx = new Fin("f1");
 203         rt.runFinalizersOnExit(true);
 204         rt.addShutdownHook(new Hook("h1") {
 205             public void go() {
 206                 ta.setText("Hooked!");
 207                 out.println("Hooked!");
 208                 try {
 209                     Thread.sleep(1000);
 210                 } catch (InterruptedException x) { }
 211                 ta.append("\nSleep 1");
 212                 out.println("Sleep 1");
 213                 try {
 214                     Thread.sleep(1000);
 215                 } catch (InterruptedException x) { }
 216                 ta.append("\nSleep 2");
 217                 out.println("Sleep 2");
 218             }});
 219         f.addWindowListener(new WindowAdapter() {
 220             public void windowClosing(WindowEvent e) {
 221                 out.println("Closing...");
 222                 ta.setText("Closing...");
 223                 try {
 224                     Thread.sleep(1000);
 225                 } catch (InterruptedException x) { }
 226                 f.dispose();
 227                 rt.exit(42);
 228             }});
 229         ta.setText("Terminate me!");
 230         f.add(ta);
 231         f.pack();
 232         f.show();
 233         Thread.sleep(10000);
 234     }
 235 
 236     /* For INT, HUP, TERM */
 237     public static void stall() throws Exception {
 238         Fin f = new Fin("f1");
 239         rt.runFinalizersOnExit(true);
 240         rt.addShutdownHook(new Hook("h1"));
 241         out.print("Type ^C now: ");
 242         out.flush();
 243         Thread.sleep(100000);
 244     }
 245 
 246 
 247     public static void main(String[] args) throws Throwable {
 248         Method m = Basic.class.getMethod(args[0], new Class[] { });
 249         String log = null;
 250         try {
 251             log = System.getProperty("log");
 252         } catch (SecurityException x) { }
 253         if (log != null) {
 254             out = new PrintStream(new FileOutputStream(log), true);
 255         }
 256         try {
 257             m.invoke(null, new Object[] { });
 258         } catch (InvocationTargetException x) {
 259             throw x.getTargetException();


  32  */
  33 
  34 import java.lang.reflect.Method;
  35 import java.lang.reflect.InvocationTargetException;
  36 import java.io.PrintStream;
  37 import java.io.FileOutputStream;
  38 import java.awt.Frame;
  39 import java.awt.TextArea;
  40 import java.awt.event.WindowEvent;
  41 import java.awt.event.WindowAdapter;
  42 
  43 import org.testng.annotations.DataProvider;
  44 import org.testng.annotations.Test;
  45 import jdk.test.lib.process.ProcessTools;
  46 
  47 public class Basic {
  48 
  49     static Runtime rt = Runtime.getRuntime();
  50     static PrintStream out = System.out;
  51 
  52     // Expect that no finalizer is invoked at exit
  53     @DataProvider(name = "testcase")
  54     public Object[][] getTestCase() {
  55         return new Object[][] {
  56             { "fallThrough", 0, "h1", "" },
  57             { "exit0",       0, "h1", "" },
  58             { "exit0NoHook", 0, "",   "" },
  59             { "exit1",       1, "h1", "" },
  60             { "exit1NoHook", 1, "",   "" },
  61             { "halt",        0, "",   "" },
  62             { "haltNoHook",  0, "",   "" },
  63             { "haltInHook",  0, "h1", "" },
  64             { "addLate",     0, "h1",
  65                 "Caught as expected: java.lang.IllegalStateException: Shutdown in progress" },
  66             { "removeLate",  0, "h2",
  67                 "Caught as expected: java.lang.IllegalStateException: Shutdown in progress" }
  68         };
  69     }
  70 
  71     @Test(dataProvider = "testcase")
  72     public void test(String testcase, int exitValue, String hook, String finalizer)
  73             throws Exception {
  74         System.out.println("Test " + testcase);
  75         ProcessTools.executeTestJava("Basic", testcase)
  76                 .shouldHaveExitValue(exitValue)
  77                 .stdoutShouldMatch(
  78                     hook + (hook.isEmpty() ? "" : System.lineSeparator()) + finalizer);


  96 
  97 
  98     public static class Hook extends Thread {
  99         String name;
 100 
 101         public Hook(String name) {
 102             this.name = name;
 103         }
 104 
 105         public void go() { }
 106 
 107         public void run() {
 108             out.println(name);
 109             go();
 110         }
 111     }
 112 
 113     public static void fallThrough() throws Exception {
 114         rt.addShutdownHook(new Hook("h1"));
 115         Fin f = new Fin("f1");

 116     }
 117 
 118     public static void exit0() throws Exception {
 119         rt.addShutdownHook(new Hook("h1"));
 120         Fin f = new Fin("f1");

 121         rt.exit(0);
 122     }
 123 
 124     public static void exit0NoHook() throws Exception {
 125         Fin f = new Fin("f1");

 126         rt.exit(0);
 127     }
 128 
 129     /* Finalizer should not run */
 130     public static void exit1() throws Exception {
 131         rt.addShutdownHook(new Hook("h1"));
 132         Fin f = new Fin("f1");

 133         rt.exit(1);
 134     }
 135 
 136     public static void exit1NoHook() throws Exception {
 137         Fin f = new Fin("f1");

 138         rt.exit(1);
 139     }
 140 
 141     public static void halt() throws Exception {
 142         rt.addShutdownHook(new Hook("h1") {
 143             public void go() { rt.halt(1); }});
 144         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};

 145         rt.halt(0);
 146     }
 147 
 148     public static void haltNoHook() throws Exception {
 149         Fin f = new Fin("f1") { public void go() { rt.halt(1); }};

 150         rt.halt(0);
 151     }
 152 
 153     public static void haltInHook() throws Exception {
 154         rt.addShutdownHook(new Hook("h1") {
 155             public void go() { rt.halt(0); }});
 156         Fin f = new Fin("f1");

 157         rt.exit(1);
 158     }
 159 
 160     public static void addLate() throws Exception {
 161         rt.addShutdownHook(new Hook("h1") {
 162             public void go() {
 163                 try {
 164                     rt.addShutdownHook(new Hook("h2"));
 165                 } catch (IllegalStateException x) {
 166                     out.println("Caught as expected: " + x);
 167                     rt.halt(0);
 168                 }
 169             }});
 170         rt.exit(1);
 171     }
 172 
 173     public static void removeLate() throws Exception {
 174         final Hook h1 = new Hook("h1");
 175         rt.addShutdownHook(new Hook("h2") {
 176             public void go() {
 177                 try {
 178                     rt.removeShutdownHook(h1);
 179                 } catch (IllegalStateException x) {
 180                     out.println("Caught as expected: " + x);
 181                     rt.halt(0);
 182                 }
 183             }});
 184         rt.exit(1);
 185     }
 186 










































 187     /* For INT, HUP, TERM */
 188     public static void stall() throws Exception {
 189         Fin f = new Fin("f1");

 190         rt.addShutdownHook(new Hook("h1"));
 191         out.print("Type ^C now: ");
 192         out.flush();
 193         Thread.sleep(100000);
 194     }
 195 
 196 
 197     public static void main(String[] args) throws Throwable {
 198         Method m = Basic.class.getMethod(args[0], new Class[] { });
 199         String log = null;
 200         try {
 201             log = System.getProperty("log");
 202         } catch (SecurityException x) { }
 203         if (log != null) {
 204             out = new PrintStream(new FileOutputStream(log), true);
 205         }
 206         try {
 207             m.invoke(null, new Object[] { });
 208         } catch (InvocationTargetException x) {
 209             throw x.getTargetException();
< prev index next >