KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > dn > TestDnFieldExtractor


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.util.dn;
15
16 import java.util.HashMap JavaDoc;
17
18 import junit.framework.TestCase;
19
20 import org.apache.log4j.Logger;
21
22
23 /**
24  * Tests the StringTools class .
25  *
26  * @version $Id: TestDnFieldExtractor.java,v 1.7 2006/12/04 10:06:26 anatom Exp $
27  */

28 public class TestDnFieldExtractor extends TestCase {
29     private static Logger log = Logger.getLogger(TestDnFieldExtractor.class);
30
31     /**
32      * Creates a new TestStringTools object.
33      *
34      * @param name name
35      */

36     public TestDnFieldExtractor(String JavaDoc name) {
37         super(name);
38     }
39
40     protected void setUp() throws Exception JavaDoc {
41         log.debug(">setUp()");
42         log.debug("<setUp()");
43     }
44
45     protected void tearDown() throws Exception JavaDoc {
46         log.debug(">tearDown()");
47         log.debug("<tearDown()");
48     }
49
50     /**
51      * @throws Exception error
52      */

53     public void test01CheckDnFields() throws Exception JavaDoc {
54         String JavaDoc comp = DnComponents.getDnExtractorFieldFromDnId(28);
55         assertEquals("DN=", comp);
56         String JavaDoc dn = "cn=Tomas Gustavsson,o=PrimeKey,L=Stockholm,dc=PrimeKey,DC=com";
57         DNFieldExtractor extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTDN);
58         HashMap JavaDoc i = extractor.getNumberOfFields();
59         assertEquals(17,i.size());
60         String JavaDoc cn = extractor.getField(DNFieldExtractor.CN, 0);
61         assertEquals("Tomas Gustavsson", cn);
62         cn = extractor.getField(DNFieldExtractor.CN, 1);
63         assertEquals("",cn);
64         String JavaDoc dc = extractor.getField(DNFieldExtractor.DC, 0);
65         assertEquals("PrimeKey", dc);
66         dc = extractor.getField(DNFieldExtractor.DC, 1);
67         assertEquals("com", dc);
68         String JavaDoc l = extractor.getField(DNFieldExtractor.L, 0);
69         assertEquals("Stockholm", l);
70         int num = extractor.getNumberOfFields(DNFieldExtractor.DC);
71         assertEquals(2, num);
72         num = extractor.getNumberOfFields(DNFieldExtractor.O);
73         assertEquals(1, num);
74         String JavaDoc fieldstr = extractor.getFieldString(DNFieldExtractor.CN);
75         assertEquals("CN=Tomas Gustavsson", fieldstr);
76         fieldstr = extractor.getFieldString(DNFieldExtractor.DC);
77         assertEquals("DC=PrimeKey,DC=com", fieldstr);
78         boolean illegal = extractor.isIllegal();
79         assertFalse(illegal);
80         boolean other = extractor.existsOther();
81         assertFalse(other);
82         
83         dn = "dn=qualifier,cn=Tomas Gustavsson,1.1.1.1=Foo,o=PrimeKey,L=Stockholm,dc=PrimeKey,DC=com";
84         extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTDN);
85         illegal = extractor.isIllegal();
86         assertFalse(illegal);
87         other = extractor.existsOther();
88         assertTrue(other);
89         num = extractor.getNumberOfFields(28);
90         assertEquals(1, num);
91         String JavaDoc field = extractor.getField(28,0);
92         assertEquals("qualifier", field);
93         field = extractor.getField(DNFieldExtractor.CN,0);
94         assertEquals("Tomas Gustavsson", field);
95         
96         dn = "qqq,cn=Tomas Gustavsson,1.1.1.1=Foo,o=PrimeKey,L=Stockholm,dc=PrimeKey,DC=com";
97         extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTDN);
98         illegal = extractor.isIllegal();
99         assertTrue(illegal);
100     }
101
102     /**
103      * @throws Exception error
104      */

