Path: blob/master/test/jdk/javax/naming/ldap/LdapName/NameTests.java
41153 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 463561826* @summary Support for manipulating LDAP Names27*/2829import javax.naming.ldap.*;30import java.util.*;31import javax.naming.*;32import java.io.*;3334/**35* LdapName tests- These tests are for testing the methods of36* javax.naming.Name interface as it applied to LdapName.37*/38public class NameTests {3940public static void main(String args[]) throws Exception {4142String[] rdnStr = new String[] {"one=voilet"};4344ArrayList<Rdn> rdnList = new ArrayList<>();4546for (int i = 0; i < rdnStr.length; i++) {47rdnList.add(i, new Rdn(rdnStr[i]));48}49LdapName dn = new LdapName(rdnList);5051Collection<Rdn> rdns = dn.getRdns();52System.out.println("size is :" + dn.size());53System.out.println("isEmpty :" + dn.isEmpty());54System.out.println("************Printing as Rdns*********");55Iterator<Rdn> iter = rdns.iterator();56while (iter.hasNext()) {57System.out.println(iter.next());58}5960System.out.println();61System.out.println("************Printing the Enumeration*********");62Enumeration<String> dnEnum = dn.getAll();63while (dnEnum.hasMoreElements()) {64System.out.println(dnEnum.nextElement());65}6667// addAll tests68System.out.println();69LdapName nameSuffix = new LdapName("two=Indigo");70System.out.println("addAll():" + dn.addAll(nameSuffix));7172ArrayList<Rdn> list = new ArrayList<>();73list.add(new Rdn("five=Yellow"));74System.out.println("Rdn- addAll():" + dn.addAll(list));7576nameSuffix = new LdapName("three=Blue");77System.out.println();78System.out.println("addAll at pos = 2");79System.out.println("addAll():" + dn.addAll(2, nameSuffix));8081list = new ArrayList<>();82list.add(new Rdn("four=Green"));83System.out.println();84System.out.println("addAll at pos = 3");85System.out.println("Rdn- addAll():" + dn.addAll(3, list));8687// add() tests88Rdn rdn;89System.out.println();90System.out.println("add():" + dn.add("eight=white"));91rdn = new Rdn("nine=Black");92System.out.println();93System.out.println("Rdn- add():" + dn.add(rdn));9495/*96Rdn nullRdn = null;97System.out.println("Rdn- add() with null RDN:" +98dn.add(nullRdn));99*/100101System.out.println();102System.out.println("add() at pos 5");103System.out.println("add():" + dn.add(5, "six=Orange"));104rdn = new Rdn("six=Orange");105System.out.println();106System.out.println("add() at pos 6");107System.out.println("Rdn- add():" + dn.add(6, "seven=Red"));108109// remove tests110System.out.println();111System.out.println("Removing entries at positions: 7, 8");112System.out.println("Removed:" + dn.remove(8));113System.out.println("Removed:" + dn.remove(7));114115// get tests116System.out.println();117System.out.println("toString():" + dn);118int size = dn.size();119System.out.println("get(0):" + dn.get(0));120System.out.println("get(size() - 1):" + dn.get(size - 1));121System.out.println("getRdn(0):" + dn.getRdn(0));122System.out.println("getRdn(size() - 1):" + dn.getRdn(size - 1));123124System.out.println();125System.out.println("********Prefixes**********");126System.out.println("getPrefix(0):" + dn.getPrefix(0));127System.out.println("getPrefix(size / 2):" + dn.getPrefix(size / 2));128System.out.println("getPrefix(size):" + dn.getPrefix(size));129130System.out.println();131System.out.println("********Suffixes**********");132System.out.println("getSuffix(0):" + dn.getSuffix(0));133System.out.println("getSuffix(size/2):" + dn.getSuffix(size / 2));134System.out.println("getSuffix(size):" + dn.getSuffix(size));135136System.out.println();137System.out.println("startsWith(" + rdnStr[0] + "):" +138dn.startsWith(new LdapName(rdnStr[0])));139140String lastEntry = "seven=red";141System.out.println("startsWith(" + lastEntry + "):" +142dn.startsWith(new LdapName(lastEntry)));143144System.out.println("compositeName- startsWith(" +145rdnStr[0] + "): " + dn.startsWith(146new CompositeName(rdnStr[0])));147148List<Rdn> prefixList = (dn.getRdns()).subList(0, size /2);149System.out.println("Rdn - startsWith(" + prefixList + "):" +150dn.startsWith(prefixList));151152System.out.println("Rdn - startsWith() - empty RDN list:" +153dn.startsWith(new ArrayList<>()));154155System.out.println();156System.out.println("endsWith(" + rdnStr[0] + "):" +157dn.endsWith(new LdapName(rdnStr[0])));158159System.out.println("endsWith(" + lastEntry + "):" +160dn.endsWith(new LdapName(lastEntry)));161162System.out.println("compositeName- endsWith(" + rdnStr[0] + "): " +163dn.endsWith(new CompositeName(rdnStr[0])));164165System.out.println("Rdn - endsWith(" + prefixList + "):" +166dn.endsWith(prefixList));167168System.out.println("Rdn - endsWith() empty RDN list:" +169dn.endsWith(new ArrayList<>()));170171// test clone172System.out.println();173System.out.println("cloned name:" + dn.clone());174175// test serialization176ObjectOutputStream out = new ObjectOutputStream(177new FileOutputStream("dn.ser"));178out.writeObject(dn);179out.close();180181ObjectInputStream in = new ObjectInputStream(182new FileInputStream("dn.ser"));183184System.out.println();185System.out.println("Deserialized name:" + in.readObject());186in.close();187}188}189190191