1 22 package test.implementation.modelmbean; 23 24 import javax.management.Attribute ; 25 import javax.management.AttributeChangeNotification ; 26 import javax.management.Descriptor ; 27 import javax.management.MBeanServer ; 28 import javax.management.MBeanServerFactory ; 29 import javax.management.Notification ; 30 import javax.management.NotificationListener ; 31 import javax.management.ObjectName ; 32 import javax.management.modelmbean.DescriptorSupport ; 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 47 public class AttributeCacheTEST extends TestCase implements XMBeanConstants 48 { 49 public AttributeCacheTEST(String s) 50 { 51 super(s); 52 } 53 54 59 public void testImplicitDisabledAttributeCaching() throws Exception 60 { 61 62 MBeanServer server = MBeanServerFactory.createMBeanServer(); 63 Test resource = new Test(); 64 65 Descriptor d = new DescriptorSupport (); 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 name = new ObjectName (":test=test"); 73 server.registerMBean(mmb, name); 74 75 for (int i = 0; i < 10; ++i) 76 { 77 server.setAttribute(name, new Attribute ("Something", "foo")); 78 server.getAttribute(name, "Something"); 79 } 80 81 assertTrue(resource.getFooCount() == 10); 82 assertTrue(resource.getBarCount() == 10); 83 } 84 85 90 public void testExplicitDisabledAttributeCaching() throws Exception 91 { 92 93 MBeanServer server = MBeanServerFactory.createMBeanServer(); 94 Test resource = new Test(); 95 Descriptor d = new DescriptorSupport (); 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 name = new ObjectName (":test=test"); 103 server.registerMBean(mmb, name); 104 105 for (int i = 0; i < 8; ++i) 106 { 107 server.setAttribute(name, new Attribute ("Something", "foo")); 108 server.getAttribute(name, "Something"); 109 } 110 111 assertTrue(resource.getFooCount() == 8); 112 assertTrue(resource.getBarCount() == 8); 113 114 } 115 116 121 public void testNeverStaleAttributeCaching() throws Exception 122 { 123 124 MBeanServer server = MBeanServerFactory.createMBeanServer(); 125 Test resource = new Test(); 126 Descriptor d = new DescriptorSupport (); 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 name = new ObjectName (":test=test"); 134 server.registerMBean(mmb, name); 135 136 for (int i = 0; i < 11; ++i) 137 { 138 server.setAttribute(name, new Attribute ("Something", "foo")); 139 server.getAttribute(name, "Something"); 140 } 141 142 assertTrue(resource.getFooCount() == 11); 143 assertTrue(resource.getBarCount() == 0); 144 } 145 146 151 public void testCachedAttribute() throws Exception 152 { 153 154 MBeanServer server = MBeanServerFactory.createMBeanServer(); 155 Test resource = new Test(); 156 Descriptor d = new DescriptorSupport (); 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 name = new ObjectName (":test=test"); 164 server.registerMBean(mmb, name); 165 166 for (int i = 0; i < 7; ++i) 167 { 168 server.setAttribute(name, new Attribute ("Something", "foo")); 169 server.getAttribute(name, "Something"); 170 } 171 172 assertTrue(resource.getFooCount() == 7); 173 assertTrue(resource.getBarCount() == 0); 174 } 175 176 181 public void testCachedAttribute2() throws Exception 182 { 183 MBeanServer server = MBeanServerFactory.createMBeanServer(); 184 Test resource = new Test(); 185 Descriptor d = new DescriptorSupport (); 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 name = new ObjectName (":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 ("Something", "yksi")); 200 201 assertTrue(resource.getFooCount() == 1); 202 203 String str = (String )server.getAttribute(name, "Something"); 204 205 assertTrue(resource.getBarCount() == 1); 206 assertTrue(str.equals("yksi")); 207 208 try { Thread.sleep(1100); } catch (Throwable t) {} 209 210 server.getAttribute(name, "Something"); 211 212 assertTrue(resource.getBarCount() == 2); 213 214 server.setAttribute(name, new Attribute ("Something", "kaksi")); 215 216 assertTrue(resource.getFooCount() == 2); 217 218 try { Thread.sleep(1100); } catch (Throwable t) {} 219 220 str = (String )server.getAttribute(name, "Something"); 221 222 assertTrue(resource.getBarCount() == 3); 223 assertTrue(str.equals("kaksi")); 224 225 str = (String )server.getAttribute(name, "Something"); 226 227 assertTrue(resource.getBarCount() == 3); 228 assertTrue(str.equals("kaksi")); 229 } 230 231 234 public void testAttributeChangeNotifications() throws Exception 235 { 236 MBeanServer server = MBeanServerFactory.createMBeanServer(); 237 Test resource = new Test(); 238 Descriptor d = new DescriptorSupport (); 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 name = new ObjectName (":test=test"); 246 server.registerMBean(mmb, name); 247 248 class MyNotificationListener implements NotificationListener 249 { 250 int notifCount = 0; 251 252 public void handleNotification(Notification notification, Object handback) 253 { 254 AttributeChangeNotification notif = (AttributeChangeNotification )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 ("Something", "somevalue")); 267 268 assertTrue(listener.notifCount == 10); 269 } 270 271 } 272 | Popular Tags |