hqlparsertest.java

来自「好东西,hibernate-3.2.0,他是一开元的树杖hibernate-3.」· Java 代码 · 共 984 行 · 第 1/5 页

JAVA
984
字号
// $Id: HqlParserTest.java 10164 2006-07-26 15:09:20Z steve.ebersole@jboss.com $
package org.hibernate.test.hql;


import antlr.RecognitionException;
import antlr.TokenStreamException;
import antlr.collections.AST;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.hibernate.hql.ast.HqlParser;
import org.hibernate.hql.ast.tree.Node;
import org.hibernate.hql.ast.util.ASTIterator;
import org.hibernate.hql.ast.util.ASTPrinter;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

/**
 * Tests the HQL parser on various inputs, just makes sure that the first phase of the parser
 * works properly (i.e. no unexpected syntax errors).
 */
public class HqlParserTest extends TestCase {

	/**
	 * Standard JUnit test case constructor.
	 *
	 * @param name The name of the test case.
	 */
	public HqlParserTest(String name) {
		super( name );
	}
	
	public void testUnion() throws Exception {
		parse("from Animal a where a in (from Cat union from Dog) ");
	}

	/**
	 * Section 9.2 - from *
	 */
	public void testDocoExamples92() throws Exception {
		parse( "from eg.Cat" );
		parse( "from eg.Cat as cat" );
		parse( "from eg.Cat cat" );
		parse( "from Formula, Parameter" );
		parse( "from Formula as form, Parameter as param" );
	}

	/**
	 * Section 9.3 - Associations and joins *
	 */
	public void testDocoExamples93() throws Exception {
		parse( "from eg.Cat as cat inner join cat.mate as mate left outer join cat.kittens as kitten" );
		parse( "from eg.Cat as cat left join cat.mate.kittens as kittens" );
		parse( "from Formula form full join form.parameter param" );
		parse( "from eg.Cat as cat join cat.mate as mate left join cat.kittens as kitten" );
		parse( "from eg.Cat as cat\ninner join fetch cat.mate\nleft join fetch cat.kittens" );
	}

	/**
	 * Section 9.4 - Select *
	 */
	public void testDocoExamples94() throws Exception {
		parse( "select mate from eg.Cat as cat inner join cat.mate as mate" );
		parse( "select cat.mate from eg.Cat cat" );
		parse( "select elements(cat.kittens) from eg.Cat cat" );
		parse( "select cat.name from eg.DomesticCat cat where cat.name like 'fri%'" );
		parse( "select cust.name.firstName from Customer as cust" );
		parse( "select mother, offspr, mate.name from eg.DomesticCat\n"
				+ " as mother inner join mother.mate as mate left outer join\n"
				+ "mother.kittens as offspr" );
		parse( "select new Family(mother, mate, offspr)\n"
				+ "from eg.DomesticCat as mother\n"
				+ "join mother.mate as mate\n"
				+ "left join mother.kittens as offspr\n" );
	}

	/**
	 * Section 9.5 - Aggregate functions *
	 */
	public void testDocoExamples95() throws Exception {
		parse( "select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat)\n"
				+ "from eg.Cat cat" );
		parse( "select cat, count( elements(cat.kittens) )\n"
				+ " from eg.Cat cat group by cat" );
		parse( "select distinct cat.name from eg.Cat cat" );
		parse( "select count(distinct cat.name), count(cat) from eg.Cat cat" );
	}

	/**
	 * Section 9.6 - Polymorphism *
	 */
	public void testDocoExamples96() throws Exception {
		parse( "from eg.Cat as cat" );
		parse( "from java.lang.Object o" );
		parse( "from eg.Named n, eg.Named m where n.name = m.name" );
	}

	/**
	 * Section 9.7 - Where *
	 */
	public void testDocoExamples97() throws Exception {
		parse( "from eg.Cat as cat where cat.name='Fritz'" );
		parse( "select foo\n"
				+ "from eg.Foo foo, eg.Bar bar\n"
				+ "where foo.startDate = bar.date\n" );
		parse( "from eg.Cat cat where cat.mate.name is not null" );
		parse( "from eg.Cat cat, eg.Cat rival where cat.mate = rival.mate" );
		parse( "select cat, mate\n"
				+ "from eg.Cat cat, eg.Cat mate\n"
				+ "where cat.mate = mate" );
		parse( "from eg.Cat as cat where cat.id = 123" );
		parse( "from eg.Cat as cat where cat.mate.id = 69" );
		parse( "from bank.Person person\n"
				+ "where person.id.country = 'AU'\n"
				+ "and person.id.medicareNumber = 123456" );
		parse( "from bank.Account account\n"
				+ "where account.owner.id.country = 'AU'\n"
				+ "and account.owner.id.medicareNumber = 123456" );
		parse( "from eg.Cat cat where cat.class = eg.DomesticCat" );
		parse( "from eg.AuditLog log, eg.Payment payment\n"
				+ "where log.item.class = 'eg.Payment' and log.item.id = payment.id" );
	}

