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