Source Code Cross Referenced for SecurityTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » auth » internal » 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 » Apache Harmony Java SE » org package » org.apache.harmony.auth.internal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  See the NOTICE file distributed with
0004:         *  this work for additional information regarding copyright ownership.
0005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
0006:         *  (the "License"); you may not use this file except in compliance with
0007:         *  the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */
0017:
0018:        /**
0019:         * @author Stepan M. Mishura
0020:         * @version $Revision$
0021:         */package org.apache.harmony.auth.internal;
0022:
0023:        import java.security.AccessControlContext;
0024:        import java.security.AccessControlException;
0025:        import java.security.AllPermission;
0026:        import java.security.Permission;
0027:        import java.security.Permissions;
0028:        import java.security.ProtectionDomain;
0029:        import java.util.HashSet;
0030:        import java.util.Iterator;
0031:        import java.util.NoSuchElementException;
0032:        import java.util.PropertyPermission;
0033:        import java.util.Set;
0034:        import java.util.Vector;
0035:        import java.util.logging.LoggingPermission;
0036:
0037:        import javax.security.auth.AuthPermission;
0038:        import javax.security.auth.Subject;
0039:        import javax.security.auth.SubjectTest.MyClass1;
0040:        import javax.security.auth.SubjectTest.MyClass2;
0041:
0042:        import junit.framework.TestCase;
0043:
0044:        /**
0045:         * Custom security manager 
0046:         * 
0047:         * Note: this class is subject to change,
0048:         * please notify the author before using it 
0049:         * 
0050:         */
0051:
0052:        public class SecurityTest extends TestCase {
0053:
0054:            public static boolean testing = false;
0055:
0056:            private Permissions permissionsDenied = new Permissions();
0057:
0058:            private Permissions permissionsGranted = new Permissions();
0059:
0060:            // Represents a mode(grant/deny) for installed Security Manager.
0061:            private boolean mode = true;
0062:
0063:            public SecurityTest() {
0064:                denyMode(); //set default mode
0065:            }
0066:
0067:            public SecurityTest(String name) {
0068:                super (name);
0069:                denyMode(); //set default mode
0070:            }
0071:
0072:            @Override
0073:            protected void runTest() throws Throwable {
0074:                if (System.getSecurityManager() != null) {
0075:                    fail("There MUST be no security manager installed!");
0076:                }
0077:
0078:                SecurityManager sm = new SecurityManager() {
0079:                    @Override
0080:                    public void checkPermission(Permission permission) {
0081:                        //System.out.println("P: " + permission);
0082:                        if (mode) { //deny mode
0083:                            if (!permissionsDenied.implies(permission)) {
0084:                                return;
0085:                            }
0086:                        } else { // grant mode
0087:
0088:                            //System.out.println("P: " + permissionsGranted);
0089:                            if (permissionsGranted.implies(permission)) {
0090:                                return;
0091:                            }
0092:                        }
0093:
0094:                        // throw exception
0095:                        ProtectionDomain domain = new ProtectionDomain(null,
0096:                                new Permissions());
0097:
0098:                        ProtectionDomain[] context = new ProtectionDomain[] { domain };
0099:                        AccessControlContext accContext = new AccessControlContext(
0100:                                context);
0101:
0102:                        accContext.checkPermission(permission);
0103:                    }
0104:                };
0105:
0106:                System.setSecurityManager(sm);
0107:
0108:                try {
0109:                    super .runTest();
0110:                } finally {
0111:                    System.setSecurityManager(null);
0112:                }
0113:            }
0114:
0115:            /**
0116:             * Verifies that exception has correct associated permission class
0117:             *
0118:             * @param exception - to be verified
0119:             * @param permission - permission class for comparing
0120:             */
0121:            public final void assertEquals(AccessControlException exception,
0122:                    Class<? extends Permission> permission) {
0123:                if (!permission.isInstance(exception.getPermission())) {
0124:                    fail("No expected " + permission.getName());
0125:                }
0126:            }
0127:
0128:            /**
0129:             * Grants specified permission
0130:             * It is used only in grant mode
0131:             */
0132:            public void grantPermission(Permission permission) {
0133:
0134:                assertFalse("Grant mode", mode);
0135:
0136:                permissionsGranted.add(permission);
0137:
0138:                //System.out.println("PG: " + permission);
0139:            }
0140:
0141:            /**
0142:             * Denies specified permission
0143:             * It is used only in deny mode
0144:             */
0145:            public void denyPermission(Permission permission) {
0146:
0147:                assertTrue("Deny mode", mode);
0148:
0149:                permissionsDenied.add(permission);
0150:            }
0151:
0152:            /**
0153:             * Sets deny mode
0154:             * all permissions are granted, test can only deny specific permission
0155:             */
0156:            public void denyMode() {
0157:                mode = true;
0158:
0159:                permissionsGranted.add(new AllPermission());
0160:                permissionsDenied = new Permissions();
0161:            }
0162:
0163:            /**
0164:             * Sets grant mode
0165:             * all permissions are denied, test can only grant specific permission
0166:             */
0167:            public void grantMode() {
0168:                mode = false;
0169:
0170:                permissionsDenied.add(new AllPermission());
0171:                permissionsGranted = new Permissions();
0172:
0173:                // junit harness stuff
0174:                permissionsGranted.add(new PropertyPermission("line.separator",
0175:                        "read"));
0176:                permissionsGranted.add(new RuntimePermission("exitVM"));
0177:                permissionsGranted.add(new LoggingPermission("control", null));
0178:
0179:                //grant permission to install security manager :-)
0180:                permissionsGranted.add(new RuntimePermission(
0181:                        "setSecurityManager"));
0182:            }
0183:
0184:            /**
0185:             * Tests iterator interface
0186:             * 
0187:             */
0188:            @SuppressWarnings("unchecked")
0189:            public static class IteratorTest extends SecurityTest {
0190:
0191:                /**
0192:                 * Tested <code>set</code>. Must be initialized in derived class 
0193:                 */
0194:                public Set set;
0195:
0196:                /**
0197:                 * Set's <code>element</code>. Must be initialized in derived class
0198:                 */
0199:                public Object element;
0200:
0201:                public IteratorTest() {
0202:                    super ("IteratorTestSuite");
0203:                }
0204:
0205:                public IteratorTest(String name) {
0206:                    super (name);
0207:                }
0208:
0209:                @Override
0210:                protected void setUp() throws Exception {
0211:                    super .setUp();
0212:
0213:                    assertTrue("IteratorTest: suite MUST be initialized",
0214:                            set != null && element != null);
0215:
0216:                    assertEquals("IteratorTest: set MUST be empty", 0, set
0217:                            .size());
0218:                }
0219:
0220:                /**
0221:                 * Checks return value of Iterator.hasNext() for empty set
0222:                 * 
0223:                 * Expected: must return false 
0224:                 */
0225:                public void testHasNext_EmptySet() {
0226:                    assertFalse("Set is empty", set.iterator().hasNext());
0227:                }
0228:
0229:                /**
0230:                 * Checks return value of Iterator.hasNext() for not empty set
0231:                 * 
0232:                 * Expected: must return true
0233:                 */
0234:                public void testHasNext() {
0235:
0236:                    set.add(element);
0237:
0238:                    assertTrue("Set is not empty", set.iterator().hasNext());
0239:                }
0240:
0241:                /**
0242:                 * Checks Iterator.next() for empty set
0243:                 * 
0244:                 * Expected: NoSuchElementException
0245:                 */
0246:                public void testNext_EmptySet_NoSuchElementException() {
0247:
0248:                    try {
0249:                        set.iterator().next();
0250:                        fail("No expected NoSuchElementException");
0251:                    } catch (NoSuchElementException e) {
0252:                    }
0253:                }
0254:
0255:                /**
0256:                 * Checks Iterator.next() for not empty set
0257:                 * 
0258:                 * Expected: no exception, returned element is equals to added
0259:                 */
0260:                public void testNext() {
0261:
0262:                    set.add(element);
0263:
0264:                    Iterator it = set.iterator();
0265:
0266:                    assertEquals("Element", it.next(), element);
0267:                    assertFalse("Next element", it.hasNext());
0268:                }
0269:
0270:                /**
0271:                 * Calls Iterator.next() twice for set with one element
0272:                 * 
0273:                 * Expected: NoSuchElementException
0274:                 */
0275:                public void testNext_NoSuchElementException() {
0276:
0277:                    set.add(element);
0278:
0279:                    Iterator it = set.iterator();
0280:
0281:                    it.next();
0282:                    try {
0283:                        it.next();
0284:                        fail("No expected NoSuchElementException");
0285:                    } catch (NoSuchElementException e) {
0286:                    }
0287:                }
0288:
0289:                /**
0290:                 * Remove element from set
0291:                 * 
0292:                 * Expected: no exception, size must be 0
0293:                 */
0294:                public void testRemove() {
0295:
0296:                    set.add(element);
0297:
0298:                    Iterator it = set.iterator();
0299:
0300:                    it.next();
0301:                    it.remove();
0302:
0303:                    assertFalse("Next element", it.hasNext());
0304:                    assertEquals("Set size", 0, set.size());
0305:                }
0306:
0307:                /**
0308:                 * Tries to remove element from empty set.
0309:                 * Iterator.next() was not invoked before
0310:                 * 
0311:                 * Expected: IllegalStateException
0312:                 */
0313:                public void testRemove_EmptySet_IllegalStateException() {
0314:
0315:                    try {
0316:                        set.iterator().remove();
0317:                        fail("No expected IllegalStateException");
0318:                    } catch (IllegalStateException e) {
0319:                    }
0320:                }
0321:
0322:                /**
0323:                 * Tries to remove element from not empty set.
0324:                 * Iterator.next() was not invoked before
0325:                 * 
0326:                 * Expected: IllegalStateException
0327:                 */
0328:                public void testRemove_IllegalStateException_NoNext() {
0329:
0330:                    set.add(element);
0331:
0332:                    try {
0333:                        set.iterator().remove();
0334:                        fail("No expected IllegalStateException");
0335:                    } catch (IllegalStateException e) {
0336:                    }
0337:                }
0338:
0339:                /**
0340:                 * Tries to remove element from not empty set twice.
0341:                 * 
0342:                 * Expected: IllegalStateException
0343:                 */
0344:                public void testRemove_IllegalStateException_2Remove() {
0345:
0346:                    set.add(element);
0347:
0348:                    Iterator it = set.iterator();
0349:
0350:                    it.next();
0351:                    it.remove();
0352:                    try {
0353:                        it.remove();
0354:                        fail("No expected IllegalStateException");
0355:                    } catch (IllegalStateException e) {
0356:                    }
0357:                }
0358:            }
0359:
0360:            /**
0361:             * Tests iterator interface for read only set
0362:             * 
0363:             */
0364:            @SuppressWarnings("unchecked")
0365:            public static class ReadOnlyIteratorTest extends IteratorTest {
0366:
0367:                public void setReadOnly() {
0368:                    throw new UnsupportedOperationException(
0369:                            "setReadOnly MUST be implemented in derived class");
0370:                }
0371:
0372:                /**
0373:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testHasNext_EmptySet()
0374:                 */
0375:                @Override
0376:                public void testHasNext_EmptySet() {
0377:                    setReadOnly();
0378:                    super .testHasNext_EmptySet();
0379:                }
0380:
0381:                /**
0382:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testHasNext()
0383:                 */
0384:                @Override
0385:                public void testHasNext() {
0386:
0387:                    set.add(element);
0388:                    setReadOnly();
0389:
0390:                    assertTrue("Set is not empty", set.iterator().hasNext());
0391:                }
0392:
0393:                /**
0394:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testNext_EmptySet_NoSuchElementException()
0395:                 */
0396:                @Override
0397:                public void testNext_EmptySet_NoSuchElementException() {
0398:                    setReadOnly();
0399:                    super .testNext_EmptySet_NoSuchElementException();
0400:                }
0401:
0402:                /**
0403:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testNext()
0404:                 */
0405:                @Override
0406:                public void testNext() {
0407:
0408:                    set.add(element);
0409:                    setReadOnly();
0410:
0411:                    Iterator it = set.iterator();
0412:
0413:                    assertEquals("Element", it.next(), element);
0414:                    assertFalse("Next element", it.hasNext());
0415:                }
0416:
0417:                /**
0418:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testNext_NoSuchElementException()
0419:                 */
0420:                @Override
0421:                public void testNext_NoSuchElementException() {
0422:
0423:                    set.add(element);
0424:                    setReadOnly();
0425:
0426:                    Iterator it = set.iterator();
0427:
0428:                    it.next();
0429:                    try {
0430:                        it.next();
0431:                        fail("No expected NoSuchElementException");
0432:                    } catch (NoSuchElementException e) {
0433:                    }
0434:                }
0435:
0436:                /**
0437:                 * Remove element from read only set
0438:                 * 
0439:                 * Expected: IllegalStateException
0440:                 */
0441:                @Override
0442:                public void testRemove() {
0443:
0444:                    set.add(element);
0445:                    setReadOnly();
0446:
0447:                    Iterator it = set.iterator();
0448:
0449:                    it.next();
0450:                    try {
0451:                        it.remove();
0452:                        fail("No expected IllegalStateException");
0453:                    } catch (IllegalStateException e) {
0454:                    }
0455:                }
0456:
0457:                /**
0458:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testRemove_EmptySet_IllegalStateException()
0459:                 */
0460:                @Override
0461:                public void testRemove_EmptySet_IllegalStateException() {
0462:                    setReadOnly();
0463:                    super .testRemove_EmptySet_IllegalStateException();
0464:                }
0465:
0466:                /**
0467:                 * Tries to remove element from read only set.
0468:                 * Iterator.next() was not invoked before
0469:                 * 
0470:                 * Expected: IllegalStateException
0471:                 */
0472:                @Override
0473:                public void testRemove_IllegalStateException_NoNext() {
0474:
0475:                    set.add(element);
0476:                    setReadOnly();
0477:
0478:                    try {
0479:                        set.iterator().remove();
0480:                        fail("No expected IllegalStateException");
0481:                    } catch (IllegalStateException e) {
0482:                    }
0483:                }
0484:
0485:                /**
0486:                 * Tries to remove element from read only set.
0487:                 * 
0488:                 * Expected: IllegalStateException
0489:                 */
0490:                @Override
0491:                public void testRemove_IllegalStateException_2Remove() {
0492:
0493:                    set.add(element);
0494:
0495:                    Iterator it = set.iterator();
0496:
0497:                    it.next();
0498:                    setReadOnly();
0499:                    try {
0500:                        it.remove();
0501:                        fail("No expected IllegalStateException");
0502:                    } catch (IllegalStateException e) {
0503:                    }
0504:                }
0505:            }
0506:
0507:            /**
0508:             * Tests iterator interface for secure set
0509:             * 
0510:             */
0511:            @SuppressWarnings("unchecked")
0512:            public static class SecureIteratorTest extends IteratorTest {
0513:
0514:                public void setSecure() {
0515:                    throw new UnsupportedOperationException(
0516:                            "setSecure MUST be implemented in derived class");
0517:                }
0518:
0519:                /**
0520:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testHasNext_EmptySet()
0521:                 */
0522:                @Override
0523:                public void testHasNext_EmptySet() {
0524:                    setSecure();
0525:                    super .testHasNext_EmptySet();
0526:                }
0527:
0528:                /**
0529:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testHasNext()
0530:                 */
0531:                @Override
0532:                public void testHasNext() {
0533:                    set.add(element);
0534:                    setSecure();
0535:
0536:                    assertTrue("Set is not empty", set.iterator().hasNext());
0537:                }
0538:
0539:                /**
0540:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testNext_EmptySet_NoSuchElementException()
0541:                 */
0542:                @Override
0543:                public void testNext_EmptySet_NoSuchElementException() {
0544:                    setSecure();
0545:                    super .testNext_EmptySet_NoSuchElementException();
0546:                }
0547:
0548:                /**
0549:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testNext_NoSuchElementException()
0550:                 */
0551:                @Override
0552:                public void testNext_NoSuchElementException() {
0553:                    set.add(element);
0554:                    setSecure();
0555:
0556:                    Iterator it = set.iterator();
0557:
0558:                    it.next();
0559:                    try {
0560:                        it.next();
0561:                        fail("No expected NoSuchElementException");
0562:                    } catch (NoSuchElementException e) {
0563:                    }
0564:                }
0565:
0566:                /**
0567:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testNext()
0568:                 */
0569:                @Override
0570:                public void testNext() {
0571:                    set.add(element);
0572:                    setSecure();
0573:
0574:                    Iterator it = set.iterator();
0575:
0576:                    assertEquals("Element", it.next(), element);
0577:                    assertFalse("Next element", it.hasNext());
0578:                }
0579:
0580:                /**
0581:                 * Tries to remove element from secure set
0582:                 * 
0583:                 * Expected: AccessControlException
0584:                 */
0585:                @Override
0586:                public void testRemove() {
0587:                    set.add(element);
0588:                    setSecure();
0589:
0590:                    Iterator it = set.iterator();
0591:
0592:                    it.next();
0593:                    try {
0594:                        it.remove();
0595:                        fail("No expected AccessControlException");
0596:                    } catch (AccessControlException e) {
0597:                        assertEquals(e, AuthPermission.class);
0598:                    }
0599:
0600:                }
0601:
0602:                /**
0603:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testRemove_EmptySet_IllegalStateException()
0604:                 * 
0605:                 * Expected: AccessControlException instead IllegalStateException for empty set
0606:                 */
0607:                @Override
0608:                public void testRemove_EmptySet_IllegalStateException() {
0609:
0610:                    setSecure();
0611:                    try {
0612:                        super .testRemove_EmptySet_IllegalStateException();
0613:                        fail("No expected AccessControlException");
0614:                    } catch (AccessControlException e) {
0615:                        assertEquals(e, AuthPermission.class);
0616:                    }
0617:                }
0618:
0619:                /**
0620:                 * @see org.apache.harmony.auth.internal.SecurityTest.IteratorTest#testRemove_IllegalStateException_NoNext()
0621:                 * 
0622:                 * Expected: AccessControlException instead IllegalStateException
0623:                 */
0624:                @Override
0625:                public void testRemove_IllegalStateException_NoNext() {
0626:
0627:                    set.add(element);
0628:                    setSecure();
0629:
0630:                    try {
0631:                        set.iterator().remove();
0632:                        fail("No expected AccessControlException");
0633:                    } catch (AccessControlException e) {
0634:                        assertEquals(e, AuthPermission.class);
0635:                    }
0636:                }
0637:
0638:                /**
0639:                 * While iterating the set was secured
0640:                 * 
0641:                 * Expected: AccessControlException
0642:                 */
0643:                @Override
0644:                public void testRemove_IllegalStateException_2Remove() {
0645:                    set.add(element);
0646:
0647:                    Iterator it = set.iterator();
0648:
0649:                    it.next();
0650:                    setSecure();
0651:                    try {
0652:                        it.remove();
0653:                        fail("No expected AccessControlException");
0654:                    } catch (AccessControlException e) {
0655:                        assertEquals(e, AuthPermission.class);
0656:                    }
0657:                }
0658:            }
0659:
0660:            @SuppressWarnings("unchecked")
0661:            public static class SetTest extends SecurityTest {
0662:
0663:                /**
0664:                 * Tested <code>set</code>. Must be initialized in derived class 
0665:                 */
0666:                public Set set;
0667:
0668:                /**
0669:                 * Set's <code>element</code>. Must be initialized in derived class
0670:                 */
0671:                public Object element;
0672:
0673:                /**
0674:                 * Is used as collection parameter
0675:                 */
0676:                public HashSet<Object> hash = new HashSet<Object>();
0677:
0678:                @Override
0679:                protected void setUp() throws Exception {
0680:                    super .setUp();
0681:
0682:                    assertTrue("SetTest: suite MUST be initialized",
0683:                            set != null && element != null);
0684:
0685:                    assertEquals("SetTest: set MUST be empty", 0, set.size());
0686:
0687:                    hash.add(element);
0688:                }
0689:
0690:                //
0691:                // Testing: boolean Set.add(Object o)
0692:                //
0693:                public void testAdd_NewElement() {
0694:                    assertTrue("Adding new element", set.add(element));
0695:                    assertEquals("Size", 1, set.size());
0696:                }
0697:
0698:                public void testAdd_ExistingElement() {
0699:                    set.add(element);
0700:                    assertFalse("Adding existing element", set.add(element));
0701:                    assertEquals("Size", 1, set.size());
0702:                }
0703:
0704:                //
0705:                // Testing: boolean Set.addAll(Collection c)
0706:                //
0707:
0708:                public void testAddAll_NullParameter() {
0709:                    try {
0710:                        set.addAll(null);
0711:                        fail("No expected NullPointerException");
0712:                    } catch (NullPointerException npe) {
0713:                    }
0714:                }
0715:
0716:                public void testAddAll_EmptySet() {
0717:                    assertFalse("Adding empty set", set.addAll(new HashSet()));
0718:                    assertEquals("Size", 0, set.size());
0719:                }
0720:
0721:                public void testAddAll_NewElement() {
0722:                    assertTrue("Adding new element", set.addAll(hash));
0723:                    assertEquals("Size", 1, set.size());
0724:                }
0725:
0726:                public void testAddAll_ExistingElement() {
0727:                    set.add(element);
0728:                    assertFalse("Adding existing element", set.addAll(hash));
0729:                    assertEquals("Size", 1, set.size());
0730:                }
0731:
0732:                //
0733:                // Testing: void Set.clear()
0734:                //
0735:                public void testClear_EmptySet() {
0736:                    set.clear();
0737:                    assertEquals("Set MUST be empty", 0, set.size());
0738:                }
0739:
0740:                public void testClear_NotEmptySet() {
0741:                    set.add(element);
0742:                    set.clear();
0743:                    assertEquals("Set MUST be empty", 0, set.size());
0744:                }
0745:
0746:                //
0747:                // Testing: boolean Set.contains(Object o)
0748:                //
0749:                public void testContains_NotExistingElement() {
0750:                    assertFalse("Set doesn't contain element", set
0751:                            .contains(element));
0752:                }
0753:
0754:                public void testContains_ExistingElement() {
0755:                    set.add(element);
0756:                    assertTrue("Set contains element", set.contains(element));
0757:                }
0758:
0759:                //
0760:                // Testing: boolean Set.containsAll(Collection c)
0761:                //
0762:                public void testContainsAll_NullParameter() {
0763:                    try {
0764:                        set.containsAll(null);
0765:                        fail("No expected NullPointerException");
0766:                    } catch (NullPointerException npe) {
0767:                    }
0768:                }
0769:
0770:                public void testContainsAll_EmptySet() {
0771:                    assertTrue("Empty set", set.containsAll(new HashSet()));
0772:                }
0773:
0774:                public void testContainsAll_NotExistingElement() {
0775:                    assertFalse("Set doesn't contain element", set
0776:                            .containsAll(hash));
0777:                }
0778:
0779:                public void testContainsAll_ExistingElement() {
0780:                    set.add(element);
0781:                    assertTrue("Set contains element", set.containsAll(hash));
0782:                }
0783:
0784:                //
0785:                // Testing: boolean Set.isEmpty()
0786:                //
0787:
0788:                public void testIsEmpty_EmptySet() {
0789:                    assertTrue("Set is empty", set.isEmpty());
0790:                }
0791:
0792:                public void testIsEmpty_NotEmptySet() {
0793:                    set.add(element);
0794:                    assertFalse("Set is not empty", set.isEmpty());
0795:                }
0796:
0797:                //
0798:                // Testing: Iterator Set.iterator()
0799:                // 
0800:                // NOTE: doesn't test Iterator interface
0801:                //
0802:
0803:                public void testIterator_EmptySet() {
0804:                    assertNotNull("Iterator", set.iterator());
0805:                }
0806:
0807:                public void testIterator_NotEmptySet() {
0808:                    set.add(element);
0809:                    assertNotNull("Iterator", set.iterator());
0810:                }
0811:
0812:                //
0813:                // Testing: boolean Set.remove(Object o)
0814:                //
0815:
0816:                public void testRemove_NotExistingElement() {
0817:                    assertFalse("Removing absent element", set.remove(element));
0818:                    assertEquals("Size", 0, set.size());
0819:                }
0820:
0821:                public void testRemove_ExistingElement() {
0822:                    set.add(element);
0823:                    assertTrue("Removing element", set.remove(element));
0824:                    assertEquals("Size", 0, set.size());
0825:                }
0826:
0827:                //
0828:                // Testing: boolean Set.removeAll(Collection c)
0829:                //
0830:
0831:                public void testRemoveAll_NullParameter_EmptySet() {
0832:
0833:                    try {
0834:                        set.removeAll(null);
0835:                    } catch (NullPointerException npe) {
0836:                    }
0837:                }
0838:
0839:                public void testRemoveAll_NullParameter_NotEmptySet() {
0840:
0841:                    set.add(element);
0842:                    try {
0843:                        set.removeAll(null);
0844:                        fail("No expected NullPointerException");
0845:                    } catch (NullPointerException npe) {
0846:                    }
0847:                }
0848:
0849:                public void testRemoveAll_EmptySet() {
0850:                    assertFalse("Removing empty set", set
0851:                            .removeAll(new HashSet()));
0852:                    assertEquals("Size", 0, set.size());
0853:                }
0854:
0855:                public void testRemoveAll_NotEmptySet() {
0856:                    set.add(element);
0857:                    assertFalse("Removing empty set", set
0858:                            .removeAll(new HashSet()));
0859:                    assertEquals("Size", 1, set.size());
0860:                }
0861:
0862:                public void testRemoveAll_NotExistingElement() {
0863:                    assertFalse("Removing elements", set.removeAll(hash));
0864:                    assertEquals("Size MUST NOT change", 0, set.size());
0865:                }
0866:
0867:                public void testRemoveAll_ExistingElement() {
0868:                    set.add(element);
0869:                    assertTrue("Removing elements", set.removeAll(hash));
0870:                    assertEquals("Size", 0, set.size());
0871:                }
0872:
0873:                //
0874:                // Testing: boolean Set.retainAll(Collection c)
0875:                //
0876:                public void testRetainAll_NullParameter_EmptySet() {
0877:
0878:                    try {
0879:                        set.retainAll(null);
0880:
0881:                        // BUG Expected:  no exceptions
0882:                        if (!testing) {
0883:                            fail("No expected NullPointerException");
0884:                        }
0885:                    } catch (NullPointerException npe) {
0886:                    }
0887:                }
0888:
0889:                public void testRetainAll_NullParameter_NotEmptySet() {
0890:
0891:                    set.add(element);
0892:                    try {
0893:                        set.retainAll(null);
0894:                        fail("No expected NullPointerException");
0895:                    } catch (NullPointerException npe) {
0896:                    }
0897:                }
0898:
0899:                public void testRetainAll_EmptySet() {
0900:                    assertFalse("Removing all elements", set
0901:                            .retainAll(new HashSet()));
0902:                    assertEquals("Size", 0, set.size());
0903:                }
0904:
0905:                public void testRetainAll_NotEmptySet() {
0906:                    set.add(element);
0907:                    assertTrue("Removing all elements", set
0908:                            .retainAll(new HashSet()));
0909:                    assertEquals("Size", 0, set.size());
0910:                }
0911:
0912:                public void testRetainAll_NotExistingElement() {
0913:                    assertFalse("Removing all elements", set.retainAll(hash));
0914:                    assertEquals("Size", 0, set.size());
0915:                }
0916:
0917:                public void testRetainAll_ExistingElement() {
0918:                    set.add(element);
0919:                    assertFalse("Removing elements", set.retainAll(hash));
0920:                    assertEquals("Size", 1, set.size());
0921:                }
0922:
0923:                //
0924:                // Testing: Object[] Set.toArray()
0925:                //
0926:                public void testToArray_EmptySet() {
0927:                    assertEquals("Set is empty", set.toArray().length, 0);
0928:                }
0929:
0930:                public void testToArray_NotEmptySet() {
0931:                    set.add(element);
0932:                    assertEquals("Set is not empty", set.toArray().length, 1);
0933:                    assertTrue("Set element", set.toArray()[0] == element);
0934:                }
0935:
0936:                public void testToArray_Immutability() {
0937:                    set.add(element);
0938:                    set.toArray()[0] = null;
0939:                    assertTrue("Element", set.toArray()[0] == element);
0940:                }
0941:
0942:                //TODO test Object[] Set.toArray(Object[] a)
0943:            }
0944:
0945:            @SuppressWarnings("unchecked")
0946:            public static class UnsupportedNullTest extends SecurityTest {
0947:
0948:                /**
0949:                 * Tested <code>set</code>. Must be initialized in derived class 
0950:                 */
0951:                public Set set;
0952:
0953:                /**
0954:                 * Set's <code>element</code>. Must be initialized in derived class
0955:                 */
0956:                public Object element;
0957:
0958:                /**
0959:                 * Is used as collection parameter
0960:                 */
0961:                public HashSet hash = new HashSet();
0962:
0963:                @Override
0964:                protected void setUp() throws Exception {
0965:                    super .setUp();
0966:
0967:                    assertTrue(
0968:                            "UnsupportedNullTest: suite MUST be initialized",
0969:                            set != null && element != null);
0970:
0971:                    assertEquals("UnsupportedNullTest: set MUST be empty", 0,
0972:                            set.size());
0973:
0974:                    hash.add(null);
0975:                }
0976:
0977:                public void testAdd() {
0978:
0979:                    if (testing) {
0980:                        try {
0981:                            set.add(null);
0982:                            // priv/pub credential set: no NullPointerException
0983:                        } catch (NullPointerException e) {
0984:                            assertEquals("Size", 0, set.size());
0985:                        } catch (SecurityException e) {
0986:                            // principal set: SecurityException
0987:                        }
0988:                    } else {
0989:                        try {
0990:                            set.add(null);
0991:                            fail("No expected NullPointerException");
0992:                        } catch (NullPointerException e) {
0993:                        }
0994:                        assertEquals("Size", 0, set.size());
0995:                    }
0996:                }
0997:
0998:                public void testAddAll() {
0999:
1000:                    if (testing) {
1001:                        try {
1002:                            set.addAll(hash);
1003:                            // priv/pub credentials set: no NullPointerException
1004:                        } catch (NullPointerException e) {
1005:                            assertEquals("Size", 0, set.size());
1006:                        } catch (SecurityException e) {
1007:                            // principal set: SecurityException
1008:                        }
1009:                    } else {
1010:                        try {
1011:                            set.addAll(hash);
1012:                            fail("No expected NullPointerException");
1013:                        } catch (NullPointerException e) {
1014:                        }
1015:                        assertEquals("Size", 0, set.size());
1016:                    }
1017:                }
1018:
1019:                public void testRemove() {
1020:                    try {
1021:                        assertFalse("Removing absent NULL element", set
1022:                                .remove(null));
1023:                    } catch (NullPointerException e) {
1024:                    }
1025:                    assertEquals("Size", 0, set.size());
1026:                }
1027:
1028:                public void testRemoveAll() {
1029:                    try {
1030:                        assertFalse("Removing NULL element", set
1031:                                .removeAll(hash));
1032:                    } catch (NullPointerException e) {
1033:                    }
1034:                    assertEquals("Size", 0, set.size());
1035:                }
1036:
1037:                public void testRetainAll_EmptySet() {
1038:                    try {
1039:                        assertFalse("Retaining NULL element", set
1040:                                .retainAll(hash));
1041:                    } catch (NullPointerException npe) {
1042:                    }
1043:                }
1044:
1045:                public void testRetainAll_NotEmptySet() {
1046:                    set.add(element);
1047:                    try {
1048:                        assertTrue("Retaining NULL element", set
1049:                                .retainAll(hash));
1050:                    } catch (NullPointerException npe) {
1051:                    }
1052:                    assertEquals("Set is empty", 0, set.size());
1053:                }
1054:            }
1055:
1056:            @SuppressWarnings("unchecked")
1057:            public static class IneligibleElementTest extends SecurityTest {
1058:
1059:                /**
1060:                 * Tested <code>set</code>. Must be initialized in derived class 
1061:                 */
1062:                public Set set;
1063:
1064:                /**
1065:                 * Set's <code>element</code>. Must be initialized in derived class
1066:                 */
1067:                public Object element;
1068:
1069:                /**
1070:                 * Is used as collection parameter
1071:                 */
1072:                public HashSet hash = new HashSet();
1073:
1074:                /**
1075:                 * Set's <code>ineligible element</code>. Must be initialized in derived class
1076:                 */
1077:                public Object iElement;
1078:
1079:                /**
1080:                 * Is used as collection parameter
1081:                 */
1082:                public HashSet iHash = new HashSet();
1083:
1084:                @Override
1085:                protected void setUp() throws Exception {
1086:                    super .setUp();
1087:
1088:                    assertTrue(
1089:                            "IneligibleElementTest: suite MUST be initialized",
1090:                            set != null && element != null && iElement != null);
1091:
1092:                    assertEquals("IneligibleElementTest: set MUST be empty", 0,
1093:                            set.size());
1094:
1095:                    hash.add(null);
1096:
1097:                    iHash.add(iElement);
1098:                }
1099:
1100:                public void testAdd() {
1101:
1102:                    try {
1103:                        set.add(iElement);
1104:
1105:                        fail("No expected ClassCastException or IllegalArgumentException");
1106:                    } catch (ClassCastException e) {
1107:                    } catch (IllegalArgumentException e) {
1108:                    } catch (SecurityException e) {
1109:                        if (!testing) {
1110:                            // all sets - SecurityException
1111:                            throw e;
1112:                        }
1113:                    }
1114:                    assertEquals("Size", 0, set.size());
1115:                }
1116:
1117:                // test against Class sets
1118:                public void testAdd_Object() {
1119:
1120:                    if (iElement.getClass() == Object.class) {
1121:                        return;
1122:                    }
1123:
1124:                    try {
1125:                        set.add(new Object());
1126:
1127:                        if (!testing) {
1128:                            // all Class sets - no exception
1129:                            fail("No expected ClassCastException or IllegalArgumentException");
1130:                        }
1131:                    } catch (ClassCastException e) {
1132:                    } catch (IllegalArgumentException e) {
1133:                    }
1134:                }
1135:
1136:                public void testAddAll() {
1137:
1138:                    try {
1139:                        set.addAll(iHash);
1140:
1141:                        fail("No expected ClassCastException or IllegalArgumentException");
1142:                    } catch (ClassCastException e) {
1143:                    } catch (IllegalArgumentException e) {
1144:                    } catch (SecurityException e) {
1145:                        if (!testing) {
1146:                            // all sets - SecurityException
1147:                            throw e;
1148:                        }
1149:                    }
1150:                    assertEquals("Size", 0, set.size());
1151:                }
1152:
1153:                public void testContains() {
1154:
1155:                    try {
1156:                        assertFalse("Set doesn't contain element", set
1157:                                .contains(iElement));
1158:                    } catch (ClassCastException e) {
1159:                    }
1160:                }
1161:
1162:                public void testContainsAll() {
1163:
1164:                    try {
1165:                        assertFalse("Set doesn't contain element", set
1166:                                .containsAll(iHash));
1167:                    } catch (ClassCastException e) {
1168:                    }
1169:                }
1170:
1171:                public void testRemove() {
1172:
1173:                    try {
1174:                        assertFalse("Removing absent element", set
1175:                                .remove(iElement));
1176:                    } catch (ClassCastException e) {
1177:                    }
1178:                }
1179:
1180:                public void testRemoveAll() {
1181:
1182:                    try {
1183:                        assertFalse("Removing absent element", set
1184:                                .removeAll(iHash));
1185:                    } catch (ClassCastException e) {
1186:                    } catch (IllegalArgumentException e) {
1187:                    }
1188:                }
1189:
1190:                public void testRetainAll_EmptySet() {
1191:
1192:                    try {
1193:                        assertFalse("Retaining ineligible element", set
1194:                                .retainAll(iHash));
1195:                    } catch (ClassCastException e) {
1196:                    } catch (IllegalArgumentException e) {
1197:                    }
1198:                }
1199:
1200:                public void testRetainAll_NotEmptySet_IneligibleElement() {
1201:
1202:                    set.add(element);
1203:                    try {
1204:                        assertTrue("Retaining ineligible element", set
1205:                                .retainAll(iHash));
1206:                    } catch (ClassCastException e) {
1207:                    } catch (IllegalArgumentException e) {
1208:                    }
1209:                    assertEquals("Now set is empty", 0, set.size());
1210:                }
1211:            }
1212:
1213:            @SuppressWarnings("unchecked")
1214:            public static class ReadOnlySetTest extends SetTest {
1215:
1216:                public void setReadOnly() {
1217:                    throw new UnsupportedOperationException(
1218:                            "setReadOnly MUST be implemented in derived class");
1219:                }
1220:
1221:                @Override
1222:                public void testAdd_ExistingElement() {
1223:
1224:                    set.add(element);
1225:                    setReadOnly();
1226:
1227:                    try {
1228:                        set.add(element);
1229:                        fail("No expected IllegalStateException");
1230:                    } catch (IllegalStateException e) {
1231:                    }
1232:                }
1233:
1234:                @Override
1235:                public void testAdd_NewElement() {
1236:
1237:                    setReadOnly();
1238:                    try {
1239:                        set.add(element);
1240:                        fail("No expected IllegalStateException");
1241:                    } catch (IllegalStateException e) {
1242:                    }
1243:                }
1244:
1245:                @Override
1246:                public void testAddAll_EmptySet() {
1247:                    setReadOnly();
1248:                    super .testAddAll_EmptySet();
1249:                }
1250:
1251:                @Override
1252:                public void testAddAll_ExistingElement() {
1253:
1254:                    set.add(element);
1255:                    setReadOnly();
1256:                    try {
1257:                        set.addAll(hash);
1258:                        fail("No expected IllegalStateException");
1259:                    } catch (IllegalStateException e) {
1260:                    }
1261:                }
1262:
1263:                @Override
1264:                public void testAddAll_NewElement() {
1265:                    setReadOnly();
1266:                    try {
1267:                        set.addAll(hash);
1268:                        fail("No expected IllegalStateException");
1269:                    } catch (IllegalStateException e) {
1270:                    }
1271:                }
1272:
1273:                @Override
1274:                public void testAddAll_NullParameter() {
1275:                    setReadOnly();
1276:                    super .testAddAll_NullParameter();
1277:                }
1278:
1279:                @Override
1280:                public void testClear_EmptySet() {
1281:                    setReadOnly();
1282:                    super .testClear_EmptySet();
1283:                }
1284:
1285:                @Override
1286:                public void testClear_NotEmptySet() {
1287:
1288:                    set.add(element);
1289:                    setReadOnly();
1290:
1291:                    try {
1292:                        set.clear();
1293:                        fail("No expected IllegalStateException");
1294:                    } catch (IllegalStateException e) {
1295:                    }
1296:                }
1297:
1298:                @Override
1299:                public void testContains_ExistingElement() {
1300:                    set.add(element);
1301:                    setReadOnly();
1302:                    assertTrue("Set contains element", set.contains(element));
1303:                }
1304:
1305:                @Override
1306:                public void testContains_NotExistingElement() {
1307:                    setReadOnly();
1308:                    super .testContains_NotExistingElement();
1309:                }
1310:
1311:                @Override
1312:                public void testContainsAll_EmptySet() {
1313:                    setReadOnly();
1314:                    super .testContainsAll_EmptySet();
1315:                }
1316:
1317:                @Override
1318:                public void testContainsAll_ExistingElement() {
1319:                    set.add(element);
1320:                    setReadOnly();
1321:                    assertTrue("Set contains element", set.containsAll(hash));
1322:                }
1323:
1324:                @Override
1325:                public void testContainsAll_NotExistingElement() {
1326:                    setReadOnly();
1327:                    super .testContainsAll_NotExistingElement();
1328:                }
1329:
1330:                @Override
1331:                public void testContainsAll_NullParameter() {
1332:                    setReadOnly();
1333:                    super .testContainsAll_NullParameter();
1334:                }
1335:
1336:                @Override
1337:                public void testIsEmpty_EmptySet() {
1338:                    setReadOnly();
1339:                    super .testIsEmpty_EmptySet();
1340:                }
1341:
1342:                @Override
1343:                public void testIsEmpty_NotEmptySet() {
1344:                    set.add(element);
1345:                    setReadOnly();
1346:                    assertFalse("Set is not empty", set.isEmpty());
1347:                }
1348:
1349:                @Override
1350:                public void testIterator_EmptySet() {
1351:                    setReadOnly();
1352:                    super .testIterator_EmptySet();
1353:                }
1354:
1355:                @Override
1356:                public void testIterator_NotEmptySet() {
1357:                    set.add(element);
1358:                    setReadOnly();
1359:                    assertNotNull("Iterator", set.iterator());
1360:                }
1361:
1362:                @Override
1363:                public void testRemove_ExistingElement() {
1364:
1365:                    set.add(element);
1366:                    setReadOnly();
1367:
1368:                    try {
1369:                        set.remove(element);
1370:                        fail("No expected IllegalStateException");
1371:                    } catch (IllegalStateException e) {
1372:                    }
1373:                }
1374:
1375:                @Override
1376:                public void testRemove_NotExistingElement() {
1377:                    setReadOnly();
1378:                    super .testRemove_NotExistingElement();
1379:                }
1380:
1381:                @Override
1382:                public void testRemoveAll_EmptySet() {
1383:                    setReadOnly();
1384:                    super .testRemoveAll_EmptySet();
1385:                }
1386:
1387:                @Override
1388:                public void testRemoveAll_ExistingElement() {
1389:
1390:                    set.add(element);
1391:                    setReadOnly();
1392:
1393:                    try {
1394:                        set.removeAll(hash);
1395:                        fail("No expected IllegalStateException");
1396:                    } catch (IllegalStateException e) {
1397:                    }
1398:                }
1399:
1400:                @Override
1401:                public void testRemoveAll_NotEmptySet() {
1402:
1403:                    set.add(element);
1404:                    setReadOnly();
1405:                    set.removeAll(new HashSet());
1406:                }
1407:
1408:                @Override
1409:                public void testRemoveAll_NotExistingElement() {
1410:                    setReadOnly();
1411:                    super .testRemoveAll_NotExistingElement();
1412:                }
1413:
1414:                @Override
1415:                public void testRemoveAll_NullParameter_EmptySet() {
1416:                    setReadOnly();
1417:                    super .testRemoveAll_NullParameter_EmptySet();
1418:                }
1419:
1420:                @Override
1421:                public void testRemoveAll_NullParameter_NotEmptySet() {
1422:
1423:                    set.add(element);
1424:                    setReadOnly();
1425:
1426:                    try {
1427:                        set.removeAll(null);
1428:                        fail("No expected NullPointerException");
1429:                    } catch (NullPointerException npe) {
1430:                    }
1431:                }
1432:
1433:                @Override
1434:                public void testRetainAll_EmptySet() {
1435:                    setReadOnly();
1436:                    super .testRetainAll_EmptySet();
1437:                }
1438:
1439:                @Override
1440:                public void testRetainAll_ExistingElement() {
1441:                    set.add(element);
1442:                    setReadOnly();
1443:                    set.retainAll(hash);
1444:                }
1445:
1446:                @Override
1447:                public void testRetainAll_NotEmptySet() {
1448:                    set.add(element);
1449:                    setReadOnly();
1450:
1451:                    try {
1452:                        set.retainAll(new HashSet());
1453:                        fail("No expected IllegalStateException");
1454:                    } catch (IllegalStateException e) {
1455:                    }
1456:                }
1457:
1458:                @Override
1459:                public void testRetainAll_NotExistingElement() {
1460:                    setReadOnly();
1461:                    super .testRetainAll_NotExistingElement();
1462:                }
1463:
1464:                @Override
1465:                public void testRetainAll_NullParameter_EmptySet() {
1466:                    setReadOnly();
1467:                    super .testRetainAll_NullParameter_EmptySet();
1468:                }
1469:
1470:                @Override
1471:                public void testRetainAll_NullParameter_NotEmptySet() {
1472:
1473:                    set.add(element);
1474:                    setReadOnly();
1475:
1476:                    try {
1477:                        set.retainAll(null);
1478:                        fail("No expected NullPointerException");
1479:                    } catch (NullPointerException npe) {
1480:                    }
1481:                }
1482:
1483:                @Override
1484:                public void testToArray_EmptySet() {
1485:                    setReadOnly();
1486:                    super .testToArray_EmptySet();
1487:                }
1488:
1489:                @Override
1490:                public void testToArray_Immutability() {
1491:                    set.add(element);
1492:                    setReadOnly();
1493:
1494:                    set.toArray()[0] = null;
1495:                    assertTrue("Element", set.toArray()[0] == element);
1496:                }
1497:
1498:                @Override
1499:                public void testToArray_NotEmptySet() {
1500:                    set.add(element);
1501:                    setReadOnly();
1502:
1503:                    assertEquals("Set is not empty", set.toArray().length, 1);
1504:                    assertTrue("Set element", set.toArray()[0] == element);
1505:                }
1506:            }
1507:
1508:            @SuppressWarnings("unchecked")
1509:            public static class SecureSetTest extends SetTest {
1510:
1511:                public void setSecure() {
1512:                    throw new UnsupportedOperationException(
1513:                            "setSecure MUST be implemented in derived class");
1514:                }
1515:
1516:                //        public void testRetainAll_NotEmptySet() {
1517:                //            try {
1518:                //                super.testRetainAll_NotEmptySet();
1519:                //                fail("No expected AccessControlException");
1520:                //            } catch (AccessControlException e) {
1521:                //                assertEquals(e, AuthPermission.class);
1522:                //            }
1523:                //        }
1524:
1525:                @Override
1526:                public void testAdd_ExistingElement() {
1527:
1528:                    set.add(element);
1529:                    setSecure();
1530:
1531:                    try {
1532:                        set.add(element);
1533:                        fail("No expected AccessControlException");
1534:                    } catch (AccessControlException e) {
1535:                        assertEquals(e, AuthPermission.class);
1536:                    }
1537:                }
1538:
1539:                @Override
1540:                public void testAdd_NewElement() {
1541:
1542:                    setSecure();
1543:                    try {
1544:                        set.add(element);
1545:                        fail("No expected AccessControlException");
1546:                    } catch (AccessControlException e) {
1547:                        assertEquals(e, AuthPermission.class);
1548:                    }
1549:                }
1550:
1551:                @Override
1552:                public void testAddAll_EmptySet() {
1553:                    setSecure();
1554:                    super .testAddAll_EmptySet();
1555:                }
1556:
1557:                @Override
1558:                public void testAddAll_ExistingElement() {
1559:
1560:                    set.add(element);
1561:                    setSecure();
1562:
1563:                    try {
1564:                        set.addAll(hash);
1565:                        fail("No expected AccessControlException");
1566:                    } catch (AccessControlException e) {
1567:                        assertEquals(e, AuthPermission.class);
1568:                    }
1569:                }
1570:
1571:                @Override
1572:                public void testAddAll_NewElement() {
1573:
1574:                    setSecure();
1575:                    try {
1576:                        set.addAll(hash);
1577:                        fail("No expected AccessControlException");
1578:                    } catch (AccessControlException e) {
1579:                        assertEquals(e, AuthPermission.class);
1580:                    }
1581:                }
1582:
1583:                @Override
1584:                public void testAddAll_NullParameter() {
1585:                    setSecure();
1586:                    super .testAddAll_NullParameter();
1587:                }
1588:
1589:                @Override
1590:                public void testClear_EmptySet() {
1591:                    setSecure();
1592:                    super .testClear_EmptySet();
1593:                }
1594:
1595:                @Override
1596:                public void testClear_NotEmptySet() {
1597:
1598:                    set.add(element);
1599:                    setSecure();
1600:
1601:                    try {
1602:                        set.clear();
1603:                        fail("No expected AccessControlException");
1604:                    } catch (AccessControlException e) {
1605:                        assertEquals(e, AuthPermission.class);
1606:                    }
1607:                }
1608:
1609:                @Override
1610:                public void testContains_ExistingElement() {
1611:                    set.add(element);
1612:                    setSecure();
1613:                    assertTrue("Set contains element", set.contains(element));
1614:                }
1615:
1616:                @Override
1617:                public void testContains_NotExistingElement() {
1618:                    setSecure();
1619:                    super .testContains_NotExistingElement();
1620:                }
1621:
1622:                @Override
1623:                public void testContainsAll_EmptySet() {
1624:                    setSecure();
1625:                    super .testContainsAll_EmptySet();
1626:                }
1627:
1628:                @Override
1629:                public void testContainsAll_ExistingElement() {
1630:                    set.add(element);
1631:                    setSecure();
1632:                    assertTrue("Set contains element", set.containsAll(hash));
1633:                }
1634:
1635:                @Override
1636:                public void testContainsAll_NotExistingElement() {
1637:                    setSecure();
1638:                    super .testContainsAll_NotExistingElement();
1639:                }
1640:
1641:                @Override
1642:                public void testContainsAll_NullParameter() {
1643:                    setSecure();
1644:                    super .testContainsAll_NullParameter();
1645:                }
1646:
1647:                @Override
1648:                public void testIsEmpty_EmptySet() {
1649:                    setSecure();
1650:                    super .testIsEmpty_EmptySet();
1651:                }
1652:
1653:                @Override
1654:                public void testIsEmpty_NotEmptySet() {
1655:                    set.add(element);
1656:                    setSecure();
1657:                    assertFalse("Set is not empty", set.isEmpty());
1658:                }
1659:
1660:                @Override
1661:                public void testIterator_EmptySet() {
1662:                    setSecure();
1663:                    super .testIterator_EmptySet();
1664:                }
1665:
1666:                @Override
1667:                public void testIterator_NotEmptySet() {
1668:                    set.add(element);
1669:                    setSecure();
1670:                    assertNotNull("Iterator", set.iterator());
1671:                }
1672:
1673:                @Override
1674:                public void testRemove_ExistingElement() {
1675:
1676:                    set.add(element);
1677:                    setSecure();
1678:
1679:                    try {
1680:                        set.remove(element);
1681:                        fail("No expected AccessControlException");
1682:                    } catch (AccessControlException e) {
1683:                        assertEquals(e, AuthPermission.class);
1684:                    }
1685:                }
1686:
1687:                @Override
1688:                public void testRemove_NotExistingElement() {
1689:                    setSecure();
1690:                    super .testRemove_NotExistingElement();
1691:                }
1692:
1693:                @Override
1694:                public void testRemoveAll_EmptySet() {
1695:                    setSecure();
1696:                    super .testRemoveAll_EmptySet();
1697:                }
1698:
1699:                @Override
1700:                public void testRemoveAll_ExistingElement() {
1701:
1702:                    set.add(element);
1703:                    setSecure();
1704:
1705:                    try {
1706:                        set.removeAll(hash);
1707:                        fail("No expected AccessControlException");
1708:                    } catch (AccessControlException e) {
1709:                        assertEquals(e, AuthPermission.class);
1710:                    }
1711:                }
1712:
1713:                @Override
1714:                public void testRemoveAll_NotEmptySet() {
1715:                    set.add(element);
1716:                    setSecure();
1717:
1718:                    assertFalse("Removing empty set", set
1719:                            .removeAll(new HashSet()));
1720:                    assertEquals("Size", 1, set.size());
1721:                }
1722:
1723:                @Override
1724:                public void testRemoveAll_NotExistingElement() {
1725:                    setSecure();
1726:                    super .testRemoveAll_NotExistingElement();
1727:                }
1728:
1729:                @Override
1730:                public void testRemoveAll_NullParameter_EmptySet() {
1731:                    setSecure();
1732:                    super .testRemoveAll_NullParameter_EmptySet();
1733:                }
1734:
1735:                @Override
1736:                public void testRemoveAll_NullParameter_NotEmptySet() {
1737:
1738:                    set.add(element);
1739:                    setSecure();
1740:
1741:                    try {
1742:                        set.removeAll(null);
1743:                        fail("No expected NullPointerException");
1744:                    } catch (NullPointerException npe) {
1745:                    }
1746:                }
1747:
1748:                @Override
1749:                public void testRetainAll_EmptySet() {
1750:                    setSecure();
1751:                    super .testRetainAll_EmptySet();
1752:                }
1753:
1754:                @Override
1755:                public void testRetainAll_ExistingElement() {
1756:                    set.add(element);
1757:                    setSecure();
1758:
1759:                    assertFalse("Removing elements", set.retainAll(hash));
1760:                    assertEquals("Size", 1, set.size());
1761:                }
1762:
1763:                @Override
1764:                public void testRetainAll_NotEmptySet() {
1765:                    set.add(element);
1766:                    setSecure();
1767:
1768:                    try {
1769:                        set.retainAll(new HashSet());
1770:                        fail("No expected AccessControlException");
1771:                    } catch (AccessControlException e) {
1772:                        assertEquals(e, AuthPermission.class);
1773:                    }
1774:                }
1775:
1776:                @Override
1777:                public void testRetainAll_NotExistingElement() {
1778:                    setSecure();
1779:                    super .testRetainAll_NotExistingElement();
1780:                }
1781:
1782:                @Override
1783:                public void testRetainAll_NullParameter_EmptySet() {
1784:                    setSecure();
1785:                    super .testRetainAll_NullParameter_EmptySet();
1786:                }
1787:
1788:                @Override
1789:                public void testRetainAll_NullParameter_NotEmptySet() {
1790:                    set.add(element);
1791:                    setSecure();
1792:
1793:                    try {
1794:                        set.retainAll(null);
1795:                        fail("No expected NullPointerException");
1796:                    } catch (NullPointerException npe) {
1797:                    }
1798:                }
1799:
1800:                @Override
1801:                public void testToArray_EmptySet() {
1802:                    setSecure();
1803:                    super .testToArray_EmptySet();
1804:                }
1805:
1806:                @Override
1807:                public void testToArray_Immutability() {
1808:                    set.add(element);
1809:                    setSecure();
1810:
1811:                    set.toArray()[0] = null;
1812:                    assertTrue("Element", set.toArray()[0] == element);
1813:                }
1814:
1815:                @Override
1816:                public void testToArray_NotEmptySet() {
1817:                    set.add(element);
1818:                    setSecure();
1819:
1820:                    assertEquals("Set is not empty", set.toArray().length, 1);
1821:                    assertTrue("Set element", set.toArray()[0] == element);
1822:                }
1823:            }
1824:
1825:            public static class ObjectTest extends SecurityTest {
1826:
1827:                //
1828:                // obj1, obj2, obj3 are equals object
1829:                // it is implied that obj1 is object of tested class  
1830:                //
1831:                public Object obj1;
1832:
1833:                public Object obj2;
1834:
1835:                public Object obj3;
1836:
1837:                // Set of objects that are not equal to obj1, obj2, obj3
1838:                public Vector<Object> notEqual;
1839:
1840:                // Checks that references to testing objects are different
1841:                // because we are not going to test Object.equals(Object)
1842:                @Override
1843:                protected void setUp() throws Exception {
1844:                    super .setUp();
1845:
1846:                    assertTrue("ObjectTest: suite MUST be initialized",
1847:                            obj1 != null && obj2 != null && obj3 != null
1848:                                    && obj1 != obj2 && obj1 != obj3
1849:                                    && obj2 != obj3 && notEqual != null);
1850:                }
1851:
1852:                public void testEquals_Reflexivity() {
1853:                    assertTrue(obj1.equals(obj1));
1854:                }
1855:
1856:                public void testEquals_Symmetry() {
1857:                    assertTrue(obj1.equals(obj2));
1858:                    assertTrue(obj2.equals(obj1));
1859:                }
1860:
1861:                public void testEquals_Transitivity() {
1862:                    assertTrue(obj1.equals(obj2));
1863:                    assertTrue(obj2.equals(obj3));
1864:                    assertTrue(obj1.equals(obj3));
1865:                }
1866:
1867:                public void testEquals_Consistenty() {
1868:                    assertTrue(obj1.equals(obj3));
1869:                    assertTrue(obj1.equals(obj3));
1870:                }
1871:
1872:                public void testEquals_NullValue() {
1873:                    assertFalse(obj1.equals(null));
1874:                }
1875:
1876:                public void testEquals_NotEqual() {
1877:                    for (Object name : notEqual) {
1878:                        assertFalse(obj1.equals(name));
1879:                    }
1880:                }
1881:
1882:                public void testHashCode() {
1883:                    assertEquals(obj1.hashCode(), obj2.hashCode());
1884:                    assertEquals(obj1.hashCode(), obj3.hashCode());
1885:                }
1886:            }
1887:
1888:            @SuppressWarnings("unchecked")
1889:            public static class SubjectSetObjectTest extends ObjectTest {
1890:
1891:                public Subject subject = new Subject();
1892:
1893:                public MyClass1 element1 = new MyClass1();
1894:
1895:                public MyClass1 element2 = new MyClass1();
1896:
1897:                public SubjectSetObjectTest() {
1898:
1899:                    subject.getPrincipals().add(element1);
1900:                    subject.getPrincipals().add(element2);
1901:
1902:                    //reverse order
1903:                    subject.getPrivateCredentials().add(element2);
1904:                    subject.getPrivateCredentials().add(element1);
1905:
1906:                    subject.getPublicCredentials().add(element1);
1907:                    subject.getPublicCredentials().add(element2);
1908:
1909:                    // init obj3
1910:                    HashSet hash = new HashSet();
1911:
1912:                    hash.add(element1);
1913:                    hash.add(element2);
1914:
1915:                    obj3 = hash;
1916:
1917:                    // init obj3
1918:                    HashSet hash1 = new HashSet();
1919:                    hash1.add(element1);
1920:                    hash1.add(new MyClass1());
1921:                    Subject s1 = new Subject(false, hash1, hash1, hash1);
1922:
1923:                    HashSet hash2 = new HashSet();
1924:                    hash1.add(element2);
1925:                    hash1.add(new MyClass2());
1926:                    Subject s2 = new Subject(false, hash2, hash2, hash2);
1927:
1928:                    Subject s3 = new Subject();
1929:
1930:                    notEqual = new Vector();
1931:
1932:                    notEqual.add(s1.getPrincipals());
1933:                    notEqual.add(s1.getPrivateCredentials());
1934:                    notEqual.add(s1.getPublicCredentials());
1935:                    notEqual.add(s1.getPrincipals(MyClass1.class));
1936:                    notEqual.add(s1.getPrivateCredentials(MyClass1.class));
1937:                    notEqual.add(s1.getPublicCredentials(MyClass1.class));
1938:
1939:                    notEqual.add(s2.getPrincipals());
1940:                    notEqual.add(s2.getPrivateCredentials());
1941:                    notEqual.add(s2.getPublicCredentials());
1942:                    notEqual.add(s2.getPrincipals(MyClass1.class));
1943:                    notEqual.add(s2.getPrivateCredentials(MyClass1.class));
1944:                    notEqual.add(s2.getPublicCredentials(MyClass1.class));
1945:
1946:                    notEqual.add(s3.getPrincipals());
1947:                    notEqual.add(s3.getPrivateCredentials());
1948:                    notEqual.add(s3.getPublicCredentials());
1949:                    notEqual.add(s3.getPrincipals(MyClass1.class));
1950:                    notEqual.add(s3.getPrivateCredentials(MyClass1.class));
1951:                    notEqual.add(s3.getPublicCredentials(MyClass1.class));
1952:
1953:                    notEqual.add(new HashSet());
1954:                    notEqual.add(new Object());
1955:                }
1956:            }
1957:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.