PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/addon-jpa/addon/src/main/java/org/springframework/roo/addon/jpa/addon/test/JpaUnitTestMetadata.java

http://github.com/SpringSource/spring-roo
Java | 586 lines | 279 code | 76 blank | 231 comment | 10 complexity | 56246151622adfd8ab2f977f4b51f8a6 MD5 | raw file
  1. package org.springframework.roo.addon.jpa.addon.test;
  2. import java.lang.reflect.Modifier;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.TreeMap;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.apache.commons.lang3.Validate;
  10. import org.apache.commons.lang3.builder.ToStringBuilder;
  11. import org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo;
  12. import org.springframework.roo.addon.jpa.annotations.test.RooJpaUnitTest;
  13. import org.springframework.roo.classpath.PhysicalTypeIdentifierNamingUtils;
  14. import org.springframework.roo.classpath.PhysicalTypeMetadata;
  15. import org.springframework.roo.classpath.details.BeanInfoUtils;
  16. import org.springframework.roo.classpath.details.FieldMetadata;
  17. import org.springframework.roo.classpath.details.FieldMetadataBuilder;
  18. import org.springframework.roo.classpath.details.MethodMetadata;
  19. import org.springframework.roo.classpath.details.MethodMetadataBuilder;
  20. import org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder;
  21. import org.springframework.roo.classpath.details.comments.CommentStructure;
  22. import org.springframework.roo.classpath.details.comments.CommentStructure.CommentLocation;
  23. import org.springframework.roo.classpath.details.comments.JavadocComment;
  24. import org.springframework.roo.classpath.itd.AbstractItdTypeDetailsProvidingMetadataItem;
  25. import org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder;
  26. import org.springframework.roo.metadata.MetadataIdentificationUtils;
  27. import org.springframework.roo.model.JavaSymbolName;
  28. import org.springframework.roo.model.JavaType;
  29. import org.springframework.roo.model.JdkJavaType;
  30. import org.springframework.roo.project.LogicalPath;
  31. /**
  32. * Metadata for {@link RooJpaUnitTest}.
  33. *
  34. * @author Sergio Clares
  35. * @since 2.0
  36. */
  37. public class JpaUnitTestMetadata extends AbstractItdTypeDetailsProvidingMetadataItem {
  38. // TODO: This class is under construction. It can contain several commented code
  39. private static final String PROVIDES_TYPE_STRING = JpaUnitTestMetadata.class.getName();
  40. private static final String PROVIDES_TYPE = MetadataIdentificationUtils
  41. .create(PROVIDES_TYPE_STRING);
  42. private static final JavaType RULE = new JavaType("org.junit.Rule");
  43. private static final JavaType MOCKITO_RULE = new JavaType("org.mockito.junit.MockitoRule");
  44. private static final JavaType MOCKITO_JUNIT = new JavaType("org.mockito.junit.MockitoJUnit");
  45. private static final JavaType MOCK = new JavaType("org.mockito.Mock");
  46. private static final JavaType BEFORE = new JavaType("org.junit.Before");
  47. private static final JavaType AFTER = new JavaType("org.junit.After");
  48. private static final JavaType TEST = new JavaType("org.junit.Test");
  49. private static final JavaType IGNORE = new JavaType("org.junit.Ignore");
  50. private Map<JavaType, FieldMetadata> entityFactories = new TreeMap<JavaType, FieldMetadata>();
  51. public static String createIdentifier(final JavaType javaType, final LogicalPath path) {
  52. return PhysicalTypeIdentifierNamingUtils.createIdentifier(PROVIDES_TYPE_STRING, javaType, path);
  53. }
  54. public static JavaType getJavaType(final String metadataIdentificationString) {
  55. return PhysicalTypeIdentifierNamingUtils.getJavaType(PROVIDES_TYPE_STRING,
  56. metadataIdentificationString);
  57. }
  58. public static String getMetadataIdentiferType() {
  59. return PROVIDES_TYPE;
  60. }
  61. public static LogicalPath getPath(final String metadataIdentificationString) {
  62. return PhysicalTypeIdentifierNamingUtils.getPath(PROVIDES_TYPE_STRING,
  63. metadataIdentificationString);
  64. }
  65. public static boolean isValid(final String metadataIdentificationString) {
  66. return PhysicalTypeIdentifierNamingUtils.isValid(PROVIDES_TYPE_STRING,
  67. metadataIdentificationString);
  68. }
  69. private final JavaType entity;
  70. private final String entityVar;
  71. private final Map<JavaType, JavaType> entityAndItsFactoryMap;
  72. private final JavaType entityFactory;
  73. /**
  74. * Constructor
  75. *
  76. * @param identifier
  77. * @param aspectName
  78. * @param governorPhysicalTypeMetadata
  79. * @param annotationValues
  80. * @param relationInfos
  81. * @param entityAndItsFactoryMap the map for each entity (key) and its entity factory (value).
  82. */
  83. public JpaUnitTestMetadata(final String identifier, final JavaType aspectName,
  84. final PhysicalTypeMetadata governorPhysicalTypeMetadata,
  85. final JpaUnitTestAnnotationValues annotationValues,
  86. final Collection<RelationInfo> relationInfos,
  87. final Map<JavaType, JavaType> entityAndItsFactoryMap) {
  88. super(identifier, aspectName, governorPhysicalTypeMetadata);
  89. Validate.isTrue(isValid(identifier),
  90. "Metadata identification string '%s' does not appear to be a valid", identifier);
  91. Validate.notNull(annotationValues, "Annotation values required");
  92. this.entity = annotationValues.getTargetClass();
  93. this.entityVar = StringUtils.uncapitalize(this.entity.getSimpleTypeName());
  94. this.entityAndItsFactoryMap = entityAndItsFactoryMap;
  95. this.entityFactory = entityAndItsFactoryMap.get(this.entity);
  96. Validate.notNull(this.entityFactory,
  97. "Unable to locate the entity factory of %s in JpaUnitTestMetadata",
  98. this.entity.getSimpleTypeName());
  99. // Add entity factory fields
  100. for (JavaType entityFactory : entityAndItsFactoryMap.values()) {
  101. FieldMetadataBuilder entityFactoryField =
  102. new FieldMetadataBuilder(this.getId(), Modifier.PRIVATE, new JavaSymbolName(
  103. StringUtils.uncapitalize(entityFactory.getSimpleTypeName())), entityFactory,
  104. String.format("new %s()", getNameOfJavaType(entityFactory)));
  105. entityFactories.put(entityFactory, entityFactoryField.build());
  106. ensureGovernorHasField(entityFactoryField);
  107. }
  108. for (RelationInfo relationInfo : relationInfos) {
  109. // Create addRelation test method
  110. if (relationInfo.addMethod != null) {
  111. ensureGovernorHasMethod(new MethodMetadataBuilder(getAddToRelationMethod(relationInfo)));
  112. }
  113. // Create removeRelation test method
  114. if (relationInfo.removeMethod != null) {
  115. ensureGovernorHasMethod(new MethodMetadataBuilder(getRemoveFromRelationMethod(relationInfo)));
  116. }
  117. }
  118. // // Build @Ignore
  119. // AnnotationMetadataBuilder ignoreAnnotationBuilder = new AnnotationMetadataBuilder(IGNORE);
  120. // ignoreAnnotationBuilder.addStringAttribute("value", "To be implemented by developer");
  121. // ignoreAnnotation = ignoreAnnotationBuilder.build();
  122. //
  123. // // Initialize @Mock
  124. // mockAnnotation.add(new AnnotationMetadataBuilder(MOCK));
  125. //
  126. // // Add @Rule field
  127. // ensureGovernorHasField(getMockitoRuleField());
  128. //
  129. // // Build one mock field for each external dependency field
  130. // for (FieldMetadata field : fieldDependencies) {
  131. // ensureGovernorHasField(getDependencyField(field));
  132. // }
  133. //
  134. // // Add target class field
  135. // ensureGovernorHasField(getTargetClassField(this.targetType));
  136. //
  137. // // Add setup method
  138. // ensureGovernorHasMethod(getSetupMethod());
  139. //
  140. // // Add clean method
  141. // ensureGovernorHasMethod(getCleanMethod());
  142. //
  143. // // Build one test method for each targetClass method
  144. // for (MethodMetadata method : methods) {
  145. //
  146. // // Add method names and avoid name repetition
  147. // int counter = 2;
  148. // String candidateName = method.getMethodName().getSymbolName();
  149. // while (this.methodNames.contains(candidateName)) {
  150. // candidateName = candidateName.concat(String.valueOf(counter));
  151. // counter++;
  152. // }
  153. // this.methodNames.add(candidateName);
  154. //
  155. // // Add method
  156. // ensureGovernorHasMethod(getTestMethod(method, candidateName));
  157. // }
  158. itdTypeDetails = builder.build();
  159. }
  160. /**
  161. * Creates a method to test item additions to a relationship.
  162. *
  163. * @param addMethod the {@link MethodMetadata} to test.
  164. * @param relationFieldName a {@link String} with the field name
  165. * representing the relation on the parent entity.
  166. * @param childEntity the {@link JavaType} of child entity.
  167. * @param field the {@link FieldMetadata} of relation field.
  168. * @param relationInfo the {@link RelationInfo} of the relation field.
  169. * @return the {@link MethodMetadata}
  170. */
  171. private MethodMetadata getAddToRelationMethod(RelationInfo relationInfo) {
  172. // Initialize local variables
  173. MethodMetadata addMethod = relationInfo.addMethod;
  174. String relationFieldName = relationInfo.fieldName;
  175. JavaType childEntity = relationInfo.childType;
  176. FieldMetadata relationField = relationInfo.fieldMetadata;
  177. // Create test method name using target method name, child entity name and relation field
  178. JavaSymbolName methodName =
  179. new JavaSymbolName(String.format("%sShouldAddThe%sToThe%sRelationship", addMethod
  180. .getMethodName().getSymbolName(), childEntity.getSimpleTypeName(), relationFieldName));
  181. MethodMetadata method = getGovernorMethod(methodName);
  182. if (method != null) {
  183. return method;
  184. }
  185. // Create body
  186. InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
  187. // Setup
  188. bodyBuilder.appendFormalLine("// Setup");
  189. // Entity entity = entityFactory.create(0);
  190. FieldMetadata entityFactory = entityFactories.get(this.entityFactory);
  191. bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(this.entity),
  192. this.entityVar, getAccessorMethod(entityFactory).getMethodName());
  193. // ChildEntity childEntity1 = childEntityFactory.create(0);
  194. FieldMetadata childEntityFactory =
  195. entityFactories.get(this.entityAndItsFactoryMap.get(childEntity));
  196. Validate.notNull(childEntityFactory,
  197. "Unable to locate the entity factory of %s in JpaUnitTestMetadata",
  198. childEntity.getSimpleTypeName());
  199. String childEntityVar1 =
  200. String.format("%s1", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
  201. bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(childEntity),
  202. childEntityVar1, getAccessorMethod(childEntityFactory).getMethodName());
  203. // ChildEntity childEntity2 = childEntityFactory.create(1);
  204. String childEntityVar2 =
  205. String.format("%s2", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
  206. bodyBuilder.appendFormalLine("%s %s = %s().create(1);", getNameOfJavaType(childEntity),
  207. childEntityVar2, getAccessorMethod(childEntityFactory).getMethodName());
  208. bodyBuilder.newLine();
  209. // Exercise
  210. bodyBuilder.appendFormalLine("// Exercise");
  211. // entity.ADD_METHOD(Arrays.asList(childEntity1, childEntity2));
  212. bodyBuilder.appendFormalLine("%s.%s(%s.asList(%s, %s));", entityVar, addMethod.getMethodName(),
  213. getNameOfJavaType(JavaType.ARRAYS), childEntityVar1, childEntityVar2);
  214. bodyBuilder.newLine();
  215. // Verify
  216. bodyBuilder.appendFormalLine("// Verify");
  217. // assertThat(entity.RELATION_FIELD_ACCESSOR()).as("Check 'ADD_METHOD_NAME' adds the FIELDNAME to the relationship")
  218. JavaSymbolName relationFieldAccessorName = BeanInfoUtils.getAccessorMethodName(relationField);
  219. bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' adds the %s to the relationship\")",
  220. getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true),
  221. this.entityVar, relationFieldAccessorName, addMethod.getMethodName(), relationFieldName);
  222. // .contains(childEntity1).contains(childEntity2);
  223. bodyBuilder.indent();
  224. bodyBuilder.appendFormalLine(".contains(%s).contains(%s);", childEntityVar1, childEntityVar2);
  225. bodyBuilder.indentRemove();
  226. // assertThat(entity).as("Check 'ADD_METHOD_NAME' updates the CHILD_ENTITY relationship side")
  227. bodyBuilder.appendFormalLine("%s(%s).as(\"Check '%s' updates the %s relationship side\")",
  228. getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true),
  229. this.entityVar, addMethod.getMethodName(), getNameOfJavaType(childEntity));
  230. // .isEqualTo(childEntity1.PARENT_ENTITY_ACCESSOR()).isEqualTo(childEntity2.PARENT_ENTITY_ACCESSOR());
  231. JavaSymbolName parentEntityAccessor =
  232. BeanInfoUtils.getAccessorMethodName(new JavaSymbolName(relationInfo.mappedBy), this.entity);
  233. bodyBuilder.indent();
  234. bodyBuilder.appendFormalLine(".isEqualTo(%1$s.%3$s()).isEqualTo(%2$s.%3$s());",
  235. childEntityVar1, childEntityVar2, parentEntityAccessor);
  236. bodyBuilder.reset();
  237. MethodMetadataBuilder methodBuilder =
  238. new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName,
  239. JavaType.VOID_PRIMITIVE, bodyBuilder);
  240. // Add @Test
  241. methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
  242. // Add throws types
  243. methodBuilder.addThrowsType(JdkJavaType.EXCEPTION);
  244. return methodBuilder.build();
  245. }
  246. /**
  247. * Creates a method to test item removes from a relationship.
  248. *
  249. * @param removeMethod the {@link MethodMetadata} to test.
  250. * @param relationFieldName a {@link String} with the field name
  251. * representing the relation on the parent entity.
  252. * @param childEntity the {@link JavaType} of child entity.
  253. * @param field the {@link FieldMetadata} of relation field.
  254. * @return the {@link MethodMetadata}
  255. */
  256. private MethodMetadata getRemoveFromRelationMethod(RelationInfo relationInfo) {
  257. // Initialize local variables
  258. String relationFieldName = relationInfo.fieldName;
  259. JavaType childEntity = relationInfo.childType;
  260. FieldMetadata relationField = relationInfo.fieldMetadata;
  261. // Create test method name using target method name, child entity name and relation field
  262. JavaSymbolName methodName =
  263. new JavaSymbolName(String.format("%sShouldRemoveThe%sFromThe%sRelationship",
  264. relationInfo.addMethod.getMethodName().getSymbolName(),
  265. childEntity.getSimpleTypeName(), relationFieldName));
  266. MethodMetadata method = getGovernorMethod(methodName);
  267. if (method != null) {
  268. return method;
  269. }
  270. // Create body
  271. InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
  272. // Setup
  273. bodyBuilder.appendFormalLine("// Setup");
  274. // Entity entity = entityFactory.create(0);
  275. FieldMetadata entityFactory = entityFactories.get(this.entityFactory);
  276. bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(this.entity),
  277. this.entityVar, getAccessorMethod(entityFactory).getMethodName());
  278. // ChildEntity childEntity1 = childEntityFactory.create(0);
  279. FieldMetadata childEntityFactory =
  280. entityFactories.get(this.entityAndItsFactoryMap.get(childEntity));
  281. Validate.notNull(childEntityFactory,
  282. "Unable to locate the entity factory of %s in JpaUnitTestMetadata",
  283. childEntity.getSimpleTypeName());
  284. String childEntityVar1 =
  285. String.format("%s1", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
  286. bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(childEntity),
  287. childEntityVar1, getAccessorMethod(childEntityFactory).getMethodName());
  288. // ChildEntity childEntity2 = childEntityFactory.create(1);
  289. String childEntityVar2 =
  290. String.format("%s2", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
  291. bodyBuilder.appendFormalLine("%s %s = %s().create(1);", getNameOfJavaType(childEntity),
  292. childEntityVar2, getAccessorMethod(childEntityFactory).getMethodName());
  293. // entity.ADD_METHOD(Arrays.asList(childEntity1, childEntity2));
  294. bodyBuilder.appendFormalLine("%s.%s(%s.asList(%s, %s));", entityVar,
  295. relationInfo.addMethod.getMethodName(), getNameOfJavaType(JavaType.ARRAYS),
  296. childEntityVar1, childEntityVar2);
  297. bodyBuilder.newLine();
  298. // Exercise
  299. bodyBuilder.appendFormalLine("// Exercise");
  300. // entity.REMOVE_METHOD(Collections.singleton(childEntity1));
  301. bodyBuilder.appendFormalLine("%s.%s(%s.singleton(%s));", this.entityVar,
  302. relationInfo.removeMethod.getMethodName(), getNameOfJavaType(JavaType.COLLECTIONS),
  303. childEntityVar1);
  304. bodyBuilder.newLine();
  305. // Verify
  306. bodyBuilder.appendFormalLine("// Verify");
  307. // assertThat(childEntity1.PARENT_ENTITY_ACCESSOR()).as("Check 'REMOVE_METHOD' updates the CHILD_ENTITY relationship side")
  308. JavaSymbolName parentEntityAccessor =
  309. BeanInfoUtils.getAccessorMethodName(new JavaSymbolName(relationInfo.mappedBy), this.entity);
  310. bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' updates the %s relationship side\")",
  311. getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true),
  312. childEntityVar1, parentEntityAccessor, relationInfo.removeMethod.getMethodName(),
  313. getNameOfJavaType(childEntity));
  314. // .isNull();
  315. bodyBuilder.indent();
  316. bodyBuilder.appendFormalLine(".isNull();");
  317. bodyBuilder.indentRemove();
  318. // assertThat(entity.RELATION_FIELD_ACCESSOR()).as("Check 'REMOVE_METHOD' removes a CHILD_ENTITY from the relationship")
  319. JavaSymbolName relationFieldAccessorName = BeanInfoUtils.getAccessorMethodName(relationField);
  320. bodyBuilder.appendFormalLine(
  321. "%s(%s.%s()).as(\"Check '%s' removes a %s from the relationship\")",
  322. getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true),
  323. this.entityVar, relationFieldAccessorName, relationInfo.removeMethod.getMethodName(),
  324. getNameOfJavaType(childEntity));
  325. // .doesNotContain(childEntity1).contains(childEntity2);
  326. bodyBuilder.indent();
  327. bodyBuilder.appendFormalLine(".doesNotContain(%s).contains(%s);", childEntityVar1,
  328. childEntityVar2);
  329. bodyBuilder.reset();
  330. MethodMetadataBuilder methodBuilder =
  331. new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName,
  332. JavaType.VOID_PRIMITIVE, bodyBuilder);
  333. // Add @Test
  334. methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
  335. // Add throws types
  336. methodBuilder.addThrowsType(JdkJavaType.EXCEPTION);
  337. return methodBuilder.build();
  338. }
  339. /**
  340. * Obtains a method annotated with @Test and @Ignore for testing one targeted
  341. * type's method. Developer should implement logic.
  342. *
  343. * @return {@link MethodMetadataBuilder}
  344. */
  345. private MethodMetadataBuilder getTestMethod(MethodMetadata method, String candidateName) {
  346. final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
  347. // bodyBuilder.newLine();
  348. //
  349. // // Setup phase
  350. // bodyBuilder.appendFormalLine("// Setup");
  351. // bodyBuilder.appendFormalLine("// Implement additional setup if necessary");
  352. // bodyBuilder.newLine();
  353. //
  354. // // Exercise phase
  355. // bodyBuilder.appendFormalLine("// Exercise");
  356. // List<JavaSymbolName> parameterNames = method.getParameterNames();
  357. // StringBuffer parameters = new StringBuffer();
  358. // for (int i = 0; i < parameterNames.size(); i++) {
  359. // parameters.append(parameterNames.get(i).getSymbolName());
  360. // if (i != parameterNames.size() - 1) {
  361. // parameters.append(", ");
  362. // }
  363. // }
  364. // if (method.getReturnType().equals(JavaType.VOID_PRIMITIVE)) {
  365. // bodyBuilder.appendFormalLine(String.format("// this.%s.%s(%s);",
  366. // StringUtils.uncapitalize(targetType.getSimpleTypeName()), method.getMethodName(),
  367. // parameters));
  368. // } else {
  369. // bodyBuilder.appendFormalLine(String.format("// %s result = this.%s.%s(%s);",
  370. // method.getReturnType().getNameIncludingTypeParameters(false, importResolver),
  371. // StringUtils.uncapitalize(targetType.getSimpleTypeName()), method.getMethodName(),
  372. // parameters));
  373. // }
  374. // bodyBuilder.newLine();
  375. //
  376. // // Verify phase
  377. // bodyBuilder.appendFormalLine("// Verify");
  378. // bodyBuilder.appendFormalLine("// Implement assertions");
  379. //
  380. // // Check if method alread exists
  381. // JavaSymbolName methodName = new JavaSymbolName(String.format("%sTest", candidateName));
  382. //
  383. // // Use the MethodMetadataBuilder for easy creation of MethodMetadata
  384. // MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC,
  385. // methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
  386. //
  387. // // Add @Test and @Ignore
  388. // methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST).build());
  389. // // methodBuilder.addAnnotation(ignoreAnnotation);
  390. //
  391. // return methodBuilder;
  392. return null;
  393. }
  394. /**
  395. * Obtains a method annotated with @After for doing the test class teardown phase
  396. * after finishing each test.
  397. *
  398. * @return {@link MethodMetadataBuilder}
  399. */
  400. private MethodMetadataBuilder getCleanMethod() {
  401. final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
  402. bodyBuilder.newLine();
  403. bodyBuilder.appendFormalLine("// Clean needed after executing each test method");
  404. bodyBuilder.appendFormalLine("// To be implemented by developer");
  405. bodyBuilder.newLine();
  406. // Use the MethodMetadataBuilder for easy creation of MethodMetadata
  407. MethodMetadataBuilder methodBuilder =
  408. new MethodMetadataBuilder(getId(), Modifier.PUBLIC, new JavaSymbolName("clean"),
  409. JavaType.VOID_PRIMITIVE, bodyBuilder);
  410. // Add @After
  411. methodBuilder.addAnnotation(new AnnotationMetadataBuilder(AFTER).build());
  412. // Add comment
  413. CommentStructure comment = new CommentStructure();
  414. JavadocComment javaDocComment =
  415. new JavadocComment(
  416. "This method will be automatically executed after each test method for freeing resources allocated with @Before annotated method.");
  417. comment.addComment(javaDocComment, CommentLocation.BEGINNING);
  418. methodBuilder.setCommentStructure(comment);
  419. return methodBuilder;
  420. }
  421. /**
  422. * Obtains a method annotated with @Before for doing the test class setup before
  423. * launching each test.
  424. *
  425. * @return {@link MethodMetadataBuilder}
  426. */
  427. private MethodMetadataBuilder getSetupMethod() {
  428. // // this.targetType = new TargetType();
  429. // final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
  430. // if (!targetTypeDetails.isInterface() && !targetTypeDetails.isAbstract()
  431. // && memberDetails.getConstructors().size() == 0) {
  432. // bodyBuilder.appendFormalLine(String.format("this.%s = new %s();",
  433. // StringUtils.uncapitalize(targetType.getSimpleTypeName()),
  434. // targetType.getNameIncludingTypeParameters(false, importResolver)));
  435. // }
  436. //
  437. // bodyBuilder.newLine();
  438. // bodyBuilder.appendFormalLine("// Setup needed before executing each test method");
  439. // bodyBuilder.appendFormalLine("// To be implemented by developer");
  440. // bodyBuilder.newLine();
  441. //
  442. // // Use the MethodMetadataBuilder for easy creation of MethodMetadata
  443. // MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC,
  444. // new JavaSymbolName("setup"), JavaType.VOID_PRIMITIVE, bodyBuilder);
  445. //
  446. // // Add @Before
  447. // methodBuilder.addAnnotation(new AnnotationMetadataBuilder(BEFORE).build());
  448. //
  449. // // Add comment
  450. // CommentStructure comment = new CommentStructure();
  451. // JavadocComment javaDocComment = new JavadocComment(
  452. // "This method will be automatically executed before each test method for configuring needed resources.");
  453. // comment.addComment(javaDocComment, CommentLocation.BEGINNING);
  454. // methodBuilder.setCommentStructure(comment);
  455. //
  456. // return methodBuilder;
  457. return null;
  458. }
  459. /**
  460. * Creates field with @Mock for using it in tests.
  461. *
  462. * @return {@link FieldMetadataBuilder} for building field into ITD
  463. */
  464. private FieldMetadataBuilder getDependencyField(FieldMetadata field) {
  465. // FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE,
  466. // mockAnnotation, field.getFieldName(), field.getFieldType());
  467. // return fieldBuilder;
  468. return null;
  469. }
  470. /**
  471. * Creates target class field for initializing in tests.
  472. *
  473. * @return {@link FieldMetadataBuilder} for building field into ITD.
  474. */
  475. private FieldMetadataBuilder getTargetClassField(JavaType targetType) {
  476. FieldMetadataBuilder fieldBuilder =
  477. new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new JavaSymbolName(
  478. StringUtils.uncapitalize(targetType.getSimpleTypeName())), targetType, null);
  479. return fieldBuilder;
  480. }
  481. /**
  482. * Creates MockitoRule field for validating and initialize mocks.
  483. *
  484. * @return {@link FieldMetadataBuilder} for building field into ITD.
  485. */
  486. private FieldMetadataBuilder getMockitoRuleField() {
  487. // Create field @Rule
  488. List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
  489. AnnotationMetadataBuilder ruleAnnotation = new AnnotationMetadataBuilder(RULE);
  490. annotations.add(ruleAnnotation);
  491. // Create field
  492. FieldMetadataBuilder ruleField =
  493. new FieldMetadataBuilder(getId(), Modifier.PUBLIC, annotations, new JavaSymbolName(
  494. "mockito"), MOCKITO_RULE);
  495. // ruleField.setFieldInitializer(String.format("%s.rule()",
  496. // new JavaType(MOCKITO_JUNIT.getNameIncludingTypeParameters(false, importResolver))));
  497. return ruleField;
  498. }
  499. @Override
  500. public String toString() {
  501. final ToStringBuilder builder = new ToStringBuilder(this);
  502. builder.append("identifier", getId());
  503. builder.append("valid", valid);
  504. builder.append("aspectName", aspectName);
  505. builder.append("destinationType", destination);
  506. builder.append("governor", governorPhysicalTypeMetadata.getId());
  507. builder.append("itdTypeDetails", itdTypeDetails);
  508. return builder.toString();
  509. }
  510. }