105     public void test01CheckAltNameFields() throws Exception JavaDoc {
106         String JavaDoc dn = "DnsName=foo.bar.se,rfc822Name=foo@bar.se";
107         DNFieldExtractor extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME);
108         HashMap JavaDoc i = extractor.getNumberOfFields();
109         assertEquals(11,i.size());
110         String JavaDoc dns = extractor.getField(DNFieldExtractor.DNSNAME, 0);
111         assertEquals("foo.bar.se", dns);
112         boolean illegal = extractor.isIllegal();
113         assertFalse(illegal);
114         boolean other = extractor.existsOther();
115         assertFalse(other);
116         String JavaDoc email= extractor.getField(DNFieldExtractor.RFC822NAME, 0);
117         assertEquals("foo@bar.se", email);
118         int num = extractor.getNumberOfFields(DNFieldExtractor.RFC822NAME);
119         assertEquals(1, num);
120         
121         dn = "uniformResourceId=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1";
122         extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME);
123         illegal = extractor.isIllegal();
124         assertFalse(illegal);
125         other = extractor.existsOther();
126         assertFalse(other);
127         num = extractor.getNumberOfFields(DNFieldExtractor.URI);
128         assertEquals(1, num);
129         String JavaDoc field = extractor.getField(DNFieldExtractor.URI, 0);
130         assertEquals("http://www.a.se/", field);
131         dn = "uniformResourceIdentifier=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1";
132         extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME);
133         illegal = extractor.isIllegal();
134         assertFalse(illegal);
135         other = extractor.existsOther();
136         assertFalse(other);
137         num = extractor.getNumberOfFields(DNFieldExtractor.URI);
138         assertEquals(1, num);
139         dn = "uri=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1";
140         extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME);
141         illegal = extractor.isIllegal();
142         assertFalse(illegal);
143         other = extractor.existsOther();
144         assertFalse(other);
145         num = extractor.getNumberOfFields(DNFieldExtractor.URI);
146         assertEquals(1, num);
147         field = extractor.getField(DNFieldExtractor.URI, 0);
148         assertEquals("http://www.a.se/", field);
149         dn = "uniformResourceIdentifier=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1";
150         extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME);
151         illegal = extractor.isIllegal();
152         assertFalse(illegal);
153         other = extractor.existsOther();
154         assertFalse(other);
155         num = extractor.getNumberOfFields(DNFieldExtractor.URI);
156         assertEquals(1, num);
157         dn = "uniformResourceId=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1";
158         extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME);
159         illegal = extractor.isIllegal();
160         assertFalse(illegal);
161         other = extractor.existsOther();
162         assertFalse(other);
163         num = extractor.getNumberOfFields(DNFieldExtractor.URI);
164         assertEquals(1, num);
165         field = extractor.getField(DNFieldExtractor.URI, 0);
166         assertEquals("http://www.a.se/", field);
167     }
168     
169     /**
170      * @throws Exception error
171      */

172     public void test01CheckDirAttrFields() throws Exception JavaDoc {
173         String JavaDoc dn = "PlaceOfBirth=Stockholm,DateOfBirth=10660911";
174         DNFieldExtractor extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTDIRATTR);
175         HashMap JavaDoc i = extractor.getNumberOfFields();
176         assertEquals(5,i.size());
177         String JavaDoc dns = extractor.getField(DNFieldExtractor.PLACEOFBIRTH, 0);
178         assertEquals("Stockholm", dns);
179         boolean illegal = extractor.isIllegal();
180         assertFalse(illegal);
181         boolean other = extractor.existsOther();
182         assertFalse(other);
183         String JavaDoc email= extractor.getField(DNFieldExtractor.DATEOFBIRTH, 0);
184         assertEquals("10660911", email);
185         int num = extractor.getNumberOfFields(DNFieldExtractor.DATEOFBIRTH);
186         assertEquals(1, num);
187
188         extractor = new DNFieldExtractor("", DNFieldExtractor.TYPE_SUBJECTDIRATTR);
189         i = extractor.getNumberOfFields();
190         assertEquals(5,i.size());
191         num = extractor.getNumberOfFields(DNFieldExtractor.DATEOFBIRTH);
192         assertEquals(0, num);
193
194     }
195
196 }
197
198
Popular Tags