1 /*
   2  * Copyright (c) 2003, 2011, 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 7059542
  27  * @summary Support for manipulating LDAP Names
  28  *          JNDI name operations should be locale independent
  29  */
  30 
  31 import javax.naming.ldap.*;
  32 import java.util.ArrayList;
  33 import java.util.Locale;
  34 import java.util.List;
  35 import javax.naming.InvalidNameException;
  36 
  37 /**
  38  * Tests for LdapName/Rdn compareTo, equals and hashCode methods.
  39  */
  40 public class CompareToEqualsTests {
  41 
  42     public static void main(String args[])
  43                 throws Exception {
  44          Locale reservedLocale = Locale.getDefault();
  45          try {
  46             /**
  47              * Test cases:
  48              * 1) Same RDNs.
  49              * 2) same RDN sequence with an AVA ordered differently.
  50              * 3) RDN sequences of a differing AVA.
  51              * 4) RDN sequence of different length.
  52              * 5) RDN sequence of different Case.
  53              * 6) Matching binary return values.
  54              * 7) Binary values that don't match.
  55              */
  56             String names1[] = new String [] {
  57                 "ou=Sales+cn=Bob", "ou=Sales+cn=Bob", "ou=Sales+cn=Bob",
  58                 "ou=Sales+cn=Scott+c=US", "cn=config"};
  59 
  60             String names2[] = new String [] {
  61                 "ou=Sales+cn=Bob", "cn=Bob+ou=Sales", "ou=Sales+cn=Scott",
  62                 "ou=Sales+cn=Scott", "Cn=COnFIG"};
  63 
  64             int expectedResults[] = {0, 0, -1, -1, 0};
  65 
  66             for (Locale locale : Locale.getAvailableLocales()) {
  67                 // reset the default locale
  68                 Locale.setDefault(locale);
  69 
  70                 for (int i = 0; i < names1.length; i++) {
  71                     checkResults(new LdapName(names1[i]),
  72                         new LdapName(names2[i]), expectedResults[i]);
  73                 }
  74 
  75                 byte[] value = "abcxyz".getBytes();
  76                 Rdn rdn1 = new Rdn("binary", value);
  77                 ArrayList rdns1 = new ArrayList();
  78                 rdns1.add(rdn1);
  79                 LdapName l1 = new LdapName(rdns1);
  80 
  81                 Rdn rdn2 = new Rdn("binary", value);
  82                 ArrayList rdns2 = new ArrayList();
  83                 rdns2.add(rdn2);
  84                 LdapName l2 = new LdapName(rdns2);
  85                 checkResults(l1, l2, 0);
  86 
  87                 l2 = new LdapName("binary=#61626378797A");
  88                 checkResults(l1, l2, 0);
  89 
  90                 l2 = new LdapName("binary=#61626378797B");
  91                 checkResults(l1, l2, -1);
  92 
  93                 System.out.println("Tests passed");
  94             }
  95         } finally {
  96             // restore the reserved locale
  97             Locale.setDefault(reservedLocale);
  98         }
  99     }
 100 
 101 
 102     static void checkResults(LdapName name1, LdapName name2, int expectedResult)
 103                 throws Exception {
 104 
 105         System.out.println("Checking name1: " + name1 +
 106                 " and name2: " + name2);
 107 
 108         boolean isEquals = (expectedResult == 0) ? true : false;
 109 
 110         int result = name1.compareTo(name2);
 111         if ((isEquals && (result != expectedResult)) ||
 112                 isPositive(result) != isPositive(expectedResult)) {
 113             throw new Exception(
 114                 "Comparison test failed for name1:" +
 115                 name1 + " name2:" + name2 +
 116                 ", expected (1 => positive, -1 => negetive): " +
 117                 expectedResult + " but got: " + result);
 118         }
 119 
 120         if (name1.equals(name2) != isEquals) {
 121             throw new Exception("Equality test failed for name1: " +
 122                         name1 + " name2:" + name2 + ", expected: " +
 123                         isEquals);
 124         }
 125 
 126         if (isEquals && (name1.hashCode() != name2.hashCode())) {
 127            System.out.println("name1.hashCode(): " + name1.hashCode() +
 128                                 " name2.hashCode(): " + name2.hashCode());
 129             throw new Exception("hashCode test failed for name1:" +
 130                         name1 + " name2:" + name2);
 131         }
 132     }
 133 
 134     static boolean isPositive(int n) {
 135         return (n >= 0) ? true : false;
 136     }
 137 }