package org.openlca.geo.io;

import java.io.File;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.openlca.core.database.IDatabase;
import org.openlca.core.database.LocationDao;
import org.openlca.core.database.NativeSql;
import org.openlca.core.database.derby.DerbyDatabase;
import org.openlca.core.database.upgrades.Upgrades;

import com.google.common.io.Resources;

public class MultiKmlImportTest {

	private IDatabase getDatabase() {
		String tmpDirPath = System.getProperty("java.io.tmpdir");
		String dbName = "olca_test_db_1.4";
		File tmpDir = new File(tmpDirPath);
		File folder = new File(tmpDir, dbName);
		IDatabase derbyDatabase = new DerbyDatabase(folder);
		try {
			// (currently) it should be always possible to run the database
			// updates on databases that were already updated as the
			// updated should check if an update is necessary or not. Thus
			// we reset the version here and test if the updates work.
			String versionReset = "update openlca_version set version = 1";
			NativeSql.on(derbyDatabase).runUpdate(versionReset);
			Upgrades.runUpgrades(derbyDatabase);
		} catch (Exception e) {
			throw new RuntimeException("DB-upgrades failed", e);
		}
		return derbyDatabase;
	}

	@Test
	public void testImport() throws Exception {
		IDatabase database = getDatabase();
		LocationDao dao = new LocationDao(database);
		Assert.assertEquals(0, dao.getAll().size());
		String kml = IOUtils.toString(Resources.getResource("org/openlca/geo/US_counties-clipped.shp.kml"), "utf-8");
		MultiKmlImport parser = new MultiKmlImport(database, kml);
		parser.parseAndInsert();
		Assert.assertEquals(703, dao.getAll().size());
	}

}

@@ -26,30 +26,8 @@
 o;%0A%0A
-import java.io.File;%0A%0A
 impo
@@ -202,52 +202,8 @@
 ao;%0A
-import org.openlca.core.database.NativeSql;%0A
 impo
@@ -255,60 +255,8 @@
 ase;
-%0Aimport org.openlca.core.database.upgrades.Upgrades;
 %0A%0Aim
@@ -332,816 +332,8 @@
  %7B%0A%0A
-%09private IDatabase getDatabase() %7B%0A%09%09String tmpDirPath = System.getProperty(%22java.io.tmpdir%22);%0A%09%09String dbName = %22olca_test_db_1.4%22;%0A%09%09File tmpDir = new File(tmpDirPath);%0A%09%09File folder = new File(tmpDir, dbName);%0A%09%09IDatabase derbyDatabase = new DerbyDatabase(folder);%0A%09%09try %7B%0A%09%09%09// (currently) it should be always possible to run the database%0A%09%09%09// updates on databases that were already updated as the%0A%09%09%09// updated should check if an update is necessary or not. Thus%0A%09%09%09// we reset the version here and test if the updates work.%0A%09%09%09String versionReset = %22update openlca_version set version = 1%22;%0A%09%09%09NativeSql.on(derbyDatabase).runUpdate(versionReset);%0A%09%09%09Upgrades.runUpgrades(derbyDatabase);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09throw new RuntimeException(%22DB-upgrades failed%22, e);%0A%09%09%7D%0A%09%09return derbyDatabase;%0A%09%7D%0A%0A
 %09@Te
@@ -407,19 +407,21 @@
 e = 
-get
+Derby
 Database
 ();%0A
@@ -416,16 +416,31 @@
 Database
+.createInMemory
 ();%0A%09%09Lo
@@ -583,16 +583,21 @@
 esource(
+%0A%09%09%09%09
 %22org/ope
@@ -635,17 +635,21 @@
 p.kml%22),
- 
+%0A%09%09%09%09
 %22utf-8%22)

use in-memory DB for MultiKmlImportTest