1 /*
   2  * Copyright (c) 2013, 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 /**
  25  * A JVM with JDP on should send multicast JDP packets regularly.
  26  *
  27  * See main for more information on running this test manually.
  28  * See Launcher classes for automated runs.
  29  *
  30  */
  31 
  32 import java.net.SocketTimeoutException;
  33 import java.util.Map;
  34 
  35 public class JdpOnTestCase extends JdpTestCase {
  36 
  37     private int receivedJDPpackets = 0;
  38 
  39     public JdpOnTestCase(ClientConnection connection) {
  40         super(connection);
  41     }
  42 
  43     /**
  44      * Subclasses: JdpOnTestCase and JdpOffTestCase have different messages.
  45      */
  46     @Override
  47     protected String initialLogMessage() {
  48         return "Waiting for 3 packets with jdp.name=" + connection.instanceName;
  49     }
  50 
  51     /**
  52      * This method is executed after a correct Jdp packet (coming from this VM) has been received.
  53      *
  54      * @param payload A dictionary containing the data if the received Jdp packet.
  55      */
  56     protected void packetFromThisVMReceived(Map<String, String> payload) {
  57         receivedJDPpackets++;
  58         final String jdpName = payload.get("INSTANCE_NAME");
  59         log.fine("Received correct JDP packet #" + String.valueOf(receivedJDPpackets) +
  60                 ", jdp.name=" + jdpName);
  61     }
  62 
  63     /**
  64      * The socket should not timeout.
  65      * It is set to wait for 10 times the defined pause between Jdp packet. See JdpOnTestCase.TIME_OUT_FACTOR.
  66      */
  67     @Override
  68     protected void onSocketTimeOut(SocketTimeoutException e) throws Exception {
  69         String message = "Timed out waiting for JDP packet. Should arrive within " +
  70                 connection.pauseInSeconds + " seconds, but waited for " +
  71                 timeOut + " seconds.";
  72         log.severe(message);
  73         throw new Exception(message, e);
  74     }
  75 
  76     /**
  77      * After receiving three Jdp packets the test should end.
  78      */
  79     @Override
  80     protected boolean shouldContinue() {
  81         return receivedJDPpackets < 3;
  82     }
  83 
  84     /**
  85      * To run this test manually you might need the following VM options:
  86      * <p/>
  87      * -Dcom.sun.management.jmxremote.authenticate=false
  88      * -Dcom.sun.management.jmxremote.ssl=false
  89      * -Dcom.sun.management.jmxremote.port=4711    (or some other port number)
  90      * -Dcom.sun.management.jmxremote=true
  91      * -Dcom.sun.management.jmxremote.autodiscovery=true
  92      * -Dcom.sun.management.jdp.pause=1
  93      * -Dcom.sun.management.jdp.name=alex  (or some other string to identify this VM)
  94      * <p/>
  95      * Recommended for nice output:
  96      * -Djava.util.logging.SimpleFormatter.format="%1$tF %1$tT %4$-7s %5$s %n"
  97      *
  98      * @param args
  99      * @throws Exception
 100      */
 101     public static void main(String[] args) throws Exception {
 102         JdpTestCase client = new JdpOnTestCase(new ClientConnection());
 103         client.run();
 104     }
 105 
 106 }