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: TestParsed.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.template;
009:
010: import com.uwyn.rife.template.exceptions.TemplateException;
011: import com.uwyn.rife.tools.ExceptionUtils;
012: import java.net.MalformedURLException;
013: import java.net.URL;
014: import junit.framework.TestCase;
015:
016: public class TestParsed extends TestCase {
017: private Parser mParser = null;
018:
019: public TestParsed(String name) {
020: super (name);
021: }
022:
023: public void setUp() {
024: mParser = TemplateFactory.HTML.getParser();
025: }
026:
027: public void testInstantiation() {
028: Parsed tp = new Parsed(mParser);
029:
030: assertNotNull(tp);
031: assertNull(tp.getResource());
032: assertNull(tp.getClassName());
033: assertNull(tp.getFullClassName());
034: assertNull(tp.getBlock("test"));
035: assertNull(tp.getDefaultValue("test"));
036: assertEquals(tp.getBlocks().size(), 0);
037: assertEquals(tp.getDefaultValues().size(), 0);
038: assertEquals(tp.getDependencies().size(), 0);
039: assertNull(tp.getContent());
040: }
041:
042: public void testResource() {
043: Parsed tp = new Parsed(mParser);
044:
045: assertNull(tp.getResource());
046: URL url = null;
047: try {
048: url = new URL("file:/test/");
049: } catch (MalformedURLException e) {
050: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
051: }
052: tp.setResource(url);
053: assertEquals(tp.getResource(), url);
054: }
055:
056: public void testClassname() {
057: Parsed tp = new Parsed(mParser);
058:
059: assertNull(tp.getClassName());
060: assertNull(tp.getFullClassName());
061: String classname = "some_template";
062: tp.setClassName(classname);
063: assertNotNull(tp.getFullClassName());
064: assertTrue(!classname.equals(tp.getFullClassName()));
065: }
066:
067: public void testBlocks() {
068: Parsed tp = new Parsed(mParser);
069:
070: assertEquals(tp.getBlocks().size(), 0);
071:
072: ParsedBlockData blockdata1 = new ParsedBlockData();
073: ParsedBlockData blockdata2 = new ParsedBlockData();
074: tp.setBlock("blockparts1", blockdata1);
075: tp.setBlock("blockparts2", blockdata2);
076:
077: assertEquals(tp.getBlocks().size(), 2);
078: assertSame(tp.getBlock("blockparts1"), blockdata1);
079: assertSame(tp.getBlock("blockparts2"), blockdata2);
080: assertNull(tp.getBlock("blockparts3"));
081: assertNull(tp.getContent());
082:
083: ParsedBlockData contentdata = new ParsedBlockData();
084: tp.setBlock("", contentdata);
085: assertEquals(tp.getBlocks().size(), 3);
086: assertSame(tp.getContent(), contentdata);
087: }
088:
089: public void testDefaultValues() {
090: Parsed tp = new Parsed(mParser);
091:
092: assertEquals(tp.getDefaultValues().size(), 0);
093:
094: String defaultvalue1 = new String();
095: String defaultvalue2 = new String();
096: tp.setDefaultValue("defaultvalue1", defaultvalue1);
097: tp.setDefaultValue("defaultvalue2", defaultvalue2);
098:
099: assertEquals(tp.getDefaultValues().size(), 2);
100: assertSame(tp.getDefaultValue("defaultvalue1"), defaultvalue1);
101: assertSame(tp.getDefaultValue("defaultvalue2"), defaultvalue2);
102: assertNull(tp.getDefaultValue("defaultvalue3"));
103: }
104:
105: public void testIncludes() {
106: Parsed tp = new Parsed(mParser);
107:
108: String include1_name = "noblocks_in";
109: Parsed include1 = mParser.prepare(include1_name, mParser
110: .resolve(include1_name));
111: String include2_name = "defaultvalues_in";
112: Parsed include2 = mParser.prepare(include2_name, mParser
113: .resolve(include2_name));
114: String include3_name = "blocks_successive_in";
115: Parsed include3 = mParser.prepare(include3_name, mParser
116: .resolve(include3_name));
117:
118: try {
119: tp.addDependency(include1);
120: assertEquals(tp.getDependencies().size(), 1);
121: tp.addDependency(include2);
122: assertEquals(tp.getDependencies().size(), 2);
123: tp.addDependency(include3);
124: assertEquals(tp.getDependencies().size(), 3);
125: } catch (TemplateException e) {
126: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
127: }
128:
129: assertEquals(include1_name.indexOf(include1.getClassName()), 0);
130: assertEquals(include2_name.indexOf(include2.getClassName()), 0);
131: assertEquals(include3_name.indexOf(include3.getClassName()), 0);
132: }
133: }
|