1 /*
   2  * Copyright (c) 2018, 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 import java.io.File;
  26 import java.lang.module.Configuration;
  27 import java.lang.module.ModuleDescriptor;
  28 import java.util.Set;
  29 import sun.hotspot.WhiteBox;
  30 
  31 //
  32 // Test archived system module graph when open archive heap objects are mapped:
  33 //
  34 public class CheckArchivedModuleApp {
  35     static WhiteBox wb;
  36     public static void main(String args[]) throws Exception {
  37         wb = WhiteBox.getWhiteBox();
  38 
  39         if (!wb.areOpenArchiveHeapObjectsMapped()) {
  40             System.out.println("Archived open_archive_heap objects are not mapped.");
  41             System.out.println("This may happen during normal operation. Test Skipped.");
  42             return;
  43         }
  44 
  45         boolean expectArchivedDescriptors = "yes".equals(args[0]);
  46         boolean expectArchivedConfiguration = "yes".equals(args[1]);
  47         checkModuleDescriptors(expectArchivedDescriptors);
  48         checkConfiguration(expectArchivedConfiguration);
  49     }
  50 
  51     private static void checkModuleDescriptors(boolean expectArchivedDescriptors) {
  52         Set<Module> modules = ModuleLayer.boot().modules();
  53         for (Module m : modules) {
  54             ModuleDescriptor md = m.getDescriptor();
  55             String name = md.name();
  56             if (expectArchivedDescriptors) {
  57                 if (wb.isShared(md)) {
  58                     System.out.println(name + " is archived. Expected.");
  59                 } else {
  60                     throw new RuntimeException(
  61                         "FAILED. " + name + " is not archived. Expect archived.");
  62                 }
  63             } else {
  64                 if (!wb.isShared(md)) {
  65                     System.out.println(name + " is not archived. Expected.");
  66                 } else {
  67                     throw new RuntimeException(
  68                         "FAILED. " + name + " is archived. Expect not archived.");
  69                 }
  70             }
  71         }
  72     }
  73 
  74     private static void checkConfiguration(boolean expectArchivedConfiguration) {
  75         Configuration cf = ModuleLayer.boot().configuration();
  76         if (expectArchivedConfiguration) {
  77             if (wb.isShared(cf)) {
  78                 System.out.println("Boot layer configuration is archived. Expected.");
  79             } else {
  80                 throw new RuntimeException(
  81                     "FAILED. Boot layer configuration is not archived.");
  82             }
  83         } else {
  84             if (!wb.isShared(cf)) {
  85                 System.out.println("Boot layer configuration is not archived. Expected.");
  86             } else {
  87                 throw new RuntimeException(
  88                     "FAILED. Boot layer configuration is archived.");
  89             }
  90         }
  91     }
  92 }