jdk/test/javax/naming/ldap/LdapName/CompareToEqualsTests.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 10369 e9d2e59e53f0
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4635618
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Support for manipulating LDAP Names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.naming.ldap.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.InvalidNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Tests for LdapName/Rdn compareTo, equals and hashCode methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class CompareToEqualsTests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
         * Test cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
         * 1) Same RDNs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
         * 2) same RDN sequence with an AVA ordered differently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
         * 3) RDN sequences of a differing AVA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
         * 4) RDN sequence of different length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
         * 5) RDN sequence of different Case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
         * 6) Matching binary return values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
         * 7) Binary values that don't match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        String names1[] = new String [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                "ou=Sales+cn=Bob", "ou=Sales+cn=Bob", "ou=Sales+cn=Bob",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                "ou=Sales+cn=Scott+c=US", "cn=config"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        String names2[] = new String [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                "ou=Sales+cn=Bob", "cn=Bob+ou=Sales", "ou=Sales+cn=Scott",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                "ou=Sales+cn=Scott", "Cn=COnFIG"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int expectedResults[] = {0, 0, -1, -1, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for (int i = 0; i < names1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            checkResults(new LdapName(names1[i]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        new LdapName(names2[i]), expectedResults[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        byte[] value = "abcxyz".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Rdn rdn1 = new Rdn("binary", value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ArrayList rdns1 = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        rdns1.add(rdn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        LdapName l1 = new LdapName(rdns1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Rdn rdn2 = new Rdn("binary", value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        ArrayList rdns2 = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        rdns2.add(rdn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        LdapName l2 = new LdapName(rdns2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        checkResults(l1, l2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        l2 = new LdapName("binary=#61626378797A");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        checkResults(l1, l2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        l2 = new LdapName("binary=#61626378797B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        checkResults(l1, l2, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.out.println("Tests passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static void checkResults(LdapName name1, LdapName name2, int expectedResult)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println("Checking name1: " + name1 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                " and name2: " + name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        boolean isEquals = (expectedResult == 0) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int result = name1.compareTo(name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if ((isEquals && (result != expectedResult)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                isPositive(result) != isPositive(expectedResult)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                "Comparison test failed for name1:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                name1 + " name2:" + name2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ", expected (1 => positive, -1 => negetive): " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                expectedResult + " but got: " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (name1.equals(name2) != isEquals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new Exception("Equality test failed for name1: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        name1 + " name2:" + name2 + ", expected: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        isEquals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (isEquals && (name1.hashCode() != name2.hashCode())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
           System.out.println("name1.hashCode(): " + name1.hashCode() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                " name2.hashCode(): " + name2.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new Exception("hashCode test failed for name1:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        name1 + " name2:" + name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static boolean isPositive(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return (n >= 0) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}