Source Code Cross Referenced for IdentityScope2Test.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » security » tests » 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 » org package » org.apache.harmony.security.tests.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:        package org.apache.harmony.security.tests.java.security;
019:
020:        import java.security.Identity;
021:        import java.security.IdentityScope;
022:        import java.security.KeyManagementException;
023:        import java.security.KeyPairGenerator;
024:        import java.security.PublicKey;
025:        import java.util.Enumeration;
026:        import java.util.Hashtable;
027:
028:        import org.apache.harmony.security.tests.java.security.Identity2Test.IdentitySubclass;
029:
030:        public class IdentityScope2Test extends junit.framework.TestCase {
031:
032:            static PublicKey pubKey;
033:            static {
034:                try {
035:                    pubKey = KeyPairGenerator.getInstance("DSA").genKeyPair()
036:                            .getPublic();
037:                } catch (Exception e) {
038:                    fail(e.toString());
039:                }
040:            }
041:
042:            public static class IdentityScopeSubclass extends IdentityScope {
043:                Hashtable identities;
044:
045:                public IdentityScopeSubclass(String name, PublicKey pk) {
046:                    super (name);
047:                    try {
048:                        setPublicKey(pk);
049:                    } catch (KeyManagementException e) {
050:                    }
051:                    identities = new Hashtable();
052:                }
053:
054:                public IdentityScopeSubclass() {
055:                    super ();
056:                    identities = new Hashtable();
057:                }
058:
059:                public IdentityScopeSubclass(String name) {
060:                    super (name);
061:                    identities = new Hashtable();
062:                }
063:
064:                public IdentityScopeSubclass(String name, IdentityScope scope)
065:                        throws KeyManagementException {
066:                    super (name, scope);
067:                    identities = new Hashtable();
068:                }
069:
070:                public int size() {
071:                    return identities.size();
072:                }
073:
074:                public Identity getIdentity(String name) {
075:                    Enumeration en = identities();
076:                    while (en.hasMoreElements()) {
077:                        Identity current = (Identity) en.nextElement();
078:                        if (current.getName().equals(name))
079:                            return current;
080:                    }
081:                    return null;
082:                }
083:
084:                public Identity getIdentity(PublicKey pk) {
085:                    Enumeration en = identities();
086:                    while (en.hasMoreElements()) {
087:                        Identity current = (Identity) en.nextElement();
088:                        if (current.getPublicKey() == pk)
089:                            return current;
090:                    }
091:                    return null;
092:                }
093:
094:                public Enumeration identities() {
095:                    return identities.elements();
096:                }
097:
098:                public void addIdentity(Identity id)
099:                        throws KeyManagementException {
100:                    if (identities.containsKey(id))
101:                        throw new KeyManagementException(
102:                                "This Identity is already contained in the scope");
103:                    if (getIdentity(id.getPublicKey()) != null)
104:                        throw new KeyManagementException(
105:                                "This Identity's public key already exists in the scope");
106:                    identities.put(id, id);
107:                }
108:
109:                public void removeIdentity(Identity id)
110:                        throws KeyManagementException {
111:                    if (!identities.containsKey(id))
112:                        throw new KeyManagementException(
113:                                "This Identity is not contained in the scope");
114:                    identities.remove(id);
115:                }
116:            }
117:
118:            /**
119:             * @tests java.security.IdentityScope#IdentityScope()
120:             */
121:            public void test_Constructor() {
122:                new IdentityScopeSubclass();
123:            }
124:
125:            /**
126:             * @tests java.security.IdentityScope#IdentityScope(java.lang.String)
127:             */
128:            public void test_ConstructorLjava_lang_String() {
129:                new IdentityScopeSubclass("test");
130:            }
131:
132:            /**
133:             * @tests java.security.IdentityScope#IdentityScope(java.lang.String,
134:             *        java.security.IdentityScope)
135:             */
136:            public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope()
137:                    throws Exception {
138:                new IdentityScopeSubclass("test", new IdentityScopeSubclass());
139:            }
140:
141:            /**
142:             * @tests java.security.IdentityScope#addIdentity(java.security.Identity)
143:             */
144:            public void test_addIdentityLjava_security_Identity()
145:                    throws Exception {
146:                IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
147:                        new IdentityScopeSubclass());
148:                Identity id = new IdentitySubclass("id1");
149:                id.setPublicKey(pubKey);
150:                sub.addIdentity(id);
151:                try {
152:                    Identity id2 = new IdentitySubclass("id2");
153:                    id2.setPublicKey(pubKey);
154:                    sub.addIdentity(id2);
155:                    fail("KeyManagementException should have been thrown");
156:                } catch (KeyManagementException e) {
157:                    // Expected
158:                }
159:            }
160:
161:            /**
162:             * @tests java.security.IdentityScope#removeIdentity(java.security.Identity)
163:             */
164:            public void test_removeIdentityLjava_security_Identity()
165:                    throws Exception {
166:                IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
167:                        new IdentityScopeSubclass());
168:                Identity id = new IdentitySubclass();
169:                id.setPublicKey(pubKey);
170:                sub.addIdentity(id);
171:                sub.removeIdentity(id);
172:                try {
173:                    sub.removeIdentity(id);
174:                    fail("KeyManagementException should have been thrown");
175:                } catch (KeyManagementException e) {
176:                    // expected
177:                }
178:            }
179:
180:            /**
181:             * @tests java.security.IdentityScope#identities()
182:             */
183:            public void test_identities() throws Exception {
184:                IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
185:                        new IdentityScopeSubclass());
186:                Identity id = new IdentitySubclass();
187:                id.setPublicKey(pubKey);
188:                sub.addIdentity(id);
189:                Enumeration en = sub.identities();
190:                assertTrue("Wrong object contained in identities", en
191:                        .nextElement().equals(id));
192:                assertTrue("Contains too many elements", !en.hasMoreElements());
193:            }
194:
195:            /**
196:             * @tests java.security.IdentityScope#getIdentity(java.security.Principal)
197:             */
198:            public void test_getIdentityLjava_security_Principal()
199:                    throws Exception {
200:                Identity id = new IdentitySubclass("principal name");
201:                id.setPublicKey(pubKey);
202:                IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
203:                        new IdentityScopeSubclass());
204:                sub.addIdentity(id);
205:                Identity returnedId = sub.getIdentity(id);
206:                assertEquals("Returned Identity not the same as the added one",
207:                        id, returnedId);
208:            }
209:
210:            /**
211:             * @tests java.security.IdentityScope#getIdentity(java.security.PublicKey)
212:             */
213:            public void test_getIdentityLjava_security_PublicKey()
214:                    throws Exception {
215:                IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
216:                        new IdentityScopeSubclass());
217:                Identity id = new IdentitySubclass();
218:                id.setPublicKey(pubKey);
219:                sub.addIdentity(id);
220:                Identity returnedId = sub.getIdentity(pubKey);
221:                assertEquals("Returned Identity not the same as the added one",
222:                        id, returnedId);
223:            }
224:
225:            /**
226:             * @tests java.security.IdentityScope#getIdentity(java.lang.String)
227:             */
228:            public void test_getIdentityLjava_lang_String() throws Exception {
229:                IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
230:                        new IdentityScopeSubclass());
231:                Identity id = new IdentitySubclass("test");
232:                id.setPublicKey(pubKey);
233:                sub.addIdentity(id);
234:                Identity returnedId = sub.getIdentity("test");
235:                assertEquals("Returned Identity not the same as the added one",
236:                        id, returnedId);
237:            }
238:
239:            /**
240:             * @tests java.security.IdentityScope#size()
241:             */
242:            public void test_size() throws Exception {
243:                IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
244:                        new IdentityScopeSubclass());
245:                Identity id = new IdentitySubclass();
246:                id.setPublicKey(pubKey);
247:                sub.addIdentity(id);
248:                assertEquals("Wrong size", 1, sub.size());
249:            }
250:
251:            /**
252:             * @tests java.security.IdentityScope#toString()
253:             */
254:            public void test_toString() throws Exception {
255:                IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
256:                        new IdentityScopeSubclass());
257:                Identity id = new IdentitySubclass();
258:                id.setPublicKey(pubKey);
259:                sub.addIdentity(id);
260:                assertNotNull("toString returned a null", sub.toString());
261:                assertTrue("Not a valid String ", sub.toString().length() > 0);
262:            }
263:
264:            public void test_getIdentity() throws Exception {
265:                //Regression for HARMONY-1173
266:                IdentityScope scope = IdentityScope.getSystemScope();
267:                try {
268:                    scope.getIdentity((String) null);
269:                    fail("NPE expected");
270:                } catch (NullPointerException npe) {
271:                }
272:            }
273:        }
ww_w__._ja___v__a2s._c__om_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.