1 /*
   2  * Copyright (c) 2005, 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 4997033
  27  * @summary Test the following in RequiredModelMBean.setAttribute():
  28  * MBeanException wrapping a ServiceNotFoundException is thrown is setAttribute
  29  * called but no setMethod field has been provided.
  30  * @author Jean-Francois Denise
  31  * @run clean RequiredModelMBeanSetAttributeTest
  32  * @run build RequiredModelMBeanSetAttributeTest
  33  * @run main RequiredModelMBeanSetAttributeTest
  34  */
  35 
  36 import javax.management.Descriptor;
  37 import javax.management.MBeanServer;
  38 import javax.management.MBeanServerFactory;
  39 import javax.management.ObjectName;
  40 import javax.management.Attribute;
  41 import javax.management.MBeanException;
  42 import javax.management.ServiceNotFoundException;
  43 import javax.management.modelmbean.DescriptorSupport;
  44 import javax.management.modelmbean.ModelMBean;
  45 import javax.management.modelmbean.ModelMBeanAttributeInfo;
  46 import javax.management.modelmbean.ModelMBeanInfo;
  47 import javax.management.modelmbean.ModelMBeanInfoSupport;
  48 import javax.management.modelmbean.ModelMBeanOperationInfo;
  49 import javax.management.modelmbean.RequiredModelMBean;
  50 
  51 public class RequiredModelMBeanSetAttributeTest {
  52 
  53     public static void main(String[] args) throws Exception {
  54 
  55         boolean ok = true;
  56 
  57         MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  58 
  59         // ModelMBeanAttributeInfo
  60 
  61         Descriptor somethingAttributeDescriptor =
  62             new DescriptorSupport(new String[] {
  63                 "name=Something",
  64                 "descriptorType=attribute",
  65                 "getMethod=getSomething"
  66             });
  67         ModelMBeanAttributeInfo somethingAttributeInfo =
  68             new ModelMBeanAttributeInfo("Something",
  69                                         "java.lang.String",
  70                                         "Something attribute",
  71                                         true,
  72                                         true,
  73                                         false,
  74                                         somethingAttributeDescriptor);
  75 
  76         Descriptor somethingCachedAttributeDescriptor =
  77             new DescriptorSupport(new String[] {
  78                 "name=SomethingCached",
  79                 "descriptorType=attribute",
  80                 "getMethod=getSomethingCached",
  81                 "currencyTimeLimit=5000"
  82             });
  83         ModelMBeanAttributeInfo somethingCachedAttributeInfo =
  84             new ModelMBeanAttributeInfo("SomethingCached",
  85                                         "java.lang.String",
  86                                         "Something cached attribute",
  87                                         true,
  88                                         true,
  89                                         false,
  90                                         somethingCachedAttributeDescriptor);
  91         // ModelMBeanInfo
  92 
  93         ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(
  94             Resource.class.getName(),
  95             "Resource MBean",
  96             new ModelMBeanAttributeInfo[] { somethingAttributeInfo, somethingCachedAttributeInfo },
  97             null,
  98             new ModelMBeanOperationInfo[] {},
  99             null);
 100 
 101         // RequiredModelMBean
 102 
 103         ModelMBean mmb = new RequiredModelMBean(mmbi);
 104         mmb.setManagedResource(resource, "ObjectReference");
 105         ObjectName mmbName = new ObjectName(":type=ResourceMBean");
 106         mbs.registerMBean(mmb, mmbName);
 107 
 108         // Run tests
 109 
 110         System.out.println("\nTest that we receive ServiceNotFoundException");
 111         try {
 112             Attribute attr = new Attribute("Something", "Some string");
 113             mbs.setAttribute(mmbName, attr);
 114             System.out.println("TEST FAILED: Didn't caught exception");
 115             ok = false;
 116         } catch(MBeanException mbex) {
 117             Exception e = mbex.getTargetException();
 118             if(e == null || !(e instanceof ServiceNotFoundException)) {
 119                 System.out.println("TEST FAILED: Caught wrong exception:" + e);
 120                 ok = false;
 121             } else
 122                 System.out.println("Received expected ServiceNotFoundException");
 123 
 124         } catch (Exception e) {
 125             System.out.println("TEST FAILED: Caught wrong exception: " + e);
 126             e.printStackTrace(System.out);
 127             ok = false;
 128         }
 129 
 130         //Now check that when caching is enabled, setAttribute is working
 131         System.out.println("\nTest that we are not receiving ServiceNotFoundException");
 132         try {
 133             Attribute attr = new Attribute("SomethingCached", "Some string");
 134             mbs.setAttribute(mmbName, attr);
 135             System.out.println("No exception thrown");
 136        } catch (Exception e) {
 137             System.out.println("TEST FAILED: Caught an exception: " + e);
 138             e.printStackTrace(System.out);
 139             ok = false;
 140        }
 141 
 142         if (ok)
 143             System.out.println("Test passed");
 144         else {
 145             System.out.println("TEST FAILED");
 146             throw new Exception("TEST FAILED");
 147         }
 148     }
 149 
 150     public static class Resource {
 151         public String getSomething() {
 152             return "Something value";
 153         }
 154         public String getSomethingCached() {
 155             return "Something cached value";
 156         }
 157     }
 158 
 159     private static Resource resource = new Resource();
 160 }