Source Code Cross Referenced for SecurityManagerTestCase.java in  » JMX » mx4j » test » javax » management » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » mx4j » test.javax.management 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) The MX4J Contributors.
003:         * All rights reserved.
004:         *
005:         * This software is distributed under the terms of the MX4J License version 1.0.
006:         * See the terms of the MX4J License in the documentation provided with this software.
007:         */
008:
009:        package test.javax.management;
010:
011:        import java.security.Permission;
012:        import java.util.Iterator;
013:        import java.util.Set;
014:        import java.io.IOException;
015:        import javax.management.*;
016:
017:        import test.MX4JTestCase;
018:
019:        /**
020:         * Test case class that defines all tests to be performed by a security manager test
021:         * both for the local (that is, MBeanServer tests) and the remote (that is, for MBeanServerConnection
022:         * tests) case.
023:         * It also defines the implementations of the test methods, leaving to subclasses
024:         * only the creation of the MBeanServerConnection (or MBeanServer) object.
025:         *
026:         * @version $Revision: 1.5 $
027:         */
028:        public abstract class SecurityManagerTestCase extends MX4JTestCase {
029:            public SecurityManagerTestCase(String s) {
030:                super (s);
031:            }
032:
033:            /**
034:             * Adds the given permission to the client codesource, that is the test codesource
035:             *
036:             * @see #resetPermissions
037:             */
038:            protected abstract void addPermission(Permission p);
039:
040:            /**
041:             * Removes all permissions add via {@link #addPermission}
042:             */
043:            protected abstract void resetPermissions();
044:
045:            public abstract void testAddRemoveNotificationListener()
046:                    throws Exception;
047:
048:            public abstract void testCreateMBean4Params() throws Exception;
049:
050:            public abstract void testCreateMBean5Params() throws Exception;
051:
052:            public abstract void testGetAttribute() throws Exception;
053:
054:            public abstract void testGetAttributes() throws Exception;
055:
056:            public abstract void testGetDefaultDomain() throws Exception;
057:
058:            public abstract void testGetDomains() throws Exception;
059:
060:            public abstract void testGetMBeanCount() throws Exception;
061:
062:            public abstract void testGetMBeanInfo() throws Exception;
063:
064:            public abstract void testGetObjectInstance() throws Exception;
065:
066:            public abstract void testInvoke() throws Exception;
067:
068:            public abstract void testIsInstanceOf() throws Exception;
069:
070:            public abstract void testIsRegistered() throws Exception;
071:
072:            public abstract void testQueryMBeans() throws Exception;
073:
074:            public abstract void testQueryNames() throws Exception;
075:
076:            public abstract void testSetAttribute() throws Exception;
077:
078:            public abstract void testSetAttributes() throws Exception;
079:
080:            public abstract void testUnregisterMBean() throws Exception;
081:
082:            protected void testAddRemoveNotificationListener(
083:                    MBeanServerConnection server) throws Exception {
084:                NotificationListener listener = new NotificationListener() {
085:                    public void handleNotification(Notification notification,
086:                            Object handback) {
087:                    }
088:                };
089:
090:                ObjectName delegate = new ObjectName("JMImplementation",
091:                        "type", "MBeanServerDelegate");
092:
093:                try {
094:                    server.addNotificationListener(delegate, listener, null,
095:                            null);
096:                    fail();
097:                } catch (SecurityException ignored) {
098:                }
099:
100:                addPermission(new MBeanPermission("["
101:                        + delegate.getCanonicalName() + "]",
102:                        "addNotificationListener"));
103:                server.addNotificationListener(delegate, listener, null, null);
104:
105:                // Clean up
106:                try {
107:                    server.removeNotificationListener(delegate, listener);
108:                    fail();
109:                } catch (SecurityException ignored) {
110:                }
111:
112:                addPermission(new MBeanPermission("["
113:                        + delegate.getCanonicalName() + "]",
114:                        "removeNotificationListener"));
115:                server.removeNotificationListener(delegate, listener);
116:            }
117:
118:            protected void testCreateMBean4Params(MBeanServerConnection server)
119:                    throws Exception {
120:                // Needed to create an MLet, which is a ClassLoader
121:                addPermission(new RuntimePermission("createClassLoader"));
122:                ObjectName mletName = new ObjectName(server.getDefaultDomain(),
123:                        "name", "mlet");
124:                String mletClassName = "javax.management.loading.MLet";
125:                addPermission(new MBeanPermission(mletClassName + "["
126:                        + mletName.getCanonicalName() + "]",
127:                        "instantiate, registerMBean"));
128:                server.createMBean(mletClassName, mletName, null, null, null);
129:
130:                // Now we have something in the CLR
131:                String mbeanClassName = "javax.management.MBeanServerDelegate";
132:                ObjectName mbeanName = new ObjectName(
133:                        server.getDefaultDomain(), "name", "delegate");
134:
135:                try {
136:                    server.createMBean(mbeanClassName, mbeanName);
137:                    fail();
138:                } catch (SecurityException ignored) {
139:                }
140:
141:                addPermission(new MBeanPermission(mbeanClassName + "["
142:                        + mbeanName.getCanonicalName() + "]",
143:                        "instantiate, registerMBean"));
144:                ObjectInstance result = server.createMBean(mbeanClassName,
145:                        mbeanName, null, null);
146:                assertNotNull(result);
147:            }
148:
149:            protected void testCreateMBean5Params(MBeanServerConnection server)
150:                    throws Exception {
151:                // Needed to create an MLet, which is a ClassLoader
152:                addPermission(new RuntimePermission("createClassLoader"));
153:
154:                ObjectName mletName = new ObjectName(server.getDefaultDomain(),
155:                        "name", "mlet");
156:                String mletClassName = "javax.management.loading.MLet";
157:
158:                try {
159:                    server.createMBean(mletClassName, mletName, null);
160:                    fail();
161:                } catch (SecurityException ignored) {
162:                }
163:
164:                addPermission(new MBeanPermission(mletClassName + "["
165:                        + mletName.getCanonicalName() + "]",
166:                        "instantiate, registerMBean"));
167:                ObjectInstance result = server.createMBean(mletClassName,
168:                        mletName, null, null, null);
169:                assertNotNull(result);
170:            }
171:
172:            protected void testGetAttribute(MBeanServerConnection server)
173:                    throws Exception {
174:                ObjectName delegate = new ObjectName("JMImplementation",
175:                        "type", "MBeanServerDelegate");
176:                String attribute = "ImplementationName";
177:                try {
178:                    server.getAttribute(delegate, attribute);
179:                    fail();
180:                } catch (SecurityException ignored) {
181:                }
182:
183:                addPermission(new MBeanPermission("#" + attribute + "["
184:                        + delegate.getCanonicalName() + "]", "getAttribute"));
185:                String result = (String) server.getAttribute(delegate,
186:                        attribute);
187:                assertNotNull(result);
188:            }
189:
190:            protected void testGetAttributes(MBeanServerConnection server)
191:                    throws Exception {
192:                ObjectName delegate = new ObjectName("JMImplementation",
193:                        "type", "MBeanServerDelegate");
194:
195:                String[] allAttributes = new String[] { "MBeanServerId",
196:                        "ImplementationName", "ImplementationVendor",
197:                        "ImplementationVersion", "SpecificationName",
198:                        "SpecificationVendor", "SpecificationVersion" };
199:                String[] allowed = new String[0];
200:                String[] wanted = new String[0];
201:
202:                try {
203:                    server.getAttributes(delegate, allAttributes);
204:                    fail();
205:                } catch (SecurityException ignored) {
206:                }
207:
208:                // Check that for wrong attribute I get an empty list
209:                allowed = new String[] { allAttributes[1] };
210:                wanted = new String[] { allAttributes[0] };
211:                addPermission(new MBeanPermission("#" + allowed[0] + "["
212:                        + delegate.getCanonicalName() + "]", "getAttribute"));
213:                AttributeList list = server.getAttributes(delegate, wanted);
214:                assertEquals(list.size(), 0);
215:
216:                // Check that for the right attribute I get it
217:                resetPermissions();
218:                allowed = new String[] { allAttributes[0] };
219:                wanted = allAttributes;
220:                addPermission(new MBeanPermission("#" + allowed[0] + "["
221:                        + delegate.getCanonicalName() + "]", "getAttribute"));
222:                list = server.getAttributes(delegate, wanted);
223:                assertEquals(list.size(), allowed.length);
224:                Attribute attrib = (Attribute) list.get(0);
225:                assertEquals(attrib.getName(), allowed[0]);
226:
227:                // Check that if I grant some I only get some
228:                resetPermissions();
229:                // Only attributes that start with 'Implementation'
230:                allowed = new String[] { allAttributes[1], allAttributes[2],
231:                        allAttributes[3] };
232:                wanted = allAttributes;
233:                addPermission(new MBeanPermission("#Implementation*["
234:                        + delegate.getCanonicalName() + "]", "getAttribute"));
235:                list = server.getAttributes(delegate, wanted);
236:                assertEquals(list.size(), allowed.length);
237:                for (int i = 0; i < list.size(); ++i) {
238:                    Attribute attr = (Attribute) list.get(i);
239:                    assertEquals(attr.getName(), allowed[i]);
240:                }
241:
242:                // Check that if I grant all I get them all
243:                resetPermissions();
244:                allowed = allAttributes;
245:                wanted = allAttributes;
246:                addPermission(new MBeanPermission("["
247:                        + delegate.getCanonicalName() + "]", "getAttribute"));
248:                list = server.getAttributes(delegate, allAttributes);
249:                assertEquals(list.size(), allowed.length);
250:                for (int i = 0; i < list.size(); ++i) {
251:                    Attribute attr = (Attribute) list.get(i);
252:                    assertEquals(attr.getName(), allowed[i]);
253:                }
254:            }
255:
256:            protected void testGetDefaultDomain(MBeanServerConnection server,
257:                    String domain) throws Exception {
258:                String result = server.getDefaultDomain();
259:                assertEquals(result, domain);
260:            }
261:
262:            protected void testGetDomains(MBeanServerConnection server)
263:                    throws Exception {
264:                try {
265:                    server.getDomains();
266:                    fail();
267:                } catch (SecurityException e) {
268:                    // OK
269:                }
270:                ObjectName name = new ObjectName("test:x=x");
271:                addPermission(new MBeanPermission(Simple.class.getName(), null,
272:                        name, "instantiate"));
273:                addPermission(new MBeanPermission(Simple.class.getName(), null,
274:                        name, "registerMBean"));
275:                addPermission(new MBeanTrustPermission("register"));
276:                server.createMBean(Simple.class.getName(), name);
277:
278:                addPermission(new MBeanPermission(null, null, name,
279:                        "getDomains"));
280:
281:                String[] results = server.getDomains();
282:                assertNotNull(results);
283:                assertEquals(results.length, 1);
284:                assertEquals(results[0], "test");
285:            }
286:
287:            protected void testGetMBeanCount(MBeanServerConnection server)
288:                    throws Exception {
289:                Integer count = server.getMBeanCount();
290:                assertNotNull(count);
291:                assertTrue(count.intValue() > 1);
292:            }
293:
294:            protected void testGetMBeanInfo(MBeanServerConnection server)
295:                    throws Exception {
296:                ObjectName delegate = new ObjectName("JMImplementation",
297:                        "type", "MBeanServerDelegate");
298:                try {
299:                    server.getMBeanInfo(delegate);
300:                    fail();
301:                } catch (SecurityException ignored) {
302:                }
303:
304:                addPermission(new MBeanPermission("["
305:                        + delegate.getCanonicalName() + "]", "getMBeanInfo"));
306:                MBeanInfo info = server.getMBeanInfo(delegate);
307:                assertNotNull(info);
308:            }
309:
310:            protected void testGetObjectInstance(MBeanServerConnection server)
311:                    throws Exception {
312:                ObjectName delegate = new ObjectName("JMImplementation",
313:                        "type", "MBeanServerDelegate");
314:                try {
315:                    server.getObjectInstance(delegate);
316:                    fail();
317:                } catch (SecurityException ignored) {
318:                }
319:
320:                addPermission(new MBeanPermission("["
321:                        + delegate.getCanonicalName() + "]",
322:                        "getObjectInstance"));
323:                ObjectInstance instance = server.getObjectInstance(delegate);
324:                assertNotNull(instance);
325:            }
326:
327:            protected void testInvoke(MBeanServerConnection server)
328:                    throws Exception {
329:                ObjectName mbeanName = new ObjectName(
330:                        server.getDefaultDomain(), "mbean", "simple");
331:                addPermission(new MBeanPermission(Simple.class.getName(),
332:                        "instantiate, registerMBean"));
333:                addPermission(new MBeanTrustPermission("register"));
334:                server.createMBean(Simple.class.getName(), mbeanName, null);
335:
336:                addPermission(new MBeanPermission(Simple.class.getName(),
337:                        "setAttribute"));
338:                String initial = "mx4j";
339:                server.setAttribute(mbeanName, new Attribute("FirstAttribute",
340:                        initial));
341:
342:                String value = "simon";
343:                String operation = "concatenateWithFirstAttribute";
344:
345:                try {
346:                    server.invoke(mbeanName, operation, new Object[] { value },
347:                            new String[] { String.class.getName() });
348:                    fail();
349:                } catch (SecurityException ignored) {
350:                }
351:
352:                addPermission(new MBeanPermission("#" + operation + "["
353:                        + mbeanName.getCanonicalName() + "]", "invoke"));
354:                String result = (String) server.invoke(mbeanName, operation,
355:                        new Object[] { value }, new String[] { String.class
356:                                .getName() });
357:                assertEquals(result, initial + value);
358:            }
359:
360:            protected void testIsInstanceOf(MBeanServerConnection server)
361:                    throws Exception {
362:                ObjectName delegate = new ObjectName("JMImplementation",
363:                        "type", "MBeanServerDelegate");
364:                String className = "javax.management.MBeanServerDelegateMBean";
365:
366:                try {
367:                    server.isInstanceOf(delegate, className);
368:                    fail();
369:                } catch (SecurityException ignored) {
370:                }
371:
372:                addPermission(new MBeanPermission("["
373:                        + delegate.getCanonicalName() + "]", "isInstanceOf"));
374:                boolean isInstance = server.isInstanceOf(delegate, className);
375:                assertTrue(isInstance);
376:            }
377:
378:            protected void testIsRegistered(MBeanServerConnection server)
379:                    throws Exception {
380:                ObjectName delegate = new ObjectName("JMImplementation",
381:                        "type", "MBeanServerDelegate");
382:                boolean registered = server.isRegistered(delegate);
383:                assertTrue(registered);
384:            }
385:
386:            protected void testQueryMBeans(MBeanServerConnection server)
387:                    throws Exception {
388:                ObjectName mbeanName = new ObjectName(
389:                        server.getDefaultDomain(), "mbean", "simple");
390:                addPermission(new MBeanPermission(Simple.class.getName(),
391:                        "instantiate, registerMBean"));
392:                addPermission(new MBeanTrustPermission("register"));
393:                server.createMBean(Simple.class.getName(), mbeanName, null);
394:
395:                try {
396:                    server.queryMBeans(null, null);
397:                    fail();
398:                } catch (SecurityException ignored) {
399:                }
400:
401:                // Check that an ObjectName that does not match returns an empty list
402:                ObjectName dummy = new ObjectName(server.getDefaultDomain(),
403:                        "type", "dummy");
404:                addPermission(new MBeanPermission("["
405:                        + dummy.getCanonicalName() + "]", "queryMBeans"));
406:                Set mbeans = server.queryMBeans(null, null);
407:                assertEquals(mbeans.size(), 0);
408:
409:                // Now check the right one
410:                resetPermissions();
411:                addPermission(new MBeanPermission("["
412:                        + mbeanName.getCanonicalName() + "]", "queryMBeans"));
413:                mbeans = server.queryMBeans(null, null);
414:                assertEquals(mbeans.size(), 1);
415:                ObjectInstance instance = (ObjectInstance) mbeans.iterator()
416:                        .next();
417:                assertEquals(instance.getObjectName(), mbeanName);
418:
419:                // Check the right one for a pattern in permission
420:                resetPermissions();
421:                addPermission(new MBeanPermission("["
422:                        + server.getDefaultDomain() + ":*]", "queryMBeans"));
423:                mbeans = server.queryMBeans(null, null);
424:                assertEquals(mbeans.size(), 1);
425:                instance = (ObjectInstance) mbeans.iterator().next();
426:                assertEquals(instance.getObjectName(), mbeanName);
427:
428:                // Check for another pattern
429:                resetPermissions();
430:                addPermission(new MBeanPermission("[JMImplementation:*]",
431:                        "queryMBeans"));
432:                mbeans = server.queryMBeans(null, null);
433:                assertTrue(mbeans.size() >= 1);
434:                for (Iterator iterator = mbeans.iterator(); iterator.hasNext();) {
435:                    instance = (ObjectInstance) iterator.next();
436:                    assertEquals(instance.getObjectName().getDomain(),
437:                            "JMImplementation");
438:                }
439:
440:                // Check for all
441:                resetPermissions();
442:                addPermission(new MBeanPermission("*", "queryMBeans"));
443:                // Try queryNames to see if the permission is implies
444:                mbeans = server.queryNames(null, null);
445:                assertEquals(mbeans.size(), server.getMBeanCount().intValue());
446:
447:                // Check for the query expression
448:                resetPermissions();
449:                String className = "mx4j.server.MX4JMBeanServerDelegate";
450:                addPermission(new MBeanPermission(className, "queryMBeans"));
451:                QueryExp exp = Query.eq(Query.attr(className,
452:                        "ImplementationName"), Query.value("MX4J"));
453:                mbeans = server.queryMBeans(null, exp);
454:                assertEquals(mbeans.size(), 0);
455:
456:                // Now grant also the permission to retrieve the attribute
457:                addPermission(new MBeanPermission(className,
458:                        "getAttribute, getObjectInstance"));
459:                mbeans = server.queryMBeans(null, exp);
460:                assertEquals(mbeans.size(), 1);
461:                instance = (ObjectInstance) mbeans.iterator().next();
462:                assertEquals(instance.getClassName(), className);
463:            }
464:
465:            protected void testQueryNames(MBeanServerConnection server)
466:                    throws Exception {
467:                ObjectName mbeanName = new ObjectName(
468:                        server.getDefaultDomain(), "mbean", "simple");
469:                addPermission(new MBeanPermission(Simple.class.getName(),
470:                        "instantiate, registerMBean"));
471:                addPermission(new MBeanTrustPermission("register"));
472:                server.createMBean(Simple.class.getName(), mbeanName, null);
473:
474:                try {
475:                    server.queryNames(null, null);
476:                    fail();
477:                } catch (SecurityException ignored) {
478:                }
479:
480:                // Check that an ObjectName that does not match returns an empty list
481:                ObjectName dummy = new ObjectName(server.getDefaultDomain(),
482:                        "type", "dummy");
483:                addPermission(new MBeanPermission("["
484:                        + dummy.getCanonicalName() + "]", "queryNames"));
485:                Set mbeans = server.queryNames(null, null);
486:                assertEquals(mbeans.size(), 0);
487:
488:                // Now check the right one
489:                resetPermissions();
490:                addPermission(new MBeanPermission("["
491:                        + mbeanName.getCanonicalName() + "]", "queryNames"));
492:                mbeans = server.queryNames(null, null);
493:                assertEquals(mbeans.size(), 1);
494:                ObjectName name = (ObjectName) mbeans.iterator().next();
495:                assertEquals(name, mbeanName);
496:
497:                // Check the right one for a pattern in permission
498:                resetPermissions();
499:                addPermission(new MBeanPermission("["
500:                        + server.getDefaultDomain() + ":*]", "queryNames"));
501:                mbeans = server.queryNames(null, null);
502:                assertEquals(mbeans.size(), 1);
503:                name = (ObjectName) mbeans.iterator().next();
504:                assertEquals(name, mbeanName);
505:
506:                // Check for another pattern
507:                resetPermissions();
508:                addPermission(new MBeanPermission("[JMImplementation:*]",
509:                        "queryNames"));
510:                mbeans = server.queryNames(null, null);
511:                assertTrue(mbeans.size() >= 1);
512:                for (Iterator iterator = mbeans.iterator(); iterator.hasNext();) {
513:                    name = (ObjectName) iterator.next();
514:                    assertEquals(name.getDomain(), "JMImplementation");
515:                }
516:
517:                // Check for all
518:                resetPermissions();
519:                addPermission(new MBeanPermission("*", "queryNames"));
520:                mbeans = server.queryNames(null, null);
521:                assertEquals(mbeans.size(), server.getMBeanCount().intValue());
522:
523:                // Check for the query expression
524:                resetPermissions();
525:                String className = "mx4j.server.MX4JMBeanServerDelegate";
526:                addPermission(new MBeanPermission(className, "queryNames"));
527:                QueryExp exp = Query.eq(Query.attr(className,
528:                        "ImplementationName"), Query.value("MX4J"));
529:                mbeans = server.queryNames(null, exp);
530:                assertEquals(mbeans.size(), 0);
531:
532:                // Now grant also the permission to retrieve the attribute
533:                addPermission(new MBeanPermission(className,
534:                        "getAttribute, getObjectInstance"));
535:                mbeans = server.queryNames(null, exp);
536:                assertEquals(mbeans.size(), 1);
537:                name = (ObjectName) mbeans.iterator().next();
538:                assertEquals(name, new ObjectName("JMImplementation", "type",
539:                        "MBeanServerDelegate"));
540:            }
541:
542:            protected void testSetAttribute(MBeanServerConnection server)
543:                    throws Exception {
544:                ObjectName name = new ObjectName(server.getDefaultDomain(),
545:                        "name", "test");
546:                addPermission(new MBeanPermission(Simple.class.getName(),
547:                        "instantiate, registerMBean"));
548:                addPermission(new MBeanTrustPermission("register"));
549:                server.createMBean(Simple.class.getName(), name, null);
550:
551:                String attributeName = "FirstAttribute";
552:                String value = "first";
553:                Attribute attribute = new Attribute(attributeName, value);
554:
555:                try {
556:                    server.setAttribute(name, attribute);
557:                    fail();
558:                } catch (SecurityException ignored) {
559:                }
560:
561:                addPermission(new MBeanPermission("#" + attributeName + "["
562:                        + name.getCanonicalName() + "]", "setAttribute"));
563:                server.setAttribute(name, attribute);
564:
565:                // Check that it worked
566:                addPermission(new MBeanPermission("#" + attributeName,
567:                        "getAttribute"));
568:                String result = (String) server.getAttribute(name,
569:                        attributeName);
570:                assertEquals(result, value);
571:            }
572:
573:            protected void testSetAttributes(MBeanServerConnection server)
574:                    throws Exception {
575:                ObjectName mbeanName = new ObjectName(
576:                        server.getDefaultDomain(), "mbean", "simple");
577:                addPermission(new MBeanPermission(Simple.class.getName(),
578:                        "instantiate, registerMBean"));
579:                addPermission(new MBeanTrustPermission("register"));
580:                server.createMBean(Simple.class.getName(), mbeanName, null);
581:
582:                AttributeList allAttributes = new AttributeList();
583:                allAttributes.add(new Attribute("FirstAttribute", "first"));
584:                allAttributes.add(new Attribute("SecondAttribute", "second"));
585:                allAttributes.add(new Attribute("ThirdAttribute", "third"));
586:                allAttributes.add(new Attribute("Running", Boolean.TRUE));
587:
588:                AttributeList allowed = new AttributeList();
589:                AttributeList wanted = new AttributeList();
590:
591:                try {
592:                    server.setAttributes(mbeanName, allAttributes);
593:                    fail();
594:                } catch (SecurityException ignored) {
595:                }
596:
597:                // Check that for wrong attribute I get an empty list
598:                allowed.clear();
599:                allowed.add(allAttributes.get(0));
600:                wanted.clear();
601:                wanted.add(allAttributes.get(1));
602:                addPermission(new MBeanPermission("#"
603:                        + ((Attribute) allowed.get(0)).getName() + "["
604:                        + mbeanName.getCanonicalName() + "]", "setAttribute"));
605:                AttributeList list = server.setAttributes(mbeanName, wanted);
606:                assertEquals(list.size(), 0);
607:
608:                // Check that for the right attribute I get it
609:                resetPermissions();
610:                allowed.clear();
611:                allowed.add(allAttributes.get(0));
612:                wanted.clear();
613:                wanted.addAll(allAttributes);
614:                addPermission(new MBeanPermission("#"
615:                        + ((Attribute) allowed.get(0)).getName() + "["
616:                        + mbeanName.getCanonicalName() + "]", "setAttribute"));
617:                list = server.setAttributes(mbeanName, wanted);
618:                assertEquals(list.size(), allowed.size());
619:                Attribute attrib = (Attribute) list.get(0);
620:                assertEquals(attrib, allowed.get(0));
621:
622:                // Check that if I grant some I only get some
623:                resetPermissions();
624:                // Only attributes that ends with 'Attribute'
625:                allowed.clear();
626:                allowed.add(allAttributes.get(0));
627:                allowed.add(allAttributes.get(1));
628:                allowed.add(allAttributes.get(2));
629:                wanted.clear();
630:                wanted.addAll(allAttributes);
631:                addPermission(new MBeanPermission("#*Attribute["
632:                        + mbeanName.getCanonicalName() + "]", "setAttribute"));
633:                list = server.setAttributes(mbeanName, wanted);
634:                assertEquals(list.size(), allowed.size());
635:                for (int i = 0; i < list.size(); ++i) {
636:                    Attribute attr = (Attribute) list.get(i);
637:                    assertEquals(attr, allowed.get(i));
638:                }
639:
640:                // Check that if I grant all I get them all
641:                resetPermissions();
642:                allowed.clear();
643:                allowed.addAll(allAttributes);
644:                wanted.clear();
645:                wanted.addAll(allAttributes);
646:                addPermission(new MBeanPermission("["
647:                        + mbeanName.getCanonicalName() + "]", "setAttribute"));
648:                list = server.setAttributes(mbeanName, wanted);
649:                assertEquals(list.size(), allowed.size());
650:                for (int i = 0; i < list.size(); ++i) {
651:                    Attribute attr = (Attribute) list.get(i);
652:                    assertEquals(attr, allowed.get(i));
653:                }
654:            }
655:
656:            protected void testUnregisterMBean(MBeanServerConnection server)
657:                    throws Exception {
658:                ObjectName name = new ObjectName(server.getDefaultDomain(),
659:                        "name", "test");
660:                addPermission(new MBeanPermission(Simple.class.getName(),
661:                        "instantiate, registerMBean"));
662:                addPermission(new MBeanTrustPermission("register"));
663:                server.createMBean(Simple.class.getName(), name, null);
664:
665:                try {
666:                    server.unregisterMBean(name);
667:                    fail();
668:                } catch (SecurityException ignored) {
669:                }
670:
671:                addPermission(new MBeanPermission(Simple.class.getName() + "["
672:                        + name.getCanonicalName() + "]", "unregisterMBean"));
673:                server.unregisterMBean(name);
674:            }
675:
676:            public interface SimpleMBean {
677:                public void setFirstAttribute(String value);
678:
679:                public String getFirstAttribute();
680:
681:                public void setSecondAttribute(String value);
682:
683:                public String getSecondAttribute();
684:
685:                public void setThirdAttribute(String value);
686:
687:                public String getThirdAttribute();
688:
689:                public void setRunning(boolean value);
690:
691:                public boolean isRunning();
692:
693:                public String concatenateWithFirstAttribute(String value);
694:            }
695:
696:            public static class Simple implements  SimpleMBean {
697:                private String firstAttribute;
698:                private String secondAttribute;
699:                private String thirdAttribute;
700:                private boolean running;
701:
702:                public String getFirstAttribute() {
703:                    return firstAttribute;
704:                }
705:
706:                public void setFirstAttribute(String value) {
707:                    this .firstAttribute = value;
708:                }
709:
710:                public String getSecondAttribute() {
711:                    return secondAttribute;
712:                }
713:
714:                public void setSecondAttribute(String secondAttribute) {
715:                    this .secondAttribute = secondAttribute;
716:                }
717:
718:                public String getThirdAttribute() {
719:                    return thirdAttribute;
720:                }
721:
722:                public void setThirdAttribute(String thirdAttribute) {
723:                    this .thirdAttribute = thirdAttribute;
724:                }
725:
726:                public boolean isRunning() {
727:                    return running;
728:                }
729:
730:                public void setRunning(boolean running) {
731:                    this .running = running;
732:                }
733:
734:                public String concatenateWithFirstAttribute(String value) {
735:                    return this.firstAttribute + value;
736:                }
737:            }
738:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.