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