/*
 * 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);
	}
	
	QueryItem left1, left2, leftX;
	SuperContainer middle1, middle2, middleX;
	SubContainer right1, right2;
	
	@Override
	public void setUp() throws Exception
	{
		super.setUp();
		
		deleteOnTearDown(leftX = new QueryItem("leftX"));
		deleteOnTearDown(left1 = new QueryItem("left1"));
		deleteOnTearDown(left2 = new QueryItem("left2"));
		deleteOnTearDown(middleX = new SuperContainer("middleX", leftX, false));
		deleteOnTearDown(middle1 = new SuperContainer("middle1", left1, false));
		deleteOnTearDown(middle2 = new SubContainer("middle2", left2, false, middleX));
		deleteOnTearDown(right1 = new SubContainer("right1", leftX, false, middle1));
		deleteOnTearDown(right2 = new SubContainer("right2", leftX, false, middle2));
	}
	
	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(leftX, left1, query.search());
		}
		{
			// with specifying join but without condition
			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));
			assertContains(leftX, left1, left2, 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(RuntimeException e)
			{
				assertEquals("feature SuperContainer#class is ambiguous, use JoinedFunction", e.getMessage());
			}
		}
	}
	
}

@@ -2063,130 +2063,8 @@
 %0A%09%7B%0A
-%09%09if(oracle)%0A%09%09%09return;%0A%09%09%0A%09%09//System.out.println(%22----------%22+model.getDatabaseInfo().getProperty(%22database.version%22));%0A%0A
 %09%09%7B%0A

enable on oracle