-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiferayServiceProcessorTest.java
286 lines (231 loc) · 8.92 KB
/
LiferayServiceProcessorTest.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package pl.bedkowski.code.liferay.service.processor.controller;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic.Kind;
import javax.tools.JavaFileObject;
import org.junit.Before;
import org.junit.Test;
import pl.bedkowski.code.liferay.service.annotation.LiferayService;
import pl.bedkowski.code.liferay.service.processor.event.AfterDoModelEvent;
import pl.bedkowski.code.liferay.service.processor.event.AfterDoViewEvent;
import pl.bedkowski.code.liferay.service.processor.exception.ProcessorLifecycleException;
import pl.bedkowski.code.liferay.service.processor.listener.ProcessorLifecycleEventListener;
import pl.bedkowski.code.liferay.service.processor.model.ProcessorModel;
import com.google.common.collect.Sets;
public class LiferayServiceProcessorTest {
private StringWriter printWriter;
private ProcessingEnvironment processingEnvironment;
private Elements elementUtils;
@Before
public void init() throws Exception {
printWriter = new StringWriter();
processingEnvironment = processingEnv();
}
@Test
public void test() throws Exception {
List<Element> methods = new ArrayList<Element>();
methods.add(method(String.class, "test1"));
methods.add(method(String.class, "test1",
new Class[]{Integer.class, String.class},
new Class[]{UnsupportedEncodingException.class, IOException.class}
));
methods.add(method(String.class, "test1",
new Class[]{String.class, String.class}
));
methods.add(method(String.class, "test1",
new Class[]{String.class, String.class, String.class}
));
doReturn(methods).when(elementUtils).getAllMembers(any(TypeElement.class));
LiferayServiceProcessor proc = new LiferayServiceProcessor();
proc.registerListener(new ProcessorLifecycleEventListener<AfterDoModelEvent>() {
@Override
public void on(AfterDoModelEvent event) throws ProcessorLifecycleException {
validateModel(event.getModel());
try {
event.getModel().put("className", "");
fail("Model base keys should be read-only.");
} catch(IllegalArgumentException e){
}
}
});
proc.registerListener(new ProcessorLifecycleEventListener<AfterDoViewEvent>() {
@Override
public void on(AfterDoViewEvent event) throws ProcessorLifecycleException {
assertEquals("MyTestIface", event.getModel().getModelMap().get("className").toString());
try {
event.getModel().getModelMap().put("this should never happen", "");
fail("Methods map should be read-only.");
} catch(UnsupportedOperationException e){
}
}
});
proc.init(processingEnvironment);
proc.process(null, roundEnv());
Messager m = processingEnvironment.getMessager();
verify(m).printMessage(eq(Kind.NOTE), eq("LiferayService for: pl.bedkowski.test.MyTestIface"), any(TypeElement.class));
verify(m).printMessage(eq(Kind.ERROR), eq("LiferayService can only be applied to interface, found: METHOD"), any(TypeElement.class));
// System.out.println(printWriter.getBuffer());
}
@Test
public void invalidViewClassName() throws Exception {
processingEnvironment.getOptions().put(LiferayServiceProcessor.OPTION_VIEW_CLASS, "invalid class name");
try {
Processor proc = new LiferayServiceProcessor();
proc.init(processingEnvironment);
fail("Should throw class not found.");
} catch(RuntimeException e) {
assertEquals("Should throw class not found.", ClassNotFoundException.class, e.getCause().getClass());
}
}
private static void validateModel(ProcessorModel model) {
assertEquals("MyTestIface", model.getClassName().toString());
assertEquals("portalClassLoader", model.getClassLoader());
assertEquals("myInitMethod", model.getInitMethod());
Map<String, Element> methods = model.getMethods();
try {
methods.put("this should never happen", mock(Element.class));
fail("Methods map should be read-only.");
} catch(UnsupportedOperationException e){
}
assertEquals(4, methods.size());
assertTrue(methods.containsKey("test1"));
assertTrue(methods.containsKey("test11"));
assertTrue(methods.containsKey("test12"));
assertTrue(methods.containsKey("test13"));
}
private ProcessingEnvironment processingEnv() throws IOException {
ProcessingEnvironment pe = mock(ProcessingEnvironment.class);
elementUtils = mock(Elements.class);
when(pe.getElementUtils()).thenReturn(elementUtils);
Filer filer = mock(Filer.class);
when(pe.getFiler()).thenReturn(filer);
JavaFileObject jfo = mock(JavaFileObject.class);
when(filer.createSourceFile(anyString())).thenReturn(jfo);
when(jfo.openWriter()).thenReturn(printWriter);
Messager m = mock(Messager.class);
when(pe.getMessager()).thenReturn(m);
Map<String, String> options = new HashMap<String, String>();
when(pe.getOptions()).thenReturn(options);
return pe;
}
private RoundEnvironment roundEnv() {
RoundEnvironment re = mock(RoundEnvironment.class);
Set<Element> elems = Sets.newHashSet();
doReturn(elems).when(re).getElementsAnnotatedWith(LiferayService.class);
elems.add(iface("MyTestIface", "pl.bedkowski.test", "myInitMethod", "portalClassLoader"));
{
TypeElement te = mock(TypeElement.class);
when(te.getKind()).thenReturn(ElementKind.METHOD);
elems.add(te);
}
return re;
}
private static Element iface(String sName, String sPkg, String initMethod, String loader) {
TypeElement te = mock(TypeElement.class);
when(te.getKind()).thenReturn(ElementKind.INTERFACE);
{
LiferayService ls = mock(LiferayService.class);
when(ls.value()).thenReturn(loader);
when(ls.initMethod()).thenReturn(initMethod);
when(te.getAnnotation(LiferayService.class)).thenReturn(ls);
}
{
Name name = name(sName);
when(te.getSimpleName()).thenReturn(name);
}
{
PackageElement pkg = mock(PackageElement.class);
Name name = name(sPkg);
when(pkg.getQualifiedName()).thenReturn(name);
when(te.getEnclosingElement()).thenReturn(pkg);
}
{
Name name = name(sPkg+"."+sName);
when(te.getQualifiedName()).thenReturn(name);
}
return te;
}
private ExecutableElement method(Class<?> ret, String sName) {
return method(ret, sName, null);
}
@SuppressWarnings("rawtypes")
private ExecutableElement method(Class<?> ret, String sName, Class[] cParams) {
return method(ret, sName, cParams, null);
}
@SuppressWarnings("rawtypes")
private ExecutableElement method(Class<?> ret, String sName, Class[] cParams, Class[] cExceptions) {
ExecutableElement execElem = mock(ExecutableElement.class);
TypeMirror type = type(ret);
when(execElem.getReturnType()).thenReturn(type);
when(execElem.getKind()).thenReturn(ElementKind.METHOD);
when(execElem.getModifiers()).thenReturn(EnumSet.of(Modifier.ABSTRACT));
{
Name name = name(sName);
when(execElem.getSimpleName()).thenReturn(name);
}
if (cParams != null) {
List<VariableElement> params = new ArrayList<VariableElement>();
for(Class<?> clz : cParams) {
params.add(param(clz));
}
doReturn(params).when(execElem).getParameters();
}
if (cExceptions != null) {
List<TypeMirror> exceptions = new ArrayList<TypeMirror>();
for(Class<?> clz : cExceptions) {
exceptions.add(type(clz));
}
doReturn(exceptions).when(execElem).getThrownTypes();
}
return execElem;
}
private static TypeMirror type(Class<?> clz) {
TypeMirror type = mock(TypeMirror.class);
when(type.toString()).thenReturn(clz.getCanonicalName());
return type;
}
private static VariableElement param(Class<?> clz) {
VariableElement elem = mock(VariableElement.class);
TypeMirror type = type(clz);
when(elem.asType()).thenReturn(type);
when(elem.toString()).thenReturn(clz.getCanonicalName());
return elem;
}
private static Name name(String n) {
Name name = mock(Name.class);
when(name.toString()).thenReturn(n);
return name;
}
}