KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > pencil > test > el > TemplateParserTestCase


1 /*
2  * Copyright 2004 Andreas Siebert (j2biz community)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7
8  * http://www.apache.org/licenses/LICENSE-2.0
9
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.j2biz.pencil.test.el;
17
18
19 import junit.framework.TestCase;
20 import antlr.collections.AST;
21
22 import com.j2biz.pencil.el.TemplateTreeParser;
23 import com.j2biz.pencil.el.TemplateTreeParserTokenTypes;
24
25
26 /**
27  * @author andrej
28  *
29  * To change the template for this generated type comment go to
30  * Window - Preferences - Java - Code Generation - Code and Comments
31  */

32 public class TemplateParserTestCase extends TestCase {
33
34     /**
35      * Dieser Abschnitt testet den Einsatz von einfachen Referenzen.
36      */

37     public void testLengthNull() {
38         TemplateTreeParser parser = TemplateParserFactory.createParser( "" );
39         try {
40             parser.parseStart();
41         } catch (Exception JavaDoc e) {
42             fail( "No exception expected." );
43         }
44     }
45     
46     public void testSimpleRef() {
47         TemplateTreeParser parser = TemplateParserFactory.createParser( "textBefore${idName.idName2.parameter}afterText" );
48         try {
49             parser.parseStart();
50         } catch (Exception JavaDoc e) {
51             e.printStackTrace();
52             fail( "No exception expected." );
53         }
54     }
55     
56     public void testSecondIdBeginsWithDigit() {
57         TemplateTreeParser parser = TemplateParserFactory.createParser( "textBefore${idName.1idName2}afterText" );
58         try {
59             parser.parseStart();
60             fail( "exception expected." );
61         } catch (Exception JavaDoc e) {
62             ;
63         }
64     }
65     
66     public void testNoClosedBracket() {
67         TemplateTreeParser parser = TemplateParserFactory.createParser( "textBefore${idName.idName2{.parameter}afterText" );
68         try {
69             parser.parseStart();
70             fail( "exception expected." );
71         } catch (Exception JavaDoc e) {
72             ;
73         }
74     }
75     
76     
77     public void testASTSimpleConstruction() {
78         TemplateTreeParser parser = TemplateParserFactory.createParser( "textBefore${idName}afterText" );
79         try {
80             parser.parseStart();
81             final AST root = parser.getAST();
82             assertEquals("root correct", root.getType(), TemplateTreeParserTokenTypes.TEMPLATE);
83             assertEquals("number of elements", root.getNumberOfChildren(), 3);
84             
85             final AST before = root.getFirstChild();
86             assertEquals("before text node", before.getType(), TemplateTreeParserTokenTypes.TEXT);
87             final AST reference = before.getNextSibling();
88             assertEquals("reference", reference.getType(), TemplateTreeParserTokenTypes.REFERENCE);
89             final AST after = reference.getNextSibling();
90             assertEquals("after", after.getType(), TemplateTreeParserTokenTypes.TEXT);
91             
92             final AST idName = reference.getFirstChild();
93             assertEquals("first child", idName.getText(), "idName");
94
95         } catch (Exception JavaDoc e) {
96             e.printStackTrace();
97             fail( "No exception expected.");
98         }
99     }
100     
101     public void testASTSimple2Construction() {
102         TemplateTreeParser parser = TemplateParserFactory.createParser( "textBefore${idName.idName.idName2}afterText" );
103         try {
104             parser.parseStart();
105             final AST root = parser.getAST();
106             assertEquals("root correct", root.getType(), TemplateTreeParserTokenTypes.TEMPLATE);
107             assertEquals("number of elements", root.getNumberOfChildren(), 3);
108             
109             final AST before = root.getFirstChild();
110             assertEquals("before text node", before.getType(), TemplateTreeParserTokenTypes.TEXT);
111             final AST reference = before.getNextSibling();
112             assertEquals("reference", reference.getType(), TemplateTreeParserTokenTypes.REFERENCE);
113             final AST after = reference.getNextSibling();
114             assertEquals("after", after.getType(), TemplateTreeParserTokenTypes.TEXT);
115             
116             final AST idName = reference.getFirstChild();
117             assertEquals("first child", idName.getText(), "idName");
118             final AST idNameChild = idName.getFirstChild();
119             assertEquals("next child but name == idName", idNameChild.getText(), "idName" );
120             final AST idName2 = idNameChild.getFirstChild();
121             assertEquals("last child: idName2", idName2.getText(), "idName2");
122         } catch (Exception JavaDoc e) {
123             e.printStackTrace();
124             fail( "No exception expected.");
125         }
126     }
127     
128     public void testASTScope() {
129         TemplateTreeParser parser = TemplateParserFactory.createParser( "textBefore${(TestScope):idName.(com.TestScope2):idName2}afterText" );
130         try {
131             parser.parseStart();
132             final AST root = parser.getAST();
133             assertEquals("root correct", root.getType(), TemplateTreeParserTokenTypes.TEMPLATE);
134             assertEquals("number of elements", root.getNumberOfChildren(), 3);
135             
136             final AST before = root.getFirstChild();
137             assertEquals("before text node", before.getType(), TemplateTreeParserTokenTypes.TEXT);
138             final AST reference = before.getNextSibling();
139             assertEquals("reference", reference.getType(), TemplateTreeParserTokenTypes.REFERENCE);
140             final AST after = reference.getNextSibling();
141             assertEquals("after", after.getType(), TemplateTreeParserTokenTypes.TEXT);
142             
143             final AST idName = reference.getFirstChild();
144             assertEquals("first child", idName.getText(), "idName");
145             
146             final AST scope = idName.getFirstChild();
147             assertNotNull("scope should be not NULL:", scope);
148             assertEquals("token type is SCOPE:", scope.getType(), TemplateTreeParserTokenTypes.SCOPE);
149             assertEquals("scope is setted:", scope.getText(), "TestScope");
150             
151             final AST idName2 = scope.getNextSibling();
152             assertNotNull("next part of id must be not NULL ", idName2);
153             assertEquals("id-name == idName2", idName2.getText(), "idName2" );
154             
155             final AST scope2firstPart = idName2.getFirstChild();
156             assertNotNull("first part of the second scope must be not NULL", scope2firstPart );
157             assertEquals("id == com", scope2firstPart.getText(),"com");
158             assertEquals("type == scope", scope2firstPart.getType(), TemplateTreeParserTokenTypes.SCOPE);
159             
160             final AST scope2secondPart = scope2firstPart.getFirstChild();
161             assertNotNull( "second part of the second scope must be not NULL", scope2secondPart);
162             assertEquals("id == TestScope2", scope2secondPart.getText(), "TestScope2" );
163             
164         } catch (Exception JavaDoc e) {
165             e.printStackTrace();
166             fail( "No exception expected.");
167         }
168     }
169     
170     
171     
172     
173     public void testASTScopeWithWhiteSpaces() {
174         TemplateTreeParser parser = TemplateParserFactory.createParser( "textBefore${ ( TestScope ) : idName . ( com . TestScope2 ) : idName2 }afterText" );
175         try {
176             parser.parseStart();
177             final AST root = parser.getAST();
178             assertEquals("root correct", root.getType(), TemplateTreeParserTokenTypes.TEMPLATE);
179             assertEquals("number of elements", root.getNumberOfChildren(), 3);
180             
181             final AST before = root.getFirstChild();
182             assertEquals("before text node", before.getType(), TemplateTreeParserTokenTypes.TEXT);
183             final AST reference = before.getNextSibling();
184             assertEquals("reference", reference.getType(), TemplateTreeParserTokenTypes.REFERENCE);
185             final AST after = reference.getNextSibling();
186             assertEquals("after", after.getType(), TemplateTreeParserTokenTypes.TEXT);
187             
188             final AST idName = reference.getFirstChild();
189             assertEquals("first child", idName.getText(), "idName");
190             
191             final AST scope = idName.getFirstChild();
192             assertNotNull("scope should be not NULL:", scope);
193             assertEquals("token type is SCOPE:", scope.getType(), TemplateTreeParserTokenTypes.SCOPE);
194             assertEquals("scope is setted:", scope.getText(), "TestScope");
195             
196             final AST idName2 = scope.getNextSibling();
197             assertNotNull("next part of id must be not NULL ", idName2);
198             assertEquals("id-name == idName2", idName2.getText(), "idName2" );
199             
200             final AST scope2firstPart = idName2.getFirstChild();
201             assertNotNull("first part of the second scope must be not NULL", scope2firstPart );
202             assertEquals("id == com", scope2firstPart.getText(),"com");
203             assertEquals("type == scope", scope2firstPart.getType(), TemplateTreeParserTokenTypes.SCOPE);
204             
205             final AST scope2secondPart = scope2firstPart.getFirstChild();
206             assertNotNull( "second part of the second scope must be not NULL", scope2secondPart);
207             assertEquals("id == TestScope2", scope2secondPart.getText(), "TestScope2" );
208             
209         } catch (Exception JavaDoc e) {
210             e.printStackTrace();
211             fail( "No exception expected.");
212         }
213     }
214     
215     
216 //
217
//
218
// public void testASTScopeCheckJavaBeginningNameChars() {
219
//// Character.isJavaIdentifierStart()
220
// TemplateTreeParser parser = TemplateParserFactory.createParser( "textBefore${ ( ÜberScope ) : äTidName . ( com . öTestScope2 ) : ?dName2 }afterText" );
221
// try {
222
// parser.parseStart();
223
// final AST root = parser.getAST();
224
// assertEquals("root correct", root.getType(), TemplateTreeParserTokenTypes.TEMPLATE);
225
// assertEquals("number of elements", root.getNumberOfChildren(), 3);
226
//
227
// final AST before = root.getFirstChild();
228
// assertEquals("before text node", before.getType(), TemplateTreeParserTokenTypes.TEXT);
229
// final AST reference = before.getNextSibling();
230
// assertEquals("reference", reference.getType(), TemplateTreeParserTokenTypes.REFERENCE);
231
// final AST after = reference.getNextSibling();
232
// assertEquals("after", after.getType(), TemplateTreeParserTokenTypes.TEXT);
233
//
234
// final AST idName = reference.getFirstChild();
235
// assertEquals("first child", idName.getText(), "idName");
236
//
237
// final AST scope = idName.getFirstChild();
238
// assertNotNull("scope should be not NULL:", scope);
239
// assertEquals("token type is SCOPE:", scope.getType(), TemplateTreeParserTokenTypes.SCOPE);
240
// assertEquals("scope is setted:", scope.getText(), "TestScope");
241
//
242
// final AST idName2 = scope.getNextSibling();
243
// assertNotNull("next part of id must be not NULL ", idName2);
244
// assertEquals("id-name == idName2", idName2.getText(), "idName2" );
245
//
246
// final AST scope2firstPart = idName2.getFirstChild();
247
// assertNotNull("first part of the second scope must be not NULL", scope2firstPart );
248
// assertEquals("id == com", scope2firstPart.getText(),"com");
249
// assertEquals("type == scope", scope2firstPart.getType(), TemplateTreeParserTokenTypes.SCOPE);
250
//
251
// final AST scope2secondPart = scope2firstPart.getFirstChild();
252
// assertNotNull( "second part of the second scope must be not NULL", scope2secondPart);
253
// assertEquals("id == TestScope2", scope2secondPart.getText(), "TestScope2" );
254
//
255
// } catch (Exception e) {
256
// e.printStackTrace();
257
// fail( "No exception expected.");
258
// }
259
// }
260

261 }
262
Popular Tags