001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.implementation.modelmbean;
023:
024: import javax.management.Attribute;
025: import javax.management.AttributeChangeNotification;
026: import javax.management.Descriptor;
027: import javax.management.MBeanServer;
028: import javax.management.MBeanServerFactory;
029: import javax.management.Notification;
030: import javax.management.NotificationListener;
031: import javax.management.ObjectName;
032: import javax.management.modelmbean.DescriptorSupport;
033:
034: import junit.framework.TestCase;
035:
036: import org.jboss.mx.modelmbean.XMBean;
037: import org.jboss.mx.modelmbean.XMBeanConstants;
038:
039: import test.implementation.modelmbean.support.Test;
040:
041: /**
042: * Tests attribute caching and operation mapping for XMBean.
043: *
044: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
045: * @version $Revision: 57200 $
046: */
047: public class AttributeCacheTEST extends TestCase implements
048: XMBeanConstants {
049: public AttributeCacheTEST(String s) {
050: super (s);
051: }
052:
053: /**
054: * Tests that attribute values are not cached if nothing is declared in xml.
055: *
056: * This test uses the xmbean.dtd
057: */
058: public void testImplicitDisabledAttributeCaching() throws Exception {
059:
060: MBeanServer server = MBeanServerFactory.createMBeanServer();
061: Test resource = new Test();
062:
063: Descriptor d = new DescriptorSupport();
064: d.setField(RESOURCE_REFERENCE, resource);
065: d
066: .setField(
067: RESOURCE_TYPE,
068: "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface.xml");
069: d.setField(SAX_PARSER,
070: "org.apache.crimson.parser.XMLReaderImpl");
071:
072: XMBean mmb = new XMBean(d, DESCRIPTOR);
073:
074: ObjectName name = new ObjectName(":test=test");
075: server.registerMBean(mmb, name);
076:
077: for (int i = 0; i < 10; ++i) {
078: server
079: .setAttribute(name, new Attribute("Something",
080: "foo"));
081: server.getAttribute(name, "Something");
082: }
083:
084: assertTrue(resource.getFooCount() == 10);
085: assertTrue(resource.getBarCount() == 10);
086: }
087:
088: /**
089: * Tests that attribute values are not cached if currencyTimeLimit = 0
090: *
091: * This test uses the xmbean.dtd
092: */
093: public void testExplicitDisabledAttributeCaching() throws Exception {
094:
095: MBeanServer server = MBeanServerFactory.createMBeanServer();
096: Test resource = new Test();
097: Descriptor d = new DescriptorSupport();
098: d.setField(RESOURCE_REFERENCE, resource);
099: d
100: .setField(
101: RESOURCE_TYPE,
102: "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface2.xml");
103: d.setField(SAX_PARSER,
104: "org.apache.crimson.parser.XMLReaderImpl");
105:
106: XMBean mmb = new XMBean(d, DESCRIPTOR);
107:
108: ObjectName name = new ObjectName(":test=test");
109: server.registerMBean(mmb, name);
110:
111: for (int i = 0; i < 8; ++i) {
112: server
113: .setAttribute(name, new Attribute("Something",
114: "foo"));
115: server.getAttribute(name, "Something");
116: }
117:
118: assertTrue(resource.getFooCount() == 8);
119: assertTrue(resource.getBarCount() == 8);
120:
121: }
122:
123: /**
124: * Tests attribute that is never stale (currencyTimeLimit = -1)
125: *
126: * This test uses the xmbean.dtd
127: */
128: public void testNeverStaleAttributeCaching() throws Exception {
129:
130: MBeanServer server = MBeanServerFactory.createMBeanServer();
131: Test resource = new Test();
132: Descriptor d = new DescriptorSupport();
133: d.setField(RESOURCE_REFERENCE, resource);
134: d
135: .setField(
136: RESOURCE_TYPE,
137: "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface3.xml");
138: d.setField(SAX_PARSER,
139: "org.apache.crimson.parser.XMLReaderImpl");
140:
141: XMBean mmb = new XMBean(d, DESCRIPTOR);
142:
143: ObjectName name = new ObjectName(":test=test");
144: server.registerMBean(mmb, name);
145:
146: for (int i = 0; i < 11; ++i) {
147: server
148: .setAttribute(name, new Attribute("Something",
149: "foo"));
150: server.getAttribute(name, "Something");
151: }
152:
153: assertTrue(resource.getFooCount() == 11);
154: assertTrue(resource.getBarCount() == 0);
155: }
156:
157: /**
158: * Tests attribute that caches the value for 10 secs.
159: *
160: * This test uses the xmbean.dtd
161: */
162: public void testCachedAttribute() throws Exception {
163:
164: MBeanServer server = MBeanServerFactory.createMBeanServer();
165: Test resource = new Test();
166: Descriptor d = new DescriptorSupport();
167: d.setField(RESOURCE_REFERENCE, resource);
168: d
169: .setField(
170: RESOURCE_TYPE,
171: "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface4.xml");
172: d.setField(SAX_PARSER,
173: "org.apache.crimson.parser.XMLReaderImpl");
174:
175: XMBean mmb = new XMBean(d, DESCRIPTOR);
176:
177: ObjectName name = new ObjectName(":test=test");
178: server.registerMBean(mmb, name);
179:
180: for (int i = 0; i < 7; ++i) {
181: server
182: .setAttribute(name, new Attribute("Something",
183: "foo"));
184: server.getAttribute(name, "Something");
185: }
186:
187: assertTrue(resource.getFooCount() == 7);
188: assertTrue(resource.getBarCount() == 0);
189: }
190:
191: /**
192: * Tests attribute that caches the value for 1 secs.
193: *
194: * This test uses the xmbean.dtd
195: */
196: public void testCachedAttribute2() throws Exception {
197: MBeanServer server = MBeanServerFactory.createMBeanServer();
198: Test resource = new Test();
199: Descriptor d = new DescriptorSupport();
200: d.setField(RESOURCE_REFERENCE, resource);
201: d
202: .setField(
203: RESOURCE_TYPE,
204: "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface5.xml");
205: d.setField(SAX_PARSER,
206: "org.apache.crimson.parser.XMLReaderImpl");
207:
208: XMBean mmb = new XMBean(d, DESCRIPTOR);
209:
210: ObjectName name = new ObjectName(":test=test");
211: server.registerMBean(mmb, name);
212:
213: server.getAttribute(name, "Something");
214:
215: assertTrue(resource.getBarCount() == 1);
216:
217: server.setAttribute(name, new Attribute("Something", "yksi"));
218:
219: assertTrue(resource.getFooCount() == 1);
220:
221: String str = (String) server.getAttribute(name, "Something");
222:
223: assertTrue(resource.getBarCount() == 1);
224: assertTrue(str.equals("yksi"));
225:
226: try {
227: Thread.sleep(1100);
228: } catch (Throwable t) {
229: }
230:
231: server.getAttribute(name, "Something");
232:
233: assertTrue(resource.getBarCount() == 2);
234:
235: server.setAttribute(name, new Attribute("Something", "kaksi"));
236:
237: assertTrue(resource.getFooCount() == 2);
238:
239: try {
240: Thread.sleep(1100);
241: } catch (Throwable t) {
242: }
243:
244: str = (String) server.getAttribute(name, "Something");
245:
246: assertTrue(resource.getBarCount() == 3);
247: assertTrue(str.equals("kaksi"));
248:
249: str = (String) server.getAttribute(name, "Something");
250:
251: assertTrue(resource.getBarCount() == 3);
252: assertTrue(str.equals("kaksi"));
253: }
254:
255: /**
256: * Tests attribute change notifications
257: */
258: public void testAttributeChangeNotifications() throws Exception {
259: MBeanServer server = MBeanServerFactory.createMBeanServer();
260: Test resource = new Test();
261: Descriptor d = new DescriptorSupport();
262: d.setField(RESOURCE_REFERENCE, resource);
263: d
264: .setField(
265: RESOURCE_TYPE,
266: "file:./src/main/test/implementation/modelmbean/support/xml/TrivialManagementInterface5.xml");
267: d.setField(SAX_PARSER,
268: "org.apache.crimson.parser.XMLReaderImpl");
269:
270: XMBean mmb = new XMBean(d, DESCRIPTOR);
271:
272: ObjectName name = new ObjectName(":test=test");
273: server.registerMBean(mmb, name);
274:
275: class MyNotificationListener implements NotificationListener {
276: int notifCount = 0;
277:
278: public void handleNotification(Notification notification,
279: Object handback) {
280: AttributeChangeNotification notif = (AttributeChangeNotification) notification;
281:
282: assertTrue(notif.getNewValue().equals("somevalue"));
283:
284: notifCount++;
285: }
286: }
287:
288: MyNotificationListener listener = new MyNotificationListener();
289: server.addNotificationListener(name, listener, null, null);
290:
291: for (int i = 0; i < 10; ++i)
292: server.setAttribute(name, new Attribute("Something",
293: "somevalue"));
294:
295: assertTrue(listener.notifCount == 10);
296: }
297:
298: }
|