Primary key class provide implementation of equals() methods test.
Enterprise Bean's primary key class
The class must provide suitable implementation of the equals(Object other)
methods to simplify the management of the primary keys by client code.
Result result = getInitializedResult();
ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
if (descriptor instanceof EjbEntityDescriptor) {
String transactionType = descriptor.getTransactionType();
if (EjbDescriptor.CONTAINER_TRANSACTION_TYPE.equals(transactionType)) {
boolean hasDefinedEqaulsMethod = false;
boolean oneFailed = false;
int lc = 0;
// RULE: Primary key class must defined equals(Object other) method
try {
Context context = getVerifierContext();
ClassLoader jcl = context.getClassLoader();
// retrieve the EJB primary key class
Class c = Class.forName(((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(), false, getVerifierContext().getClassLoader());
Method methods[] = c.getDeclaredMethods();
for (int i=0; i< methods.length; i++) {
if (methods[i].getName().equals("equals")){
// this is the right primary key class method equals()
hasDefinedEqaulsMethod = true;
// used in output below
lc = i;
break;
}
}
if (hasDefinedEqaulsMethod) {
result.addGoodDetails(smh.getLocalString
("tests.componentNameConstructor",
"For [ {0} ]",
new Object[] {compName.toString()}));
result.addGoodDetails(smh.getLocalString
(getClass().getName() + ".debug1",
"For EJB primary key class [ {0} ]",
new Object[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
result.addGoodDetails(smh.getLocalString
(getClass().getName() + ".passed",
"Primary key class method [ {0} ] was defined in the primary key class.",
new Object[] {methods[lc].getName()}));
} else if (!hasDefinedEqaulsMethod) {
oneFailed = true;
result.addErrorDetails(smh.getLocalString
("tests.componentNameConstructor",
"For [ {0} ]",
new Object[] {compName.toString()}));
result.addErrorDetails(smh.getLocalString
(getClass().getName() + ".debug1",
"For EJB primary key class [ {0} ]",
new Object[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
result.addErrorDetails(smh.getLocalString
(getClass().getName() + ".failed",
"Error: Primary key class method equal() was not defined in the primary key class."));
}
} catch (ClassNotFoundException e) {
Verifier.debug(e);
result.addErrorDetails(smh.getLocalString
("tests.componentNameConstructor",
"For [ {0} ]",
new Object[] {compName.toString()}));
result.failed(smh.getLocalString
(getClass().getName() + ".failedException",
"Error: Primary Key Class [ {0} ] not found within bean [ {1} ]",
new Object[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(), descriptor.getName()})
);
}
if (oneFailed) {
result.setStatus(result.FAILED);
} else {
result.setStatus(result.PASSED);
}
} else {
// not container managed, but is a entity bean
result.addNaDetails(smh.getLocalString
("tests.componentNameConstructor",
"For [ {0} ]",
new Object[] {compName.toString()}));
result.notApplicable(smh.getLocalString
(getClass().getName() + ".notApplicable2",
"Bean [ {0} ] is not [ {1} ] managed, it is [ {2} ] managed.",
new Object[] {descriptor.getName(),EjbDescriptor.CONTAINER_TRANSACTION_TYPE,transactionType}));
}
return result;
} else {
result.addNaDetails(smh.getLocalString
("tests.componentNameConstructor",
"For [ {0} ]",
new Object[] {compName.toString()}));
result.notApplicable(smh.getLocalString
(getClass().getName() + ".notApplicable",
"{0} expected {1} bean, but called with {2} bean.",
new Object[] {getClass(),"Entity","Session"}));
return result;
}