/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2004-2006, Geotools Project Managment Committee (PMC)
 *
 *    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.
 */
package org.geotools.validation;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Map;

import org.geotools.data.DefaultRepository;
import org.geotools.feature.FeatureIterator;
import org.geotools.data.Repository;
import org.geotools.data.memory.MemoryDataStore;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.test.TestData;
import org.geotools.validation.xml.XMLReader;


/**
 * A proper test fixture for the ValidationProcessor (and friends to hit).
 * 
 * <p>
 * For geoserver developers this environment is similar to confDemo. Where
 * possible names have been forced to agree with geoserver.
 * </p>
 * <p>
 * In the interests of saving space the contents of confDemo have been reduced to:
 * <ul>
 * <li>lakes - MultiPolygon
 * <li>streams - MultiLinestring
 * <li>swamps - MultiPolygon
 * <li>rivers - MultiPolygon
 * </ul>
 * The complete confDemo also includes a large road and building shapefile.
 * </p>
 * @author Jody Garnett
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/test/java/org/geotools/validation/TestFixture.java $
 */
public class TestFixture {
    Repository data = new DefaultRepository();
    public Map pluginDTOs;
    public Map testSuiteDTOs;
    public ValidationProcessor processor;
    public DefaultRepository repository;

    /** Starting point for your own TestFixture */
    public TestFixture( File pluginDir, File testDir, File propertiesFile ) throws Exception {
        repository = new DefaultRepository();
        repository.load( propertiesFile );

        pluginDTOs = XMLReader.loadPlugIns( pluginDir);
        testSuiteDTOs = XMLReader.loadValidations( testDir, pluginDTOs);
        processor = new ValidationProcessor();
        processor.load(pluginDTOs, testSuiteDTOs);
    }
    /**
     * Sets up a test fixture based on GeoServer confDemo.
     * <p>
     * The shapefiles are copied into MemoryDataStores - allowing tests to be more
     * abusive.
     * </p>
     */
    public TestFixture() throws Exception {
        repository = new DefaultRepository();        
        File directory = TestData.file( this, "shapefiles" );
        String shapefiles[] = directory.list( new FilenameFilter(){
			public boolean accept(File dir, String name) {
				return name.toUpperCase().endsWith(".SHP"); 
			}        
        });
        for( int i=0; i<shapefiles.length;i++){
        	File shapefile = new File( directory, shapefiles[i] );
        	ShapefileDataStore dataStore = new ShapefileDataStore( shapefile.toURL() );
        	String dataStoreId = dataStore.getTypeNames()[0].toUpperCase();
        	String typeName = dataStore.getTypeNames()[0];
        	FeatureIterator features = dataStore.getFeatureSource( typeName ).getFeatures().features();
        	MemoryDataStore cache = new MemoryDataStore( features );
        	
        	repository.register( dataStoreId, cache );
        }
        File pluginDir = TestData.file(this, "plugins");
        File validationDir = TestData.file(this, "validation" );
        
        pluginDTOs = XMLReader.loadPlugIns( pluginDir );
        testSuiteDTOs = XMLReader.loadValidations( validationDir, pluginDTOs);
        
        processor = new ValidationProcessor();
        processor.load(pluginDTOs, testSuiteDTOs);
    }
}
