1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package tests.api.javax.security.auth.x500;
19
20import java.util.HashMap;
21import java.util.Map;
22
23import javax.security.auth.x500.X500Principal;
24
25public class X500PrincipalTest extends junit.framework.TestCase {
26
27	/**
28	 * @tests javax.security.auth.x500.X500Principal#X500Principal(java.lang.String)
29	 */
30	public void test_ConstructorLjava_lang_String() {
31		X500Principal principal = new X500Principal(
32				"CN=Hermione Granger, O=Apache Software Foundation, OU=Harmony, L=Hogwarts, ST=Hants, C=GB");
33		String name = principal.getName();
34		String expectedOuput = "CN=Hermione Granger,O=Apache Software Foundation,OU=Harmony,L=Hogwarts,ST=Hants,C=GB";
35		assertEquals("Output order precedence problem", expectedOuput, name);
36	}
37
38    /**
39     * @tests javax.security.auth.x500.X500Principal#X500Principal(java.lang.String, java.util.Map)
40     */
41    public void test_ConstructorLjava_lang_String_java_util_Map(){
42        Map<String, String> keyword = new HashMap<String, String>();
43        keyword.put("CN", "2.19");
44        keyword.put("OU", "1.2.5.19");
45        keyword.put("O", "1.2.5");
46        X500Principal X500p = new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US ,CN=DD",keyword);
47        String name = X500p.getName();
48        String expectedOut = "2.19=#130444756b65,1.2.5.19=#13084a617661536f6674,1.2.5=#131053756e204d6963726f73797374656d73,C=US,2.19=#13024444";
49        assertEquals("Output order precedence problem", expectedOut, name);
50    }
51
52	/**
53	 * @tests javax.security.auth.x500.X500Principal#getName(java.lang.String)
54	 */
55	public void test_getNameLjava_lang_String() {
56		X500Principal principal = new X500Principal(
57				"CN=Dumbledore, OU=Administration, O=Hogwarts School, C=GB");
58		String canonical = principal.getName(X500Principal.CANONICAL);
59		String expected = "cn=dumbledore,ou=administration,o=hogwarts school,c=gb";
60		assertEquals("CANONICAL output differs from expected result", expected,
61				canonical);
62	}
63
64    /**
65     * @tests javax.security.auth.x500.X500Principal#getName(java.lang.String, java.util.Map)
66     */
67    public void test_getNameLjava_lang_String_java_util_Map() {
68        Map<String, String> keyword = new HashMap<String, String>();
69        keyword.put("CN", "2.19");
70        keyword.put("OU", "1.2.5.19");
71        keyword.put("O", "1.2.5");
72        X500Principal X500p = new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US ,CN=DD",keyword);
73        keyword = new HashMap<String, String>();
74        keyword.put("2.19", "mystring");
75        String rfc1779Name = X500p.getName("RFC1779",keyword);
76        String rfc2253Name = X500p.getName("RFC2253",keyword);
77        String expected1779Out = "mystring=Duke, OID.1.2.5.19=JavaSoft, OID.1.2.5=Sun Microsystems, C=US, mystring=DD";
78        String expected2253Out = "mystring=Duke,1.2.5.19=#13084a617661536f6674,1.2.5=#131053756e204d6963726f73797374656d73,C=US,mystring=DD";
79        assertEquals("Output order precedence problem", expected1779Out, rfc1779Name);
80        assertEquals("Output order precedence problem", expected2253Out, rfc2253Name);
81        try{
82            X500p.getName("CANONICAL",keyword);
83            fail("Should throw IllegalArgumentException exception here");
84        }
85        catch(IllegalArgumentException e){
86            //expected IllegalArgumentException here
87        }
88    }
89}