001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestTemplate.java 3737 2007-05-08 01:48:00Z gbevin $
007: */
008: package com.uwyn.rife.template;
009:
010: import com.uwyn.rife.site.ConstrainedProperty;
011: import com.uwyn.rife.template.exceptions.BlockUnknownException;
012: import com.uwyn.rife.template.exceptions.TemplateException;
013: import com.uwyn.rife.template.exceptions.ValueUnknownException;
014: import com.uwyn.rife.tools.ExceptionUtils;
015: import java.util.ArrayList;
016: import java.util.Collection;
017:
018: public class TestTemplate extends TemplateTestCase {
019: public TestTemplate(String name) {
020: super (name);
021: }
022:
023: public void setUp() {
024: }
025:
026: public void testInstantiation() {
027: Template template = TemplateFactory.HTML.get("empty");
028: assertNotNull(template);
029: assertTrue(template.getModificationTime() <= System
030: .currentTimeMillis());
031: assertEquals("", template.getContent());
032: assertEquals("empty", template.getName());
033: assertEquals("empty", template.getFullName());
034: try {
035: template.getBlock("TEST");
036: fail();
037: } catch (BlockUnknownException e) {
038: assertEquals("TEST", e.getId());
039: }
040: try {
041: template.getValue("TEST");
042: fail();
043: } catch (ValueUnknownException e) {
044: assertEquals("TEST", e.getId());
045: }
046: assertEquals(template.countValues(), 0);
047: assertEquals(template.getAvailableValueIds().length, 0);
048: assertEquals(template.getFilteredValues("empty").size(), 0);
049: assertEquals(template.getUnsetValueIds().size(), 0);
050: }
051:
052: public void testClone() {
053: Template template1 = TemplateFactory.HTML.get("values");
054: assertEquals("values", template1.getName());
055: assertEquals("values", template1.getFullName());
056: String value1 = "aaab";
057: String value2 = "bbbc";
058: String value3 = "ccccd";
059: try {
060: template1.setValue("VALUE1", value1);
061: template1.setValue("VALUE2", value2);
062: template1.setValue("VALUE3", value3);
063: } catch (TemplateException e) {
064: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
065: }
066:
067: Template template2 = (Template) template1.clone();
068: assertNotNull(template2);
069: assertNotSame(template1, template2);
070: assertEquals(template1.getContent(), template2.getContent());
071: }
072:
073: public void testSetValues() {
074: Template template = TemplateFactory.HTML.get("values");
075: assertNull(template.getValue("VALUE1"));
076: assertNull(template.getValue("VALUE2"));
077: assertNull(template.getValue("VALUE3"));
078: assertEquals(template.countValues(), 0);
079:
080: try {
081: template.getValue("VALUE4");
082: fail();
083: } catch (ValueUnknownException e) {
084: assertEquals("VALUE4", e.getId());
085: }
086:
087: String value1 = "aaab";
088: String value2 = "bbbc";
089: String value3 = "ccccd";
090: try {
091: template.setValue("VALUE1", value1);
092: assertEquals(template.countValues(), 1);
093: template.setValue("VALUE2", value2);
094: assertEquals(template.countValues(), 2);
095: template.setValue("VALUE3", value3);
096: assertEquals(template.countValues(), 3);
097: } catch (TemplateException e) {
098: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
099: }
100: assertEquals(template.getValue("VALUE1"), value1);
101: assertEquals(template.getValue("VALUE2"), value2);
102: assertEquals(template.getValue("VALUE3"), value3);
103: }
104:
105: public void testSetValuesTyped() {
106: Template template = TemplateFactory.HTML.get("values_typed");
107:
108: String value_string = "abcde";
109: boolean value_boolean = false;
110: char value_char = 'k';
111: char[] value_chararray = "abcdefgh".toCharArray();
112: double value_double = 7483.343d;
113: float value_float = 233.45f;
114: int value_int = 34878;
115: long value_long = 938649837L;
116: Object value_object = new Boolean(true);
117: Template value_template = TemplateFactory.HTML.get("values");
118:
119: value_template.setValue("VALUE1", "thevalue1");
120: value_template.setValue("VALUE3", "thevalue3");
121:
122: try {
123: template.setValue("STRING", value_string);
124: assertEquals(template.countValues(), 1);
125: template.setValue("BOOLEAN", value_boolean);
126: assertEquals(template.countValues(), 2);
127: template.setValue("CHAR", value_char);
128: assertEquals(template.countValues(), 3);
129: template.setValue("CHAR[]", value_chararray, 3, 2);
130: assertEquals(template.countValues(), 4);
131: template.setValue("DOUBLE", value_double);
132: assertEquals(template.countValues(), 5);
133: template.setValue("FLOAT", value_float);
134: assertEquals(template.countValues(), 6);
135: template.setValue("INT", value_int);
136: assertEquals(template.countValues(), 7);
137: template.setValue("LONG", value_long);
138: assertEquals(template.countValues(), 8);
139: template.setValue("OBJECT", value_object);
140: assertEquals(template.countValues(), 9);
141: template.setValue("TEMPLATE", value_template);
142: assertEquals(template.countValues(), 10);
143: } catch (TemplateException e) {
144: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
145: }
146: assertEquals(template.getValue("STRING"), value_string);
147: assertEquals(template.getValue("BOOLEAN"), "" + value_boolean);
148: assertEquals(template.getValue("CHAR"), "" + value_char);
149: assertEquals(template.getValue("CHAR[]").toString(), "de");
150: assertEquals(template.getValue("DOUBLE"), "" + value_double);
151: assertEquals(template.getValue("FLOAT"), "" + value_float);
152: assertEquals(template.getValue("INT"), "" + value_int);
153: assertEquals(template.getValue("LONG"), "" + value_long);
154: assertEquals(template.getValue("OBJECT"), "" + value_object);
155: assertEquals(template.getValue("TEMPLATE"),
156: "thevalue1<!--V VALUE2/-->thevalue3\n");
157: }
158:
159: public void testAppendValuesTyped() {
160: Template template = TemplateFactory.HTML.get("values_typed");
161:
162: String value_string = "abcde";
163: boolean value_boolean = false;
164: char value_char = 'k';
165: char[] value_chararray = "abcdefgh".toCharArray();
166: double value_double = 7483.343d;
167: float value_float = 233.45f;
168: int value_int = 34878;
169: long value_long = 938649837L;
170: Object value_object = new Boolean(true);
171:
172: try {
173: template.appendValue("VALUE", value_string);
174: template.appendValue("VALUE", value_boolean);
175: template.appendValue("VALUE", value_char);
176: template.appendValue("VALUE", value_chararray, 3, 2);
177: template.appendValue("VALUE", value_double);
178: template.appendValue("VALUE", value_float);
179: template.appendValue("VALUE", value_int);
180: template.appendValue("VALUE", value_long);
181: template.appendValue("VALUE", value_object);
182: } catch (TemplateException e) {
183: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
184: }
185: assertEquals(template.getValue("VALUE"), value_string
186: + value_boolean + value_char + "de" + value_double
187: + value_float + value_int + value_long + value_object);
188: }
189:
190: public void testRemoveValues() {
191: Template template = TemplateFactory.HTML.get("values");
192: String value1 = "aaab";
193: String value2 = "bbbc";
194: String value3 = "ccccd";
195: try {
196: template.setValue("VALUE1", value1);
197: template.setValue("VALUE2", value2);
198: template.setValue("VALUE3", value3);
199: assertEquals(template.countValues(), 3);
200: } catch (TemplateException e) {
201: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
202: }
203: template.removeValue("VALUE1");
204: assertEquals(template.countValues(), 2);
205: template.removeValue("VALUE2");
206: assertEquals(template.countValues(), 1);
207: template.removeValue("VALUE3");
208: assertEquals(template.countValues(), 0);
209:
210: try {
211: template.removeValue("VALUE4");
212: fail();
213: } catch (ValueUnknownException e) {
214: assertEquals("VALUE4", e.getId());
215: }
216: }
217:
218: public void testBlankValues() {
219: Template template = TemplateFactory.HTML.get("values");
220: String value1 = "aaab";
221: String value2 = "bbbc";
222: String value3 = "ccccd";
223: try {
224: template.setValue("VALUE1", value1);
225: template.setValue("VALUE2", value2);
226: template.setValue("VALUE3", value3);
227: assertEquals(template.countValues(), 3);
228: } catch (TemplateException e) {
229: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
230: }
231:
232: assertFalse(template.getValue("VALUE1").equals(""));
233: assertFalse(template.getValue("VALUE2").equals(""));
234: assertFalse(template.getValue("VALUE3").equals(""));
235:
236: template.blankValue("VALUE1");
237: template.blankValue("VALUE2");
238: template.blankValue("VALUE3");
239: assertEquals(template.countValues(), 3);
240:
241: assertEquals(template.getValue("VALUE1"), "");
242: assertEquals(template.getValue("VALUE2"), "");
243: assertEquals(template.getValue("VALUE3"), "");
244:
245: try {
246: template.blankValue("VALUE4");
247: fail();
248: } catch (ValueUnknownException e) {
249: assertEquals("VALUE4", e.getId());
250: }
251: }
252:
253: public void testClearValues() {
254: Template template = TemplateFactory.HTML.get("values");
255: String value1 = "aaab";
256: String value2 = "bbbc";
257: String value3 = "ccccd";
258: try {
259: template.setValue("VALUE1", value1);
260: template.setValue("VALUE2", value2);
261: template.setValue("VALUE3", value3);
262: assertEquals(template.countValues(), 3);
263: } catch (TemplateException e) {
264: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
265: }
266: template.clear();
267: assertEquals(template.countValues(), 0);
268: }
269:
270: public void testDefaultValues() {
271: Template template = TemplateFactory.HTML.get("values_default");
272: String defaultvalue1 = "azerty";
273: assertTrue(template.hasDefaultValue("DEFAULTVALUE"));
274: assertEquals(template.getDefaultValue("DEFAULTVALUE"),
275: defaultvalue1);
276: assertEquals(template.getValue("DEFAULTVALUE"), defaultvalue1);
277: String value1 = "hdijjk";
278: try {
279: template.setValue("DEFAULTVALUE", value1);
280: } catch (TemplateException e) {
281: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
282: }
283: assertTrue(template.hasDefaultValue("DEFAULTVALUE"));
284: assertEquals(template.getDefaultValue("DEFAULTVALUE"),
285: defaultvalue1);
286: assertEquals(template.getValue("DEFAULTVALUE"), value1);
287: template.removeValue("DEFAULTVALUE");
288: assertTrue(template.hasDefaultValue("DEFAULTVALUE"));
289: assertEquals(template.getDefaultValue("DEFAULTVALUE"),
290: defaultvalue1);
291: assertEquals(template.getValue("DEFAULTVALUE"), defaultvalue1);
292: }
293:
294: public void testUnsetValues() {
295: Template template = null;
296: Collection<String> unset_valueids = null;
297: try {
298: template = TemplateFactory.HTML.get("values_short_in");
299:
300: unset_valueids = template.getUnsetValueIds();
301: assertNotNull(unset_valueids);
302: assertEquals(unset_valueids.size(), 7);
303: assertTrue(unset_valueids.contains("VALUE1"));
304: assertTrue(unset_valueids.contains("VALUE2"));
305: assertTrue(unset_valueids.contains("VALUE3"));
306: assertTrue(unset_valueids.contains("VALUE4"));
307: assertTrue(unset_valueids.contains("VALUE5"));
308: assertTrue(unset_valueids.contains("VALUE6"));
309: assertTrue(unset_valueids.contains("VALUE7"));
310:
311: template.setValue("VALUE1", "value");
312: unset_valueids = template.getUnsetValueIds();
313: assertEquals(unset_valueids.size(), 6);
314: assertTrue(!unset_valueids.contains("VALUE1"));
315: assertTrue(unset_valueids.contains("VALUE2"));
316: assertTrue(unset_valueids.contains("VALUE3"));
317: assertTrue(unset_valueids.contains("VALUE4"));
318: assertTrue(unset_valueids.contains("VALUE5"));
319: assertTrue(unset_valueids.contains("VALUE6"));
320: assertTrue(unset_valueids.contains("VALUE7"));
321:
322: template.setValue("VALUE2", "value");
323: unset_valueids = template.getUnsetValueIds();
324: assertEquals(unset_valueids.size(), 5);
325: assertTrue(!unset_valueids.contains("VALUE1"));
326: assertTrue(!unset_valueids.contains("VALUE2"));
327: assertTrue(unset_valueids.contains("VALUE3"));
328: assertTrue(unset_valueids.contains("VALUE4"));
329: assertTrue(unset_valueids.contains("VALUE5"));
330: assertTrue(unset_valueids.contains("VALUE6"));
331: assertTrue(unset_valueids.contains("VALUE7"));
332:
333: template.setValue("VALUE3", "value");
334: unset_valueids = template.getUnsetValueIds();
335: assertEquals(unset_valueids.size(), 4);
336: assertTrue(!unset_valueids.contains("VALUE1"));
337: assertTrue(!unset_valueids.contains("VALUE2"));
338: assertTrue(!unset_valueids.contains("VALUE3"));
339: assertTrue(unset_valueids.contains("VALUE4"));
340: assertTrue(unset_valueids.contains("VALUE5"));
341: assertTrue(unset_valueids.contains("VALUE6"));
342: assertTrue(unset_valueids.contains("VALUE7"));
343:
344: template.setValue("VALUE4", "value");
345: unset_valueids = template.getUnsetValueIds();
346: assertEquals(unset_valueids.size(), 3);
347: assertTrue(!unset_valueids.contains("VALUE1"));
348: assertTrue(!unset_valueids.contains("VALUE2"));
349: assertTrue(!unset_valueids.contains("VALUE3"));
350: assertTrue(!unset_valueids.contains("VALUE4"));
351: assertTrue(unset_valueids.contains("VALUE5"));
352: assertTrue(unset_valueids.contains("VALUE6"));
353: assertTrue(unset_valueids.contains("VALUE7"));
354:
355: template.setValue("VALUE5", "value");
356: unset_valueids = template.getUnsetValueIds();
357: assertEquals(unset_valueids.size(), 2);
358: assertTrue(!unset_valueids.contains("VALUE1"));
359: assertTrue(!unset_valueids.contains("VALUE2"));
360: assertTrue(!unset_valueids.contains("VALUE3"));
361: assertTrue(!unset_valueids.contains("VALUE4"));
362: assertTrue(!unset_valueids.contains("VALUE5"));
363: assertTrue(unset_valueids.contains("VALUE6"));
364: assertTrue(unset_valueids.contains("VALUE7"));
365:
366: template.setValue("VALUE6", "value");
367: unset_valueids = template.getUnsetValueIds();
368: assertEquals(unset_valueids.size(), 1);
369: assertTrue(!unset_valueids.contains("VALUE1"));
370: assertTrue(!unset_valueids.contains("VALUE2"));
371: assertTrue(!unset_valueids.contains("VALUE3"));
372: assertTrue(!unset_valueids.contains("VALUE4"));
373: assertTrue(!unset_valueids.contains("VALUE5"));
374: assertTrue(!unset_valueids.contains("VALUE6"));
375: assertTrue(unset_valueids.contains("VALUE7"));
376:
377: template.setValue("VALUE7", "value");
378: unset_valueids = template.getUnsetValueIds();
379: assertEquals(unset_valueids.size(), 0);
380: assertTrue(!unset_valueids.contains("VALUE1"));
381: assertTrue(!unset_valueids.contains("VALUE2"));
382: assertTrue(!unset_valueids.contains("VALUE3"));
383: assertTrue(!unset_valueids.contains("VALUE4"));
384: assertTrue(!unset_valueids.contains("VALUE5"));
385: assertTrue(!unset_valueids.contains("VALUE6"));
386: assertTrue(!unset_valueids.contains("VALUE7"));
387: } catch (TemplateException e) {
388: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
389: }
390: }
391:
392: public void testUnsetValuesDefaults() {
393: Template template = null;
394: Collection<String> unset_valueids = null;
395: try {
396: template = TemplateFactory.HTML.get("values_long_in");
397:
398: unset_valueids = template.getUnsetValueIds();
399: assertNotNull(unset_valueids);
400: assertEquals(unset_valueids.size(), 0);
401:
402: template.setValue("VALUE1", "value");
403: unset_valueids = template.getUnsetValueIds();
404: assertEquals(unset_valueids.size(), 0);
405:
406: template.setValue("VALUE2", "value");
407: unset_valueids = template.getUnsetValueIds();
408: assertEquals(unset_valueids.size(), 0);
409:
410: template.setValue("VALUE3", "value");
411: unset_valueids = template.getUnsetValueIds();
412: assertEquals(unset_valueids.size(), 0);
413:
414: template.setValue("VALUE4", "value");
415: unset_valueids = template.getUnsetValueIds();
416: assertEquals(unset_valueids.size(), 0);
417:
418: template.setValue("VALUE5", "value");
419: unset_valueids = template.getUnsetValueIds();
420: assertEquals(unset_valueids.size(), 0);
421:
422: template.setValue("VALUE6", "value");
423: unset_valueids = template.getUnsetValueIds();
424: assertEquals(unset_valueids.size(), 0);
425:
426: template.setValue("VALUE7", "value");
427: unset_valueids = template.getUnsetValueIds();
428: assertEquals(unset_valueids.size(), 0);
429: } catch (TemplateException e) {
430: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
431: }
432: }
433:
434: public void testConditionalBlockRetrieval() {
435: Template template = null;
436: try {
437: template = TemplateFactory.TXT
438: .get("construction_simple_in");
439: assertTrue(template.hasBlock("BLOCK1"));
440: assertFalse(template.hasBlock("BLOCK1b"));
441: try {
442: template.getBlock("BLOCK1b");
443: fail();
444: } catch (BlockUnknownException e) {
445: assertTrue(true);
446: }
447: } catch (TemplateException e) {
448: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
449: }
450: }
451:
452: public void testConstructionSimple() {
453: Template template = null;
454: try {
455: template = TemplateFactory.TXT
456: .get("construction_simple_in");
457: template.appendBlock("CONTENT", "BLOCK1");
458: template.appendBlock("CONTENT", "BLOCK3");
459: template.appendBlock("CONTENT", "BLOCK2");
460: template.appendBlock("CONTENT", "BLOCK4");
461: template.appendBlock("CONTENT", "BLOCK1");
462: template.setValue("VALUE3", "value 3 early"); // will be overridden
463: template.appendBlock("CONTENT", "BLOCK3");
464: template.appendBlock("CONTENT", "BLOCK2");
465: template.setValue("VALUE4", "value 4 early"); // will be removed
466: template.appendBlock("CONTENT", "BLOCK4");
467: template.removeValue("VALUE4");
468: template.setValue("VALUE1", "value 1 late"); // late setting
469: template.setValue("VALUE3", "value 3 late"); // late setting
470: assertEquals(template.getContent(), getTemplateContent(
471: "construction_simple_out", TemplateFactory.TXT
472: .getParser()));
473: } catch (TemplateException e) {
474: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
475: }
476: }
477:
478: public void testConstructionRepeated() {
479: Template template = null;
480: try {
481: template = TemplateFactory.TXT
482: .get("construction_repeated_in");
483: template.setBlock("VALUE2", "BLOCK1");
484: template.setBlock("VALUE3", "BLOCK2");
485: template.setBlock("VALUE4", "BLOCK3");
486: template.setBlock("CONTENT", "BLOCK4");
487: template.setValue("VALUE1", "value 1 late");
488: template.setValue("VALUE2", "value 2 late"); // has no influence
489: template.setValue("VALUE3", "value 3 late"); // has no influence
490: template.setValue("VALUE4", "value 4 late"); // has no influence
491: assertEquals(template.getContent(), getTemplateContent(
492: "construction_repeated_out", TemplateFactory.TXT
493: .getParser()));
494: } catch (TemplateException e) {
495: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
496: }
497: }
498:
499: public void testConstructionOverriding() {
500: Template template = null;
501: try {
502: template = TemplateFactory.TXT
503: .get("construction_overriding_in");
504:
505: assertTrue(!template.isValueSet("VALUE2"));
506: template.setValue("VALUE2", "value2");
507: assertTrue(template.isValueSet("VALUE2"));
508: template.setBlock("VALUE2", "BLOCK1");
509: assertTrue(template.isValueSet("VALUE2"));
510: template.setValue("VALUE1", "value1");
511: assertEquals(template.getBlock("BLOCK2"),
512: getTemplateContent("construction_overriding_out_1",
513: TemplateFactory.TXT.getParser()));
514: template.clear();
515:
516: assertTrue(!template.isValueSet("VALUE2"));
517: template.setBlock("VALUE2", "BLOCK1");
518: assertTrue(template.isValueSet("VALUE2"));
519: template.setValue("VALUE2", "value2");
520: assertTrue(template.isValueSet("VALUE2"));
521: template.setValue("VALUE1", "value1");
522: assertEquals(template.getBlock("BLOCK2"),
523: getTemplateContent("construction_overriding_out_2",
524: TemplateFactory.TXT.getParser()));
525: template.clear();
526:
527: assertTrue(!template.isValueSet("VALUE2"));
528: template.setValue("VALUE2", "value2 ");
529: assertTrue(template.isValueSet("VALUE2"));
530: template.appendBlock("VALUE2", "BLOCK1");
531: assertTrue(template.isValueSet("VALUE2"));
532: template.setValue("VALUE1", "value1");
533: assertEquals(template.getBlock("BLOCK2"),
534: getTemplateContent("construction_overriding_out_3",
535: TemplateFactory.TXT.getParser()));
536: template.clear();
537:
538: assertTrue(!template.isValueSet("VALUE2"));
539: template.setBlock("VALUE2", "BLOCK1");
540: assertTrue(template.isValueSet("VALUE2"));
541: template.appendValue("VALUE2", " value2");
542: assertTrue(template.isValueSet("VALUE2"));
543: template.setValue("VALUE1", "value1");
544: assertEquals(template.getBlock("BLOCK2"),
545: getTemplateContent("construction_overriding_out_4",
546: TemplateFactory.TXT.getParser()));
547: template.clear();
548: } catch (TemplateException e) {
549: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
550: }
551: }
552:
553: public void testConstructionDefaultValue() {
554: Template template = null;
555: try {
556: template = TemplateFactory.TXT
557: .get("construction_defaultvalue_in");
558:
559: template.setValue("VALUE1", "value1");
560: assertEquals(template.getBlock("BLOCK2"),
561: getTemplateContent("construction_defaultvalue_out",
562: TemplateFactory.TXT.getParser()));
563: } catch (TemplateException e) {
564: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
565: }
566: }
567:
568: public void testConstructionEmbedded() {
569: Template template = null;
570: try {
571: template = TemplateFactory.TXT
572: .get("contruction_embedded_in");
573:
574: template.setValue("member_value1", 1);
575: template.appendBlock("rows", "row_first");
576: template.setValue("member_value1", 2);
577: template.appendBlock("rows", "row_second");
578: template.setValue("member_value1", 3);
579: template.appendBlock("rows", "row_first");
580: template.setValue("member_value1", 4);
581: template.appendBlock("rows", "row_second");
582: template.appendBlock("rows", "row_first");
583: template.setValue("member_value2", 5);
584: assertEquals(template.getContent(), getTemplateContent(
585: "contruction_embedded_out", TemplateFactory.TXT
586: .getParser()));
587: } catch (TemplateException e) {
588: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
589: }
590: }
591:
592: public void testConstructionInternalValues() {
593: Template template = null;
594: try {
595: template = TemplateFactory.TXT
596: .get("contruction_internalvalues_in");
597:
598: TreeNode tree = new TreeNode(template);
599: TreeNode node1 = new TreeNode(tree, "node1");
600: TreeNode node2 = new TreeNode(tree, "node2");
601: TreeNode node3 = new TreeNode(tree, "node3");
602: new TreeNode(node1, "node1a");
603: new TreeNode(node1, "node1b");
604: new TreeNode(node1, "node1c");
605: TreeNode node2a = new TreeNode(node2, "node2a");
606: new TreeNode(node2, "node2b");
607: new TreeNode(node3, "node3a");
608: new TreeNode(node3, "node3b");
609: new TreeNode(node2a, "node2a1");
610: new TreeNode(node2a, "node2a2");
611:
612: tree.output();
613: assertEquals(template.getContent(), getTemplateContent(
614: "contruction_internalvalues_out",
615: TemplateFactory.TXT.getParser()));
616: } catch (TemplateException e) {
617: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
618: }
619: }
620:
621: public void testConstructionInternalBlocksNameHashcodeConflicts() {
622: try {
623: assertTrue("DMn0".hashCode() == "Cln0".hashCode());
624: assertTrue("DMn0".hashCode() == "DNNO".hashCode());
625: assertTrue("FMmO".hashCode() == "EmMn".hashCode());
626: assertTrue("DMn0".hashCode() != "FMmO".hashCode());
627: assertTrue("DMn0".hashCode() != "HNMn".hashCode());
628: assertTrue("FMmO".hashCode() != "HNMn".hashCode());
629: Template template = TemplateFactory.HTML
630: .get("blocks_stringconflicts_in");
631: InternalValue internal = template.createInternalValue();
632: internal.appendBlock("DMn0");
633: internal.appendBlock("Cln0");
634: internal.appendBlock("DNNO");
635: internal.appendBlock("FMmO");
636: internal.appendBlock("EmMn");
637: internal.appendBlock("HNMn");
638: template.setValue("result", internal);
639: assertEquals(template.getValue("result"),
640: "1 : the first block" + "1 : the second block"
641: + "1 : the third block"
642: + "2 : the first block"
643: + "2 : the second block"
644: + "3 : the first block");
645: } catch (TemplateException e) {
646: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
647: }
648: }
649:
650: class TreeNode {
651: private Template mTemplate = null;
652: private String mText = null;
653: private ArrayList<TreeNode> mChildren = new ArrayList<TreeNode>();
654: private TreeNode mParent = null;
655:
656: public TreeNode(Template template) {
657: mTemplate = template;
658: }
659:
660: public TreeNode(TreeNode parent, String text) {
661: if (null == text)
662: throw new IllegalArgumentException(
663: "title can't be null.");
664:
665: parent.addChild(this );
666: mText = text;
667: mTemplate = parent.mTemplate;
668: }
669:
670: public void output() {
671: if (0 == mChildren.size()) {
672: mTemplate.setValue("level", "");
673: } else {
674: InternalValue nodes = mTemplate.createInternalValue();
675: InternalValue text = null;
676: int depth = 0;
677: int counter = 0;
678:
679: for (TreeNode child : mChildren) {
680: child.output();
681: depth = child.getDepth();
682: mTemplate.removeValue("indent");
683: if (1 == depth) {
684: mTemplate.setValue("indent", "");
685: } else {
686: for (int i = 1; i < depth; i++) {
687: mTemplate.appendBlock("indent", "indent");
688: }
689: }
690: mTemplate.setValue("depth", depth);
691: text = mTemplate.createInternalValue();
692: text.appendValue(++counter);
693: text.appendValue("-");
694: text.appendValue(child.getText());
695: mTemplate.setValue("text", text);
696: nodes.appendBlock("node");
697: }
698: mTemplate.setValue("nodes", nodes);
699: mTemplate.setBlock("level", "level");
700: }
701: }
702:
703: private void addChild(TreeNode child) {
704: child.mParent = this ;
705: mChildren.add(child);
706: }
707:
708: public String getText() {
709: return mText;
710: }
711:
712: public TreeNode getParent() {
713: return mParent;
714: }
715:
716: public int getDepth() {
717: TreeNode parent = getParent();
718: int depth = 0;
719: while (parent != null) {
720: parent = parent.getParent();
721: depth++;
722: }
723:
724: return depth;
725: }
726: }
727:
728: public void testHasValues() {
729: Template template = null;
730: try {
731: template = TemplateFactory.HTML.get("defaultvalues_in");
732: assertTrue(template.hasValueId("VALUE1"));
733: assertTrue(template.hasValueId("VALUE2"));
734: assertTrue(template.hasValueId("VALUE3"));
735: assertTrue(false == template.hasValueId("VALUE4"));
736: } catch (TemplateException e) {
737: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
738: }
739: }
740:
741: public void testUnsetValuesOutput() {
742: Template template = TemplateFactory.HTML
743: .get("unsetvalues_output_in");
744: assertTrue(template.hasValueId("VALUE1"));
745: assertTrue(template.hasValueId("VALUE2"));
746: assertTrue(template.hasValueId("VALUE3"));
747: assertTrue(template.hasValueId("VALUE4"));
748: assertTrue(template.hasValueId("VALUE5"));
749: assertTrue(template.hasValueId("VALUE6"));
750: assertTrue(template.hasValueId("VALUE7"));
751: assertEquals(template.getContent(), getTemplateContent(
752: "unsetvalues_output_in", TemplateFactory.HTML
753: .getParser()));
754: }
755:
756: public void testSetBeanValues() {
757: try {
758: Template template = TemplateFactory.HTML
759: .get("values_bean_in");
760: BeanImpl bean = new BeanImpl();
761: template.setBean(bean);
762: assertEquals(template.getContent(),
763: getTemplateContent("values_bean_out",
764: TemplateFactory.HTML.getParser()));
765: } catch (TemplateException e) {
766: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
767: }
768: }
769:
770: public void testRemoveBeanValues() {
771: try {
772: Template template = TemplateFactory.HTML
773: .get("values_bean_in");
774: BeanImpl bean = new BeanImpl();
775: template.setBean(bean);
776: assertEquals(template.getContent(),
777: getTemplateContent("values_bean_out",
778: TemplateFactory.HTML.getParser()));
779: template.removeBean(bean);
780: assertEquals(template.getContent(), getTemplateContent(
781: "values_bean_in", TemplateFactory.HTML.getParser()));
782: } catch (TemplateException e) {
783: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
784: }
785: }
786:
787: public void testSetBeanValuesPrefix() {
788: try {
789: Template template = TemplateFactory.HTML
790: .get("values_bean_prefix_in");
791: BeanImpl bean = new BeanImpl();
792: template.setBean(bean, "PREFIX:");
793: assertEquals(template.getContent(), getTemplateContent(
794: "values_bean_prefix_out", TemplateFactory.HTML
795: .getParser()));
796: } catch (TemplateException e) {
797: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
798: }
799: }
800:
801: public void testRemoveBeanValuesPrefix() {
802: try {
803: Template template = TemplateFactory.HTML
804: .get("values_bean_prefix_in");
805: BeanImpl bean = new BeanImpl();
806: template.setBean(bean, "PREFIX:");
807: assertEquals(template.getContent(), getTemplateContent(
808: "values_bean_prefix_out", TemplateFactory.HTML
809: .getParser()));
810: template.removeBean(bean, "WRONGPREFIX:");
811: assertEquals(template.getContent(), getTemplateContent(
812: "values_bean_prefix_out", TemplateFactory.HTML
813: .getParser()));
814: template.removeBean(bean, "PREFIX:");
815: assertEquals(template.getContent(), getTemplateContent(
816: "values_bean_prefix_in", TemplateFactory.HTML
817: .getParser()));
818: } catch (TemplateException e) {
819: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
820: }
821: }
822:
823: public void testSetBeanValuesHtml() {
824: try {
825: Template template = null;
826: HtmlBeanImpl bean = new HtmlBeanImpl();
827:
828: template = TemplateFactory.HTML.get("values_bean_html_in");
829: template.setBean(bean, "PARAM:");
830: assertEquals(template.getContent(), getTemplateContent(
831: "values_bean_html_out_emptyproperty",
832: TemplateFactory.HTML.getParser()));
833:
834: bean = new HtmlBeanImpl();
835: bean.setColors(new String[] { "red", "blue", "yellow" });
836: bean.setWantsupdates(true);
837: bean.setFirstname("Geert");
838: bean.setLastname("Bevin");
839: template = TemplateFactory.HTML.get("values_bean_html_in");
840: template.setBean(bean, "PARAM:");
841: assertEquals(template.getContent(), getTemplateContent(
842: "values_bean_html_out_content1",
843: TemplateFactory.HTML.getParser()));
844:
845: bean = new HtmlBeanImpl();
846: bean.setColors(new String[] { "red", "orange", "white" });
847: bean.setWantsupdates(false);
848: bean.setFirstname("Nathalie");
849: bean.setLastname("&<>");
850: template = TemplateFactory.HTML.get("values_bean_html_in");
851: template.setBean(bean, "PARAM:");
852: assertEquals(template.getContent(), getTemplateContent(
853: "values_bean_html_out_content2",
854: TemplateFactory.HTML.getParser()));
855:
856: bean.addConstraint(new ConstrainedProperty("lastname")
857: .displayedRaw(true));
858: template = TemplateFactory.HTML.get("values_bean_html_in");
859: template.setBean(bean, "PARAM:");
860: assertEquals(template.getContent(), getTemplateContent(
861: "values_bean_html_out_content3",
862: TemplateFactory.HTML.getParser()));
863: } catch (TemplateException e) {
864: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
865: }
866: }
867:
868: public void testRemoveBeanValuesHtml() {
869: try {
870: Template template = null;
871: HtmlBeanImpl bean = new HtmlBeanImpl();
872:
873: bean = new HtmlBeanImpl();
874: bean.setColors(new String[] { "red", "blue", "yellow" });
875: bean.setWantsupdates(true);
876: bean.setFirstname("Geert");
877: bean.setLastname("Bevin");
878: template = TemplateFactory.HTML.get("values_bean_html_in");
879: template.setBean(bean, "PARAM:");
880: assertEquals(template.getContent(), getTemplateContent(
881: "values_bean_html_out_content1",
882: TemplateFactory.HTML.getParser()));
883: template.removeBean(bean, "WRONGPARAM:");
884: assertEquals(template.getContent(), getTemplateContent(
885: "values_bean_html_out_content1",
886: TemplateFactory.HTML.getParser()));
887: template.removeBean(bean, "PARAM:");
888: assertEquals(template.getContent(), getTemplateContent(
889: "values_bean_html_out_empty", TemplateFactory.HTML
890: .getParser()));
891:
892: bean = new HtmlBeanImpl();
893: bean.setColors(new String[] { "red", "orange", "white" });
894: bean.setWantsupdates(false);
895: bean.setFirstname("Nathalie");
896: bean.setLastname("&<>");
897: template = TemplateFactory.HTML.get("values_bean_html_in");
898: template.setBean(bean, "PARAM:");
899: assertEquals(template.getContent(), getTemplateContent(
900: "values_bean_html_out_content2",
901: TemplateFactory.HTML.getParser()));
902: template.removeBean(bean, "WRONGPARAM:");
903: assertEquals(template.getContent(), getTemplateContent(
904: "values_bean_html_out_content2",
905: TemplateFactory.HTML.getParser()));
906: template.removeBean(bean, "PARAM:");
907: assertEquals(template.getContent(), getTemplateContent(
908: "values_bean_html_out_empty", TemplateFactory.HTML
909: .getParser()));
910: } catch (TemplateException e) {
911: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
912: }
913: }
914: }
|