1 23 package com.sun.enterprise.tools.verifier.tests.ejb.entity.ejbfindermethod; 24 25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest; 26 import java.lang.reflect.*; 27 import java.util.*; 28 import com.sun.enterprise.deployment.EjbEntityDescriptor; 29 import com.sun.enterprise.deployment.EjbDescriptor; 30 import com.sun.enterprise.deployment.MethodDescriptor; 31 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck; 32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils; 33 import com.sun.enterprise.tools.verifier.*; 34 import java.lang.ClassLoader ; 35 import com.sun.enterprise.tools.verifier.tests.*; 36 37 57 public class EjbFinderMethodObjectNotFoundException extends EjbTest implements EjbCheck { 58 59 60 83 public Result check(EjbDescriptor descriptor) { 84 85 Result result = getInitializedResult(); 86 ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor(); 87 88 if (descriptor instanceof EjbEntityDescriptor) { 89 String persistence = 90 ((EjbEntityDescriptor)descriptor).getPersistenceType(); 91 if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)) { 92 93 boolean okayToThrowObjectNotFoundException = false; 94 boolean throwsObjectNotFoundException = false; 95 boolean oneFailed = false; 96 int findMethodModifiers = 0; 97 int foundAtLeastOne = 0; 98 try { 99 Context context = getVerifierContext(); 101 ClassLoader jcl = context.getClassLoader(); 102 Class EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader()); 103 do { 105 Method [] ejbFinderMethods = EJBClass.getDeclaredMethods(); 106 String primaryKeyType = ((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(); 107 108 for (int j = 0; j < ejbFinderMethods.length; ++j) { 109 if (ejbFinderMethods[j].getName().startsWith("ejbFind")) { 110 okayToThrowObjectNotFoundException = false; 111 throwsObjectNotFoundException = false; 112 Class returnByPrimaryKeyValue = ejbFinderMethods[j].getReturnType(); 113 foundAtLeastOne++; 114 if (returnByPrimaryKeyValue.getName().equals(primaryKeyType)) { 117 okayToThrowObjectNotFoundException = true; 118 } else if ((returnByPrimaryKeyValue.getName().equals("java.util.Collection")) || 119 (returnByPrimaryKeyValue.getName().equals("java.util.Enumeration"))) { 120 okayToThrowObjectNotFoundException = false; 121 } 122 123 if (!okayToThrowObjectNotFoundException) { 127 Class [] exceptions = ejbFinderMethods[j].getExceptionTypes(); 130 if (EjbUtils.isValidObjectNotFoundExceptionException(exceptions)) { 131 throwsObjectNotFoundException = true; 132 } 133 } 134 135 136 if ((okayToThrowObjectNotFoundException) || ((!okayToThrowObjectNotFoundException) && (!throwsObjectNotFoundException))) { 137 result.addGoodDetails(smh.getLocalString 138 ("tests.componentNameConstructor", 139 "For [ {0} ]", 140 new Object [] {compName.toString()})); 141 result.addGoodDetails(smh.getLocalString 142 (getClass().getName() + ".debug1", 143 "For EJB Class [ {0} ] Finder Method [ {1} ]", 144 new Object [] {EJBClass.getName(),ejbFinderMethods[j].getName()})); 145 result.addGoodDetails(smh.getLocalString 146 (getClass().getName() + ".passed", 147 "An [ {0} ] method was found with valid exception types declared in throws clause.", 148 new Object [] {ejbFinderMethods[j].getName()})); 149 } else if ((!okayToThrowObjectNotFoundException) && throwsObjectNotFoundException) { 150 oneFailed = true; 151 result.addErrorDetails(smh.getLocalString 152 ("tests.componentNameConstructor", 153 "For [ {0} ]", 154 new Object [] {compName.toString()})); 155 result.addErrorDetails(smh.getLocalString 156 (getClass().getName() + ".debug1", 157 "For EJB Class [ {0} ] Finder Method [ {1} ]", 158 new Object [] {EJBClass.getName(),ejbFinderMethods[j].getName()})); 159 result.addErrorDetails(smh.getLocalString 160 (getClass().getName() + ".failed", 161 "Error: An [ {0} ] method was found, but [ {1} ] throws ObjectNotFoundException. Multi-object finder methods must not throw this exception.", 162 new Object [] {ejbFinderMethods[j].getName(),ejbFinderMethods[j].getName()})); 163 } 164 } 165 } 166 } while (((EJBClass = EJBClass.getSuperclass()) != null) && (foundAtLeastOne == 0)); 167 168 if (foundAtLeastOne == 0) { 169 result.addNaDetails(smh.getLocalString 170 ("tests.componentNameConstructor", 171 "For [ {0} ]", 172 new Object [] {compName.toString()})); 173 result.notApplicable(smh.getLocalString 174 (getClass().getName() + ".notApplicable1", 175 "[ {0} ] does not declare any ejbFind<METHOD>(...) methods.", 176 new Object [] {descriptor.getEjbClassName()})); 177 } 178 179 } catch (ClassNotFoundException e) { 180 Verifier.debug(e); 181 result.addErrorDetails(smh.getLocalString 182 ("tests.componentNameConstructor", 183 "For [ {0} ]", 184 new Object [] {compName.toString()})); 185 result.failed(smh.getLocalString 186 (getClass().getName() + ".failedException", 187 "Error: EJB Class [ {1} ] does not exist or is not loadable.", 188 new Object [] {descriptor.getEjbClassName()})); 189 oneFailed = true; 190 } 191 192 if (oneFailed) { 193 result.setStatus(result.FAILED); 194 } else if (foundAtLeastOne == 0) { 195 result.setStatus(result.NOT_APPLICABLE); 196 } else { 197 result.setStatus(result.PASSED); 198 } 199 200 return result; 201 202 } else { result.addNaDetails(smh.getLocalString 204 ("tests.componentNameConstructor", 205 "For [ {0} ]", 206 new Object [] {compName.toString()})); 207 result.notApplicable(smh.getLocalString 208 (getClass().getName() + ".notApplicable2", 209 "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.", 210 new Object [] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistence})); 211 return result; 212 } 213 214 215 } else { 216 result.addNaDetails(smh.getLocalString 217 ("tests.componentNameConstructor", 218 "For [ {0} ]", 219 new Object [] {compName.toString()})); 220 result.notApplicable(smh.getLocalString 221 (getClass().getName() + ".notApplicable", 222 "[ {0} ] expected {1} bean, but called with {2} bean.", 223 new Object [] {getClass(),"Entity","Session"})); 224 return result; 225 } 226 } 227 } 228 | Popular Tags |