jdk/test/sun/tools/jinfo/JInfoLauncherTest.java
changeset 38360 fb63be22ffa6
parent 38359 fd9b36598481
child 38361 8ea2d56bfdf3
equal deleted inserted replaced
38359:fd9b36598481 38360:fb63be22ffa6
     1 /*
       
     2  * Copyright (c) 2014, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 import org.testng.annotations.Test;
       
    27 import org.testng.annotations.BeforeClass;
       
    28 import sun.tools.jinfo.JInfo;
       
    29 
       
    30 import java.lang.reflect.Constructor;
       
    31 import java.lang.reflect.Field;
       
    32 import java.util.Arrays;
       
    33 
       
    34 import static org.testng.Assert.*;
       
    35 
       
    36 /**
       
    37  * @test
       
    38  * @bug 8039080
       
    39  * @modules jdk.jcmd/sun.tools.jinfo
       
    40  * @run testng JInfoLauncherTest
       
    41  * @summary Test JInfo launcher argument parsing
       
    42  */
       
    43 @Test
       
    44 public class JInfoLauncherTest {
       
    45     public static final String VALIDATION_EXCEPTION_CLSNAME =
       
    46                                 IllegalArgumentException.class.getName();
       
    47 
       
    48     private Constructor<JInfo> jInfoConstructor;
       
    49     private Field fldUseSA;
       
    50 
       
    51     @BeforeClass
       
    52     public void setup() throws Exception {
       
    53         jInfoConstructor = JInfo.class.getDeclaredConstructor(String[].class);
       
    54         jInfoConstructor.setAccessible(true);
       
    55         fldUseSA = JInfo.class.getDeclaredField("useSA");
       
    56         fldUseSA.setAccessible(true);
       
    57     }
       
    58 
       
    59     private JInfo newJInfo(String[] args) throws Exception {
       
    60         try {
       
    61             return jInfoConstructor.newInstance((Object) args);
       
    62         } catch (Exception e) {
       
    63             if (isValidationException(e.getCause())) {
       
    64                 throw (Exception)e.getCause();
       
    65             }
       
    66             throw e;
       
    67         }
       
    68     }
       
    69 
       
    70     private boolean getUseSA(JInfo jinfo) throws Exception {
       
    71         return fldUseSA.getBoolean(jinfo);
       
    72     }
       
    73 
       
    74     private void cmdPID(String cmd, String ... params) throws Exception {
       
    75         int offset = (cmd != null ? 1 : 0);
       
    76         String[] args = new String[offset + params.length];
       
    77         args[0] = cmd;
       
    78         System.arraycopy(params, 0, args, offset, params.length);
       
    79         JInfo j = newJInfo(args);
       
    80         assertFalse(getUseSA(j), "Local jinfo must not forward to SA");
       
    81     }
       
    82 
       
    83     private void cmdCore(String cmd, String ... params) throws Exception {
       
    84         int offset = (cmd != null ? 1 : 0);
       
    85         String[] args = new String[offset + params.length];
       
    86         args[0] = cmd;
       
    87         System.arraycopy(params, 0, args, offset, params.length);
       
    88         JInfo j = newJInfo(args);
       
    89         assertTrue(getUseSA(j), "Core jinfo must forward to SA");
       
    90     }
       
    91 
       
    92     private void cmdRemote(String cmd, String ... params) throws Exception {
       
    93         int offset = (cmd != null ? 1 : 0);
       
    94         String[] args = new String[offset + params.length];
       
    95         args[0] = cmd;
       
    96         System.arraycopy(params, 0, args, offset, params.length);
       
    97         JInfo j = newJInfo(args);
       
    98         assertTrue(getUseSA(j), "Remote jinfo must forward to SA");
       
    99     }
       
   100 
       
   101     private void cmdExtraArgs(String cmd, int argsLen) throws Exception {
       
   102         String[] args = new String[argsLen + 1 + (cmd != null ? 1 : 0)];
       
   103         Arrays.fill(args, "a");
       
   104         if (cmd != null) {
       
   105             args[0] = cmd;
       
   106         } else {
       
   107             cmd = "default";
       
   108         }
       
   109         try {
       
   110             JInfo j = newJInfo(args);
       
   111             fail("\"" + cmd + "\" does not support more than " + argsLen +
       
   112                  " arguments");
       
   113         } catch (Exception e) {
       
   114             if (!isValidationException(e)) {
       
   115                 throw e;
       
   116             }
       
   117             // ignore
       
   118         }
       
   119     }
       
   120 
       
   121     private void cmdMissingArgs(String cmd, int reqArgs) throws Exception {
       
   122         String[] args = new String[reqArgs - 1 + (cmd != null ? 1 : 0)];
       
   123         Arrays.fill(args, "a");
       
   124         if (cmd != null) {
       
   125             args[0] = cmd;
       
   126         } else {
       
   127             cmd = "default";
       
   128         }
       
   129         try {
       
   130             JInfo j = newJInfo(args);
       
   131             fail("\"" + cmd + "\" requires at least " + reqArgs + " argument");
       
   132         } catch (Exception e) {
       
   133             if (!isValidationException(e)) {
       
   134                 throw e;
       
   135             }
       
   136             // ignore
       
   137         }
       
   138     }
       
   139 
       
   140     public void testDefaultPID() throws Exception {
       
   141         cmdPID(null, "1234");
       
   142     }
       
   143 
       
   144     public void testFlagsPID() throws Exception {
       
   145         cmdPID("-flags", "1234");
       
   146     }
       
   147 
       
   148     public void testSyspropsPID() throws Exception {
       
   149         cmdPID("-sysprops", "1234");
       
   150     }
       
   151 
       
   152     public void testReadFlagPID() throws Exception {
       
   153         cmdPID("-flag", "SomeManagementFlag", "1234");
       
   154     }
       
   155 
       
   156     public void testSetFlag1PID() throws Exception {
       
   157         cmdPID("-flag", "+SomeManagementFlag", "1234");
       
   158     }
       
   159 
       
   160     public void testSetFlag2PID() throws Exception {
       
   161         cmdPID("-flag", "-SomeManagementFlag", "1234");
       
   162     }
       
   163 
       
   164     public void testSetFlag3PID() throws Exception {
       
   165         cmdPID("-flag", "SomeManagementFlag=314", "1234");
       
   166     }
       
   167 
       
   168     public void testDefaultCore() throws Exception {
       
   169         cmdCore(null, "myapp.exe", "my.core");
       
   170     }
       
   171 
       
   172     public void testFlagsCore() throws Exception {
       
   173         cmdCore("-flags", "myapp.exe", "my.core");
       
   174     }
       
   175 
       
   176     public void testSyspropsCore() throws Exception {
       
   177         cmdCore("-sysprops", "myapp.exe", "my.core");
       
   178     }
       
   179 
       
   180     public void testReadFlagCore() throws Exception {
       
   181         try {
       
   182             cmdCore("-flag", "SomeManagementFlag", "myapp.exe", "my.core");
       
   183             fail("Flags can not be read from core files");
       
   184         } catch (Exception e) {
       
   185             if (!isValidationException(e)) {
       
   186                 throw e;
       
   187             }
       
   188             // ignore
       
   189         }
       
   190     }
       
   191 
       
   192     public void testSetFlag1Core() throws Exception {
       
   193         try {
       
   194             cmdCore("-flag", "+SomeManagementFlag", "myapp.exe", "my.core");
       
   195             fail("Flags can not be set in core files");
       
   196         } catch (Exception e) {
       
   197             if (!isValidationException(e)) {
       
   198                 throw e;
       
   199             }
       
   200             // ignore
       
   201         }
       
   202     }
       
   203 
       
   204     public void testSetFlag2Core() throws Exception {
       
   205         try {
       
   206             cmdCore("-flag", "-SomeManagementFlag", "myapp.exe", "my.core");
       
   207             fail("Flags can not be set in core files");
       
   208         } catch (Exception e) {
       
   209             if (!isValidationException(e)) {
       
   210                 throw e;
       
   211             }
       
   212             // ignore
       
   213         }
       
   214     }
       
   215 
       
   216     public void testSetFlag3Core() throws Exception {
       
   217         try {
       
   218             cmdCore("-flag", "SomeManagementFlag=314", "myapp.exe", "my.core");
       
   219             fail("Flags can not be set in core files");
       
   220         } catch (Exception e) {
       
   221             if (!isValidationException(e)) {
       
   222                 throw e;
       
   223             }
       
   224             // ignore
       
   225         }
       
   226     }
       
   227 
       
   228     public void testDefaultRemote() throws Exception {
       
   229         cmdRemote(null, "serverid@host");
       
   230     }
       
   231 
       
   232     public void testFlagsRemote() throws Exception {
       
   233         cmdRemote("-flags", "serverid@host");
       
   234     }
       
   235 
       
   236     public void testSyspropsRemote() throws Exception {
       
   237         cmdRemote("-sysprops", "serverid@host");
       
   238     }
       
   239 
       
   240     public void testReadFlagRemote() throws Exception {
       
   241         try {
       
   242             cmdCore("-flag", "SomeManagementFlag", "serverid@host");
       
   243             fail("Flags can not be read from SA server");
       
   244         } catch (Exception e) {
       
   245             if (!isValidationException(e)) {
       
   246                 throw e;
       
   247             }
       
   248             // ignore
       
   249         }
       
   250     }
       
   251 
       
   252     public void testSetFlag1Remote() throws Exception {
       
   253         try {
       
   254             cmdCore("-flag", "+SomeManagementFlag","serverid@host");
       
   255             fail("Flags can not be set on SA server");
       
   256         } catch (Exception e) {
       
   257             if (!isValidationException(e)) {
       
   258                 throw e;
       
   259             }
       
   260             // ignore
       
   261         }
       
   262     }
       
   263 
       
   264     public void testSetFlag2Remote() throws Exception {
       
   265         try {
       
   266             cmdCore("-flag", "-SomeManagementFlag", "serverid@host");
       
   267             fail("Flags can not be read set on SA server");
       
   268         } catch (Exception e) {
       
   269             if (!isValidationException(e)) {
       
   270                 throw e;
       
   271             }
       
   272             // ignore
       
   273         }
       
   274     }
       
   275 
       
   276     public void testSetFlag3Remote() throws Exception {
       
   277         try {
       
   278             cmdCore("-flag", "SomeManagementFlag=314", "serverid@host");
       
   279             fail("Flags can not be read set on SA server");
       
   280         } catch (Exception e) {
       
   281             if (!isValidationException(e)) {
       
   282                 throw e;
       
   283             }
       
   284             // ignore
       
   285         }
       
   286     }
       
   287 
       
   288     public void testDefaultExtraArgs() throws Exception {
       
   289         cmdExtraArgs(null, 2);
       
   290     }
       
   291 
       
   292     public void testFlagsExtraArgs() throws Exception {
       
   293         cmdExtraArgs("-flags", 2);
       
   294     }
       
   295 
       
   296     public void testSyspropsExtraArgs() throws Exception {
       
   297         cmdExtraArgs("-sysprops", 2);
       
   298     }
       
   299 
       
   300     public void testFlagExtraArgs() throws Exception {
       
   301         cmdExtraArgs("-flag", 2);
       
   302     }
       
   303 
       
   304     public void testHelp1ExtraArgs() throws Exception {
       
   305         cmdExtraArgs("-h", 0);
       
   306     }
       
   307 
       
   308     public void testHelp2ExtraArgs() throws Exception {
       
   309         cmdExtraArgs("-help", 0);
       
   310     }
       
   311 
       
   312     public void testDefaultMissingArgs() throws Exception {
       
   313         cmdMissingArgs(null, 1);
       
   314     }
       
   315 
       
   316     public void testFlagsMissingArgs() throws Exception {
       
   317         cmdMissingArgs("-flags", 1);
       
   318     }
       
   319 
       
   320     public void testSyspropsMissingArgs() throws Exception {
       
   321         cmdMissingArgs("-sysprops", 1);
       
   322     }
       
   323 
       
   324     public void testFlagMissingArgs() throws Exception {
       
   325         cmdMissingArgs("-flag", 2);
       
   326     }
       
   327 
       
   328     public void testUnknownCommand() throws Exception {
       
   329         try {
       
   330             JInfo j = newJInfo(new String[]{"-unknown_command"});
       
   331             fail("JInfo accepts unknown commands");
       
   332         } catch (Exception e) {
       
   333             if (!isValidationException(e)) {
       
   334                 throw e;
       
   335             }
       
   336             // ignore
       
   337         }
       
   338     }
       
   339 
       
   340     private static boolean isValidationException(Throwable e) {
       
   341         return e.getClass().getName().equals(VALIDATION_EXCEPTION_CLSNAME);
       
   342     }
       
   343 }