	/**
	 * Section 9.8 - Expressions *
	 */
	public void testDocoExamples98() throws Exception {
		parse( "from eg.DomesticCat cat where cat.name between 'A' and 'B'" );
		parse( "from eg.DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' )" );
		parse( "from eg.DomesticCat cat where cat.name not between 'A' and 'B'" );
		parse( "from eg.DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )" );
		parse( "from eg.Cat cat where cat.kittens.size > 0" );
		parse( "from eg.Cat cat where size(cat.kittens) > 0" );
// This is a little odd.  I'm not sure whether 'current' is a keyword.
//        parse("from Calendar cal where cal.holidays.maxElement > current date");
// Using the token 'order' as both a keyword and an identifier works now, but
// the second instance causes some problems because order is valid in the second instance.
//        parse("from Order order where maxindex(order.items) > 100");
//        parse("from Order order where minelement(order.items) > 10000");
		parse( "from Order ord where maxindex(ord.items) > 100" );
		parse( "from Order ord where minelement(ord.items) > 10000" );

		parse( "select mother from eg.Cat as mother, eg.Cat as kit\n"
				+ "where kit in elements(foo.kittens)" );
		parse( "select p from eg.NameList list, eg.Person p\n"
				+ "where p.name = some elements(list.names)" );
		parse( "from eg.Cat cat where exists elements(cat.kittens)" );
		parse( "from eg.Player p where 3 > all elements(p.scores)" );
		parse( "from eg.Show show where 'fizard' in indices(show.acts)" );

		// Yet another example of the pathological 'order' token.
//        parse("from Order order where order.items[0].id = 1234");
//        parse("select person from Person person, Calendar calendar\n"
//        + "where calendar.holidays['national day'] = person.birthDay\n"
//        + "and person.nationality.calendar = calendar");
//        parse("select item from Item item, Order order\n"
//        + "where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11");
//        parse("select item from Item item, Order order\n"
//        + "where order.items[ maxindex(order.items) ] = item and order.id = 11");

		parse( "from Order ord where ord.items[0].id = 1234" );
		parse( "select person from Person person, Calendar calendar\n"
				+ "where calendar.holidays['national day'] = person.birthDay\n"
				+ "and person.nationality.calendar = calendar" );
		parse( "select item from Item item, Order ord\n"
				+ "where ord.items[ ord.deliveredItemIndices[0] ] = item and ord.id = 11" );
		parse( "select item from Item item, Order ord\n"
				+ "where ord.items[ maxindex(ord.items) ] = item and ord.id = 11" );

		parse( "select item from Item item, Order ord\n"
				+ "where ord.items[ size(ord.items) - 1 ] = item" );

		parse( "from eg.DomesticCat cat where upper(cat.name) like 'FRI%'" );

		parse( "select cust from Product prod, Store store\n"
				+ "inner join store.customers cust\n"
				+ "where prod.name = 'widget'\n"
				+ "and store.location.name in ( 'Melbourne', 'Sydney' )\n"
				+ "and prod = all elements(cust.currentOrder.lineItems)" );

	}

	public void testDocoExamples99() throws Exception {
		parse( "from eg.DomesticCat cat\n"
				+ "order by cat.name asc, cat.weight desc, cat.birthdate" );
	}

	public void testDocoExamples910() throws Exception {
		parse( "select cat.color, sum(cat.weight), count(cat)\n"
				+ "from eg.Cat cat group by cat.color" );
		parse( "select foo.id, avg( elements(foo.names) ), max( indices(foo.names) )\n"
				+ "from eg.Foo foo group by foo.id" );
		parse( "select cat.color, sum(cat.weight), count(cat)\n"
				+ "from eg.Cat cat group by cat.color\n"
				+ "having cat.color in (eg.Color.TABBY, eg.Color.BLACK)" );
		parse( "select cat from eg.Cat cat join cat.kittens kitten\n"

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?