view gnv-artifacts/src/test/java/de/intevation/gnv/artifacts/GNVArtifactsTestCase.java @ 104:5e086156e3d1

Bugfix: Remove leading- and trailingwhitespaces from Date- and Integervalues gnv-artifacts/trunk@153 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 29 Sep 2009 12:03:39 +0000
parents 4042844339ee
children 11b2d43da90d
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.artifacts;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import junit.framework.TestCase;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import de.intevation.artifactdatabase.Config;
import de.intevation.artifactdatabase.FactoryBootstrap;
import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.ArtifactFactory;
import de.intevation.artifacts.CallContext;
import de.intevation.gnv.artifacts.context.GNVArtifactContext;
import de.intevation.gnv.utils.ArtifactXMLUtilities;

/**
 * @author Tim Englich <tim.englich@intevation.de>
 *
 */
public class GNVArtifactsTestCase extends TestCase {

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = null;
    
    static {
        BasicConfigurator.configure();
        log = Logger.getLogger(GNVArtifactContext.class);
    }
    
    private String configurationDir = "src/test/ressources/";
    
    private FactoryBootstrap bootstrap = null;
    /**
     * Constructor
     * @param name
     */
    public GNVArtifactsTestCase(String name) {
        super(name);
    }

    /**
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        log.debug("GNVArtifactsTestCase.setUp");
        super.setUp();
        log.info(Config.CONFIG_DIR + " ==> "+configurationDir);
        System.setProperty(Config.CONFIG_DIR, configurationDir);
        log.info("Bootstrap wird initialisiert.");
        bootstrap = new FactoryBootstrap();
        bootstrap.boot();
    }
    
    public void testTimeSeriesArtifact(){
        log.debug("GNVArtifactsTestCase.testTimeSeriesArtifact");
        String artefactName = "fis_marnet"; 
        ArtifactFactory artifactFactory = this.getArtifactFactory(artefactName);
        assertNotNull(artifactFactory);
        log.debug("TimeSeries-ArtifactFactory is available");
        Artifact artifact = artifactFactory.createArtifact(""+System.currentTimeMillis(),bootstrap.getContext());
        assertNotNull(artifact);
        log.debug("TimeSeries-Artifact is available");
        
        
        CallContext cc = new TestCallContext(bootstrap.getContext());
        
        // Erster Schritt
        Document outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/timeseries/timeseries_step_01_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/timeseries/timeseries_step_01_advance.xml"), cc);
        this.writeDocument2Log(outputData);
        
        // Zweiter Schritt
        outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/timeseries/timeseries_step_02_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/timeseries/timeseries_step_02_advance.xml"), cc);
        this.writeDocument2Log(outputData);
        
        // Dritter Schritt
        outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/timeseries/timeseries_step_03_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/timeseries/timeseries_step_03_advance.xml"), cc);
        this.writeDocument2Log(outputData);
        
        
        // Vierter Schritt
        outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/timeseries/timeseries_step_04_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/timeseries/timeseries_step_04_advance.xml"),cc);
        this.writeDocument2Log(outputData);
        
        // F�nfter Schritt
        outputData = artifact.describe(null);
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream("src/test/results/timeseriesdiagramm"+System.currentTimeMillis()+".png");
            artifact.feed(this.readDocument("src/test/ressources/timeseries/timeseries_step_05_feed.xml"), cc);
            artifact.out(this.readDocument("src/test/ressources/timeseries/timeseries_step_05_out.xml"),fos, cc);
        } catch (Exception e){
            log.error(e,e);
            fail();
        }finally{
            try {
                fos.flush();
                fos.close();
            } catch (Exception e) {
                log.error(e,e);
            }
        }
    }
    
    public void testVerticalProfileArtifact(){
        log.debug("GNVArtifactsTestCase.testVerticalProfileArtifact");
        String artefactName = "fis_marnet"; 
        ArtifactFactory artifactFactory = this.getArtifactFactory(artefactName);
        assertNotNull(artifactFactory);
        log.debug("VerticalProfile-ArtifactFactory is available");
        Artifact artifact = artifactFactory.createArtifact(""+System.currentTimeMillis(),bootstrap.getContext());
        assertNotNull(artifact);
        log.debug("VerticalProfile-Artifact is available");
        
        
        CallContext cc = new TestCallContext(bootstrap.getContext());
        
        // Erster Schritt
        Document outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_01_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_01_advance.xml"), cc);
        this.writeDocument2Log(outputData);
        
        // Zweiter Schritt
        outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_02_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_02_advance.xml"), cc);
        this.writeDocument2Log(outputData);
        
        // Dritter Schritt
        outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_03_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_03_advance.xml"), cc);
        this.writeDocument2Log(outputData);
        
        
       // Vierter Schritt
        outputData = artifact.describe(null);
        FileOutputStream fos = null;
        FileOutputStream fos2 = null;
        FileOutputStream fos3 = null;
        try{
            fos = new FileOutputStream("src/test/results/verticalprofile"+System.currentTimeMillis()+".png");
            fos2 = new FileOutputStream("src/test/results/verticalprofile"+System.currentTimeMillis()+".xml");
            fos3 = new FileOutputStream("src/test/results/verticalprofile"+System.currentTimeMillis()+".csv");
            artifact.feed(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_04_feed.xml"), cc);
            artifact.out(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_04_out_statistics.xml"),fos2, cc);
            artifact.out(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_04_out_chart.xml"),fos, cc);
            artifact.out(this.readDocument("src/test/ressources/verticalprofile/verticalprofile_step_04_out_csv.xml"),fos3, cc);
        } catch (Exception e){
            log.error(e,e);
            fail();
        }finally{
            try {
                fos.flush();
                fos.close();
                fos2.flush();
                fos2.close();
                fos3.flush();
                fos3.close();
            } catch (Exception e) {
                log.error(e,e);
            }
        }
    }
    
    public void testVerticalProfileInstantaneousPointArtifact(){
        log.debug("GNVArtifactsTestCase.testVerticalProfileArtifact");
        String artefactName = "fis_instantaneouspoint"; 
        ArtifactFactory artifactFactory = this.getArtifactFactory(artefactName);
        assertNotNull(artifactFactory);
        log.debug("VerticalProfile-ArtifactFactory is available");
        Artifact artifact = artifactFactory.createArtifact(""+System.currentTimeMillis(),bootstrap.getContext());
        assertNotNull(artifact);
        log.debug("VerticalProfile-Artifact is available");
        
        
        CallContext cc = new TestCallContext(bootstrap.getContext());
        
        // Erster Schritt
        Document outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_01_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_01_advance.xml"), cc);
        this.writeDocument2Log(outputData);
        
     // Zweiter Schritt
        outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_02_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_02_advance.xml"), cc);
        this.writeDocument2Log(outputData);
        
        // Dritter Schritt
        outputData = artifact.describe(null);
        this.writeDocument2Log(outputData);
        artifact.feed(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_03_feed.xml"), cc);
        outputData = artifact.advance(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_03_advance.xml"), cc);
        this.writeDocument2Log(outputData);
        
        
       // Vierter Schritt
        outputData = artifact.describe(null);
        FileOutputStream fos = null;
        FileOutputStream fos2 = null;
        FileOutputStream fos3 = null;
        try{
            fos = new FileOutputStream("src/test/results/verticalprofile_instantaneouspoint"+System.currentTimeMillis()+".png");
            fos2 = new FileOutputStream("src/test/results/verticalprofile_instantaneouspoint"+System.currentTimeMillis()+".xml");
            fos3 = new FileOutputStream("src/test/results/verticalprofile_instantaneouspoint"+System.currentTimeMillis()+".csv");
            artifact.feed(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_04_feed.xml"), cc);
            artifact.out(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_04_out_statistics.xml"),fos2, cc);
            artifact.out(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_04_out_chart.xml"),fos, cc);
            artifact.out(this.readDocument("src/test/ressources/verticalprofile_instantaneouspoint/verticalprofile_step_04_out_csv.xml"),fos3, cc);
        } catch (Exception e){
            log.error(e,e);
            fail();
        }finally{
            try {
                fos.flush();
                fos.close();
                fos2.flush();
                fos2.close();
                fos3.flush();
                fos3.close();
            } catch (Exception e) {
                log.error(e,e);
            }
        }
    }
    
    protected void createFile(byte[] content, String fileName){
        try {
            FileOutputStream fos = new FileOutputStream(new File(fileName));
            ByteArrayInputStream bis = new ByteArrayInputStream(content);
            byte[] buf = new byte[4096];
            while (bis.read(buf) > 0){
              fos.write(buf);   
            }
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            log.error(e,e);
        } catch (IOException e) {
            log.error(e,e);
        }
    }
    

    /**
     * @param artefactName
     */
    private ArtifactFactory getArtifactFactory(String artefactName){
        log.debug("GNVArtifactsTestCase.getArtifactFactory");
        ArtifactFactory[] artifactFactories = bootstrap.getArtifactFactories();
        for (int i = 0; i < artifactFactories.length; i++){
            if (artifactFactories[i].getName().equals(artefactName)){
                log.debug("ArtifactFactory wurde gefunden.");
                return artifactFactories[i];
            }
        }
        return null;
    }
    
    protected void writeDocument2Log(Document document){
        log.debug(new ArtifactXMLUtilities().writeDocument2String(document));
    }
    
    
    protected Document readDocument(String fileName){
        Document returnValue = null;
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            returnValue = docBuilder.parse (new File(fileName));
        } catch (ParserConfigurationException e) {
            log.error(e,e);
        } catch (SAXException e) {
            log.error(e,e);
        } catch (IOException e) {
            log.error(e,e);
        }
        return returnValue;
    }
    

}

http://dive4elements.wald.intevation.org