1 /*
   2  * Copyright (c) 2019, 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 import sun.hotspot.WhiteBox;
  25 
  26 /**
  27  * This is a generic test app for testing if classes are loaded from the CDS archive
  28  * or not (without having to parse -Xlog:class+load, or writing your own WhiteBox apps).
  29  * Usage:
  30  *     [1] Create an archive with WhiteBox enabled.
  31  *     [2] Run this app with arguments such as
  32  *             "assertShared:java.lang.Object"
  33  *             "assertNotShared:NotSharedClassName"
  34  *
  35  * We can probably add other kinds of simple tests as well ....
  36  *
  37  * FIXME: enhance WB API to check if a particular archive has failed. So you can say
  38  *        assertShared:0,java.lang.Object
  39  * to assert that java.lang.Object is shared from archive #0 (i.e., base archive).
  40  */
  41 public class GenericTestApp {
  42     private static final WhiteBox wb = WhiteBox.getWhiteBox();
  43 
  44     public static void main(String args[]) throws Exception {
  45         System.out.println("GenericTestApp started. WhiteBox = " + wb);
  46         System.out.println("cdsMemoryMappingFailed() = " + cdsMemoryMappingFailed());
  47 
  48         for (String s : args) {
  49             Class c;
  50             if ((c = getClass(s, "assertShared:")) != null) {
  51                 assertShared(c);
  52             }
  53             else if ((c = getClass(s, "assertNotShared:")) != null) {
  54                 assertNotShared(c);
  55             }
  56             else {
  57                 throw new RuntimeException("Unknown option: " + s);
  58             }
  59             System.out.println("passed: " + s);
  60         }
  61     }
  62 
  63     private static Class getClass(String s, String prefix) throws Exception {
  64         if (s.startsWith(prefix)) {
  65             return Class.forName(s.substring(prefix.length()));
  66         } else {
  67             return null;
  68         }
  69     }
  70 
  71     private static boolean cdsMemoryMappingFailed() {
  72         return wb.cdsMemoryMappingFailed();
  73     }
  74 
  75     private static void assertShared(Class klass) {
  76         if (!cdsMemoryMappingFailed()) {
  77             if (!wb.isSharedClass(klass)) {
  78                 throw new RuntimeException("Class should be shared but is not: " + klass);
  79             }
  80         } else {
  81             // FIXME -- need to throw jtreg.SkippedException
  82             System.out.println("Cannot test for wb.isSharedClass(" + klass + ") because CDS mapping has failed");
  83         }
  84     }
  85 
  86     private static void assertNotShared(Class klass) {
  87         if (wb.isSharedClass(klass)) {
  88             throw new RuntimeException("Class should be shared but is not: " + klass);
  89         }
  90     }
  91 }