Path: blob/master/test/jdk/javax/naming/ldap/LdapName/CompareToEqualsTests.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 4635618 705954226* @summary Support for manipulating LDAP Names27* JNDI name operations should be locale independent28*/2930import javax.naming.ldap.*;31import java.util.ArrayList;32import java.util.Locale;33import java.util.List;34import javax.naming.InvalidNameException;3536/**37* Tests for LdapName/Rdn compareTo, equals and hashCode methods.38*/39public class CompareToEqualsTests {4041public static void main(String args[])42throws Exception {43Locale reservedLocale = Locale.getDefault();44try {45/**46* Test cases:47* 1) Same RDNs.48* 2) same RDN sequence with an AVA ordered differently.49* 3) RDN sequences of a differing AVA.50* 4) RDN sequence of different length.51* 5) RDN sequence of different Case.52* 6) Matching binary return values.53* 7) Binary values that don't match.54*/55String names1[] = new String [] {56"ou=Sales+cn=Bob", "ou=Sales+cn=Bob", "ou=Sales+cn=Bob",57"ou=Sales+cn=Scott+c=US", "cn=config"};5859String names2[] = new String [] {60"ou=Sales+cn=Bob", "cn=Bob+ou=Sales", "ou=Sales+cn=Scott",61"ou=Sales+cn=Scott", "Cn=COnFIG"};6263int expectedResults[] = {0, 0, -1, -1, 0};6465for (Locale locale : Locale.getAvailableLocales()) {66// reset the default locale67Locale.setDefault(locale);6869for (int i = 0; i < names1.length; i++) {70checkResults(new LdapName(names1[i]),71new LdapName(names2[i]), expectedResults[i]);72}7374byte[] value = "abcxyz".getBytes();75Rdn rdn1 = new Rdn("binary", value);76ArrayList<Rdn> rdns1 = new ArrayList<>();77rdns1.add(rdn1);78LdapName l1 = new LdapName(rdns1);7980Rdn rdn2 = new Rdn("binary", value);81ArrayList<Rdn> rdns2 = new ArrayList<>();82rdns2.add(rdn2);83LdapName l2 = new LdapName(rdns2);84checkResults(l1, l2, 0);8586l2 = new LdapName("binary=#61626378797A");87checkResults(l1, l2, 0);8889l2 = new LdapName("binary=#61626378797B");90checkResults(l1, l2, -1);9192System.out.println("Tests passed");93}94} finally {95// restore the reserved locale96Locale.setDefault(reservedLocale);97}98}99100101static void checkResults(LdapName name1, LdapName name2, int expectedResult)102throws Exception {103104System.out.println("Checking name1: " + name1 +105" and name2: " + name2);106107boolean isEquals = (expectedResult == 0) ? true : false;108109int result = name1.compareTo(name2);110if ((isEquals && (result != expectedResult)) ||111isPositive(result) != isPositive(expectedResult)) {112throw new Exception(113"Comparison test failed for name1:" +114name1 + " name2:" + name2 +115", expected (1 => positive, -1 => negetive): " +116expectedResult + " but got: " + result);117}118119if (name1.equals(name2) != isEquals) {120throw new Exception("Equality test failed for name1: " +121name1 + " name2:" + name2 + ", expected: " +122isEquals);123}124125if (isEquals && (name1.hashCode() != name2.hashCode())) {126System.out.println("name1.hashCode(): " + name1.hashCode() +127" name2.hashCode(): " + name2.hashCode());128throw new Exception("hashCode test failed for name1:" +129name1 + " name2:" + name2);130}131}132133static boolean isPositive(int n) {134return (n >= 0) ? true : false;135}136}137138139