1 /*
2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4635618
27  * @summary Support for manipulating LDAP Names
28  */
29 
30 import javax.naming.ldap.*;
31 import java.util.*;
32 import javax.naming.*;
33 import java.io.*;
34 
35 /**
36  * LdapName tests- These tests are for testing the methods of
37  * javax.naming.Name interface as it applied to LdapName.
38  */
39 public class NameTests {
40 
main(String args[])41     public static void main(String args[]) throws Exception {
42 
43         String[] rdnStr = new String[] {"one=voilet"};
44 
45         ArrayList rdnList = new ArrayList();
46 
47         for (int i = 0; i < rdnStr.length; i++) {
48             rdnList.add(i, new Rdn(rdnStr[i]));
49         }
50         LdapName dn = new LdapName(rdnList);
51 
52         Collection rdns = dn.getRdns();
53         System.out.println("size is :" + dn.size());
54         System.out.println("isEmpty :" + dn.isEmpty());
55         System.out.println("************Printing as Rdns*********");
56         Iterator iter = rdns.iterator();
57         while (iter.hasNext()) {
58             System.out.println(iter.next());
59         }
60 
61         System.out.println();
62         System.out.println("************Printing the Enumeration*********");
63         Enumeration dnEnum = dn.getAll();
64         while (dnEnum.hasMoreElements()) {
65             System.out.println(dnEnum.nextElement());
66         }
67 
68         // addAll tests
69         System.out.println();
70         LdapName nameSuffix = new LdapName("two=Indigo");
71         System.out.println("addAll():" + dn.addAll(nameSuffix));
72 
73         ArrayList list = new ArrayList();
74         list.add(new Rdn("five=Yellow"));
75         System.out.println("Rdn- addAll():" + dn.addAll(list));
76 
77         nameSuffix = new LdapName("three=Blue");
78         System.out.println();
79         System.out.println("addAll at pos = 2");
80         System.out.println("addAll():" + dn.addAll(2, nameSuffix));
81 
82         list = new ArrayList();
83         list.add(new Rdn("four=Green"));
84         System.out.println();
85         System.out.println("addAll at pos = 3");
86         System.out.println("Rdn- addAll():" + dn.addAll(3, list));
87 
88         // add() tests
89         Rdn rdn;
90         System.out.println();
91         System.out.println("add():" + dn.add("eight=white"));
92         rdn = new Rdn("nine=Black");
93         System.out.println();
94         System.out.println("Rdn- add():" + dn.add(rdn));
95 
96         /*
97         Rdn nullRdn = null;
98         System.out.println("Rdn- add() with null RDN:" +
99                         dn.add(nullRdn));
100         */
101 
102         System.out.println();
103         System.out.println("add() at pos 5");
104         System.out.println("add():" + dn.add(5, "six=Orange"));
105         rdn = new Rdn("six=Orange");
106         System.out.println();
107         System.out.println("add() at pos 6");
108         System.out.println("Rdn- add():" + dn.add(6, "seven=Red"));
109 
110         // remove tests
111         System.out.println();
112         System.out.println("Removing entries at positions: 7, 8");
113         System.out.println("Removed:" + dn.remove(8));
114         System.out.println("Removed:" + dn.remove(7));
115 
116         // get tests
117         System.out.println();
118         System.out.println("toString():" + dn);
119         int size  = dn.size();
120         System.out.println("get(0):" + dn.get(0));
121         System.out.println("get(size() - 1):" + dn.get(size - 1));
122         System.out.println("getRdn(0):" + dn.getRdn(0));
123         System.out.println("getRdn(size() - 1):" + dn.getRdn(size - 1));
124 
125         System.out.println();
126         System.out.println("********Prefixes**********");
127         System.out.println("getPrefix(0):" + dn.getPrefix(0));
128         System.out.println("getPrefix(size / 2):" + dn.getPrefix(size / 2));
129         System.out.println("getPrefix(size):" + dn.getPrefix(size));
130 
131         System.out.println();
132         System.out.println("********Suffixes**********");
133         System.out.println("getSuffix(0):" + dn.getSuffix(0));
134         System.out.println("getSuffix(size/2):" + dn.getSuffix(size / 2));
135         System.out.println("getSuffix(size):" + dn.getSuffix(size));
136 
137         System.out.println();
138         System.out.println("startsWith(" + rdnStr[0] + "):" +
139                                 dn.startsWith(new LdapName(rdnStr[0])));
140 
141         String lastEntry = "seven=red";
142         System.out.println("startsWith(" + lastEntry + "):" +
143                                 dn.startsWith(new LdapName(lastEntry)));
144 
145         System.out.println("compositeName- startsWith(" +
146                         rdnStr[0] + "): " + dn.startsWith(
147                                         new CompositeName(rdnStr[0])));
148 
149         java.util.List prefixList = (dn.getRdns()).subList(0, size /2);
150         System.out.println("Rdn - startsWith(" + prefixList + "):" +
151                                 dn.startsWith(prefixList));
152 
153         System.out.println("Rdn - startsWith() - empty RDN list:" +
154                                 dn.startsWith(new ArrayList()));
155 
156         System.out.println();
157         System.out.println("endsWith(" + rdnStr[0] + "):" +
158                                 dn.endsWith(new LdapName(rdnStr[0])));
159 
160         System.out.println("endsWith(" + lastEntry + "):" +
161                                 dn.endsWith(new LdapName(lastEntry)));
162 
163         System.out.println("compositeName- endsWith(" + rdnStr[0] + "):    " +
164                 dn.endsWith(new CompositeName(rdnStr[0])));
165 
166         System.out.println("Rdn - endsWith(" + prefixList + "):" +
167                                 dn.endsWith(prefixList));
168 
169         System.out.println("Rdn - endsWith() empty RDN list:" +
170                                 dn.endsWith(new ArrayList()));
171 
172         // test clone
173         System.out.println();
174         System.out.println("cloned name:" + dn.clone());
175 
176         // test serialization
177         ObjectOutputStream out = new ObjectOutputStream(
178                                     new FileOutputStream("dn.ser"));
179         out.writeObject(dn);
180         out.close();
181 
182         ObjectInputStream in = new ObjectInputStream(
183                                     new FileInputStream("dn.ser"));
184 
185         System.out.println();
186         System.out.println("Deserialized name:" + in.readObject());
187         in.close();
188     }
189 }
190