/*
 * 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;

import java.util.Iterator;

import com.exedio.cope.junit.CopeAssert;
import com.exedio.dsmf.Column;
import com.exedio.dsmf.Schema;
import com.exedio.dsmf.Table;

public class MigrationTest extends CopeAssert
{
	private static final Model model1 = new Model(0, null, MigrationItem1.TYPE);
	
	static final MigrationStep[] steps2 = new MigrationStep[]{
		new MigrationStep(1, "add column field2", "alter table \"MigrationItem\" add column \"field2\" varchar(100)"),
	};
	
	private static final Model model2 = new Model(1, steps2, MigrationItem2.TYPE);
	
	public void test()
	{
		final Properties props = new Properties();
		
		model1.connect(props);
		if(!"HsqldbDatabase".equals(model1.getDatabase().getClass().getSimpleName())) // TODO remove
			return;
		model1.createDatabase();

		assertSchema(model1.getVerifiedSchema(), false, false);
		model1.disconnect();
		
		model2.connect(props);
		assertSchema(model2.getVerifiedSchema(), true, false);
		
		model2.migrate();
		assertSchema(model2.getVerifiedSchema(), true, true);
		
		model2.tearDownDatabase();
	}
	
	private void assertSchema(final Schema schema, final boolean model2, final boolean migrated)
	{
		final Iterator<Table> tables = schema.getTables().iterator();
		
		final Table table = tables.next();
		assertEquals("MigrationItem", table.getName());
		assertEquals(true, table.required());
		assertEquals(true, table.exists());
		final Iterator<Column> columns = table.getColumns().iterator();

		final Column columnThis = columns.next();
		assertEquals("this", columnThis.getName());
		assertEquals(true, columnThis.required());
		assertEquals(true, columnThis.exists());
		assertNotNull(columnThis.getType());
		
		final Column column1 = columns.next();
		assertEquals("field1", column1.getName());
		assertEquals(true, column1.required());
		assertEquals(true, column1.exists());
		assertNotNull(column1.getType());
		
		if(model2)
		{
			final Column column2 = columns.next();
			assertEquals("field2", column2.getName());
			assertEquals(true, column2.required());
			assertEquals(migrated, column2.exists());
			assertNotNull(column2.getType());
		}
		
		assertFalse(columns.hasNext());
		
		final Table migrationTable = tables.next();
		assertEquals("while", migrationTable.getName());
		assertEquals(true, migrationTable.required());
		assertEquals(true, migrationTable.exists());
		
		assertFalse(tables.hasNext());
	}
}

@@ -1995,30 +1995,19 @@
 nal 
-Iterator%3C
 Table
-%3E
  table
-s
  = s
@@ -2024,61 +2024,24 @@
 able
-s().iterator();%0A%09%09%0A%09%09final Table table = tables.next(
+(%22MigrationItem%22
 );%0A%09
@@ -2970,20 +2970,31 @@
 e = 
-tables.next(
+schema.getTable(%22while%22
 );%0A%09
@@ -3143,44 +3143,8 @@
 ));%0A
-%09%09%0A%09%09assertFalse(tables.hasNext());%0A
 %09%7D%0A%7D

fix test, so it works in schemata with tables from other tests