1 /*
   2  * Copyright (c) 2004, 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 /*
  25  * @test
  26  * @bug 5001857
  27  * @summary Test queryNames() and queryMBeans() with a buggy DynamicMBean
  28  * @author Daniel Fuchs
  29  * @modules java.management
  30  * @run clean MBeanInfoFailTest
  31  * @run build MBeanInfoFailTest
  32  * @run main MBeanInfoFailTest
  33  */
  34 
  35 import javax.management.*;
  36 import java.util.*;
  37 
  38 public class MBeanInfoFailTest {
  39 
  40     public static class UnspeakableException extends RuntimeException {
  41         public UnspeakableException(String unspeakableMessage) {
  42             super(unspeakableMessage);
  43         }
  44     }
  45 
  46     public interface ThornyDevilMBean {
  47         public boolean isDormant();
  48         public void setDormant(boolean sleep);
  49     }
  50 
  51     public static class ThornyDevil
  52         extends StandardMBean implements ThornyDevilMBean {
  53         private boolean sleep=true;
  54         public ThornyDevil() throws NotCompliantMBeanException {
  55             super(ThornyDevilMBean.class);
  56         }
  57         public boolean isDormant() {
  58             return this.sleep;
  59         }
  60         public void setDormant(boolean sleep) {
  61             this.sleep = sleep;
  62         }
  63         public MBeanInfo getMBeanInfo() {
  64             if (isDormant()) return super.getMBeanInfo();
  65             throw new UnspeakableException("The Thorny Devil has awoken!");
  66         }
  67     }
  68 
  69     public static void printInstances(Set instances) {
  70         for (Iterator it1 = instances.iterator(); it1.hasNext();) {
  71             final ObjectInstance oi = (ObjectInstance)it1.next();
  72             final ObjectName     on = oi.getObjectName();
  73             final String         cn = oi.getClassName();
  74             System.err.println(String.valueOf(on) + ": class is " + cn);
  75         }
  76     }
  77 
  78     public static void main(String[] args) throws Exception {
  79         System.out.println("Test queryNames() and queryMBeans() with a "+
  80                            "buggy DynamicMBean");
  81         try {
  82             final MBeanServer server = MBeanServerFactory.createMBeanServer();
  83 
  84             final ObjectName  troubleKeeper =
  85                 new ObjectName("ThornyDevil:name=TroubleKeeper");
  86             final ObjectName  troubleMaker =
  87                 new ObjectName("ThornyDevil:name=TroubleMaker");
  88             final ObjectName  thornyPattern =
  89                 new ObjectName("ThornyDevil:*");
  90 
  91             server.createMBean(ThornyDevil.class.getName(),
  92                                troubleKeeper);
  93             server.createMBean(ThornyDevil.class.getName(),
  94                                troubleMaker);
  95 
  96             final MBeanInfo info1 = server.getMBeanInfo(troubleKeeper);
  97             System.out.println(String.valueOf(troubleKeeper)+" is a " +
  98                                info1.getClassName());
  99             final MBeanInfo info2 = server.getMBeanInfo(troubleMaker);
 100             System.out.println(String.valueOf(troubleMaker)+" is a " +
 101                                info2.getClassName());
 102             final Set thorny1 = server.queryNames(thornyPattern,null);
 103             if (thorny1.size() != 2) {
 104                 System.err.println("queryNames(): " +
 105                                    "Expected to find 2 ThornyDevils before" +
 106                                    " trouble started! ");
 107                 System.err.println("Found "+thorny1.size()+" instead: "+
 108                                    thorny1);
 109                 System.exit(1);
 110             }
 111             final Set thornyM1 = server.queryMBeans(thornyPattern,null);
 112             if (thornyM1.size() != 2) {
 113                 System.err.println("queryMBeans(): " +
 114                                    "Expected to find 2 ThornyDevils before" +
 115                                    " trouble started! ");
 116                 System.err.println("Found "+thornyM1.size()+" instead: ");
 117                 printInstances(thornyM1);
 118                 System.exit(2);
 119             }
 120             for (Iterator it1 = thornyM1.iterator(); it1.hasNext();) {
 121                 final ObjectInstance oi = (ObjectInstance)it1.next();
 122                 final ObjectName     on = oi.getObjectName();
 123                 final String         cn = oi.getClassName();
 124                 if (cn == null || !ThornyDevil.class.getName().
 125                     equals(cn)) {
 126                     System.err.println("Expected no trouble yet!");
 127                     System.err.println(String.valueOf(on) + ": class is " +
 128                                        cn);
 129                     System.exit(3);
 130                 }
 131                 System.out.println(String.valueOf(on) + ": class is " + cn);
 132             }
 133 
 134             System.out.println("Starting trouble with "+troubleMaker+" ...");
 135             ThornyDevilMBean troubleMakerproxy =
 136                 (ThornyDevilMBean) MBeanServerInvocationHandler.
 137                 newProxyInstance(server, troubleMaker, ThornyDevilMBean.class,
 138                                  false);
 139             troubleMakerproxy.setDormant(false);
 140 
 141             try {
 142                 final MBeanInfo mbi = server.getMBeanInfo(troubleMaker);
 143                 System.err.println("No trouble started!: " + mbi);
 144                 System.exit(2);
 145             } catch (Exception x) {
 146                 if (x.getCause() instanceof UnspeakableException)
 147                     System.out.println("Trouble started as expected: "
 148                                        + x.getCause());
 149                 else {
 150                     System.err.println("Unexpected trouble: " + x.getCause());
 151                     x.printStackTrace();
 152                     System.exit(3);
 153                 }
 154             }
 155 
 156             final Set thorny2 = server.queryNames(thornyPattern,null);
 157             if (thorny2.size() != 2) {
 158                 System.err.println("Expected to find 2 ThornyDevils after" +
 159                                    " trouble started! ");
 160                 System.err.println("Found "+thorny2.size()+" instead: "+
 161                                    thorny2);
 162                 System.exit(4);
 163             }
 164 
 165             final Set thornyM2 = server.queryMBeans(thornyPattern,null);
 166             if (thornyM2.size() != 2) {
 167                 System.err.println("queryMBeans(): " +
 168                                    "Expected to find 2 ThornyDevils after" +
 169                                    " trouble started! ");
 170                 System.err.println("Found "+thornyM2.size()+" instead: ");
 171                 printInstances(thornyM2);
 172                 System.exit(5);
 173             }
 174             for (Iterator it1 = thornyM2.iterator(); it1.hasNext();) {
 175                 final ObjectInstance oi = (ObjectInstance)it1.next();
 176                 final ObjectName     on = oi.getObjectName();
 177                 final String         cn = oi.getClassName();
 178                 if (!on.equals(troubleMaker)) {
 179                     if (cn == null || !ThornyDevil.class.getName().
 180                         equals(cn)) {
 181                         System.err.println("Expected no trouble for " + on);
 182                         System.err.println(String.valueOf(on) + ": class is " +
 183                                            cn);
 184                         System.exit(6);
 185                     }
 186                 } else {
 187                     if (cn != null) {
 188                         System.err.println("Expected trouble for " + on);
 189                         System.err.println(String.valueOf(on) + ": class is " +
 190                                            cn);
 191                         System.exit(7);
 192                     }
 193                 }
 194                 System.out.println(String.valueOf(on) + ": class is " + cn);
 195             }
 196 
 197             System.out.println("Ahahah! " + troubleMaker +
 198                                " successfully thwarted!");
 199         } catch( Exception x) {
 200             System.err.println("Unexpected exception: " + x);
 201             x.printStackTrace();
 202             System.exit(8);
 203         }
 204     }
 205 
 206 }