1 package org.melati.template.test;
2
3 import junit.framework.TestCase;
4 import org.melati.Melati;
5 import org.melati.MelatiConfig;
6 import org.melati.PoemContext;
7 import org.melati.poem.*;
8 import org.melati.template.AttributeMarkupLanguage;
9 import org.melati.template.MarkupLanguage;
10 import org.melati.template.TemplateEngine;
11 import org.melati.template.TemplateEngineException;
12 import org.melati.util.*;
13 import org.melati.util.test.Node;
14 import org.melati.util.test.TreeDatabase;
15
16 import java.util.ArrayList;
17 import java.util.Properties;
18
19
20
21
22
23
24
25
26 abstract public class MarkupLanguageSpec extends TreeTestCase {
27
28 protected static MelatiConfig mc = null;
29 protected static TemplateEngine templateEngine = null;
30 protected static MarkupLanguage ml = null;
31 protected static AttributeMarkupLanguage aml = null;
32 protected static Melati m = null;
33
34
35
36
37
38 public MarkupLanguageSpec(String arg0) {
39 super(arg0);
40 }
41
42
43
44 public MarkupLanguageSpec() {
45 super();
46 }
47
48
49
50
51
52
53 protected void setUp() throws Exception
54 {
55 super.setUp();
56 melatiConfig();
57 templateEngine = mc.getTemplateEngine();
58
59 templateEngine.init(mc);
60 m = new Melati(mc, new MelatiStringWriter());
61 m.setTemplateEngine(templateEngine);
62 m.setPoemContext(new PoemContext());
63 assertNotNull(m.getTemplateEngine());
64 }
65
66 abstract protected void melatiConfig() throws MelatiException ;
67
68
69
70
71
72
73
74
75
76
77 public void testRenderedAccessPoemException() throws Exception {
78
79 assertEquals("java.lang.Exception",
80 aml.rendered(new Exception()));
81 assertEquals("[java.lang.Exception]",
82 ml.rendered(new Exception()).trim());
83
84 AccessPoemException ape = new AccessPoemException(
85 getDb().getUserTable().guestUser(), new Capability("Cool"));
86 assertTrue(ml.rendered(ape),
87 ml.rendered(ape).indexOf("[Access denied to Melati guest user]") != -1);
88 ape = new AccessPoemException();
89 assertEquals("", aml.rendered(ape));
90 assertTrue(m.getWriter().toString().indexOf("[Access denied to [UNRENDERABLE EXCEPTION!]") != -1);
91 ape = new AccessPoemException(
92 getDb().getUserTable().guestUser(), new Capability("Cool"));
93 assertEquals("", aml.rendered(ape));
94
95
96
97 assertTrue(m.getWriter().toString().indexOf("[Access denied to _guest_]") != -1);
98 }
99
100
101
102
103
104
105
106 public void testGetAttr() {
107 assertEquals(aml.getClass(), ml.getAttr().getClass());
108 }
109
110
111
112
113
114
115 public void testEscapedString() throws Exception {
116
117 }
118
119 public void testEntitySubstitution() throws Exception {
120 char pound[] = {163};
121 assertEquals("£", ml.rendered(new String(pound)));
122 assertEquals("£", aml.rendered(new String(pound)));
123 }
124
125
126
127
128
129
130 public void testEscapedPersistent() {
131 assertEquals("Melati guest user",ml.escaped(getDb().getUserTable().getUserObject(0)));
132 }
133
134
135
136
137
138
139 public void testEncoded() {
140 assertEquals("+", ml.encoded(" "));
141 assertEquals("+", aml.encoded(" "));
142 assertEquals("%26", ml.encoded("&"));
143 assertEquals("%26", aml.encoded("&"));
144 }
145
146
147
148
149
150
151 public void testRenderedObject() throws Exception {
152 assertEquals("Fredd$", ml.rendered("Fredd$"));
153
154 assertEquals("[1]", ml.rendered(new Integer("1")).trim());
155
156 assertEquals("1", ml.getAttr().rendered(new Integer("1")));
157 try {
158 ml.getAttr().rendered(new Bomber());
159 fail("Should have bombed");
160 } catch (Exception e) {
161 e = null;
162 }
163
164 try {
165 ml.rendered(new Bomber());
166 fail("Should have bombed");
167 } catch (Exception e) {
168 e = null;
169 }
170
171 Node persistent = (Node)getDb().getTable("node").newPersistent();
172 persistent.setName("Mum");
173 persistent.makePersistent();
174 m.setPoemContext(new PoemContext());
175
176 String renderedPersistent = ml.rendered(persistent);
177
178 assertEquals("Mum", renderedPersistent);
179
180 }
181
182
183
184 public void testRenderedMarkupString() throws Exception {
185 assertEquals("</a>",ml.renderedMarkup("</a>"));
186 assertEquals("</a>",ml.rendered("</a>"));
187 }
188
189
190
191
192 public void testSyntaxErrorInTemplet() throws Exception {
193 Object templated = new TemplatedWithSyntaxError();
194 try {
195 ml.rendered(templated);
196 fail("Should have bombed");
197 } catch (TemplateEngineException e) {
198 e = null;
199 } catch (MelatiBugMelatiException e) {
200 e = null;
201 }
202 }
203
204
205
206 public void testEvaluationErrorInTemplet() throws Exception {
207 Object templated = new TemplatedWithBombingToString();
208 try {
209 ml.rendered(templated);
210 fail("Should have bombed");
211 } catch (TemplateEngineException e) {
212 e = null;
213 }
214 }
215
216
217
218
219 public void testSyntaxErrorInWMTemplet() throws Exception {
220 Object templated = new TemplatedWithWMSyntaxError();
221 try {
222 ml.rendered(templated);
223 fail("Should have bombed");
224 } catch (TemplateEngineException e) {
225 e = null;
226 }
227 }
228
229
230
231
232 public void testTemplateFoundOnClasspath() throws Exception {
233 Templated templated = new Templated();
234 String rendered = ml.rendered(templated);
235 assertEquals("Hi, this is from a template.", rendered);
236 }
237
238
239
240
241
242 public void testUntemplatedObjectUsesToString() throws Exception {
243 assertEquals("[{}]", ml.rendered(new Properties()).trim());
244 }
245
246
247
248
249 public void testSpecialTemplateFound() throws Exception {
250 Column<Integer> column = getDb().getGroupMembershipTable().getUserColumn();
251 BaseFieldAttributes<Integer> fa = new BaseFieldAttributes<Integer>(column, column.getType());
252 Field<Integer> field = new Field<Integer>(getDb().getUserTable().administratorUser().troid(), fa);
253 Object adminUtil = m.getContextUtil("org.melati.admin.AdminUtils");
254 assertTrue(adminUtil instanceof org.melati.admin.AdminUtils);
255 assertTrue(ml.input(field).indexOf("add_rule(\"field_user\",") != -1);
256 }
257
258
259
260
261
262
263 public void testRenderedString() throws Exception {
264 assertEquals("Fredd$", ml.rendered("Fredd$"));
265 }
266
267
268
269
270 public void testNull() throws Exception {
271 try {
272 ml.rendered(null);
273 fail("should have bombed");
274 } catch (NullPointerException e) {
275 e = null;
276 }
277 }
278
279
280
281
282
283
284 public void testRenderedStringInt() throws Exception {
285 assertEquals("Fre...", ml.rendered("Fredd$", 3));
286 }
287
288
289
290
291
292
293 public void testRenderedField() throws Exception {
294 Field<?> userName = getDb().getUserTable().getUserObject(0).getField("login");
295 assertEquals("_guest_", ml.rendered(userName));
296 }
297
298
299
300
301
302
303 public void testRenderedFieldInt() throws Exception {
304 Field<?> userName = getDb().getUserTable().getUserObject(0).getField("login");
305 assertEquals("_guest_", ml.rendered(userName,3));
306 }
307
308
309
310
311
312
313 public void testRenderedFieldIntInt() throws Exception {
314 Field<?> userName = getDb().getUserTable().getUserObject(0).getField("login");
315 assertEquals("_gu...", ml.rendered(userName,3,3));
316 }
317
318
319
320
321
322
323 public void testRenderedStart() throws Exception {
324 Field<?> userName = getDb().getUserTable().getUserObject(0).getField("login");
325 assertEquals("_guest_", ml.renderedStart(userName));
326 }
327
328
329
330
331
332
333 public void testInputField() throws Exception {
334 Field<?> userName = getDb().getUserTable().getUserObject(0).getField("login");
335 assertTrue(ml.input(userName).toLowerCase().indexOf("<input name=\"field_login\"") != -1);
336 }
337
338
339
340
341
342
343 public void testInputFieldSelection() throws Exception {
344
345 Field<?> group = getDb().getGroupMembershipTable().getGroupMembershipObject(0).getField("group");
346
347 assertTrue(ml.input(group).indexOf("name=\"field_group\"") != -1);
348 assertTrue(ml.input(group).indexOf("id=\"field_group\"") != -1);
349 assertTrue(ml.input(group).indexOf("<select") != -1);
350 }
351
352
353
354
355
356
357 public void testSelectionWindowField() throws Exception {
358 Field<?> owningTable = getDb().getColumnInfoTable().getColumnInfoObject(0).getField("tableinfo");
359 assertTrue(ml.input(owningTable).indexOf("<input type=\"text\" name=\"field_tableinfo_dummy\"") != -1);
360 Field<?> user = getDb().getGroupMembershipTable().getGroupMembershipObject(0).getField("user");
361 assertTrue(ml.input(user).indexOf("<input type=\"text\" name=\"field_user_dummy\"") != -1);
362 }
363
364
365
366
367 public void testInputFieldForRestrictedField() throws Exception {
368 PoemThread.setAccessToken(getDb().getUserTable().guestUser());
369 Field<?> password = getDb().getUserTable().getPasswordColumn().asEmptyField();
370 assertTrue(ml.input(password).toLowerCase().indexOf("name=\"field_password\"") != -1);
371
372 }
373
374
375
376
377
378
379 public void testInputAs() throws Exception {
380 Field<?> userName = getDb().getUserTable().getUserObject(0).getField("login");
381 try {
382 assertTrue(ml.inputAs(userName, "nonExistantTemplateName").toLowerCase().indexOf("<input name=\"field_login\"") != -1);
383 fail("Should have bombed");
384 } catch (Exception e) {
385 e = null;
386 }
387 assertTrue(ml.inputAs(userName, "org.melati.poem.StringPoemType").toLowerCase().indexOf("<input name=\"field_login\"") != -1);
388 }
389
390
391
392
393
394
395 public void testSearchInput() throws Exception {
396 Field<?> userName = getDb().getUserTable().getUserObject(0).getField("login");
397 assertTrue(ml.searchInput(userName, "None").toLowerCase().indexOf("<input name=\"field_login\"") != -1);
398 }
399
400
401
402
403
404
405 public void testRenderedTreeable() throws Exception {
406 Node parent = (Node)((TreeDatabase)getDb()).getNodeTable().newPersistent();
407 parent.setName("Mum");
408 parent.makePersistent();
409 Node kid1 = (Node)((TreeDatabase)getDb()).getNodeTable().newPersistent();
410 kid1.setName("K1");
411 kid1.setParent(parent);
412 kid1.makePersistent();
413 Node kid2 = (Node)((TreeDatabase)getDb()).getNodeTable().newPersistent();
414 kid2.setName("K2");
415 kid2.setParent(parent);
416 kid2.makePersistent();
417 Tree testTree = new Tree(parent);
418 JSStaticTree tree = new JSStaticTree(testTree, "/melati-static/admin/static");
419 m.setPoemContext(new PoemContext());
420
421 String renderedTree = ml.rendered(tree);
422
423 assertTrue(renderedTree.indexOf("init") != -1);
424
425 }
426
427 public void testRenderedList() {
428 assertEquals("[[]]", ml.rendered(new ArrayList<String>()));
429 }
430
431
432
433
434 class Bomber {
435
436
437
438 public Bomber() {
439 }
440
441
442
443
444 public String toString() {
445 throw new RuntimeException("Bomber bombed.");
446 }
447 }
448 }