< prev index next >

test/jdk/java/lang/ClassLoader/Assert.java

Print this page

  1 /*
  2  * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 4290640 4785473


 27  * @build package1.Class1 package2.Class2 package1.package3.Class3 Assert
 28  * @run main/othervm Assert
 29  * @summary Test the assertion facility
 30  * @author Mike McCloskey
 31  * @key randomness
 32  */
 33 

 34 import package1.*;
 35 import package2.*;
 36 import package1.package3.*;
 37 import java.io.*;


 38 import java.util.Random;
 39 


 40 public class Assert {
 41 
 42     private static Class1 testClass1;
 43     private static Class2 testClass2;
 44     private static Class3 testClass3;
 45     private static final  boolean debug = false;
 46     private static Random generator = new Random();
 47 
 48     /**
 49      * The first invocation of this test starts a loop which exhaustively tests
 50      * the object tree with all of its different settings.
 51      * There are 7 test objects in a tree that has 7 different places to set
 52      * assertions untouched/on/off so this tests 3^7 or 2187 different
 53      * configurations.
 54      *
 55      * This test spawns a new VM for each run because assertions are set on or
 56      * off at class load time. Once the class is loaded its assertion status
 57      * does not change.
 58      */
 59     public static void main(String[] args) throws Exception {
 60 
 61         // Switch values: 0=don't touch, 1=off, 2 = on
 62         int[] switches = new int[7];
 63 
 64         int switchSource = 0;
 65         if (args.length == 0) { // This is the controller
 66 
 67             // This code is for an exhaustive test
 68             //while(switchSource < 2187) {
 69             //    int temp = switchSource++;
 70 
 71             // This code is for a weaker but faster test
 72             for(int x=0; x<100; x++) {
 73                 int temp = generator.nextInt(2187);
 74                 for(int i=0; i<7; i++) {
 75                     switches[i] = temp % 3;
 76                     temp = temp / 3;
 77                 }
 78 
 79                 // Spawn new VM and load classes
 80                 String command = System.getProperty("java.home") +
 81                     File.separator + "bin" + File.separator + "java Assert";
 82 
 83                 StringBuffer commandString = new StringBuffer(command);
 84                 for(int j=0; j<7; j++)
 85                     commandString.append(" "+switches[j]);
 86 
 87                 Process p = null;
 88                 p = Runtime.getRuntime().exec(commandString.toString());
 89 
 90                 if (debug) { // See output of test VMs
 91                     BufferedReader blah = new BufferedReader(
 92                                           new InputStreamReader(p.getInputStream()));
 93                     String outString = blah.readLine();
 94                     while (outString != null) {
 95                         System.out.println("from BufferedReader:"+outString);
 96                         outString = blah.readLine();
 97                     }
 98                 }
 99 
100                 p.waitFor();
101                 int result = p.exitValue();
102                 if (debug) { // See which switch configs failed
103                     if (result == 0) {
104                         for(int k=6; k>=0; k--)
105                             System.out.print(switches[k]);
106                         System.out.println();
107                     } else {
108                         System.out.print("Nonzero Exit: ");
109                         for(int k=6; k>=0; k--)
110                             System.out.print(switches[k]);
111                         System.out.println();
112                     }
113                 } else {
114                     if (result != 0) {
115                         System.err.print("Nonzero Exit: ");
116                         for(int k=6; k>=0; k--)
117                             System.err.print(switches[k]);
118                         System.err.println();
119                         throw new RuntimeException("Assertion test failure.");
120                     }
121                 }

  1 /*
  2  * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 4290640 4785473
 27  * @requires vm.flagless
 28  * @library /test/lib
 29  * @build package1.Class1 package2.Class2 package1.package3.Class3 Assert
 30  * @run main/othervm Assert
 31  * @summary Test the assertion facility
 32  * @author Mike McCloskey
 33  * @key randomness
 34  */
 35 
 36 import jdk.test.lib.process.OutputAnalyzer;
 37 import package1.*;
 38 import package2.*;
 39 import package1.package3.*;
 40 
 41 import java.util.ArrayList;
 42 import java.util.List;
 43 import java.util.Random;
 44 
 45 import static jdk.test.lib.process.ProcessTools.*;
 46 
 47 public class Assert {
 48 
 49     private static Class1 testClass1;
 50     private static Class2 testClass2;
 51     private static Class3 testClass3;
 52     private static final  boolean debug = false;
 53     private static Random generator = new Random();
 54 
 55     /**
 56      * The first invocation of this test starts a loop which exhaustively tests
 57      * the object tree with all of its different settings.
 58      * There are 7 test objects in a tree that has 7 different places to set
 59      * assertions untouched/on/off so this tests 3^7 or 2187 different
 60      * configurations.
 61      *
 62      * This test spawns a new VM for each run because assertions are set on or
 63      * off at class load time. Once the class is loaded its assertion status
 64      * does not change.
 65      */
 66     public static void main(String[] args) throws Throwable {
 67 
 68         // Switch values: 0=don't touch, 1=off, 2 = on
 69         int[] switches = new int[7];
 70 
 71         int switchSource = 0;
 72         if (args.length == 0) { // This is the controller
 73 
 74             // This code is for an exhaustive test
 75             //while(switchSource < 2187) {
 76             //    int temp = switchSource++;
 77 
 78             // This code is for a weaker but faster test
 79             for(int x=0; x<100; x++) {
 80                 int temp = generator.nextInt(2187);
 81                 for(int i=0; i<7; i++) {
 82                     switches[i] = temp % 3;
 83                     temp = temp / 3;
 84                 }
 85 
 86                 // Spawn new VM and load classes
 87                 List<String> commands = new ArrayList<>();
 88                 commands.add("Assert");


 89                 for(int j=0; j<7; j++)
 90                     commands.add(Integer.toString(switches[j]));
 91                 OutputAnalyzer outputAnalyzer = executeCommand(createLimitedTestJavaProcessBuilder(commands));



 92                 if (debug) { // See output of test VMs
 93                     outputAnalyzer.asLines()
 94                                   .stream()
 95                                   .forEach(s -> System.out.println(s));




 96                 }
 97                 int result = outputAnalyzer.getExitValue();


 98                 if (debug) { // See which switch configs failed
 99                     if (result == 0) {
100                         for(int k=6; k>=0; k--)
101                             System.out.print(switches[k]);
102                         System.out.println();
103                     } else {
104                         System.out.print("Nonzero Exit: ");
105                         for(int k=6; k>=0; k--)
106                             System.out.print(switches[k]);
107                         System.out.println();
108                     }
109                 } else {
110                     if (result != 0) {
111                         System.err.print("Nonzero Exit: ");
112                         for(int k=6; k>=0; k--)
113                             System.err.print(switches[k]);
114                         System.err.println();
115                         throw new RuntimeException("Assertion test failure.");
116                     }
117                 }
< prev index next >