/*
 * Copyright (C) 2004-2006  exedio GmbH (www.exedio.com)
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.badquery;

import com.exedio.cope.AbstractLibTest;
import com.exedio.cope.Join;
import com.exedio.cope.JoinedItemFunction;
import com.exedio.cope.Model;
import com.exedio.cope.Query;
import com.exedio.cope.Type;
import com.exedio.dsmf.SQLRuntimeException;

public class BadQueryTest extends AbstractLibTest
{
	static final Model model = new Model(new Type[]{SuperItem.TYPE, QueryItem.TYPE, SuperContainer.TYPE, SubContainer.TYPE});
	
	public BadQueryTest()
	{
		super(model);
	}
	
	public void testIt()
	{
		if(oracle)
			return;
		
		//System.out.println("----------"+model.getDatabaseInfo().getProperty("database.version"));

		{
			// with specifying join
			final Query<QueryItem> query = QueryItem.TYPE.newQuery(null);
			final Join superJoin = query.join(SuperContainer.TYPE);
			superJoin.setCondition(new JoinedItemFunction<QueryItem>(SuperContainer.queryItem, superJoin).equalTarget());
			query.join(SubContainer.TYPE, SubContainer.superContainer.equalTarget(superJoin));
			query.setCondition(new JoinedItemFunction<SuperContainer>(SuperContainer.TYPE.getThis(), superJoin).typeNotIn(SubContainer.TYPE));
			assertContains(query.search());
		}
		
		{
			// without specifying join
			final Query<QueryItem> query = QueryItem.TYPE.newQuery(null);
			final Join superJoin = query.join(SuperContainer.TYPE);
			superJoin.setCondition(new JoinedItemFunction<QueryItem>(SuperContainer.queryItem, superJoin).equalTarget());
			query.join(SubContainer.TYPE, SubContainer.superContainer.equalTarget(superJoin));
			query.setCondition(SuperContainer.TYPE.getThis().typeNotIn(SubContainer.TYPE));
			try
			{
				query.search();
				fail();
			}
			catch(SQLRuntimeException e)
			{
				assertTrue(e.getMessage(), e.getMessage().startsWith("select "));
				if(hsqldb)
					assertTrue(e.getCause().getMessage(), e.getCause().getMessage().startsWith("Column not found: SuperContainer.class in statement [select "));
				else
					assertEquals(
							model.getDatabaseInfo().getProperty("database.version").endsWith("(5.0)")
							? "Unknown column 'SuperContainer.class' in 'where clause'"
							: "Unknown table 'SuperContainer' in where clause",
							e.getCause().getMessage());
			}
		}
	}
	
}

@@ -1302,16 +1302,748 @@
 );%0A%09%7D%0A%09%0A
+%09QueryItem left1, left2, leftX;%0A%09SuperContainer middle1, middle2, middleX;%0A%09SubContainer right1, right2;%0A%09%0A%09@Override%0A%09public void setUp() throws Exception%0A%09%7B%0A%09%09super.setUp();%0A%09%09%0A%09%09deleteOnTearDown(leftX = new QueryItem(%22leftX%22));%0A%09%09deleteOnTearDown(left1 = new QueryItem(%22left1%22));%0A%09%09deleteOnTearDown(left2 = new QueryItem(%22left2%22));%0A%09%09deleteOnTearDown(middleX = new SuperContainer(%22middleX%22, leftX, false));%0A%09%09deleteOnTearDown(middle1 = new SuperContainer(%22middle1%22, left1, false));%0A%09%09deleteOnTearDown(middle2 = new SubContainer(%22middle2%22, left2, false, middleX));%0A%09%09deleteOnTearDown(right1 = new SubContainer(%22right1%22, leftX, false, middle1));%0A%09%09deleteOnTearDown(right2 = new SubContainer(%22right2%22, leftX, false, middle2));%0A%09%7D%0A%09%0A
 %09public 
@@ -2687,16 +2687,466 @@
 ontains(
+leftX, left1, query.search());%0A%09%09%7D%0A%09%09%7B%0A%09%09%09// with specifying join but without condition%0A%09%09%09final Query%3CQueryItem%3E query = QueryItem.TYPE.newQuery(null);%0A%09%09%09final Join superJoin = query.join(SuperContainer.TYPE);%0A%09%09%09superJoin.setCondition(new JoinedItemFunction%3CQueryItem%3E(SuperContainer.queryItem, superJoin).equalTarget());%0A%09%09%09query.join(SubContainer.TYPE, SubContainer.superContainer.equalTarget(superJoin));%0A%09%09%09assertContains(leftX, left1, left2, 
 query.se

add data to be found by test