KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: ASTUtilTest.java,v 1.5 2005/07/12 20:49:25 oneovthafew Exp $
2
package org.hibernate.test.ast;
3
4 import junit.framework.Test;
5 import junit.framework.TestCase;
6 import junit.framework.TestSuite;
7
8 import org.hibernate.hql.ast.util.ASTUtil;
9
10 import antlr.ASTFactory;
11 import antlr.collections.AST;
12
13 /**
14  * Unit test for ASTUtil.
15  */

16 public class ASTUtilTest extends TestCase {
17     private ASTFactory factory;
18
19     /**
20      * Standard JUnit test case constructor.
21      *
22      * @param name The name of the test case.
23      */

24     public ASTUtilTest(String JavaDoc name) {
25         super( name );
26     }
27
28     protected void setUp() throws Exception JavaDoc {
29         super.setUp();
30         factory = new ASTFactory();
31     }
32
33     public void testCreate() throws Exception JavaDoc {
34         AST n = ASTUtil.create( factory, 1, "one");
35         assertNull( n.getFirstChild() );
36         assertEquals("one",n.getText());
37         assertEquals(1,n.getType());
38     }
39     /**
40      * Test adding a tree of children.
41      */

42     public void testCreateTree() throws Exception JavaDoc {
43         AST[] tree = new AST[4];
44         AST grandparent = tree[0] = ASTUtil.create(factory, 1, "grandparent");
45         AST parent = tree[1] = ASTUtil.create(factory,2,"parent");
46         AST child = tree[2] = ASTUtil.create(factory,3,"child");
47         AST baby = tree[3] = ASTUtil.create(factory,4,"baby");
48         AST t = ASTUtil.createTree( factory, tree);
49         assertSame(t,grandparent);
50         assertSame(parent,t.getFirstChild());
51         assertSame(child,t.getFirstChild().getFirstChild());
52         assertSame(baby,t.getFirstChild().getFirstChild().getFirstChild());
53     }
54
55     public void testFindPreviousSibling() throws Exception JavaDoc {
56         AST child1 = ASTUtil.create(factory,2, "child1");
57         AST child2 = ASTUtil.create(factory,3, "child2");
58         AST n = factory.make( new AST[] {
59             ASTUtil.create(factory, 1, "parent"),
60             child1,
61             child2,
62         });
63         assertSame(child1,ASTUtil.findPreviousSibling( n,child2));
64         Exception JavaDoc e = null;
65         try {
66             ASTUtil.findPreviousSibling(child1,null);
67         }
68         catch (Exception JavaDoc x) {
69             e = x;
70         }
71         assertNotNull(e);
72     }
73
74     public static Test suite() {
75         return new TestSuite( ASTUtilTest.class );
76     }
77
78 }
79
Popular Tags