Source Code Cross Referenced for ASN1TestUtils.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » jndi » provider » ldap » asn1 » 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.jndi.provider.ldap.asn1 
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:        package org.apache.harmony.jndi.provider.ldap.asn1;
018:
019:        import java.lang.reflect.Field;
020:        import java.util.Collection;
021:
022:        import junit.framework.Assert;
023:
024:        import org.apache.harmony.jndi.provider.ldap.DeleteOp;
025:        import org.apache.harmony.jndi.provider.ldap.asn1.ASN1ChoiceWrap.ChosenValue;
026:        import org.apache.harmony.security.asn1.ASN1Boolean;
027:        import org.apache.harmony.security.asn1.ASN1Enumerated;
028:        import org.apache.harmony.security.asn1.ASN1Implicit;
029:        import org.apache.harmony.security.asn1.ASN1Integer;
030:        import org.apache.harmony.security.asn1.ASN1OctetString;
031:        import org.apache.harmony.security.asn1.ASN1SequenceOf;
032:        import org.apache.harmony.security.asn1.ASN1SetOf;
033:        import org.apache.harmony.security.asn1.ASN1Type;
034:        import org.apache.harmony.security.asn1.ASN1ValueCollection;
035:
036:        public class ASN1TestUtils {
037:            private static final String ASN1ENCODABLE_NAME = ASN1Encodable.class
038:                    .getCanonicalName();
039:            private static final String CHOSENVALUE_NAME = ChosenValue.class
040:                    .getCanonicalName();
041:
042:            public static void checkEncode(Object value, ASN1Type type) {
043:                if (type instanceof  ASN1Implicit) {
044:                    type = getWrappedType((ASN1Implicit) type);
045:                }
046:
047:                if (type instanceof  ASN1Integer
048:                        || type instanceof  ASN1Enumerated) {
049:                    Assert.assertTrue(value instanceof  byte[]);
050:
051:                    Assert.assertTrue(
052:                            "value should not be zero-length byte array",
053:                            ((byte[]) value).length != 0);
054:                } else if (type instanceof  ASN1Boolean) {
055:                    Assert.assertTrue(value instanceof  Boolean);
056:                } else if (type instanceof  ASN1OctetString) {
057:                    if (value instanceof  DeleteOp) {
058:                        Object[] objs = new Object[1];
059:                        ((DeleteOp) value).encodeValues(objs);
060:                        value = objs[0];
061:                    }
062:                    Assert.assertTrue(value instanceof  byte[]);
063:                } else if (type instanceof  ASN1SequenceWrap) {
064:                    checkEncodeSequence(value, (ASN1SequenceWrap) type);
065:                } else if (type instanceof  ASN1SequenceOf
066:                        || type instanceof  ASN1SetOf) {
067:                    Assert.assertTrue(value instanceof  Collection);
068:                    Collection collection = (Collection) value;
069:                    for (Object object : collection) {
070:                        checkEncode(object, ((ASN1ValueCollection) type).type);
071:                    }
072:                } else if (type instanceof  ASN1ChoiceWrap) {
073:                    checkEncodeChoice(value, (ASN1ChoiceWrap) type);
074:                } else if (type instanceof  ASN1LdapFilter) {
075:                    checkEncodeFilter(value, (ASN1LdapFilter) type);
076:                } else {
077:                    Assert.fail("Not supported ASN.1 type");
078:                }
079:            }
080:
081:            private static void checkEncodeFilter(Object value,
082:                    ASN1LdapFilter filter) {
083:                checkEncode(value, LdapASN1Constant.Filter);
084:            }
085:
086:            private static void checkEncodeChoice(Object value,
087:                    ASN1ChoiceWrap type) {
088:                if (value instanceof  Object[]) {
089:                    Object[] objs = (Object[]) value;
090:                    Assert.assertEquals(2, objs.length);
091:                    Assert.assertTrue(objs[0] instanceof  Integer);
092:
093:                    int index = ((Integer) objs[0]).intValue();
094:                    checkEncode(objs[1], type.type[index]);
095:                } else if (value instanceof  ChosenValue) {
096:                    ChosenValue chosen = (ChosenValue) value;
097:                    checkEncode(chosen.getValue(), type.type[chosen.getIndex()]);
098:                } else if (value instanceof  ASN1Encodable) {
099:                    Object[] objs = new Object[1];
100:                    ((ASN1Encodable) value).encodeValues(objs);
101:                    checkEncode(objs[0], type);
102:                } else {
103:
104:                    Assert
105:                            .fail("Value for ASN1ChoiceWrap should be Object[2], "
106:                                    + CHOSENVALUE_NAME
107:                                    + " or "
108:                                    + ASN1ENCODABLE_NAME);
109:                }
110:            }
111:
112:            private static void checkEncodeSequence(Object value,
113:                    ASN1SequenceWrap type) {
114:                if (value instanceof  Object[]) {
115:                    Object[] objs = (Object[]) value;
116:                    Assert.assertEquals(type.type.length, objs.length);
117:                    for (int i = 0; i < objs.length; i++) {
118:                        if (objs[i] == null && type.OPTIONAL[i]) {
119:                            continue;
120:                        }
121:                        checkEncode(objs[i], type.type[i]);
122:                    }
123:                } else if (value instanceof  ASN1Encodable) {
124:                    Object[] objs = new Object[type.type.length];
125:                    ((ASN1Encodable) value).encodeValues(objs);
126:                    checkEncodeSequence(objs, type);
127:                } else {
128:                    Assert
129:                            .fail("Value for ASN1SequenceWrap should be Object[], or "
130:                                    + ASN1ENCODABLE_NAME);
131:                }
132:            }
133:
134:            private static ASN1Type getWrappedType(ASN1Implicit type) {
135:                try {
136:                    Field field = ASN1Implicit.class.getDeclaredField("type");
137:                    field.setAccessible(true);
138:                    return (ASN1Type) field.get(type);
139:                } catch (Exception e) {
140:                    // can't reach, unless implement changed
141:                    throw new RuntimeException("Can't get wrapped type.", e);
142:                }
143:            }
144:        }
w__ww___.___ja__v_a__2s__.___c___o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.