📄 jnditest.java
字号:
//TE: Deletes attribute values of a specified attribute within a specified entry,
// for example:
// dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
// changetype: modify
// delete: drink
// drink: red wine
// drink: white wine
// drink: water
//
// will delete these three 'drink' attributes from the entry.
if(delete != null) //TE: tests if the modify operation is 'delete'.
{
String attrString = "";
try{ attrString = delete.get().toString(); } catch (Exception e) {} // never happen
DXAttribute attr = (DXAttribute)entry.get(attrString);
try
{
myOps.deleteAttribute(myDN, attr);
if (expectedValue == false)
error("\nmodify-delete operation unexpectedly succeeded for: " + entry + "\n", null);
}
catch (NamingException e)
{
if(expectedValue == true)
error("\nmodify-delete operation failed for: " + entry + "\n", e);
}
}
//TE: Replaces all of the values of a specified attribute in the specified entry,
// for example:
// dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
// changetype: modify
// replace: drink
// drink: beer
//
// will replace all of the current values of attribute 'drink' with value 'beer'.
// In other words, if there are currently multiple 'drink' attributes they all
// will be removed and replaced with this one new value.
if(replace != null) //TE: tests if the modify operation is 'replace'.
{
String attrString = "";
try{ attrString = replace.get().toString(); } catch (Exception e) {} // never happen
DXAttribute attr = (DXAttribute)entry.get(attrString);
try
{
myOps.updateAttribute(myDN, attr);
if (expectedValue == false)
error("\nmodify-replace operation unexpectedly succeeded for: " + entry + "\n", null);
}
catch (NamingException e)
{
if(expectedValue == true)
error("\nmodify-replace operation failed for: " + entry + "\n", e);
}
}
}
/**
* Gets the 'listresult' from the test ldif file and parses it into a string.
* 'searchresult' is an integer value pertaining to the amount of list results
* expected to be returned from a search. Converts to int. Removes the 'listresult' attribute.
* @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
* @return list an integer value representing the amount of entries that are expected to be returned by the list.
*/
public int list(DXEntry entry)
{
DXAttribute listResult = (DXAttribute)entry.get("listresult");
String myListResult = "";
try{ myListResult = listResult.get().toString(); } catch (Exception e) {}
int list = -1;
try{list = Integer.parseInt(myListResult); } catch (Exception e) {}
if(listResult != null)
{
if (debug) System.out.println("\n\nparsed listresult(DXAttribute): " + listResult + "\n to myListResult(String): " + myListResult + ", to list(int): " + list);
entry.remove("listresult");
}
return list;
}
/**
* Lists an entry in a directory. Compares the 'listresult' value in the ldif file to the
* number of entries returned by the list. If the two are not equal the program exists.
* @param entry contains the dn to start searching from, and a 'filter' attribute with a search filter.
* @param expectedList a flag that indicates the number of entries that are expected to be listed.
*/
public void listEntry(DXEntry entry, int expectedList)
throws NamingException
{
if (debug) System.out.println("\nlist: " + entry);
NamingEnumeration names = myOps.list(entry.getDN());
if (debug) out.println("\nlist of children:");
int i = 0; //TE: the flag to compare the expected number of list results with the actual number of list results.
while (names.hasMore()) //TE: Counts & lists entries.
{
i++;
if (debug) out.println(((NameClassPair)names.next()).getName());
}
if(i != expectedList)
error("\nList operation failed for: " + entry + "\nExpected list results: " + expectedList + "\nActual list results: " + i, null);
}
/**
* Gets the 'searchresult' value from the test ldif file and parses it into a string.
* 'searchresult' is an integer value pertaining to the amount of search results
* expected to be returned from a search. Converts to int. Removes the 'searchresult' attribute.
* @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
* @return search an integer value representing the amount of entries expected to be returned by the search.
*/
public int getSearchResultAtt(DXEntry entry)
{
DXAttribute searchResult = (DXAttribute)entry.get("searchresult");
String mySearch = "";
try{ mySearch = searchResult.get().toString(); } catch (Exception e) {}
int search = -1;
try{search = Integer.parseInt(mySearch); } catch (Exception e) {}
if(searchResult != null)
{
if (debug) System.out.println("\n\nparsed searchresult(DXAttribute): " + searchResult + "\n to mySearch(String): " + mySearch + ", to myResult(int): " + search);
entry.remove("searchresult");
}
return search;
}
/**
* Searches a directory. Compares the 'searchresult' value in the ldif file to the
* number of entries returned by the search. If the two are not equal the program exists.
* @param entry contains.
* @param expectedSearch a flag that indicates the number of results returned by the search.
* <ul>
* <li> dn to start searching from
* <li> a 'filter' attribute containing an ldap search filter
* <li> an (optional) 'limit' attribute containing a number of returned entries limit
* <li> an (optional) 'timeout' attribute containing a time limit (milliseconds).
* </ul>
*/
public void searchEntry(DXEntry entry, int expectedSearch)
throws NamingException
{
String filter = entry.getString("filter");
String limit = entry.getString("limit");
String timeout = entry.getString("timeout");
if (limit == null) limit = "0";
if (timeout == null) timeout = "0";
int lim = Integer.parseInt(limit);
int time = Integer.parseInt(timeout);
if (debug) System.out.println("\nSEARCH " + entry + "\n filter: " + filter + "\n limit: " + limit + "\n timeout: " + timeout);
NamingEnumeration names = myOps.searchSubTree(entry.getDN(), filter, lim, time);
if (debug) out.println("\nNAMES: " + names + "\n\nDN: " + entry);
if (debug) out.println("\nsubtree search results:");
int i = 0; //TE: the flag to compare the expected number of search results with the actual number of search results.
while (names.hasMore()) //TE: Counts & lists search results.
{
i++;
if (debug) out.println(((SearchResult)names.next()).getName());
}
if(i != expectedSearch)
error("\nSearch operation failed for: " + entry + "\nExpected search results: " + expectedSearch + "\nActual search results: " + i, null);
}
/**
* Searches one level of a directory. Compares the 'searchresult' value in the ldif file to the
* number of entries returned by the search one level. If the two are not equal the program exists.
* @param entry contains.
* @param expectedSearch a flag that indicates the number of results returned by the search.
* <ul>
* <li> dn to start searching from.
* <li> a 'filter' attribute containing an ldap search filter.
* <li> an (optional) 'limit' attribute containing a number of returned entries limit.
* <li> an (optional) 'timeout' attribute containing a time limit (milliseconds).
* </ul>
*/
public void searchOneLevelEntry(DXEntry entry, int expectedSearch)
throws NamingException
{
String filter = entry.getString("filter");
String limit = entry.getString("limit");
String timeout = entry.getString("timeout");
if (limit == null) limit = "0";
if (timeout == null) timeout = "0";
if (debug) System.out.println("\n\nSEARCHONELEVEL: " + entry + "\n filter: " + filter + "\n limit: " + limit + "\n timeout: " + timeout);
int lim = Integer.parseInt(limit);
int time = Integer.parseInt(timeout);
NamingEnumeration names = myOps.searchOneLevel(entry.getDN(), filter, lim, time);
if (debug) out.println("\n\none level search results:");
int i = 0; //TE: the flag to compare the expected number of search results with the actual number of search results.
while (names.hasMore()) //TE: Counts & lists search results.
{
i++;
if (debug) out.println(((SearchResult)names.next()).getName());
}
if(i != expectedSearch)
error("\n\nSearchOneLevel operation failed for: " + entry + "\n\nExpected search results: " + expectedSearch + "\nActual search results: " + i, null);
}
/**
* Searches one level of a directory, and verifies that the returned results are sorted in the
* requested manner.
* @param entry contains.
* @param expectedSearch a flag that indicates the number of results returned by the search.
* <ul>
* <li> dn to start searching from.
* <li> a 'filter' attribute containing an ldap search filter.
* <li> an (optional) 'limit' attribute containing a number of returned entries limit.
* <li> an (optional) 'timeout' attribute containing a time limit (milliseconds).
* <li> a 'searchAttribute1' attribute as the primary search key
* <li> an (optional) 'searchAttribute2' attribute as the secondary search key
* </ul>
* <p>WARNING: This is a LIMITED test of sorting behaviour. Specifically, it uses case sensitive string matching,
* which is not appropriate to all attributes. However, any other behaviour would require this client to be
* aware of syntax matching rules, which is currently not implemented.
* </p>
*/
public void searchSorted(DXEntry entry, int expectedSearch) {}
/* // REQUIRES SORT CONTROL CAPABILITY - USUALLY VIA AN EXTENSION JAR FILE...
public void searchSorted(DXEntry entry, int expectedSearch)
throws NamingException
{
try
{
String filter = entry.getString("filter");
String limit = entry.getString("limit");
String timeout = entry.getString("timeout");
String att1 = entry.getString("searchAttribute1");
String att2 = entry.getString("searchAttribute2");
if (limit == null) limit = "0";
if (timeout == null) timeout = "0";
if (debug) System.out.println("\n\nSEARCHSORTED: " + entry + "\n filter: " + filter + "\n limit: " + limit + "\n timeout: " + timeout);
int lim = Integer.parseInt(limit);
int time = Integer.parseInt(timeout);
// set up server side sorting control
Control[] ctxCtls;
String[] searchAttributes;
if (att2 == null)
searchAttributes = new String[]{att1};
else
searchAttributes = new String[]{att1, att2};
ctxCtls = new Control[] { new SortControl(searchAttributes, Control.CRITICAL) };
LdapContext ctx = new InitialLdapContext(myOps.getContext().getEnvironment(), null);
ctx.setRequestControls(ctxCtls);
// set up search controls
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
constraints.setCountLimit(lim);
constraints.setTimeLimit(time);
constraints.setReturningAttributes(searchAttributes); // return the search attributes for verification
// do search
NamingEnumeration names = ctx.search(entry.getDN(), filter, null);
if (debug)
out.println("\n\nsorted one level search results:");
int i = 0; //TE: the flag to compare the expected number of search results with the actual number of search results.
String previousAtt1 = "";
String previousAtt2 = "";
while (names.hasMore()) //TE: Counts & lists search results.
{
i++;
SearchResult latest = (SearchResult)names.next();
if (debug) out.println(latest.getName());
String currentAtt1 = (String)latest.getAttributes().get(att1).get();
if (debug) out.println(" ... " + att1 + ": " + currentAtt1);
if (previousAtt1.compareTo(currentAtt1) < 0)
// error("sorted results failed: attribute " + currentAtt1 + " is not greater than " + previousAtt1 + " in entry " + latest.getName(), null);
out.println("sorted results failed: attribute " + currentAtt1 + " is not greater than " + previousAtt1 + " in entry " + latest.getName());
if (att2 != null)
{
String currentAtt2 = (String)latest.getAttributes().get(att2).get();
if (debug) out.println(" ... " + att2 + ": " + currentAtt2);
if ((previousAtt1.equals(currentAtt1)) && (previousAtt2.compareTo(currentAtt2) < 0))
// error("sorted results failed: attribute " + currentAtt2 + " is not greater than " + previousAtt2 + " in entry " + latest.getName(), null);
out.println("sorted results failed: attribute " + currentAtt2 + " is not greater than " + previousAtt2 + " in entry " + latest.getName());
}
}
if(i != expectedSearch)
error("\n\nSearchSorted (OneLevel) operation failed for: " + entry + "\n\nExpected search results: " + expectedSearch + "\nActual search results: " + i, null);
}
catch (IOException e)
{
error("unexpected IOException constructing sorted search control", e);
}
}
*/
/**
* Gets the 'readresultcn' value from the test ldif file and parses into a string.
* 'readresultcn' is the 'cn' attribute of the entry, for example cn: Cora BALDWIN.
* Removes the 'readresultcn' attribute. Note: 'readresultcn' can be configured to
* represent any string attribute.
* @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
* @return myReadCn a string 'cn' that is expected to be returned by the read.
*/
public String read(DXEntry entry)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -