1 /*
   2  * Copyright (c) 2014, 2016, 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 jdk.testlibrary.Asserts;
  25 import jdk.testlibrary.OutputAnalyzer;
  26 import jdk.test.lib.apps.LingeredApp;
  27 
  28 /*
  29  * @test
  30  * @summary This test verifies jps usage and checks that appropriate error message is shown
  31  *          when running jps with illegal arguments.
  32  *
  33  * @library /lib/testlibrary /test/lib
  34  * @modules jdk.jartool/sun.tools.jar
  35  *          java.base/jdk.internal.misc
  36  *          java.management
  37  *          jdk.jcmd
  38  *
  39  * @build jdk.testlibrary.* jdk.test.lib.apps.* JpsHelper
  40  * @run driver TestJpsSanity
  41  */
  42 public class TestJpsSanity {
  43 
  44     public static void main(String[] args) throws Throwable {
  45         testJpsUsage();
  46         testJpsVersion();
  47         testJpsUnknownHost();
  48         testJpsShort();
  49         testJpsLong();
  50         testJpsShortPkg();
  51         testJpsLongPkg();
  52     }
  53 
  54     private static void testJpsShort() throws Exception {
  55         OutputAnalyzer output = JpsHelper.jps();
  56         output.shouldMatch("^[0-9]+ Jps$");
  57     }
  58 
  59     private static void testJpsLong() throws Exception {
  60         OutputAnalyzer output = JpsHelper.jps("-l");
  61         output.shouldMatch("^[0-9]+ jdk\\.jcmd/sun\\.tools\\.jps\\.Jps$");
  62     }
  63 
  64     private static void testJpsShortPkg() throws Exception {
  65         LingeredApp app = null;
  66         try {
  67             app = LingeredApp.startApp();
  68             OutputAnalyzer output = JpsHelper.jps();
  69             output.shouldMatch("^[0-9]+ LingeredApp$");
  70         } finally {
  71             LingeredApp.stopApp(app);
  72         }
  73     }
  74 
  75     private static void testJpsLongPkg() throws Exception {
  76         LingeredApp app = null;
  77         try {
  78             app = LingeredApp.startApp();
  79             OutputAnalyzer output = JpsHelper.jps("-l");
  80             output.shouldMatch("^[0-9]+ jdk\\.test\\.lib\\.apps\\.LingeredApp$");
  81         } finally {
  82             LingeredApp.stopApp(app);
  83         }
  84     }
  85 
  86     private static void testJpsUsage() throws Exception {
  87         OutputAnalyzer output = JpsHelper.jps("-?");
  88         JpsHelper.verifyOutputAgainstFile(output);
  89 
  90         output = JpsHelper.jps("-help");
  91         JpsHelper.verifyOutputAgainstFile(output);
  92     }
  93 
  94     private static void testJpsVersion() throws Exception {
  95         OutputAnalyzer output = JpsHelper.jps("-version");
  96         Asserts.assertNotEquals(output.getExitValue(), 0, "Exit code shouldn't be 0");
  97         Asserts.assertFalse(output.getStderr().isEmpty(), "Error output should not be empty");
  98         output.shouldContain("illegal argument: -version");
  99     }
 100 
 101     private static void testJpsUnknownHost() throws Exception {
 102         String invalidHostName = "Oja781nh2ev7vcvbajdg-Sda1-C.invalid";
 103         OutputAnalyzer output = JpsHelper.jps(invalidHostName);
 104         Asserts.assertNotEquals(output.getExitValue(), 0, "Exit code shouldn't be 0");
 105         Asserts.assertFalse(output.getStderr().isEmpty(), "Error output should not be empty");
 106         output.shouldMatch(".*(RMI Registry not available at|Unknown host\\:) " + invalidHostName + ".*");
 107     }
 108 
 109 }