KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > SecurityManagerTestCase


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management;
10
11 import java.security.Permission JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.io.IOException JavaDoc;
15 import javax.management.*;
16
17 import test.MX4JTestCase;
18
19 /**
20  * Test case class that defines all tests to be performed by a security manager test
21  * both for the local (that is, MBeanServer tests) and the remote (that is, for MBeanServerConnection
22  * tests) case.
23  * It also defines the implementations of the test methods, leaving to subclasses
24  * only the creation of the MBeanServerConnection (or MBeanServer) object.
25  *
26  * @version $Revision: 1.5 $
27  */

28 public abstract class SecurityManagerTestCase extends MX4JTestCase
29 {
30    public SecurityManagerTestCase(String JavaDoc s)
31    {
32       super(s);
33    }
34
35    /**
36     * Adds the given permission to the client codesource, that is the test codesource
37     *
38     * @see #resetPermissions
39     */

40    protected abstract void addPermission(Permission JavaDoc p);
41
42    /**
43     * Removes all permissions add via {@link #addPermission}
44     */

45    protected abstract void resetPermissions();
46
47    public abstract void testAddRemoveNotificationListener() throws Exception JavaDoc;
48
49    public abstract void testCreateMBean4Params() throws Exception JavaDoc;
50
51    public abstract void testCreateMBean5Params() throws Exception JavaDoc;
52
53    public abstract void testGetAttribute() throws Exception JavaDoc;
54
55    public abstract void testGetAttributes() throws Exception JavaDoc;
56
57    public abstract void testGetDefaultDomain() throws Exception JavaDoc;
58
59    public abstract void testGetDomains() throws Exception JavaDoc;
60
61    public abstract void testGetMBeanCount() throws Exception JavaDoc;
62
63    public abstract void testGetMBeanInfo() throws Exception JavaDoc;
64
65    public abstract void testGetObjectInstance() throws Exception JavaDoc;
66
67    public abstract void testInvoke() throws Exception JavaDoc;
68
69    public abstract void testIsInstanceOf() throws Exception JavaDoc;
70
71    public abstract void testIsRegistered() throws Exception JavaDoc;
72
73    public abstract void testQueryMBeans() throws Exception JavaDoc;
74
75    public abstract void testQueryNames() throws Exception JavaDoc;
76
77    public abstract void testSetAttribute() throws Exception JavaDoc;
78
79    public abstract void testSetAttributes() throws Exception JavaDoc;
80
81    public abstract void testUnregisterMBean() throws Exception JavaDoc;
82
83    protected void testAddRemoveNotificationListener(MBeanServerConnection server) throws Exception JavaDoc
84    {
85       NotificationListener listener = new NotificationListener()
86       {
87          public void handleNotification(Notification notification, Object JavaDoc handback)
88          {
89          }
90       };
91
92       ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
93
94       try
95       {
96          server.addNotificationListener(delegate, listener, null, null);
97          fail();
98       }
99       catch (SecurityException JavaDoc ignored)
100       {
101       }
102
103       addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "addNotificationListener"));
104       server.addNotificationListener(delegate, listener, null, null);
105
106       // Clean up
107
try
108       {
109          server.removeNotificationListener(delegate, listener);
110          fail();
111       }
112       catch (SecurityException JavaDoc ignored)
113       {
114       }
115
116       addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "removeNotificationListener"));
117       server.removeNotificationListener(delegate, listener);
118    }
119
120    protected void testCreateMBean4Params(MBeanServerConnection server) throws Exception JavaDoc
121    {
122       // Needed to create an MLet, which is a ClassLoader
123
addPermission(new RuntimePermission JavaDoc("createClassLoader"));
124       ObjectName mletName = new ObjectName(server.getDefaultDomain(), "name", "mlet");
125       String JavaDoc mletClassName = "javax.management.loading.MLet";
126       addPermission(new MBeanPermission(mletClassName + "[" + mletName.getCanonicalName() + "]", "instantiate, registerMBean"));
127       server.createMBean(mletClassName, mletName, null, null, null);
128
129       // Now we have something in the CLR
130
String JavaDoc mbeanClassName = "javax.management.MBeanServerDelegate";
131       ObjectName mbeanName = new ObjectName(server.getDefaultDomain(), "name", "delegate");
132
133       try
134       {
135          server.createMBean(mbeanClassName, mbeanName);
136          fail();
137       }
138       catch (SecurityException JavaDoc ignored)
139       {
140       }
141
142       addPermission(new MBeanPermission(mbeanClassName + "[" + mbeanName.getCanonicalName() + "]", "instantiate, registerMBean"));
143       ObjectInstance result = server.createMBean(mbeanClassName, mbeanName, null, null);
144       assertNotNull(result);
145    }
146
147    protected void testCreateMBean5Params(MBeanServerConnection server) throws Exception JavaDoc
148    {
149       // Needed to create an MLet, which is a ClassLoader
150
addPermission(new RuntimePermission JavaDoc("createClassLoader"));
151
152       ObjectName mletName = new ObjectName(server.getDefaultDomain(), "name", "mlet");
153       String JavaDoc mletClassName = "javax.management.loading.MLet";
154
155       try
156       {
157          server.createMBean(mletClassName, mletName, null);
158          fail();
159       }
160       catch (SecurityException JavaDoc ignored)
161       {
162       }
163
164       addPermission(new MBeanPermission(mletClassName + "[" + mletName.getCanonicalName() + "]", "instantiate, registerMBean"));
165       ObjectInstance result = server.createMBean(mletClassName, mletName, null, null, null);
166       assertNotNull(result);
167    }
168
169    protected void testGetAttribute(MBeanServerConnection server) throws Exception JavaDoc
170    {
171       ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
172       String JavaDoc attribute = "ImplementationName";
173       try
174       {
175          server.getAttribute(delegate, attribute);
176          fail();
177       }
178       catch (SecurityException JavaDoc ignored)
179       {
180       }
181
182       addPermission(new MBeanPermission("#" + attribute + "[" + delegate.getCanonicalName() + "]", "getAttribute"));
183       String JavaDoc result = (String JavaDoc)server.getAttribute(delegate, attribute);
184       assertNotNull(result);
185    }
186
187    protected void testGetAttributes(MBeanServerConnection server) throws Exception JavaDoc
188    {
189       ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
190
191       String JavaDoc[] allAttributes = new String JavaDoc[]{"MBeanServerId", "ImplementationName", "ImplementationVendor", "ImplementationVersion", "SpecificationName", "SpecificationVendor", "SpecificationVersion"};
192       String JavaDoc[] allowed = new String JavaDoc[0];
193       String JavaDoc[] wanted = new String JavaDoc[0];
194
195       try
196       {
197          server.getAttributes(delegate, allAttributes);
198          fail();
199       }
200       catch (SecurityException JavaDoc ignored)
201       {
202       }
203
204       // Check that for wrong attribute I get an empty list
205
allowed = new String JavaDoc[]{allAttributes[1]};
206       wanted = new String JavaDoc[]{allAttributes[0]};
207       addPermission(new MBeanPermission("#" + allowed[0] + "[" + delegate.getCanonicalName() + "]", "getAttribute"));
208       AttributeList list = server.getAttributes(delegate, wanted);
209       assertEquals(list.size(), 0);
210
211       // Check that for the right attribute I get it
212
resetPermissions();
213       allowed = new String JavaDoc[]{allAttributes[0]};
214       wanted = allAttributes;
215       addPermission(new MBeanPermission("#" + allowed[0] + "[" + delegate.getCanonicalName() + "]", "getAttribute"));
216       list = server.getAttributes(delegate, wanted);
217       assertEquals(list.size(), allowed.length);
218       Attribute attrib = (Attribute)list.get(0);
219       assertEquals(attrib.getName(), allowed[0]);
220
221       // Check that if I grant some I only get some
222
resetPermissions();
223       // Only attributes that start with 'Implementation'
224
allowed = new String JavaDoc[]{allAttributes[1], allAttributes[2], allAttributes[3]};
225       wanted = allAttributes;
226       addPermission(new MBeanPermission("#Implementation*[" + delegate.getCanonicalName() + "]", "getAttribute"));
227       list = server.getAttributes(delegate, wanted);
228       assertEquals(list.size(), allowed.length);
229       for (int i = 0; i < list.size(); ++i)
230       {
231          Attribute attr = (Attribute)list.get(i);
232          assertEquals(attr.getName(), allowed[i]);
233       }
234
235       // Check that if I grant all I get them all
236
resetPermissions();
237       allowed = allAttributes;
238       wanted = allAttributes;
239       addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "getAttribute"));
240       list = server.getAttributes(delegate, allAttributes);
241       assertEquals(list.size(), allowed.length);
242       for (int i = 0; i < list.size(); ++i)
243       {
244          Attribute attr = (Attribute)list.get(i);
245          assertEquals(attr.getName(), allowed[i]);
246       }
247    }
248
249    protected void testGetDefaultDomain(MBeanServerConnection server, String JavaDoc domain) throws Exception JavaDoc
250    {
251       String JavaDoc result = server.getDefaultDomain();
252       assertEquals(result, domain);
253    }
254
255    protected void testGetDomains(MBeanServerConnection server) throws Exception JavaDoc
256    {
257       try
258       {
259          server.getDomains();
260          fail();
261       }
262       catch (SecurityException JavaDoc e)
263       {
264          // OK
265
}
266       ObjectName name = new ObjectName("test:x=x");
267       addPermission(new MBeanPermission(Simple.class.getName(), null, name, "instantiate"));
268       addPermission(new MBeanPermission(Simple.class.getName(), null, name, "registerMBean"));
269       addPermission(new MBeanTrustPermission("register"));
270       server.createMBean(Simple.class.getName(), name);
271
272       addPermission(new MBeanPermission(null, null, name, "getDomains"));
273
274       String JavaDoc[] results = server.getDomains();
275       assertNotNull(results);
276       assertEquals(results.length, 1);
277       assertEquals(results[0], "test");
278    }
279
280    protected void testGetMBeanCount(MBeanServerConnection server) throws Exception JavaDoc
281    {
282       Integer JavaDoc count = server.getMBeanCount();
283       assertNotNull(count);
284       assertTrue(count.intValue() > 1);
285    }
286
287    protected void testGetMBeanInfo(MBeanServerConnection server) throws Exception JavaDoc
288    {
289       ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
290       try
291       {
292          server.getMBeanInfo(delegate);
293          fail();
294       }
295       catch (SecurityException JavaDoc ignored)
296       {
297       }
298
299       addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "getMBeanInfo"));
300       MBeanInfo info = server.getMBeanInfo(delegate);
301       assertNotNull(info);
302    }
303
304    protected void testGetObjectInstance(MBeanServerConnection server) throws Exception JavaDoc
305    {
306       ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
307       try
308       {
309          server.getObjectInstance(delegate);
310          fail();
311       }
312       catch (SecurityException JavaDoc ignored)
313       {
314       }
315
316       addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "getObjectInstance"));
317       ObjectInstance instance = server.getObjectInstance(delegate);
318       assertNotNull(instance);
319    }
320
321    protected void testInvoke(MBeanServerConnection server) throws Exception JavaDoc
322    {
323       ObjectName mbeanName = new ObjectName(server.getDefaultDomain(), "mbean", "simple");
324       addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
325       addPermission(new MBeanTrustPermission("register"));
326       server.createMBean(Simple.class.getName(), mbeanName, null);
327
328       addPermission(new MBeanPermission(Simple.class.getName(), "setAttribute"));
329       String JavaDoc initial = "mx4j";
330       server.setAttribute(mbeanName, new Attribute("FirstAttribute", initial));
331
332       String JavaDoc value = "simon";
333       String JavaDoc operation = "concatenateWithFirstAttribute";
334
335       try
336       {
337          server.invoke(mbeanName, operation, new Object JavaDoc[]{value}, new String JavaDoc[]{String JavaDoc.class.getName()});
338          fail();
339       }
340       catch (SecurityException JavaDoc ignored)
341       {
342       }
343
344       addPermission(new MBeanPermission("#" + operation + "[" + mbeanName.getCanonicalName() + "]", "invoke"));
345       String JavaDoc result = (String JavaDoc)server.invoke(mbeanName, operation, new Object JavaDoc[]{value}, new String JavaDoc[]{String JavaDoc.class.getName()});
346       assertEquals(result, initial + value);
347    }
348
349    protected void testIsInstanceOf(MBeanServerConnection server) throws Exception JavaDoc
350    {
351       ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
352       String JavaDoc className = "javax.management.MBeanServerDelegateMBean";
353
354       try
355       {
356          server.isInstanceOf(delegate, className);
357          fail();
358       }
359       catch (SecurityException JavaDoc ignored)
360       {
361       }
362
363       addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "isInstanceOf"));
364       boolean isInstance = server.isInstanceOf(delegate, className);
365       assertTrue(isInstance);
366    }
367
368    protected void testIsRegistered(MBeanServerConnection server) throws Exception JavaDoc
369    {
370       ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
371       boolean registered = server.isRegistered(delegate);
372       assertTrue(registered);
373    }
374
375    protected void testQueryMBeans(MBeanServerConnection server) throws Exception JavaDoc
376    {
377       ObjectName mbeanName = new ObjectName(server.getDefaultDomain(), "mbean", "simple");
378       addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
379       addPermission(new MBeanTrustPermission("register"));
380       server.createMBean(Simple.class.getName(), mbeanName, null);
381
382       try
383       {
384          server.queryMBeans(null, null);
385          fail();
386       }
387       catch (SecurityException JavaDoc ignored)
388       {
389       }
390
391       // Check that an ObjectName that does not match returns an empty list
392
ObjectName dummy = new ObjectName(server.getDefaultDomain(), "type", "dummy");
393       addPermission(new MBeanPermission("[" + dummy.getCanonicalName() + "]", "queryMBeans"));
394       Set JavaDoc mbeans = server.queryMBeans(null, null);
395       assertEquals(mbeans.size(), 0);
396
397       // Now check the right one
398
resetPermissions();
399       addPermission(new MBeanPermission("[" + mbeanName.getCanonicalName() + "]", "queryMBeans"));
400       mbeans = server.queryMBeans(null, null);
401       assertEquals(mbeans.size(), 1);
402       ObjectInstance instance = (ObjectInstance)mbeans.iterator().next();
403       assertEquals(instance.getObjectName(), mbeanName);
404
405       // Check the right one for a pattern in permission
406
resetPermissions();
407       addPermission(new MBeanPermission("[" + server.getDefaultDomain() + ":*]", "queryMBeans"));
408       mbeans = server.queryMBeans(null, null);
409       assertEquals(mbeans.size(), 1);
410       instance = (ObjectInstance)mbeans.iterator().next();
411       assertEquals(instance.getObjectName(), mbeanName);
412
413       // Check for another pattern
414
resetPermissions();
415       addPermission(new MBeanPermission("[JMImplementation:*]", "queryMBeans"));
416       mbeans = server.queryMBeans(null, null);
417       assertTrue(mbeans.size() >= 1);
418       for (Iterator JavaDoc iterator = mbeans.iterator(); iterator.hasNext();)
419       {
420          instance = (ObjectInstance)iterator.next();
421          assertEquals(instance.getObjectName().getDomain(), "JMImplementation");
422       }
423
424       // Check for all
425
resetPermissions();
426       addPermission(new MBeanPermission("*", "queryMBeans"));
427       // Try queryNames to see if the permission is implies
428
mbeans = server.queryNames(null, null);
429       assertEquals(mbeans.size(), server.getMBeanCount().intValue());
430
431       // Check for the query expression
432
resetPermissions();
433       String JavaDoc className = "mx4j.server.MX4JMBeanServerDelegate";
434       addPermission(new MBeanPermission(className, "queryMBeans"));
435       QueryExp exp = Query.eq(Query.attr(className, "ImplementationName"), Query.value("MX4J"));
436       mbeans = server.queryMBeans(null, exp);
437       assertEquals(mbeans.size(), 0);
438
439       // Now grant also the permission to retrieve the attribute
440
addPermission(new MBeanPermission(className, "getAttribute, getObjectInstance"));
441       mbeans = server.queryMBeans(null, exp);
442       assertEquals(mbeans.size(), 1);
443       instance = (ObjectInstance)mbeans.iterator().next();
444       assertEquals(instance.getClassName(), className);
445    }
446
447    protected void testQueryNames(MBeanServerConnection server) throws Exception JavaDoc
448    {
449       ObjectName mbeanName = new ObjectName(server.getDefaultDomain(), "mbean", "simple");
450       addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
451       addPermission(new MBeanTrustPermission("register"));
452       server.createMBean(Simple.class.getName(), mbeanName, null);
453
454       try
455       {
456          server.queryNames(null, null);
457          fail();
458       }
459       catch (SecurityException JavaDoc ignored)
460       {
461       }
462
463       // Check that an ObjectName that does not match returns an empty list
464
ObjectName dummy = new ObjectName(server.getDefaultDomain(), "type", "dummy");
465       addPermission(new MBeanPermission("[" + dummy.getCanonicalName() + "]", "queryNames"));
466       Set JavaDoc mbeans = server.queryNames(null, null);
467       assertEquals(mbeans.size(), 0);
468
469       // Now check the right one
470
resetPermissions();
471       addPermission(new MBeanPermission("[" + mbeanName.getCanonicalName() + "]", "queryNames"));
472       mbeans = server.queryNames(null, null);
473       assertEquals(mbeans.size(), 1);
474       ObjectName name = (ObjectName)mbeans.iterator().next();
475       assertEquals(name, mbeanName);
476
477       // Check the right one for a pattern in permission
478
resetPermissions();
479       addPermission(new MBeanPermission("["+ server.getDefaultDomain() + ":*]", "queryNames"));
480       mbeans = server.queryNames(null, null);
481       assertEquals(mbeans.size(), 1);
482       name = (ObjectName)mbeans.iterator().next();
483       assertEquals(name, mbeanName);
484
485       // Check for another pattern
486
resetPermissions();
487       addPermission(new MBeanPermission("[JMImplementation:*]", "queryNames"));
488       mbeans = server.queryNames(null, null);
489       assertTrue(mbeans.size() >= 1);
490       for (Iterator JavaDoc iterator = mbeans.iterator(); iterator.hasNext();)
491       {
492          name = (ObjectName)iterator.next();
493          assertEquals(name.getDomain(), "JMImplementation");
494       }
495
496       // Check for all
497
resetPermissions();
498       addPermission(new MBeanPermission("*", "queryNames"));
499       mbeans = server.queryNames(null, null);
500       assertEquals(mbeans.size(), server.getMBeanCount().intValue());
501
502       // Check for the query expression
503
resetPermissions();
504       String JavaDoc className = "mx4j.server.MX4JMBeanServerDelegate";
505       addPermission(new MBeanPermission(className, "queryNames"));
506       QueryExp exp = Query.eq(Query.attr(className, "ImplementationName"), Query.value("MX4J"));
507       mbeans = server.queryNames(null, exp);
508       assertEquals(mbeans.size(), 0);
509
510       // Now grant also the permission to retrieve the attribute
511
addPermission(new MBeanPermission(className, "getAttribute, getObjectInstance"));
512       mbeans = server.queryNames(null, exp);
513       assertEquals(mbeans.size(), 1);
514       name = (ObjectName)mbeans.iterator().next();
515       assertEquals(name, new ObjectName("JMImplementation", "type", "MBeanServerDelegate"));
516    }
517
518    protected void testSetAttribute(MBeanServerConnection server) throws Exception JavaDoc
519    {
520       ObjectName name = new ObjectName(server.getDefaultDomain(), "name", "test");
521       addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
522       addPermission(new MBeanTrustPermission("register"));
523       server.createMBean(Simple.class.getName(), name, null);
524
525       String JavaDoc attributeName = "FirstAttribute";
526       String JavaDoc value = "first";
527       Attribute attribute = new Attribute(attributeName, value);
528
529       try
530       {
531          server.setAttribute(name, attribute);
532          fail();
533       }
534       catch (SecurityException JavaDoc ignored)
535       {
536       }
537
538       addPermission(new MBeanPermission("#" + attributeName + "[" + name.getCanonicalName() + "]", "setAttribute"));
539       server.setAttribute(name, attribute);
540
541       // Check that it worked
542
addPermission(new MBeanPermission("#" + attributeName, "getAttribute"));
543       String JavaDoc result = (String JavaDoc)server.getAttribute(name, attributeName);
544       assertEquals(result, value);
545    }
546
547    protected void testSetAttributes(MBeanServerConnection server) throws Exception JavaDoc
548    {
549       ObjectName mbeanName = new ObjectName(server.getDefaultDomain(), "mbean", "simple");
550       addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
551       addPermission(new MBeanTrustPermission("register"));
552       server.createMBean(Simple.class.getName(), mbeanName, null);
553
554       AttributeList allAttributes = new AttributeList();
555       allAttributes.add(new Attribute("FirstAttribute", "first"));
556       allAttributes.add(new Attribute("SecondAttribute", "second"));
557       allAttributes.add(new Attribute("ThirdAttribute", "third"));
558       allAttributes.add(new Attribute("Running", Boolean.TRUE));
559
560       AttributeList allowed = new AttributeList();
561       AttributeList wanted = new AttributeList();
562
563       try
564       {
565          server.setAttributes(mbeanName, allAttributes);
566          fail();
567       }
568       catch (SecurityException JavaDoc ignored)
569       {
570       }
571
572       // Check that for wrong attribute I get an empty list
573
allowed.clear();
574       allowed.add(allAttributes.get(0));
575       wanted.clear();
576       wanted.add(allAttributes.get(1));
577       addPermission(new MBeanPermission("#" + ((Attribute)allowed.get(0)).getName() + "[" + mbeanName.getCanonicalName() + "]", "setAttribute"));
578       AttributeList list = server.setAttributes(mbeanName, wanted);
579       assertEquals(list.size(), 0);
580
581       // Check that for the right attribute I get it
582
resetPermissions();
583       allowed.clear();
584       allowed.add(allAttributes.get(0));
585       wanted.clear();
586       wanted.addAll(allAttributes);
587       addPermission(new MBeanPermission("#" + ((Attribute)allowed.get(0)).getName() + "[" + mbeanName.getCanonicalName() + "]", "setAttribute"));
588       list = server.setAttributes(mbeanName, wanted);
589       assertEquals(list.size(), allowed.size());
590       Attribute attrib = (Attribute)list.get(0);
591       assertEquals(attrib, allowed.get(0));
592
593       // Check that if I grant some I only get some
594
resetPermissions();
595       // Only attributes that ends with 'Attribute'
596
allowed.clear();
597       allowed.add(allAttributes.get(0));
598       allowed.add(allAttributes.get(1));
599       allowed.add(allAttributes.get(2));
600       wanted.clear();
601       wanted.addAll(allAttributes);
602       addPermission(new MBeanPermission("#*Attribute[" + mbeanName.getCanonicalName() + "]", "setAttribute"));
603       list = server.setAttributes(mbeanName, wanted);
604       assertEquals(list.size(), allowed.size());
605       for (int i = 0; i < list.size(); ++i)
606       {
607          Attribute attr = (Attribute)list.get(i);
608          assertEquals(attr, allowed.get(i));
609       }
610
611       // Check that if I grant all I get them all
612
resetPermissions();
613       allowed.clear();
614       allowed.addAll(allAttributes);
615       wanted.clear();
616       wanted.addAll(allAttributes);
617       addPermission(new MBeanPermission("[" + mbeanName.getCanonicalName() + "]", "setAttribute"));
618       list = server.setAttributes(mbeanName, wanted);
619       assertEquals(list.size(), allowed.size());
620       for (int i = 0; i < list.size(); ++i)
621       {
622          Attribute attr = (Attribute)list.get(i);
623          assertEquals(attr, allowed.get(i));
624       }
625    }
626
627    protected void testUnregisterMBean(MBeanServerConnection server) throws Exception JavaDoc
628    {
629       ObjectName name = new ObjectName(server.getDefaultDomain(), "name", "test");
630       addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
631       addPermission(new MBeanTrustPermission("register"));
632       server.createMBean(Simple.class.getName(), name, null);
633
634       try
635       {
636          server.unregisterMBean(name);
637          fail();
638       }
639       catch (SecurityException JavaDoc ignored)
640       {
641       }
642
643       addPermission(new MBeanPermission(Simple.class.getName() + "[" + name.getCanonicalName() + "]", "unregisterMBean"));
644       server.unregisterMBean(name);
645    }
646
647    public interface SimpleMBean
648    {
649       public void setFirstAttribute(String JavaDoc value);
650
651       public String JavaDoc getFirstAttribute();
652
653       public void setSecondAttribute(String JavaDoc value);
654
655       public String JavaDoc getSecondAttribute();
656
657       public void setThirdAttribute(String JavaDoc value);
658
659       public String JavaDoc getThirdAttribute();
660
661       public void setRunning(boolean value);
662
663       public boolean isRunning();
664
665       public String JavaDoc concatenateWithFirstAttribute(String JavaDoc value);
666    }
667
668    public static class Simple implements SimpleMBean
669    {
670       private String JavaDoc firstAttribute;
671       private String JavaDoc secondAttribute;
672       private String JavaDoc thirdAttribute;
673       private boolean running;
674
675       public String JavaDoc getFirstAttribute()
676       {
677          return firstAttribute;
678       }
679
680       public void setFirstAttribute(String JavaDoc value)
681       {
682          this.firstAttribute = value;
683       }
684
685       public String JavaDoc getSecondAttribute()
686       {
687          return secondAttribute;
688       }
689
690       public void setSecondAttribute(String JavaDoc secondAttribute)
691       {
692          this.secondAttribute = secondAttribute;
693       }
694
695       public String JavaDoc getThirdAttribute()
696       {
697          return thirdAttribute;
698       }
699
700       public void setThirdAttribute(String JavaDoc thirdAttribute)
701       {
702          this.thirdAttribute = thirdAttribute;
703       }
704
705       public boolean isRunning()
706       {
707          return running;
708       }
709
710       public void setRunning(boolean running)
711       {
712          this.running = running;
713       }
714
715       public String JavaDoc concatenateWithFirstAttribute(String JavaDoc value)
716       {
717          return this.firstAttribute + value;
718       }
719    }
720 }
721
Popular Tags