1 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 16 public class ASTUtilTest extends TestCase { 17 private ASTFactory factory; 18 19 24 public ASTUtilTest(String name) { 25 super( name ); 26 } 27 28 protected void setUp() throws Exception { 29 super.setUp(); 30 factory = new ASTFactory(); 31 } 32 33 public void testCreate() throws Exception { 34 AST n = ASTUtil.create( factory, 1, "one"); 35 assertNull( n.getFirstChild() ); 36 assertEquals("one",n.getText()); 37 assertEquals(1,n.getType()); 38 } 39 42 public void testCreateTree() throws Exception { 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 { 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 e = null; 65 try { 66 ASTUtil.findPreviousSibling(child1,null); 67 } 68 catch (Exception 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 |