1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
package pt.digitalis.dif.sanitycheck.manager; |
7 | |
|
8 | |
import java.lang.reflect.Method; |
9 | |
|
10 | |
import pt.digitalis.dif.controller.interfaces.IDIFContext; |
11 | |
import pt.digitalis.dif.dem.annotations.AnnotationTags; |
12 | |
import pt.digitalis.dif.sanitycheck.ExecutionResult; |
13 | |
import pt.digitalis.dif.sanitycheck.ISanityCheckTestSuite; |
14 | |
import pt.digitalis.dif.sanitycheck.TestResult; |
15 | |
import pt.digitalis.dif.sanitycheck.annotations.SanityCheckTest; |
16 | |
import pt.digitalis.dif.sanitycheck.exceptions.WrongTestMethodSignature; |
17 | |
import pt.digitalis.dif.utils.DIFConstants; |
18 | |
import pt.digitalis.utils.common.StringUtils; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class TestMethodDefinition { |
27 | |
|
28 | |
|
29 | |
private String name; |
30 | |
|
31 | |
|
32 | |
private boolean onlyAdministrators; |
33 | |
|
34 | |
|
35 | |
private boolean passContext; |
36 | |
|
37 | |
|
38 | |
private ISanityCheckTestSuite testSuiteInstance; |
39 | |
|
40 | |
|
41 | |
private Method theMethod; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public TestMethodDefinition(ISanityCheckTestSuite testSuiteInstance, Method theMethod) |
50 | |
throws WrongTestMethodSignature |
51 | |
{ |
52 | 0 | SanityCheckTest declarationAnnotation = theMethod.getAnnotation(SanityCheckTest.class); |
53 | |
|
54 | 0 | this.theMethod = theMethod; |
55 | 0 | this.onlyAdministrators = declarationAnnotation.onlyAdministrators(); |
56 | 0 | this.testSuiteInstance = testSuiteInstance; |
57 | |
|
58 | 0 | if (declarationAnnotation.name().equals(AnnotationTags.NONE)) |
59 | 0 | this.name = StringUtils.camelCaseToString(theMethod.getName()); |
60 | |
else |
61 | 0 | this.name = StringUtils.camelCaseToString(declarationAnnotation.name()); |
62 | |
|
63 | |
|
64 | 0 | if (theMethod.getParameterTypes().length > 0) |
65 | |
{ |
66 | 0 | if (theMethod.getParameterTypes().length > 1) |
67 | |
|
68 | 0 | throw new WrongTestMethodSignature(theMethod); |
69 | |
|
70 | 0 | else if (theMethod.getParameterTypes()[0] != IDIFContext.class) |
71 | |
|
72 | 0 | throw new WrongTestMethodSignature(theMethod); |
73 | |
|
74 | |
else |
75 | |
|
76 | 0 | this.passContext = true; |
77 | |
} |
78 | |
|
79 | |
|
80 | 0 | if (theMethod.getReturnType() != TestResult.class) |
81 | 0 | throw new WrongTestMethodSignature(theMethod); |
82 | 0 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public String getName() |
90 | |
{ |
91 | 0 | return name; |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public boolean isOnlyAdministrators() |
100 | |
{ |
101 | 0 | return onlyAdministrators; |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public TestResult run(IDIFContext context) |
112 | |
{ |
113 | |
TestResult result; |
114 | |
|
115 | |
try |
116 | |
{ |
117 | 0 | if (onlyAdministrators |
118 | 0 | && (!context.getSession().isLogged() || !context.getSession().getUser().getGroupIDs() |
119 | 0 | .contains(DIFConstants.ADMINISTRATORS_GROUP.toLowerCase()))) |
120 | |
{ |
121 | 0 | result = new TestResult(ExecutionResult.SKIPPED); |
122 | |
} |
123 | |
else |
124 | |
{ |
125 | 0 | if (passContext) |
126 | 0 | result = (TestResult) theMethod.invoke(testSuiteInstance, context); |
127 | |
else |
128 | 0 | result = (TestResult) theMethod.invoke(testSuiteInstance); |
129 | |
} |
130 | |
} |
131 | 0 | catch (Exception e) |
132 | |
{ |
133 | 0 | result = new TestResult(ExecutionResult.FAILED); |
134 | 0 | result.setErrorMessage(e.getMessage()); |
135 | |
} |
136 | |
|
137 | 0 | return result; |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
public void setName(String name) |
147 | |
{ |
148 | 0 | this.name = name; |
149 | 0 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public void setOnlyAdministrators(boolean onlyAdministrators) |
158 | |
{ |
159 | 0 | this.onlyAdministrators = onlyAdministrators; |
160 | 0 | } |
161 | |
} |