📄 testjndiops.java
字号:
//TE: Adds new attribute values to the specified attribute in the directory,
// for example:
// dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
// changetype: modify
// add: drink
// drink: red wine
// drink: white wine
// drink: water
//
// will add three new 'drink' attributes to the entry.
if (add != null) //TE: tests if the modify operation is 'add'.
{
String attrString = "";
try
{
attrString = add.get().toString();
}
catch (Exception e)
{
} // never happen
DXAttribute attr = (DXAttribute) entry.get(attrString);
boolean ret = false;
try
{
myOps.addAttribute(myDN, attr);
ret = true;
}
catch (NamingException e1)
{
}
//TE: flag to test if the modification succeeded or not.
if (ret != expectedValue) //TE: tests if the modification status is what we expected.
{
if (debug) System.out.println("\n\nRET " + ret + " EXPECTEDVALUE " + expectedValue);
if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n");
error("\nmodify-add operation failed for: " + entry + "\n", null);
}
}
//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);
boolean ret = false;
try
{
myOps.deleteAttribute(myDN, attr);
ret = true;
}
catch (NamingException e1)
{
}
//TE: flag to test if the modification succeeded or not.
if (ret != expectedValue) //TE: tests if the modification status is what we expected.
{
if (debug) System.out.println("\n\nRET " + ret + " EXPECTEDVALUE " + expectedValue);
if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n");
error("\nmodify-delete operation failed for: " + entry + "\n", null);
}
}
//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);
boolean ret = false;
try
{
myOps.updateAttribute(myDN, attr);
ret = true;
}
catch (NamingException e1)
{
}
//TE: flag to test if the modification succeeded or not.
if (ret != expectedValue) //TE: tests if the modification status is what we expected.
{
if (debug) System.out.println("\n\nRET " + ret + " EXPECTEDVALUE " + expectedValue);
if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n");
error("\nmodify-replace operation failed for: " + entry + "\n", null);
}
}
}
/**
* 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 search(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);
}
/**
* 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)
{
DXAttribute readResultCn = (DXAttribute) entry.get("readresultcn");
String myReadCn = "";
try
{
myReadCn = readResultCn.get().toString();
}
catch (Exception e)
{
}
if (readResultCn != null)
{
if (debug) System.out.println("\n\nparsed readresultcn (DXAttribute): " + readResultCn + "\n to myReadCn(String): " + myReadCn);
entry.remove("readresultcn");
}
return myReadCn;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -