KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > implementation > modelmbean > AttributeCacheTEST


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package test.implementation.modelmbean;
23
24 import javax.management.Attribute JavaDoc;
25 import javax.management.AttributeChangeNotification JavaDoc;
26 import javax.management.Descriptor JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerFactory JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.NotificationListener JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.modelmbean.DescriptorSupport JavaDoc;
33
34 import junit.framework.TestCase;
35
36 import org.jboss.mx.modelmbean.XMBean;
37 import org.jboss.mx.modelmbean.XMBeanConstants;
38
39 import test.implementation.modelmbean.support.Test;
40
41 /**
42  * Tests attribute caching and operation mapping for XMBean.
43  *
44  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
45  * @version $Revision: 45164 $
46  */

47 public class AttributeCacheTEST extends TestCase implements XMBeanConstants
48 {
49    public AttributeCacheTEST(String JavaDoc s)
50    {
51       super(s);
52    }
53
54    /**
55     * Tests that attribute values are not cached if nothing is declared in xml.
56     *
57     * This test uses the xmbean.dtd
58     */

59    public void testImplicitDisabledAttributeCaching() throws Exception JavaDoc
60    {
61    
62       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
63       Test resource = new Test();
64       
65       Descriptor JavaDoc d = new DescriptorSupport JavaDoc();
66       d.setField(RESOURCE_REFERENCE, resource);
67       d.setField(RESOURCE_TYPE, "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface.xml");
68       d.setField(SAX_PARSER, "org.apache.crimson.parser.XMLReaderImpl");
69
70       XMBean mmb = new XMBean(d, DESCRIPTOR);
71       
72       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
73       server.registerMBean(mmb, name);
74       
75       for (int i = 0; i < 10; ++i)
76       {
77          server.setAttribute(name, new Attribute JavaDoc("Something", "foo"));
78          server.getAttribute(name, "Something");
79       }
80       
81       assertTrue(resource.getFooCount() == 10);
82       assertTrue(resource.getBarCount() == 10);
83    }
84
85    /**
86     * Tests that attribute values are not cached if currencyTimeLimit = 0
87     *
88     * This test uses the xmbean.dtd
89     */

90    public void testExplicitDisabledAttributeCaching() throws Exception JavaDoc
91    {
92    
93       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
94       Test resource = new Test();
95       Descriptor JavaDoc d = new DescriptorSupport JavaDoc();
96       d.setField(RESOURCE_REFERENCE, resource);
97       d.setField(RESOURCE_TYPE, "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface2.xml");
98       d.setField(SAX_PARSER, "org.apache.crimson.parser.XMLReaderImpl");
99
100       XMBean mmb = new XMBean(d, DESCRIPTOR);
101
102       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
103       server.registerMBean(mmb, name);
104       
105       for (int i = 0; i < 8; ++i)
106       {
107          server.setAttribute(name, new Attribute JavaDoc("Something", "foo"));
108          server.getAttribute(name, "Something");
109       }
110
111       assertTrue(resource.getFooCount() == 8);
112       assertTrue(resource.getBarCount() == 8);
113       
114    }
115
116    /**
117     * Tests attribute that is never stale (currencyTimeLimit = -1)
118     *
119     * This test uses the xmbean.dtd
120     */

121    public void testNeverStaleAttributeCaching() throws Exception JavaDoc
122    {
123    
124       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
125       Test resource = new Test();
126       Descriptor JavaDoc d = new DescriptorSupport JavaDoc();
127       d.setField(RESOURCE_REFERENCE, resource);
128       d.setField(RESOURCE_TYPE, "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface3.xml");
129       d.setField(SAX_PARSER, "org.apache.crimson.parser.XMLReaderImpl");
130
131       XMBean mmb = new XMBean(d, DESCRIPTOR);
132       
133       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
134       server.registerMBean(mmb, name);
135       
136       for (int i = 0; i < 11; ++i)
137       {
138          server.setAttribute(name, new Attribute JavaDoc("Something", "foo"));
139          server.getAttribute(name, "Something");
140       }
141
142       assertTrue(resource.getFooCount() == 11);
143       assertTrue(resource.getBarCount() == 0);
144    }
145
146    /**
147     * Tests attribute that caches the value for 10 secs.
148     *
149     * This test uses the xmbean.dtd
150     */

151    public void testCachedAttribute() throws Exception JavaDoc
152    {
153    
154       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
155       Test resource = new Test();
156       Descriptor JavaDoc d = new DescriptorSupport JavaDoc();
157       d.setField(RESOURCE_REFERENCE, resource);
158       d.setField(RESOURCE_TYPE, "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface4.xml");
159       d.setField(SAX_PARSER, "org.apache.crimson.parser.XMLReaderImpl");
160
161       XMBean mmb = new XMBean(d, DESCRIPTOR);
162       
163       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
164       server.registerMBean(mmb, name);
165       
166       for (int i = 0; i < 7; ++i)
167       {
168          server.setAttribute(name, new Attribute JavaDoc("Something", "foo"));
169          server.getAttribute(name, "Something");
170       }
171
172       assertTrue(resource.getFooCount() == 7);
173       assertTrue(resource.getBarCount() == 0);
174    }
175
176    /**
177     * Tests attribute that caches the value for 1 secs.
178     *
179     * This test uses the xmbean.dtd
180     */

181    public void testCachedAttribute2() throws Exception JavaDoc
182    {
183       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
184       Test resource = new Test();
185       Descriptor JavaDoc d = new DescriptorSupport JavaDoc();
186       d.setField(RESOURCE_REFERENCE, resource);
187       d.setField(RESOURCE_TYPE, "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface5.xml");
188       d.setField(SAX_PARSER, "org.apache.crimson.parser.XMLReaderImpl");
189
190       XMBean mmb = new XMBean(d, DESCRIPTOR);
191       
192       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
193       server.registerMBean(mmb, name);
194       
195       server.getAttribute(name, "Something");
196
197       assertTrue(resource.getBarCount() == 1);
198       
199       server.setAttribute(name, new Attribute JavaDoc("Something", "yksi"));
200
201       assertTrue(resource.getFooCount() == 1);
202       
203       String JavaDoc str = (String JavaDoc)server.getAttribute(name, "Something");
204       
205       assertTrue(resource.getBarCount() == 1);
206       assertTrue(str.equals("yksi"));
207       
208       try { Thread.sleep(1100); } catch (Throwable JavaDoc t) {}
209       
210       server.getAttribute(name, "Something");
211       
212       assertTrue(resource.getBarCount() == 2);
213       
214       server.setAttribute(name, new Attribute JavaDoc("Something", "kaksi"));
215       
216       assertTrue(resource.getFooCount() == 2);
217       
218       try { Thread.sleep(1100); } catch (Throwable JavaDoc t) {}
219       
220       str = (String JavaDoc)server.getAttribute(name, "Something");
221       
222       assertTrue(resource.getBarCount() == 3);
223       assertTrue(str.equals("kaksi"));
224       
225       str = (String JavaDoc)server.getAttribute(name, "Something");
226       
227       assertTrue(resource.getBarCount() == 3);
228       assertTrue(str.equals("kaksi"));
229    }
230
231    /**
232     * Tests attribute change notifications
233     */

234    public void testAttributeChangeNotifications() throws Exception JavaDoc
235    {
236       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
237       Test resource = new Test();
238       Descriptor JavaDoc d = new DescriptorSupport JavaDoc();
239       d.setField(RESOURCE_REFERENCE, resource);
240       d.setField(RESOURCE_TYPE, "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface5.xml");
241       d.setField(SAX_PARSER, "org.apache.crimson.parser.XMLReaderImpl");
242
243       XMBean mmb = new XMBean(d, DESCRIPTOR);
244       
245       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
246       server.registerMBean(mmb, name);
247
248       class MyNotificationListener implements NotificationListener JavaDoc
249       {
250          int notifCount = 0;
251          
252          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
253          {
254             AttributeChangeNotification JavaDoc notif = (AttributeChangeNotification JavaDoc)notification;
255             
256             assertTrue(notif.getNewValue().equals("somevalue"));
257             
258             notifCount++;
259          }
260       }
261       
262       MyNotificationListener listener = new MyNotificationListener();
263       server.addNotificationListener(name, listener, null, null);
264       
265       for (int i = 0; i < 10; ++i)
266          server.setAttribute(name, new Attribute JavaDoc("Something", "somevalue"));
267          
268       assertTrue(listener.notifCount == 10);
269    }
270    
271 }
272
Popular Tags