📄 testjndiops.java
字号:
/**
* 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 = (Attribute) atts.get("cn");
if (debug) out.println("\nREAD: " + readCn);
String myReadCn = "";
try
{
myReadCn = readCn.get().toString();
}
catch (Exception e)
{
}
int compare = -2; //TE: the flag to compare the expected read with the actual read.
compare = expectedRead.compareTo(myReadCn); //TE: the compare of the expected read with the actual read.
if (compare != 0) //TE: if 0, the two results are the same, therefore the read performed as expected.
{
if (debug) out.println("\n\nREAD CN String: " + myReadCn);
if (debug) out.println("EXPECTEDVALUE : " + expectedRead);
error("\nRead operation failed for: " + entry + "\nExpected read result for cn: " + expectedRead + "\nActual read result for cn: " + myReadCn, null);
}
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;
try
{
namesOld = myOps.list(oldDN);
}
catch (NamingException e1)
{
System.err.println("failed getting old names");
e1.printStackTrace();
return;
}
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);
}
catch (NamingException e2)
{
System.err.println("error in copyTree()");
e2.printStackTrace();
}
//TE: copies old entry to new entry.
if (debug) System.out.println("Copy: " + oldEntry);
NamingEnumeration namesNew;
try
{
namesNew = myOps.list(newDN);
}
catch (NamingException e3)
{
System.err.println("error in getting new list");
e3.printStackTrace();
return;
}
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;
try
{
namesOld = myOps.list(oldDN);
}
catch (NamingException e1)
{
System.err.println("error getting namesOld");
e1.printStackTrace();
return;
}
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);
}
catch (NamingException e2)
{
System.err.println("error in moveTree()");
e2.printStackTrace();
} //TE: cuts (or moves) old entry to new entry.
if (debug) System.out.println("Copy: " + oldEntry);
NamingEnumeration namesNew;
try
{
namesNew = myOps.list(newDN);
}
catch (NamingException e3)
{
System.err.println("error getting namesNew");
e3.printStackTrace();
return;
}
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 a 'fake' entry with no dn, but a bunch of attributes.
*/
public void connect(DXEntry entry)
{
if (debug) System.out.println("connect: " + entry);
if (myOps != null)
try
{
myOps.close();
}
catch (NamingException e)
{
System.err.println("error in myOps.close()");
e.printStackTrace();
}
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)
{
System.err.println("error in myOps.close()");
e.printStackTrace();
}
}
/**
* Prints error message then terminates.
*/
public void error(String msg, Exception e)
{
out.println(msg + "\n");
if (e != null && printstack)
e.printStackTrace();
if (terminating)
System.exit(-1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -