/* * Reference GTL Parser for Java * Copyright (c) 2000-2004 Constantine A Plotnikov * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package net.sf.etl.tests.term_parser; import net.sf.etl.parsers.StandardGrammars; import net.sf.etl.parsers.TermContext; import net.sf.etl.parsers.TextPos; /** * test HelloWorld.ej.etl structure reading * * @author const */ public class HelloWorldTermTest extends TermStructureTestCase { /** namespace for EJ grammar */ private static final String ns = "http://etl.sf.net/2006/samples/ej/0.1"; /** * system id of minimal XJ grammar */ private static final String MinimalEJ_SYSTEM_ID = HelloWorldTermTest.class .getResource("/net/sf/etl/tests/data/MinimalEJ.g.etl").toString(); /** * a constructor */ public HelloWorldTermTest() { super(); } /** * a constructor * * @param name * test name */ public HelloWorldTermTest(String name) { super(name); } /** * check for identifier * * @param name * text of identifier */ private void identifier(String name) { this.objectStart(ns, "Identifier"); { propStart("value"); value(name); propEnd("value"); } this.objectEnd(ns, "Identifier"); } /** * check for modifier * * @param name * text of modifier */ private void modifier(String name) { this.objectStart(ns, "Modifier"); { propStart("value"); value(name); propEnd("value"); } this.objectEnd(ns, "Modifier"); } /** * check for documentation line * * @param text * text of documentation line */ private void docline(String text) { this.objectStart(ns, "DocumentationLine"); { propStart("text"); value(text); propEnd("text"); } this.objectEnd(ns, "DocumentationLine"); } /** * read hello world using full grammar * * @throws Exception * in case of error */ public void testFullGrammar() throws Exception { startWithResource("/net/sf/etl/samples/ej/samples/HelloWorld.ej.etl"); walkThrougHelloWorld("\"../grammars/EJ.g.etl\""); } /** * read hello world from URI */ public void testHelloWorld() { startWithResource("/net/sf/etl/tests/data/HelloWorld.ej.etl"); walkThrougHelloWorld("\"MinimalEJ.g.etl\""); } /** * read hello world from URI and reader */ public void testHelloWorldReader() { startWithResourceAsReader("/net/sf/etl/tests/data/HelloWorld.ej.etl"); walkThrougHelloWorld("\"MinimalEJ.g.etl\""); } /** * read hello world * * @param grammarRef * reference to grammar. */ private void walkThrougHelloWorld(String grammarRef) { boolean errorExit = true; try { readDocType(grammarRef, null); readPackageStatement(); readTopLevelClassStatement(); errorExit = false; } finally { endParsing(errorExit); } } /** * Read top level class statement */ private void readTopLevelClassStatement() { this.objectStart(ns, "TopLevelClassifier"); { propStart("classifier"); this.objectStart(ns, "ClassStatement"); { { listStart("documentation"); docline("/// Classical \"Hello, World!\" program."); listEnd("documentation"); } { propStart("visibilityModifier"); modifier("public"); propEnd("visibilityModifier"); } { identifierProp("name", "HelloWorld"); } { this.listStart("contents"); this.objectStart(ns, "MethodStatement"); { { listStart("documentation"); docline("/// Application entry point"); docline("/// @param args application arguments"); listEnd("documentation"); } { listStart("attributeSets"); objectStart(ns, "AttributeSet"); listStart("attributes"); identifier("SampleAttribute"); listEnd("attributes"); objectEnd(ns, "AttributeSet"); listEnd("attributeSets"); } { propStart("staticModifier"); modifier("static"); propEnd("staticModifier"); propStart("visibilityModifier"); modifier("public"); propEnd("visibilityModifier"); } { propStart("returnType"); readPrimitiveType("void"); propEnd("returnType"); identifierProp("name", "main"); listStart("parameters"); this.objectStart(ns, "Parameter"); { propStart("classifier"); this.objectStart(ns, "ApplySquareOp"); { propStart("functor"); readPrimitiveType("array"); propEnd("functor"); this.listStart("args"); identifier("String"); this.listEnd("args"); } this.objectEnd(ns, "ApplySquareOp"); propEnd("classifier"); identifierProp("name", "args"); } this.objectEnd(ns, "Parameter"); listEnd("parameters"); } { this.propStart("body"); this.objectStart(ns, "MethodBlock"); { listStart("content"); this.objectStart(ns, "ExpressionStatement"); { this.propStart("expression"); this.objectStart(ns, "ApplyRoundOp"); { propStart("functor"); this.objectStart(ns, "AccessOp"); { propStart("accessed"); this.objectStart(ns, "AccessOp"); { identifierProp("accessed", "System"); identifierProp("feature", "out"); } this.objectEnd(ns, "AccessOp"); propEnd("accessed"); identifierProp("feature", "println"); } this.objectEnd(ns, "AccessOp"); propEnd("functor"); listStart("args"); this.objectStart(ns, "StringLiteral"); { propStart("value"); value("\"Hello, World!\""); propEnd("value"); } this.objectEnd(ns, "StringLiteral"); listEnd("args"); } this.objectEnd(ns, "ApplyRoundOp"); this.propEnd("expression"); } this.objectEnd(ns, "ExpressionStatement"); listEnd("content"); } this.objectEnd(ns, "MethodBlock"); this.propEnd("body"); } } this.objectEnd(ns, "MethodStatement"); this.listEnd("contents"); } } this.objectEnd(ns, "ClassStatement"); propEnd("classifier"); } this.objectEnd(ns, "TopLevelClassifier"); } /** * Read primitive type * * @param name * a name of primitive type * */ private void readPrimitiveType(String name) { this.objectStart(ns, "PrimitiveType"); { propStart("name"); value(name); propEnd("name"); } this.objectEnd(ns, "PrimitiveType"); } /** * Read package statement * */ private void readPackageStatement() { this.objectStart(ns, "PackageStatement"); { identifierProp("name", "test"); } this.objectEnd(ns, "PackageStatement"); } /** * @param name * a name of property * @param identifer * identifier value * */ private void identifierProp(String name, String identifer) { propStart(name); identifier(identifer); propEnd(name); } /** * Test how default grammar works. Default context of grammar is used. */ public void testDefaultGrammarDefaultContext() { final String text = "package test;"; startWithStringAndDefaultGrammar(text, MinimalEJ_SYSTEM_ID, null, null); boolean errorExit = true; try { { readPackageStatement(); errorExit = false; } } finally { endParsing(errorExit); } } /** * Test how default grammar works. Non default context of grammar is used. */ public void testDefaultGrammarNewContext() { final String text = "test;"; startWithStringAndDefaultGrammar(text, MinimalEJ_SYSTEM_ID, null, "Code"); boolean errorExit = true; try { { this.objectStart(ns, "ExpressionStatement"); { propStart("expression"); identifier("test"); propEnd("expression"); } this.objectEnd(ns, "ExpressionStatement"); errorExit = false; } } finally { endParsing(errorExit); } } /** * Test reparsing doctype */ public void testReparseDoctype() { final TextPos start = TextPos.START; final TextPos end = new TextPos(1, 27, 26); final TermContext context = StandardGrammars.DOCTYPE_CONTEXT; startReparsingResource("/net/sf/etl/tests/data/HelloWorld.ej.etl", context, start, end); boolean errorExit = true; try { readDocType("\"MinimalEJ.g.etl\"", null); errorExit = false; } finally { endParsing(errorExit); } } /** * Test reparsing top level fragments after doctype */ public void testReparseToplevel() { final TextPos start = new TextPos(2, 1, 28); final TextPos end = new TextPos(11, 3, 306); final TermContext context = new TermContext( "net.sf.etl.tests.data.MinimalEJ", "TopLevel", MinimalEJ_SYSTEM_ID); startReparsingResource("/net/sf/etl/tests/data/HelloWorld.ej.etl", context, start, end); boolean errorExit = true; try { readPackageStatement(); readTopLevelClassStatement(); errorExit = false; } finally { endParsing(errorExit); } } }