1
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 public TestMethodDefinition(ISanityCheckTestSuite testSuiteInstance, Method theMethod)
50 throws WrongTestMethodSignature
51 {
52 SanityCheckTest declarationAnnotation = theMethod.getAnnotation(SanityCheckTest.class);
53
54 this.theMethod = theMethod;
55 this.onlyAdministrators = declarationAnnotation.onlyAdministrators();
56 this.testSuiteInstance = testSuiteInstance;
57
58 if (declarationAnnotation.name().equals(AnnotationTags.NONE))
59 this.name = StringUtils.camelCaseToString(theMethod.getName());
60 else
61 this.name = StringUtils.camelCaseToString(declarationAnnotation.name());
62
63
64 if (theMethod.getParameterTypes().length > 0)
65 {
66 if (theMethod.getParameterTypes().length > 1)
67
68 throw new WrongTestMethodSignature(theMethod);
69
70 else if (theMethod.getParameterTypes()[0] != IDIFContext.class)
71
72 throw new WrongTestMethodSignature(theMethod);
73
74 else
75
76 this.passContext = true;
77 }
78
79
80 if (theMethod.getReturnType() != TestResult.class)
81 throw new WrongTestMethodSignature(theMethod);
82 }
83
84
85
86
87
88
89 public String getName()
90 {
91 return name;
92 }
93
94
95
96
97
98
99 public boolean isOnlyAdministrators()
100 {
101 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 if (onlyAdministrators
118 && (!context.getSession().isLogged() || !context.getSession().getUser().getGroupIDs()
119 .contains(DIFConstants.ADMINISTRATORS_GROUP.toLowerCase())))
120 {
121 result = new TestResult(ExecutionResult.SKIPPED);
122 }
123 else
124 {
125 if (passContext)
126 result = (TestResult) theMethod.invoke(testSuiteInstance, context);
127 else
128 result = (TestResult) theMethod.invoke(testSuiteInstance);
129 }
130 }
131 catch (Exception e)
132 {
133 result = new TestResult(ExecutionResult.FAILED);
134 result.setErrorMessage(e.getMessage());
135 }
136
137 return result;
138 }
139
140
141
142
143
144
145
146 public void setName(String name)
147 {
148 this.name = name;
149 }
150
151
152
153
154
155
156
157 public void setOnlyAdministrators(boolean onlyAdministrators)
158 {
159 this.onlyAdministrators = onlyAdministrators;
160 }
161 }