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