Source Code Cross Referenced for UnresolvedPermissionCollection_ImplTest.java in  » Apache-Harmony-Java-SE » java-package » java » security » 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 » java package » java.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        /**
019:         * @author Alexey V. Varlamov
020:         * @version $Revision$
021:         */package java.security;
022:
023:        import java.util.*;
024:
025:        import junit.framework.TestCase;
026:
027:        /**
028:         * Tests for <code>UnresolvedPermissionCollection</code> class fields and methods
029:         * 
030:         */
031:
032:        public class UnresolvedPermissionCollection_ImplTest extends TestCase {
033:
034:            /** 
035:             * Can add any number of UnresolvedPermission instances, but no any other permissions.
036:             * Cannot add if collection is read-only.
037:             */
038:            public void testAdd() {
039:                PermissionCollection pc = new UnresolvedPermissionCollection();
040:                Permission sp = new SecurityPermission("abc");
041:                Permission up1 = new UnresolvedPermission("131234", null, null,
042:                        null);
043:                Permission up2 = new UnresolvedPermission("KUJKHVKJgyuygjhb",
044:                        "xcv456", "26r ytf",
045:                        new java.security.cert.Certificate[0]);
046:
047:                try {
048:                    pc.add(sp);
049:                    fail("Should not add non-UnresolvedPermission");
050:                } catch (IllegalArgumentException ok) {
051:                }
052:
053:                pc.add(up1);
054:                pc.add(up1);
055:                pc.add(up2);
056:
057:                pc.setReadOnly();
058:                try {
059:                    pc.add(up1);
060:                    fail("read-only flag is ignored");
061:                } catch (SecurityException ok) {
062:                }
063:            }
064:
065:            /** This collection never implies any permission. */
066:            public void testImplies() {
067:                Permission ap = new AllPermission();
068:                Permission up = new UnresolvedPermission("131234", null, null,
069:                        null);
070:                PermissionCollection pc = up.newPermissionCollection();
071:
072:                assertFalse(pc.implies(ap));
073:                assertFalse(pc.implies(up));
074:
075:                //pc.add(up);
076:                //assertFalse(pc.implies(up));
077:            }
078:
079:            /**
080:             * Should return non-null empty enumeration for empty collection.
081:             * For non-empty collection, should always return enumeration over unique elements.
082:             */
083:            public void testElements() {
084:                PermissionCollection pc = new UnresolvedPermissionCollection();
085:                Permission up1 = new UnresolvedPermission("131234", null, null,
086:                        null);
087:                Permission up2 = new UnresolvedPermission("131234", "ui23rjh",
088:                        null, null);
089:                Permission up3 = new UnresolvedPermission("KUJKHVKJgyuygjhb",
090:                        "xcv456", "26r ytf",
091:                        new java.security.cert.Certificate[0]);
092:
093:                Enumeration en = pc.elements();
094:                assertNotNull(en);
095:                assertFalse(en.hasMoreElements());
096:
097:                pc.add(up1);
098:                en = pc.elements();
099:                assertTrue(en.hasMoreElements());
100:                assertTrue(up1.equals(en.nextElement()));
101:                assertFalse(en.hasMoreElements());
102:
103:                //no check for duplicate elements - this is too implementation specific.
104:                /*pc.add(up1);
105:                en = pc.elements();
106:                assertTrue(en.hasMoreElements());
107:                assertTrue(up1.equals(en.nextElement()));
108:                assertFalse(en.hasMoreElements());*/
109:
110:                pc.add(up2);
111:                pc.add(up3);
112:                en = pc.elements();
113:                Collection els = new ArrayList();
114:                while (en.hasMoreElements()) {
115:                    els.add(en.nextElement());
116:                }
117:                assertEquals(3, els.size());
118:                assertTrue(els.contains(up1) && els.contains(up2)
119:                        && els.contains(up3));
120:            }
121:
122:            /**
123:             * For null collection passed, should behave correctly:
124:             * <ul>
125:             * <li>If nothing resolved, returns null and does not remove elements
126:             * <li>If some permission resolved, returns proper collection and removes resolved elements
127:             * </ul>  
128:             */
129:            public void testResolveCollection() {
130:                UnresolvedPermissionCollection upc = new UnresolvedPermissionCollection();
131:                Permission up = new UnresolvedPermission(
132:                        "java.security.AllPermission", "xcv456", "26r ytf",
133:                        new java.security.cert.Certificate[0]);
134:                Permission ap = new AllPermission();
135:                Permission bp = new BasicPermission("sfwertsdg") {
136:                };
137:
138:                PermissionCollection resolved = upc.resolveCollection(ap, null);
139:                assertNull(resolved);
140:
141:                upc.add(up);
142:                resolved = upc.resolveCollection(bp, null);
143:                assertNull(resolved);
144:                assertTrue(up.equals(upc.elements().nextElement()));
145:
146:                resolved = upc.resolveCollection(ap, null);
147:                assertNotNull(resolved);
148:                assertTrue(ap.equals(resolved.elements().nextElement()));
149:                assertFalse(
150:                        "resolved permission should be removed from unresolved collection",
151:                        upc.elements().hasMoreElements());
152:            }
153:
154:            /**
155:             * For real collection passed, should behave correctly:
156:             * <ul>
157:             * <li>If nothing resolved, returns the collection and does not remove elements
158:             * <li>If some permission resolved, returns the collection and removes resolved elements
159:             * </ul>  
160:             */
161:            public void testResolveCollectionReturnedCollection() {
162:                UnresolvedPermissionCollection upc = new UnresolvedPermissionCollection();
163:                Permission up3 = new UnresolvedPermission(
164:                        "java.security.AllPermission", "xcv456", null, null);
165:                Permission ap = new AllPermission();
166:                PermissionCollection apc = new AllPermissionCollection();
167:
168:                PermissionCollection resolved = upc.resolveCollection(ap, apc);
169:                assertSame(
170:                        "should return the passed collection if it is not null",
171:                        apc, resolved);
172:                // retest the same for case of actually resolved permission
173:                upc.add(up3);
174:                resolved = upc.resolveCollection(ap, apc);
175:                assertSame(
176:                        "should return the passed collection if it is not null",
177:                        apc, resolved);
178:            }
179:
180:            /**
181:             * Test for case when some permissions of the expected type were not resolved for some reason,
182:             * while others were resolved. Returned collection should contain resolved permissions only,
183:             * and the unresolved collection should retain unresolved ones only.  
184:             */
185:            public void testResolveCollectionPartial() {
186:                UnresolvedPermissionCollection upc = new UnresolvedPermissionCollection();
187:                String name = "ui23rjh";
188:                Permission up1 = new UnresolvedPermission(
189:                        "java.security.SecurityPermission", null, null, null);
190:                Permission up2 = new UnresolvedPermission(
191:                        "java.security.SecurityPermission", name, null, null);
192:                Permission sp = new SecurityPermission(name);
193:
194:                upc.add(up1);
195:                upc.add(up2);
196:                PermissionCollection resolved = upc.resolveCollection(
197:                        new SecurityPermission("34po5ijh"), null);
198:                assertNotNull(resolved);
199:                Enumeration els = resolved.elements();
200:                assertTrue(sp.equals(els.nextElement()));
201:                assertFalse(els.hasMoreElements());
202:                els = upc.elements();
203:                assertTrue(
204:                        "resolved permission should be removed from unresolved collection",
205:                        up1.equals(els.nextElement()));
206:                assertFalse(els.hasMoreElements());
207:            }
208:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.