My favorites
▼
|
Sign in
testng-eclipse
Eclipse plug-in for TestNG
Project Home
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
32
attachment: annotations_patch.txt
(5.8 KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
### Eclipse Workspace Patch 1.0
#P testng-eclipse
Index: src/main/org/testng/eclipse/ui/conversion/AnnotationRewriter.java
===================================================================
--- src/main/org/testng/eclipse/ui/conversion/AnnotationRewriter.java (revision 105)
+++ src/main/org/testng/eclipse/ui/conversion/AnnotationRewriter.java (working copy)
@@ -4,12 +4,12 @@
import java.util.List;
import org.eclipse.jdt.core.dom.AST;
-import org.eclipse.jdt.core.dom.BooleanLiteral;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
@@ -18,33 +18,45 @@
* using JDK5 annotations
*/
public class AnnotationRewriter
- extends BaseRewriter
- implements IRewriteProvider
+extends BaseRewriter
+implements IRewriteProvider
{
public ASTRewrite createRewriter(CompilationUnit astRoot,
AST ast,
JUnitVisitor visitor
- )
+ )
{
final ASTRewrite result = ASTRewrite.create(astRoot.getAST());
-
+
performCommonRewrites(astRoot, ast, visitor, result);
-
+
//
// Add TestNG imports
//
{
ListRewrite lr = result.getListRewrite(astRoot, CompilationUnit.IMPORTS_PROPERTY);
- String[] annotations = {
- "Test", "Configuration", "Factory",
- };
- for (int i = 0; i < annotations.length; i++) {
+ if (visitor.getSetUp() != null) {
ImportDeclaration id = ast.newImportDeclaration();
- id.setName(ast.newName("org.testng.annotations." + annotations[i]));
+ id.setName(ast.newName("org.testng.annotations.BeforeMethod"));
lr.insertFirst(id, null);
}
+ if (visitor.getTearDown() != null) {
+ ImportDeclaration id = ast.newImportDeclaration();
+ id.setName(ast.newName("org.testng.annotations.AfterMethod"));
+ lr.insertFirst(id, null);
+ }
+ if (visitor.getTestMethods().size() > 0) {
+ ImportDeclaration id = ast.newImportDeclaration();
+ id.setName(ast.newName("org.testng.annotations.Test"));
+ lr.insertFirst(id, null);
+ }
+ if (visitor.getSuite() != null) {
+ ImportDeclaration id = ast.newImportDeclaration();
+ id.setName(ast.newName("org.testng.annotations.Factory"));
+ lr.insertFirst(id, null);
+ }
}
-
+
//
// Add @Test annotations
//
@@ -52,20 +64,25 @@
for (int i = 0; i < testMethods.size(); i++) {
NormalAnnotation a = ast.newNormalAnnotation();
a.setTypeName(ast.newName("Test"));
- addAnnotation(result, (MethodDeclaration) testMethods.get(i), a);
+ addAnnotation(ast, visitor, result, (MethodDeclaration) testMethods.get(i), a);
}
-
+
//
- // Addd @Configuration annotations
+ // Add @BeforeMethod and @AfterMethod annotations
//
MethodDeclaration setUp = visitor.getSetUp();
if (null != setUp) {
- addConfiguration(ast, result, setUp, "beforeTestMethod");
+ NormalAnnotation a = ast.newNormalAnnotation();
+ a.setTypeName(ast.newName("BeforeMethod"));
+ addAnnotation(ast, visitor, result, setUp, a);
+
}
-
+
MethodDeclaration tearDown = visitor.getTearDown();
if (null != tearDown) {
- addConfiguration(ast, result, tearDown, "afterTestMethod");
+ NormalAnnotation a = ast.newNormalAnnotation();
+ a.setTypeName(ast.newName("AfterMethod"));
+ addAnnotation(ast, visitor, result, tearDown, a);
}
//
@@ -75,29 +92,34 @@
if (null != suite) {
NormalAnnotation a = ast.newNormalAnnotation();
a.setTypeName(ast.newName("Factory"));
- addAnnotation(result, suite, a);
+ addAnnotation(ast, visitor, result, suite, a);
}
-
+
return result;
}
- private void addConfiguration(AST ast, final ASTRewrite rewriter,
- MethodDeclaration md, String annotation)
- {
- NormalAnnotation a = ast.newNormalAnnotation();
- a.setTypeName(ast.newName("Configuration"));
- List l = a.values();
- MemberValuePair mvp = ast.newMemberValuePair();
- mvp.setName(ast.newSimpleName(annotation));
- BooleanLiteral sl = ast.newBooleanLiteral(true);
- mvp.setValue(sl);
- l.add(mvp);
- addAnnotation(rewriter, md, a);
+ /**
+ * This method attempts to set an initial "groups" property in the annotation, by
+ * combining functional with the name of the class.
+ * @param ast
+ * @param md
+ * @param a
+ */
+ private void setGroupsProperty(AST ast, String testCaseClassName, NormalAnnotation a) {
+ List annotationValues = a.values();
+ MemberValuePair suggestedGroupAttribute = ast.newMemberValuePair();
+ suggestedGroupAttribute.setName(ast.newSimpleName("groups"));
+ StringLiteral suggestedGroup = ast.newStringLiteral();
+ suggestedGroup.setLiteralValue("functional." + testCaseClassName);
+ suggestedGroupAttribute.setValue(suggestedGroup);
+ annotationValues.add(suggestedGroupAttribute);
}
-
- private void addAnnotation(ASTRewrite rewriter, MethodDeclaration md,
+
+ private void addAnnotation(AST ast, JUnitVisitor visitor, ASTRewrite rewriter, MethodDeclaration md,
NormalAnnotation a)
{
+ setGroupsProperty(ast, visitor.getTypeDeclaration().getName().toString(), a);
+
List oldModifiers = md.modifiers();
List newModifiers = new ArrayList();
for (int k = 0; k < oldModifiers.size(); k++) {
Powered by
Google Project Hosting