/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.query.jpql.internal;

import junit.framework.TestCase;
import org.xwiki.query.jpql.internal.JPQLParser;

public class JPQLParserTest extends TestCase
{
    JPQLParser parser = new JPQLParser();

    public void testStartSpeed() throws Exception
    {
        // heat up parser engine for more accuracy test timing
        // this is needed because of much static data in the parser
        parser.parse("select a from A as a");
    }

    public void testJPQL() throws Exception
    {
        // quotes
        parser.parse("select a from A as a where a.f='str'");
        parser.parse("select a from A as a where a.f=\"str\"");

        // order by
        parser.parse("select doc from Document doc, doc.object(XWiki.XWikiGroups) as g order by g.number");
        parser.parse("select doc from Document doc, doc.object(XWiki.XWikiGroups) as g order by g.number desc");
        parser.parse("select doc from Document doc, doc.object(XWiki.XWikiGroups) as g order by g.number asc");

        // member of
        parser.parse("select a from A as a where :param member of a.prop");

        // input parameters
        parser.parse("select a from A as a where a.p = ?1 or :name = a.p or ?2 member of a.p or a.p like :qwe");
    }

    public void testXWQLExtensions() throws Exception
    {
        // object() in from clause
        parser.parse("select doc from Document as doc, doc.object('XWiki.Test') as test where test.some=1");
        parser.parse("select doc from Document as doc, doc.object(XWiki.Test) as test where test.some=1");

        // object() in where clause
        parser.parse("select doc from Document as doc where doc.object('XWiki.Test').prop=1");
        parser.parse("select doc from Document as doc where doc.object(XWiki.Test).prop=1");
    }
}

/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.query.jpql.internal;

import junit.framework.TestCase;

public class JPQLParserTest extends TestCase
{
    JPQLParser parser = new JPQLParser();

    public void testStartSpeed() throws Exception
    {
        // heat up parser engine for more accuracy test timing
        // this is needed because of much static data in the parser
        parser.parse("select a from A as a");
    }

    public void testJPQL() throws Exception
    {
        // quotes
        parser.parse("select a from A as a where a.f='str'");
        parser.parse("select a from A as a where a.f=\"str\"");

        // order by
        parser.parse("select doc from Document doc, doc.object(XWiki.XWikiGroups) as g order by g.number");
        parser.parse("select doc from Document doc, doc.object(XWiki.XWikiGroups) as g order by g.number desc");
        parser.parse("select doc from Document doc, doc.object(XWiki.XWikiGroups) as g order by g.number asc");

        // Aggregate function in order by
        parser.parse("select g.name, count(g.number) from Document doc, doc.object(XWiki.XWikiGroups) as g "
                   + "group by g.name order by count(g.number)");

        // member of
        parser.parse("select a from A as a where :param member of a.prop");

        // input parameters
        parser.parse("select a from A as a where a.p = ?1 or :name = a.p or ?2 member of a.p or a.p like :qwe");
    }

    public void testXWQLExtensions() throws Exception
    {
        // object() in from clause
        parser.parse("select doc from Document as doc, doc.object('XWiki.Test') as test where test.some=1");
        parser.parse("select doc from Document as doc, doc.object(XWiki.Test) as test where test.some=1");

        // object() in where clause
        parser.parse("select doc from Document as doc where doc.object('XWiki.Test').prop=1");
        parser.parse("select doc from Document as doc where doc.object(XWiki.Test).prop=1");
    }
}

ORDER BY does not work with XWQL with aggregate functions.