Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* **************************************************************************
*
* Copyright (C) 2002 Octet String, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM OCTET STRING, INC.,
* COULD SUBJECT THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
/*
* TestSelectRetrieve.java
*
* Created on March 13, 2002, 10:29 PM
*/
package com.octetstring.jdbcLdap.junit.sql;
import junit.framework.*;
import com.octetstring.jdbcLdap.sql.statements.JdbcLdapSelect;
import com.octetstring.jdbcLdap.jndi.JndiLdapConnection;
import com.octetstring.jdbcLdap.sql.*;
import java.sql.*;
import javax.naming.directory.*;
import javax.naming.*;
import java.io.*;
import java.util.*;
/**
*Tests using a JdbcLdapSelect instance to retrieve results from a LDAP server
*@author Marc Boorshtein, OctetString
*/
public class TestSelectRetrieve extends junit.framework.TestCase {
JndiLdapConnection con;
/** Creates new TestSelectRetrieve */
public TestSelectRetrieve(String name) {
super(name);
}
protected void tearDown() throws java.lang.Exception {
con.close();
}
protected void setUp() throws java.lang.Exception {
Class.forName("com.octetstring.jdbcLdap.sql.JdbcLdapDriver");
con = (JndiLdapConnection) DriverManager.getConnection(System.getProperty("ldapConnString") + "?SEARCH_SCOPE:=subTreeScope",System.getProperty("ldapUser"),System.getProperty("ldapPass"));
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
}
/**
*Retrieves info with :SELECT cn,sn,ou FROM WHERE ou=Payroll OR ou=Peons
*/
public void testSelectRetrieve() throws Exception {
String sql = "SELECT cn,sn,ou FROM WHERE ou=Payroll OR ou=Peons";
JdbcLdapSelect sel = new JdbcLdapSelect();
sel.init(con,sql);
NamingEnumeration enum = (NamingEnumeration) sel.executeQuery();
DirContext dir = con.getContext();
SearchControls ctls = new SearchControls();
String[] atts = {"cn","sn","ou"};
String filter = "(|(ou=Payroll)(ou=Peons))";
String base = "";
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
ctls.setReturningAttributes(atts);
NamingEnumeration enumCtl = dir.search(base,filter,ctls);
LinkedList l1 = load(enum);
LinkedList l2 = load(enumCtl);
if (l1.size() != l2.size()) fail("Same results not gotten");
assertTrue("Results don't match",this.compareLists(l1,l2));
}
/**
*Retrieves info with :SELECT cn,sn,ou FROM WHERE ou=? OR ou=?
*/
public void testSelectRetrieveArgs() throws Exception {
String sql = "SELECT cn,sn,ou FROM WHERE ou=? OR ou=?";
JdbcLdapSelect sel = new JdbcLdapSelect();
sel.init(con,sql);
sel.getArgs()[0] = "Payroll";
sel.getArgs()[1] = "Peons";
NamingEnumeration enum = (NamingEnumeration) sel.executeQuery();
DirContext dir = con.getContext();
SearchControls ctls = new SearchControls();
String[] atts = {"cn","sn","ou"};
String filter = "(|(ou=Payroll)(ou=Peons))";
String base = "";
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
ctls.setReturningAttributes(atts);
NamingEnumeration enumCtl = dir.search(base,filter,ctls);
LinkedList l1 = load(enum);
LinkedList l2 = load(enumCtl);
if (l1.size() != l2.size()) fail("Same results not gotten");
assertTrue("Results don't match",this.compareLists(l1,l2));
}
/**
*Retrieves info with :SELECT cn,sn,ou FROM ou=Peons
*/
public void testSelectRetrieveNoWhere() throws Exception {
String sql = "SELECT cn,sn,ou FROM ou=Peons";
JdbcLdapSelect sel = new JdbcLdapSelect();
sel.init(con,sql);
try {
NamingEnumeration enum = (NamingEnumeration) sel.executeQuery();
}
catch (SQLException e) {
StringWriter out = new StringWriter();
e.printStackTrace(new PrintWriter(out, true));
fail(out.toString());
}
DirContext dir = con.getContext();
SearchControls ctls = new SearchControls();
assertTrue(true);
}
/**
*Loads a NamingEnumeration into a LinkedList
*@param enum Search Results
*/
LinkedList load(NamingEnumeration enum) throws Exception {
LinkedList list = new LinkedList();
LinkedList row;
SearchResult res;
Attributes atts;
NamingEnumeration enum2;
Enumeration vals;
Attribute att;
String attrid;
String val;
Object obj;
while (enum.hasMore()) {
row = new LinkedList();
res = (SearchResult) enum.next();
//System.out.println(res.getName());
atts = (Attributes) res.getAttributes();
for (enum2 = atts.getAll();enum2.hasMore();) {
att = (Attribute) enum2.next();
attrid = att.getID();
//System.out.println(attrid);
val = "";
for (vals = att.getAll();vals.hasMoreElements();) {
obj = vals.nextElement();
val += obj.toString();
}
//System.out.println(val);
row.add(attrid + val);
}
list.add(row);
}
return list;
}
/**
*Compares two lists
*@param l1 List 1
*@param l2 List 2
*@return True if lists contain identical data
*/
boolean compareLists(LinkedList l1, LinkedList l2) {
int i,j;
LinkedList ll1, ll2;
for (i=0;i<l1.size();i++) {
ll1 = (LinkedList) l1.get(i);
ll2 = (LinkedList) l2.get(i);
if (ll1.size() != ll2.size()) return false;
for (j=0;j<ll1.size();j++) {
if (! ll1.get(j).toString().equalsIgnoreCase(ll2.get(j).toString())) return false;
}
}
return true;
}
/**
*Prints the contents of a table
*@param l LinkedList of data
*/
String printTable(LinkedList l) {
int i,j;
StringBuffer buf = new StringBuffer();
LinkedList row;
for (i=0;i< l.size();i++) {
row = (LinkedList) l.get(i);
for (j=0;j<row.size();j++) {
buf.append(row.get(j)).append("\t\t");
}
buf.append("\n");
}
return buf.toString();
}
}