📄 jnditest.java
字号:
{
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;
}
/**
* Reads an entry in a directory. Currently compares the 'readresultcn' value in the ldif file
* (can be configured to any value), to the 'cn' value of the result of the read. If the two
* values are not equal the program exits.
* @param entry contains the dn to list the attributes of.
* @param expectedRead a flag that indicates the value that the read is expected to return.
*/
public void readEntry(DXEntry entry, String expectedRead)
throws NamingException
{
if (debug) System.out.println("\nread: " + entry);
Attributes atts = myOps.read(entry.getDN());
if (atts == null)
{
throw new NamingException("\nUnable to read entry " + entry.getDN());
}
Attribute readCn = atts.get("cn");
if (debug) out.println("\nREAD: " + readCn);
try
{
if(!readCn.contains(expectedRead)) //TE: if 0, the two results are the same, therefore the read performed as expected.
{
for(int i=0; i<readCn.size();i++)
if (debug) out.println("\n\nREAD CN String: " + readCn.get(i));
if (debug) out.println( "EXPECTEDVALUE : " + expectedRead);
error("\nRead operation failed for: " + entry + "\nExpected read result for cn: " + expectedRead + "\nActual read result for cn: " + readCn.toString(), null);
}
}
catch (Exception e) {}
DXEntry val = new DXEntry(atts);
if (debug) out.println(val);
}
/**
* Gets the 'copyTo' value from the test ldif file and parses into a string.
* 'copyTo' is the 'DN' of the new entry to where the DN of the old entry gets copied to.
* Removes the 'copyTo' attribute.
* @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
* @param command the type of test to be performed, i.e copy.
* @return myCopyTo the DN of the where the old entry is to be copy (or moved) to.
*/
public String copy(DXEntry entry, DXAttribute command)
{
DXAttribute copyTo = (DXAttribute)entry.get("copyTo");
String myCopyTo = "";
try{ myCopyTo = copyTo.get().toString(); } catch (Exception e) {}
if(copyTo != null)
{
if (debug) System.out.println("copyTo: " + copyTo + "\ncommand: " + command + "\nmyCopyTo: " + myCopyTo);
if (debug) System.out.println("\n\nparsed copyTo (DXAttribute): " + copyTo + "\n to myCopyTo(String): " + myCopyTo);
entry.remove("copyTo");
}
return myCopyTo;
}
/**
* Copies an entry to a new DN.
* @param oldEntry is what is being copied.
* @param newEntry is the new DN of where the entry is copied to.
*/
public void copy(DXEntry oldEntry, String newEntry)
{
Name newDN = myOps.postParse(newEntry); //TE: Converts the new DN string (copyTo) from the LDIF file into Name.
Name oldDN = oldEntry.getDN();
if (debug) System.out.println("old DN: " + oldDN);
if (debug) System.out.println("new DN: " + newDN);
NamingEnumeration namesOld = null;
try
{
namesOld = myOps.list(oldDN);
}
catch (NamingException e)
{
error("\nUnable to list: " + oldDN, e);
}
if (debug) out.println("\nCopy of OLD children:");
int n = 0; //TE: Counter for OLD entries.
try
{
while (namesOld.hasMore()) //TE: Counts & lists OLD entries.
{
n++;
if (debug) out.println("Old Entries: " + ((NameClassPair)namesOld.next()).getName());
}
}
catch (Exception e)
{
if (debug) System.out.println("List for OLD entries failed during copy process");
}
try
{
myOps.copyTree(oldDN, newDN); //TE: copies old entry to new entry.
}
catch (NamingException e)
{
error("\nUnable to copy tree" + newDN, e);
}
if (debug) System.out.println("Copy: " + oldEntry);
NamingEnumeration namesNew = null;
try
{
namesNew = myOps.list(newDN);
}
catch (NamingException e)
{
error("\nUnable to list " + newDN, e);
}
if (debug) out.println("\nCopy of NEW children:");
int i = 0; //TE: Counter for NEW entries.
try
{
while (namesNew.hasMore()) //TE: Counts & lists NEW entries.
{
i++;
if (debug) out.println("New Entries: " + ((NameClassPair)namesNew.next()).getName());
}
}
catch (Exception e)
{
if (debug) System.out.println("List for NEW entries failed during copy process");
}
if(i != n) //TE: checks that the list contains the same number of entries.
error("\nCopy operation failed for: " + oldEntry + "\nExpected number of copied entries: " + n + "\nActual number of copied entries: " + i, null);
}
/**
* Gets the 'cutTo' value from the test ldif file and parses into a string.
* 'cutTo' is the 'DN' of the new entry to where the DN of the old entry gets cut (or moved) to.
* Removes the 'cutTo' attribute.
* @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
* @param command the type of test to be performed, i.e cut.
* @return myCutTo the DN of the where the old entry is to be cut (or moved) to.
*/
public String cut(DXEntry entry, DXAttribute command)
{
DXAttribute cutTo = (DXAttribute)entry.get("cutTo");
String myCutTo = "";
try{ myCutTo = cutTo.get().toString(); } catch (Exception e) {}
if(cutTo != null)
{
if (debug) System.out.println("cutTo: " + cutTo + "\ncommand: " + command + "\nmyCutTo: " + myCutTo);
if (debug) System.out.println("\n\nparsed cutTo (DXAttribute): " + cutTo + "\n to myCutTo(String): " + myCutTo);
entry.remove("cutTo");
}
return myCutTo;
}
/**
* Cuts an entry to a new DN.
* @param oldEntry is what is being cut.
* @param newEntry is the new DN of where the entry is cut to.
*/
public void cut(DXEntry oldEntry, String newEntry)
{
Name newDN = myOps.postParse(newEntry); //TE: Converts the new DN string (copyTo) from the LDIF file into Name.
Name oldDN = oldEntry.getDN();
if (debug) System.out.println("old: " + oldDN);
if (debug) System.out.println("new: " + newDN);
NamingEnumeration namesOld = null;
try
{
namesOld = myOps.list(oldDN);
}
catch (NamingException e)
{
error("\nUnable to list " + oldDN, e);
}
if (debug) out.println("\nCut of OLD children:");
int n = 0; //TE: Counter for OLD entries.
try
{
while (namesOld.hasMore()) //TE: Counts & lists OLD entries.
{
n++;
if (debug) out.println("Old Entries: " + ((NameClassPair)namesOld.next()).getName());
}
}
catch (Exception e)
{
if (debug) System.out.println("List for OLD entries failed during cut process");
}
try
{
myOps.moveTree(oldDN, newDN); //TE: cuts (or moves) old entry to new entry.
}
catch (NamingException e)
{
error("\nUnable to list " + newDN, e);
}
if (debug) System.out.println("Copy: " + oldEntry);
NamingEnumeration namesNew = null;
try
{
namesNew = myOps.list(newDN);
}
catch (NamingException e)
{
error("\nUnable to list " + newDN, e);
}
if (debug) out.println("\nCut of NEW children:");
int i = 0; //TE: Counter for NEW entries.
try
{
while (namesNew.hasMore()) //TE: Counts & lists NEW entries.
{
i++;
if (debug) out.println("New Entries: " + ((NameClassPair)namesNew.next()).getName());
}
}
catch (Exception e)
{
if (debug) System.out.println("List for NEW entries failed during cut process");
}
if(i != n) //TE: checks that the list contains the same number of entries.
error("\nCut operation failed for: " + oldEntry + "\nExpected number of cut entries: " + n + "\nActual number of cut entries: " + i, null);
}
/**
* Opens a connection.
* @param entry contains a 'fake' entry with no dn, but a bunch of attributes.
*/
public void connect(DXEntry entry)
{
if (debug) System.out.println("connect: " + entry);
try
{
if (myOps != null) myOps.close();
}
catch (NamingException e)
{
error("\nUnable to close old connection when opening new connection", e);
}
String url = entry.getString("url");
String user = entry.getString("user");
String pwd = entry.getString("pwd");
String tracing = entry.getString("tracing");
String version = entry.getString("ldapVersion");
String referral = entry.getString("referral");
String useSSL = entry.getString("useSSL");
boolean trace = ((tracing != null) && (tracing.equalsIgnoreCase("true")));
boolean ssl = ((useSSL != null) && (useSSL.equalsIgnoreCase("true")));
openConnection(url, user, pwd, trace, version, referral, ssl);
}
/**
* Disconnected from the directory.
*/
public void disconnect(DXEntry entry)
{
if (debug) System.out.println("disconnected. ");
try
{
myOps.close();
}
catch (NamingException e)
{
error("\nUnable to close connection in disconnect", e);
}
}
/**
* Prints error message then terminates.
*/
public void error(String msg, Exception e)
{
out.println(msg + "\n");
if (e==null)
out.println(" (null error)");
else
if(printstack)
e.printStackTrace(out);
else
out.println(" error: " + e);
if (terminating)
System.exit(-1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -