package org.geotools.data.postgis;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStoreFinder;

public class VersionedDataStoreStressTest extends
        AbstractVersionedPostgisDataTestCase {

    Map remote;

    private PostgisTests.Fixture f;

    public VersionedDataStoreStressTest(String name) {
        super(name);
    }

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        f = PostgisTests.newFixture("versioned.properties");
        remote = new HashMap();
        remote.put("dbtype", "postgis-versioned");
        remote.put("charset", "");
        remote.put("host", f.host);
        remote.put("port", f.port);
        remote.put("database", f.database);
        remote.put("user", f.user);
        remote.put("passwd", f.password);
        remote.put("namespace", f.namespace);

        super.setUp();
    }

    /**
     * Versioned data store used to leak connections pretty badly due to a buglet in JCBDUtils,
     * let's make sure this does not happen again
     * 
     * @throws IOException
     */
    public void testMetadataStress() throws IOException {
        for (int i = 0; i < 20; i++) {
            VersionedPostgisDataStore ds = (VersionedPostgisDataStore) DataStoreFinder
                    .getDataStore(remote);

            String[] typeNames = new String[] {"road", "river", "lake"};
            for (int j = 0; j < typeNames.length; j++) {
                String typeName = typeNames[j];
                ds.getSchema(typeName);
                ds.getFeatureSource(typeName).getBounds();
                boolean versioned = ds.isVersioned(typeName);
                try {
                    ds.setVersioned(typeName, !versioned, "mad cow",
                            "I like to change versioning status...");
                } catch (IOException e) {
                    // some feature types cannot be versioned, np
                }
            }
            ds.dispose();
        }
    }
    

}
