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