1 /*
   2  * Copyright (c) 2015, 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 com.oracle.java.testlibrary.OutputAnalyzer;
  25 import com.oracle.java.testlibrary.dcmd.CommandExecutor;
  26 import com.oracle.java.testlibrary.dcmd.JMXExecutor;
  27 import org.testng.annotations.Test;
  28 
  29 import java.text.NumberFormat;
  30 import java.text.ParseException;
  31 
  32 /*
  33  * @test
  34  * @library /testlibrary
  35  * @build com.oracle.java.testlibrary.*
  36  * @build com.oracle.java.testlibrary.dcmd.*
  37  * @run testng UptimeTest
  38  */
  39 public class UptimeTest {
  40     public void run(CommandExecutor executor) {
  41         double someUptime = 1.0;
  42         try {
  43             synchronized (this) {
  44                 wait((int) someUptime * 1000);
  45             }
  46         } catch (InterruptedException e) {
  47             throw new RuntimeException("Test error: Exception caught when sleeping:", e);
  48         }
  49 
  50         OutputAnalyzer output = executor.execute("VM.uptime");
  51 
  52         output.stderrShouldBeEmpty();
  53 
  54         /*
  55          * Output should be:
  56          * [pid]:
  57          * xx.yyy s
  58          *
  59          * If there is only one line in output there is no "[pid]:" printout;
  60          * skip first line, split on whitespace and grab first half
  61          */
  62         int index = output.asLines().size() == 1 ? 0 : 1;
  63         String uptimeString = output.asLines().get(index).split("\\s+")[0];
  64 
  65         try {
  66             double uptime = NumberFormat.getNumberInstance().parse(uptimeString).doubleValue();
  67             if (uptime < someUptime) {
  68                 throw new RuntimeException(String.format(
  69                         "Test failure: Uptime was less than intended sleep time: %.3f s < %.3f s",
  70                         uptime, someUptime));
  71             }
  72         } catch (ParseException e) {
  73             throw new RuntimeException("Test failure: Could not parse uptime string: " +
  74                     uptimeString, e);
  75         }
  76     }
  77 
  78     @Test
  79     public void jmx() {
  80         run(new JMXExecutor());
  81     }
  82 }