001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.jxp;
022:
023: import java.io.*;
024: import java.util.HashMap;
025: import java.util.Map;
026: import junit.framework.TestCase;
027: import org.onemind.jxp.parser.ParseException;
028:
029: /**
030: * JxpProcessor unit test
031: * @author TiongHiang Lee (thlee@onemindsoft.org)
032: *
033: */
034: public class JxpProcessorTest extends TestCase {
035:
036: /** the processor * */
037: private JxpProcessor _processor;
038:
039: /** the source * */
040: private JxpPageSource _source;
041:
042: public void setUp() throws Exception {
043: //_source = new ResourceStreamPageSource("/org/onemind/jxp", "UTF-8"); //eclipse bin
044: _source = new FilePageSource("src/test/org/onemind/jxp",
045: "UTF-8"); //eclipse bin
046: _processor = new JxpProcessor(new JxpContext(_source));
047: _processor.setDoMethodStats(true);
048: }
049:
050: /** test import and declaration * */
051: public void testImportAndDeclaration() throws Exception {
052: _process("testImportAndDeclaration.jxp");
053: }
054:
055: /** test the include **/
056: public void testInclude() throws Exception {
057: _process("/included/testInclude.jxp");
058: }
059:
060: /** test try statement * */
061: public void testTry() throws Exception {
062: _process("testTry.jxp");
063: }
064:
065: /** test switch statement **/
066: public void testSwitch() throws Exception {
067: _process("testSwitch.jxp");
068: }
069:
070: /** test function declaration and call **/
071: public void testFunction() throws Exception {
072: _process("testFunction.jxp");
073: }
074:
075: /** test function declaration and call **/
076: public void testJsp() throws Exception {
077: _process("testJsp.jxp");
078: }
079:
080: /** test statements **/
081: public void testPrintStatement() throws Exception {
082: Map m = new HashMap();
083: m.put("test1", "test1");
084: m.put("test2", "test2");
085: _process("testPrintStatement.jxp", m);
086: }
087:
088: /** test statements **/
089: public void testStatements() throws Exception {
090: _process("testStatements.jxp");
091: }
092:
093: /** test array related operation * */
094: public void testArray() throws Exception {
095: _process("testArray.jxp");
096: }
097:
098: /** test misc control flow statements * */
099: public void testControlFlow() throws Exception {
100: _process("testControlFlow.jxp");
101: }
102:
103: /** test misc control flow statements * */
104: public void testStatic() throws Exception {
105: Map m = new HashMap();
106: m.put("result", new Integer(1));
107: _process("testStatic.jxp", m);
108: m.put("result", new Integer(2));
109: _process("testStatic.jxp", m);
110: }
111:
112: /** test operators * */
113: public void testOperators() throws Exception {
114: _process("testOperators.jxp");
115: }
116:
117: /** test synchronize * */
118: public void testSynchronized() throws Exception {
119: _process("testSynchronized.jxp");
120: }
121:
122: public void testParsing() throws Exception {
123: _process("testParsing.jxp");
124: }
125:
126: /** test return * */
127: public void testReturn() throws Exception {
128: _process("testReturn.jxp");
129: }
130:
131: /** test reflection * */
132: public void testReflect() throws Exception {
133: _process("testReflect.jxp");
134: }
135:
136: /** test jdk15 **/
137: public void testJdk15() throws Exception {
138: _process("testJdk15.jxp");
139: }
140:
141: public void testBufferedPageSource() throws Exception {
142: ByteArrayPageSource ps = new ByteArrayPageSource();
143: JxpContext context = new JxpContext(ps);
144: JxpProcessor processor = new JxpProcessor(context);
145: ps.putPageBuffer("test", "<% System.out.println(\"a\"); %> "
146: .getBytes());
147: processor.process("test", new OutputStreamWriter(System.out));
148: }
149:
150: public void dumpSyntax(String filename) throws Exception {
151: _source.getJxpPage(filename).getJxpDocument().dump("");
152: }
153:
154: /** process the given script */
155: private void _process(String filename) throws Exception {
156: _process(filename, new HashMap());
157: }
158:
159: /** process the given script */
160: private void _process(String filename, Map env) throws Exception {
161: Writer writer = new OutputStreamWriter(System.out, "UTF-8");
162: try {
163: _processor.process(filename, writer, env);
164: } catch (ParseException pe) {
165: throw pe;
166: } catch (Exception e) {
167: System.out.println("Dumping syntax of " + filename);
168: System.out.println(e.getMessage());
169: // /dumpSyntax(filename);
170: throw e;
171: } finally {
172: writer.flush();
173: }
174: }
175:
176: public void printMethodStats() {
177: System.out.println(_processor.getMethodStats());
178: }
179:
180: public static void main(String argsp[]) throws Exception {
181: JxpProcessorTest test = new JxpProcessorTest();
182: test.setUp();
183: // test.testArray();
184: // test.testControlFlow();
185: // test.testFunction();
186: // test.testImportAndDeclaration();
187: // test.testInclude();
188: // test.testOperators();
189: // test.testReflect();
190: // test.testReturn();
191: // test.testStatements();
192: // test.testSwitch();
193: // test.testSynchronized();
194: // test.testTry();
195: //test.testPrintStatement();
196: test.testParsing();
197: test.printMethodStats();
198: }
199: }
|