/*
 *    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.xml.ogc;

import java.io.IOException;
import java.io.StringWriter;
import java.util.logging.Logger;

import javax.naming.OperationNotSupportedException;

import junit.framework.TestCase;

import org.geotools.filter.BetweenFilter;
import org.geotools.filter.Expression;
import org.geotools.filter.FidFilter;
import org.geotools.filter.FilterFactory;
import org.geotools.filter.FilterFactoryFinder;
import org.geotools.filter.IllegalFilterException;
import org.geotools.filter.LikeFilter;
import org.geotools.xml.DocumentWriter;
import org.geotools.xml.filter.FilterSchema;

/**
 *  For now just writes the expression built.
 *  
 * @author David Zwiers, Refractions Research
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/test/java/org/geotools/xml/ogc/ExpresionEncodeTest.java $
 */
public class ExpresionEncodeTest extends TestCase {
    /** Standard logging instance */
    protected static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger(
            "org.geotools.filter");
    /** Constructor with test name. */
    String dataFolder = "";

    public ExpresionEncodeTest(String testName) {
        super(testName);

        //_log.getLoggerRepository().setThreshold(Level.DEBUG);
        LOGGER.finer("running XMLEncoderTests");
        
        dataFolder = System.getProperty("dataFolder");

        if (dataFolder == null) {
            //then we are being run by maven
            dataFolder = System.getProperty("basedir");
            dataFolder = "file:////" + "tests/unit/testData"; //url.toString();
            LOGGER.finer("data folder is " + dataFolder);
        }
    }
    
    public void testPropBetweenFilter() throws IllegalFilterException, OperationNotSupportedException, IOException{
        FilterFactory ff = FilterFactoryFinder.createFilterFactory();
        BetweenFilter bf = ff.createBetweenFilter();
        bf.addLeftValue(ff.createLiteralExpression(60000));
        bf.addMiddleValue(ff.createAttributeExpression("testDouble"));
        bf.addRightValue(ff.createLiteralExpression(200000));
        

        StringWriter output = new StringWriter();
        DocumentWriter.writeFragment(bf,
            FilterSchema.getInstance(), output, null);
        
//        System.out.println(output);
    }
    
    public void testLikeFilter() throws IllegalFilterException, OperationNotSupportedException, IOException{
        FilterFactory ff = FilterFactoryFinder.createFilterFactory();
        Expression testAttribute = ff.createAttributeExpression("testString");

        LikeFilter lf = ff.createLikeFilter();
        lf.setValue(testAttribute);
        lf.setPattern(ff.createLiteralExpression("test*"), "*", ".", "!");

        StringWriter output = new StringWriter();
        DocumentWriter.writeFragment(lf,
            FilterSchema.getInstance(), output, null);
        
        //System.out.println(output);
    }
    
    public void testFidFilter() throws OperationNotSupportedException, IOException{
        FilterFactory ff = FilterFactoryFinder.createFilterFactory();
        FidFilter fif = ff.createFidFilter();
        fif.addFid("f1");
        fif.addFid("f2");
        fif.addFid("f3");
        fif.addFid("f4");

        StringWriter output = new StringWriter();
        DocumentWriter.writeFragment(fif,
            FilterSchema.getInstance(), output, null);
        
        //System.out.println(output);
    }
}
