view geo-backend/src/test/java/de/intevation/gnv/geobackend/base/query/QueryExecutorTestCase.java @ 1127:ebeb56428409

Added license headers and license file. geo-backend/trunk@1261 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:52:22 +0000
parents b757def3ff55
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.geobackend.base.query;

import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.geobackend.base.ResultDescriptor;

import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory;

import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory;

import de.intevation.gnv.geobackend.base.query.exception.QueryException;

import java.io.FileInputStream;
import java.io.InputStream;

import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import junit.framework.TestCase;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

/**
 * TestCase for the QueryExecutor interface
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 *
 */
public class QueryExecutorTestCase extends TestCase {


    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = null;

    static{
        BasicConfigurator.configure();
        log = Logger.getLogger(QueryExecutorTestCase.class);
    }

    /**
     * Constructor
     * @param name
     */
    public QueryExecutorTestCase(String name) {
        super(name);
    }


    public void  testSpatialQuery(){
            try {

            this.testQuery(5, "spatial_query", null);
       } catch (QueryException e) {
            log.error(e,e);
            fail();
        }
    }

    public void  testSpatialQueryWithoutIntersects(){
            try {
//            this.testQuery(286, "spatial_query_without_intersects", null);
            this.testQuery(0, "spatial_query_without_intersects2", null);
            this.testQuery(2060, "spatial_query_without_intersects3", null);
            this.testQuery(0, "spatial_query_without_intersects4", null);
            this.testQuery(27031, "spatial_query_without_intersects5", null);
            this.testQuery(8464, "spatial_query_without_intersects6", null);
       } catch (QueryException e) {
            log.error(e,e);
            fail();
        }
    }

    public void testSpatialQueryWithInnerSelect(){
        try {
            this.testQuery(1998, "spatial_query_with_innerselect", null);
       } catch (QueryException e) {
            log.error(e,e);
            fail();
        }
    }


    public void  testChooseFis(){
        try {
            this.testQuery(1, "choose_fis_query", null);
        } catch (QueryException e) {
            log.error(e, e);
            fail();
        }
    }


    /**
     * Tests if the Databaseconnection can be established and
     * Data could be read from the Database
     */
    public void testQueryExecutor(){

        try {

            this.testQuery(6, "mesh", null);

            this.testQuery(1, "mesh_id", new String[]{"5"});



        } catch (QueryException e) {
            log.error(e,e);
            fail();
        }
    }

    public void testTimeSeriesQueries(){

        try{
            // Test zum initialisieren der umgebung
            this.testQuery(6, "mesh", null);


            this.testQuery(11, "timeseries_stations", new String[]{"4"});
            this.testQuery(11, "timeseries_stations_op", new String[]{"4"});
            this.testQuery(1, "timeseries_interval", new String[]{"500042 ", "54"});

        } catch (QueryException e) {
            log.error(e,e);
            fail();
        }

    }

    /**
     * @param resultsize
     * @param queryID
     * @param filter
     * @throws QueryException
     */
    private void testQuery(int resultsize, String queryID, String[] filter)
            throws QueryException {
        long start = System.currentTimeMillis();
        QueryExecutor queryExecutor = QueryExecutorFactory.getInstance().getQueryExecutor();
        Collection<Result> results = queryExecutor.executeQuery(queryID, filter);
//        if (results == null || results.size() == 0){
//            log.error("Keine Daten erhalten");
//            fail();
//        }else {
            assertEquals(resultsize, results.size());
            log.debug(resultsize+" Datens�tze erhalten");
            Iterator<Result> it = results.iterator();
            while (it.hasNext()){
                Result tmpResult = it.next();
                ResultDescriptor resultDescriptor = tmpResult.getResultDescriptor();
                int columns = resultDescriptor.getColumnCount();
                for (int i = 0; i < columns; i++){
                    String columnName = resultDescriptor.getColumnName(i);
                    Object value = tmpResult.getString(columnName);
                    log.debug(columnName + " ==> "+value.toString());
                }
            }
//        }
        log.debug("Query dauerte: "+(System.currentTimeMillis()-start) +"ms");
    }



    /**
     * @see junit.framework.TestCase#setUp()
     */
    @Override
    protected void setUp() throws Exception {

        super.setUp();
        InputStream inputStream = new FileInputStream("src/test/ressources/ArcSDEConnectionPoolTestCase.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        ConnectionPoolFactory cpf = ConnectionPoolFactory.getInstance();
        cpf.initializeConnectionPool(properties);


        inputStream = new FileInputStream("src/test/ressources/QueryExecutorTestCase.properties");
        properties = new Properties();
        properties.load(inputStream);

        QueryContainerFactory qcf = QueryContainerFactory.getInstance();
        qcf.initializeQueryContainer(properties);
    }

}

http://dive4elements.wald.intevation.org