Path: blob/master/test/jdk/javax/naming/ldap/LdapName/EscapeUnescapeTests.java
41153 views
/*1* Copyright (c) 2003, 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.Rdn;3031/**32* Tests for Rdn escapeValue and unescapeValue methods33*/34public class EscapeUnescapeTests {3536public static void main(String args[]) throws Exception {3738/**39* Unescape tests40*/41String[] invalids = new String[] {"\\", "\\\\\\" };4243String[] valids = new String[] {"\\\\", "\\\\\\\\"};4445String val;46Object unescVal = null;47System.out.println("##### Unescape value tests #####");4849for (int i = 0; i < valids.length; i++) {50unescVal = Rdn.unescapeValue(valids[i]);51System.out.println("Orig val: " + valids[i] +52" Unescaped val: " + unescVal);53}5455boolean isExcepThrown = false;56for (int i = 0; i < invalids.length; i++) {57val = "Juicy" + invalids[i] + "Fruit";58try {59unescVal = Rdn.unescapeValue(val);60} catch (IllegalArgumentException e) {61System.out.println("Caught the right exception: " + e);62isExcepThrown = true;63}64if (!isExcepThrown) {65throw new Exception(66"Unescaped successfully an invalid string "67+ val + " as Rdn: " + unescVal);68}69isExcepThrown = false;70}7172/**73* Escape tests74*/75String[] values = new String[] {";", "<<<", "###", "=="};76System.out.println("##### Escape value tests #####");77printEscapedVal(values);7879// leading space, trailing space80values = new String[] {" leading space", "trailing space "};81printEscapedVal(values);8283// binary values84byte[] bytes = new byte[] {1, 2, 3, 4};85String escVal = Rdn.escapeValue(bytes);86System.out.println("Orig val: " + bytes +87" Escaped val: " + escVal);88}8990static void printEscapedVal(Object[] values) {91String escVal;92for (int i = 0; i < values.length; i++) {93escVal = Rdn.escapeValue(values[i]);94System.out.println("Orig val: " + values[i] +95" Escaped val: " + escVal);96}97}98}99100101