/*
 * This is part of the Ktunaxa referral system.
 *
 * Copyright 2011 Ktunaxa Nation Council, http://www.ktunaxa.org/, Canada.
 */

package org.ktunaxa.referral.shapereader;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.geotools.data.DataStore;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ktunaxa.referral.server.domain.ReferenceLayer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Test cases for the ShapeReaderService interface.
 * 
 * @author Pieter De Graef
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/org/geomajas/spring/geomajasContext.xml",
		"/org/ktunaxa/referral/server/HibernateContext.xml", "applicationContext.xml" })
public class ShapeReaderServiceTest {

	private static final int NR_SHAPES = 3;

	@Autowired
	private ShapeReaderService service;

	@Test
	public void testGetAllFiles() throws IOException {
		Assert.assertNotNull(service);
		File[] files = service.getAllFiles(null);
		Assert.assertNotNull(files);
		Assert.assertTrue(files.length == NR_SHAPES);
	}

	@Test
	public void testsubDirectory() throws IOException {
		Assert.assertNotNull(service);
		File[] files = service.getAllFiles("sub");
		Assert.assertNotNull(files);
		Assert.assertTrue(files.length == 1);
	}

	@Test
	public void testRead() throws IOException {
		Assert.assertNotNull(service);
		File[] files = service.getAllFiles(null);
		Assert.assertNotNull(files);
		Assert.assertTrue(files.length == NR_SHAPES);
		DataStore ds = service.read(files[0]);
		Assert.assertNotNull(ds);
		Assert.assertTrue(ds instanceof ShapefileDataStore);
	}

	@Test
	public void testValidateFormat() throws IOException {
		Assert.assertNotNull(service);
		File[] files = service.getAllFiles(null);
		Assert.assertNotNull(files);
		Assert.assertTrue(files.length == NR_SHAPES);
		try {
			service.validateFormat(service.read(getFile(files, "OK.shp")));
			Assert.assertTrue(true);
		} catch (IOException e) {
			Assert.fail(); // should never get here.
		}
		try {
			service.validateFormat(service.read(getFile(files, "NoStyle.shp")));
			Assert.fail(); // should never get here.
		} catch (IOException e) {
			System.err.println(e.getMessage() + " ..... as expected.");
			Assert.assertTrue(true);
		}
		try {
			service.validateFormat(service.read(getFile(files, "Invalid.shp")));
			Assert.assertTrue(true);
		} catch (IOException e) {
			Assert.fail(); // should never get here.
		}
	}

	private File getFile(File[] files, String nameSuffix) {
		for (File file : files) {
			if (file.getAbsolutePath().endsWith(nameSuffix)) {
				return file;
			}
		}
		return null;
	}

	@Test
	public void testGetAllLayers() throws IOException {
		Assert.assertNotNull(service);
		List<ReferenceLayer> layers = service.getAllLayers();
		Assert.assertNotNull(layers);
		Assert.assertTrue(layers.size() > 50);
	}

	@Test
	public void testPersist() throws IOException {
		Assert.assertNotNull(service);
		ReferenceLayer layer = service.getAllLayers().get(0);
		File[] files = service.getAllFiles(null);

		try {
			service.persist(layer, service.read(getFile(files, "Invalid.shp")));
			Assert.fail(); // should never get here.
		} catch (IOException e) {
			System.err.println(e.getMessage() + " ..... as expected.");
			Assert.assertTrue(true);
		}
		try {
			service.persist(layer, service.read(getFile(files, "OK.shp")));
			Assert.assertTrue(true);
		} catch (IOException e) {
			Assert.fail(); // should never get here.
		}
	}
}
@@ -1339,17 +1339,17 @@
 oid test
-s
+S
 ubDirect
@@ -2082,25 +2082,16 @@
 HAPES);%0A
-%09%09try %7B%0A%09
 %09%09servic
@@ -2152,112 +2152,8 @@
 ));%0A
-%09%09%09Assert.assertTrue(true);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09Assert.fail(); // should never get here.%0A%09%09%7D%0A
 %09%09tr
@@ -2391,33 +2391,24 @@
 (true);%0A%09%09%7D%0A
-%09%09try %7B%0A%09
 %09%09service.va
@@ -2470,112 +2470,8 @@
 ));%0A
-%09%09%09Assert.assertTrue(true);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09Assert.fail(); // should never get here.%0A%09%09%7D%0A
 %09%7D%0A%0A
@@ -3071,23 +3071,81 @@
 ll);%0A%0A%09%09
-try %7B%0A%09
+// invalid shapes should now be accepted, though with a  warning%0A
 %09%09servic
@@ -3211,353 +3211,74 @@
 ));%0A
-%09%09%09Assert.fail(); // should never get here.%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09System.err.println(e.getMessage() + %22 ..... as expected.%22);%0A%09%09%09Assert.assertTrue(true);%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09service.persist(layer, service.read(getFile(files, %22OK.shp%22)));%0A%09%09%09Assert.assertTrue(true);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09Assert.fail(); // should never get here.%0A%09%09%7D
+%0A%09%09service.persist(layer, service.read(getFile(files, %22OK.shp%22)));
 %0A%09%7D%0A

simplify and fix cfr allowing invalid geometries