KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > ast > ASTIteratorTest


1 // $Id: ASTIteratorTest.java,v 1.9 2005/07/12 20:49:25 oneovthafew Exp $
2
package org.hibernate.test.ast;
3
4 import java.io.PrintWriter JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9
10 import org.hibernate.hql.antlr.HqlTokenTypes;
11 import org.hibernate.hql.ast.HqlParser;
12 import org.hibernate.hql.ast.util.ASTIterator;
13 import org.hibernate.hql.ast.util.ASTParentsFirstIterator;
14 import org.hibernate.hql.ast.util.ASTPrinter;
15 import org.hibernate.hql.ast.util.ASTUtil;
16
17 import antlr.ASTFactory;
18 import antlr.collections.AST;
19
20 /**
21  * Test ASTIterator.
22  */

23 public class ASTIteratorTest extends TestCase {
24     private ASTFactory factory;
25
26     /**
27      * Standard JUnit test case constructor.
28      *
29      * @param name The name of the test case.
30      */

31     public ASTIteratorTest(String JavaDoc name) {
32         super( name );
33     }
34
35     public static Test suite() {
36         return new TestSuite( ASTIteratorTest.class );
37     }
38
39     protected void setUp() throws Exception JavaDoc {
40         super.setUp();
41         factory = new ASTFactory();
42     }
43
44     /**
45      * Test a simple tree, make sure the iterator encounters every node.
46      */

47     public void testSimpleTree() throws Exception JavaDoc {
48         String JavaDoc input = "select foo from foo in class org.hibernate.test.Foo, fee in class org.hibernate.test.Fee where foo.dependent = fee order by foo.string desc, foo.component.count asc, fee.id";
49         HqlParser parser = HqlParser.getInstance( input );
50         parser.statement();
51         AST ast = parser.getAST();
52         ASTPrinter printer = new ASTPrinter( HqlTokenTypes.class );
53         printer.showAst( ast, new PrintWriter JavaDoc( System.out ) );
54         ASTIterator iterator = new ASTIterator( ast );
55         int count = 0;
56         while ( iterator.hasNext() ) {
57             assertTrue( iterator.next() instanceof AST );
58             count++;
59         }
60         assertEquals( 43, count );
61
62         UnsupportedOperationException JavaDoc uoe = null;
63         try {
64             iterator.remove();
65         }
66         catch ( UnsupportedOperationException JavaDoc e ) {
67             uoe = e;
68         }
69         assertNotNull( uoe );
70     }
71
72     public void testParentsFirstIterator() throws Exception JavaDoc {
73         AST[] tree = new AST[4];
74         AST grandparent = tree[0] = ASTUtil.create( factory, 1, "grandparent" );
75         AST parent = tree[1] = ASTUtil.create( factory, 2, "parent" );
76         AST child = tree[2] = ASTUtil.create( factory, 3, "child" );
77         AST baby = tree[3] = ASTUtil.create( factory, 4, "baby" );
78         AST t = ASTUtil.createTree( factory, tree );
79         AST brother = ASTUtil.create( factory, 10, "brother" );
80         child.setNextSibling( brother );
81         AST sister = ASTUtil.create( factory, 11, "sister" );
82         brother.setNextSibling( sister );
83         AST uncle = factory.make( new AST[]{
84             factory.create( 20, "uncle" ),
85             factory.create( 21, "cousin1" ),
86             factory.create( 22, "cousin2" ),
87             factory.create( 23, "cousin3" )} );
88         parent.setNextSibling( uncle );
89         System.out.println( t.toStringTree() );
90
91         System.out.println( "--- ASTParentsFirstIterator ---" );
92         ASTParentsFirstIterator iter = new ASTParentsFirstIterator( t );
93         int count = 0;
94         while ( iter.hasNext() ) {
95             AST n = iter.nextNode();
96             count++;
97             System.out.println( n );
98         }
99         assertEquals( 10, count );
100
101         System.out.println( "--- ASTIterator ---" );
102         ASTIterator iter2 = new ASTIterator( t );
103         int count2 = 0;
104         while ( iter2.hasNext() ) {
105             AST n = iter2.nextNode();
106             count2++;
107             System.out.println( n );
108         }
109         assertEquals( 10, count2 );
110
111         System.out.println( "--- ASTParentsFirstIterator (parent) ---" );
112         ASTParentsFirstIterator iter3 = new ASTParentsFirstIterator( parent );
113         int count3 = 0;
114         while ( iter3.hasNext() ) {
115             AST n = iter3.nextNode();
116             count3++;
117             System.out.println( n );
118         }
119         assertEquals( 5, count3 );
120     }
121 }
122
Popular Tags