1 /*
   2  * Copyright (c) 2010, 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 package nsk.sysdict.share;
  25 
  26 import java.lang.reflect.Field;
  27 import nsk.share.Failure;
  28 import nsk.share.TestFailure;
  29 import nsk.share.sysdict.ClassLoadersChain;
  30 import nsk.share.test.Tests;
  31 
  32 /**
  33  * This is a main class for all chain tests.
  34  */
  35 public class ChainTest extends SysDictTest {
  36 
  37     public ChainTest(String[] args) {
  38         parseArgs(args);
  39     }
  40 
  41     public static void main(String args[]) {
  42         Tests.runTest(new ChainTest(args), args);
  43     }
  44     private static final int NxM_FACTOR = 450;
  45     private int classesHeight;
  46     private int loadersHeight;
  47     String[] classNames;
  48 
  49     @Override
  50     protected void parseArgs(String args[]) {
  51         super.parseArgs(args);
  52         boolean isLoaderHeightDefault = true;
  53         boolean isHeightDefault = true;
  54         for (int i = 0; i < args.length; i++) {
  55             if (args[i].equals("-classloaderHeight")) {
  56                 loadersHeight = Integer.parseInt(args[i + 1]);
  57                 if (loadersHeight > 0) {
  58                     isLoaderHeightDefault = false;
  59                 }
  60             }
  61             if (args[i].equals("-height")) {
  62                 classesHeight = Integer.parseInt(args[i + 1]);
  63                 if (classesHeight > 0) {
  64                     isHeightDefault = false;
  65                 }
  66             }
  67         }
  68 
  69         Class info;
  70         try {
  71             if (useFats) {
  72                 info = createJarLoader().loadClass(PACKAGE_PREFIX + "FatsInfo");
  73             } else {
  74                 info = createJarLoader().loadClass(PACKAGE_PREFIX + "LeansInfo");
  75             }
  76             System.out.println("name=" + info.getName());
  77             for (Field field : info.getDeclaredFields()) {
  78                 System.err.println("field = " + field.getName());
  79             }
  80             if (isHeightDefault) {
  81                 classesHeight = info.getField("HEIGHT").getInt(null);
  82             }
  83 
  84 
  85             if (isLoaderHeightDefault) {
  86                 loadersHeight = NxM_FACTOR / classesHeight;
  87             }
  88 
  89             if (loadersHeight * classesHeight != NxM_FACTOR) {
  90                 throw new Failure("classes height must divide " + NxM_FACTOR);
  91             }
  92 
  93             if (loadersHeight == 0) {
  94                 throw new Failure("loaders height should be positive");
  95             }
  96             if (classesHeight == 0) {
  97                 throw new Failure("classes height should be positive");
  98             }
  99             classNames = new String[classesHeight];
 100             for (int i = 0; i < classesHeight; i++) {
 101                 classNames[i] = PACKAGE_PREFIX + info.getField("rootName").get(null)
 102                         + ((String[]) info.getField("nodeNames").get(null))[i];
 103             }
 104             if (classNames == null || classNames.length == 0) {
 105                 throw new TestFailure("No classes names for loading");
 106             }
 107             System.out.println("The classHeight = " + classesHeight + " the loadersHeight = " + loadersHeight);
 108         } catch (Exception e) {
 109             throw new TestFailure("Can't initialize test correctly", e);
 110         }
 111 
 112     }
 113 
 114     @Override
 115     String[] getClassNames() {
 116         return classNames;
 117     }
 118 
 119     @Override
 120     ClassLoader[] createLoaders() {
 121         ClassLoader loaders[] = new ClassLoader[loadersHeight];
 122         ClassLoader jarLoader = createJarLoader();
 123         ClassLoadersChain loadersChain =
 124                 new ClassLoadersChain(jarLoader, loadersHeight);
 125         // direct ordering: root loader first
 126         for (int i = 0; i < loadersHeight; i++) {
 127             loaders[i] = loadersChain.getLoader(loadersHeight - 1 - i);
 128         }
 129         return loaders;
 130     }
 131 }