package org.geotools.jdbc;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import org.geotools.data.DefaultQuery;
import org.geotools.data.FeatureSource;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory;

/**
 * 
 *
 * @source $URL$
 */
public abstract class JDBCDateTest extends JDBCTestSupport {

    @Override
    protected abstract JDBCDateTestSetup createTestSetup();

    public void testMappings() throws Exception {
        SimpleFeatureType ft = dataStore.getSchema( tname("dates") );
        
        assertEquals( Date.class, ft.getDescriptor( aname("d") ).getType().getBinding() );
        assertEquals( Timestamp.class, ft.getDescriptor( aname("dt") ).getType().getBinding() );
        assertEquals( Time.class, ft.getDescriptor( aname("t") ).getType().getBinding() );
    }
    
    public void testFiltersByDate() throws Exception {
        FeatureSource fs = dataStore.getFeatureSource( tname("dates") );
        
        FilterFactory ff = dataStore.getFilterFactory();
        
        //less than
        Filter f = ff.lessOrEqual( ff.property( aname("d") ), ff.literal( /*df.parse(*/"2009-06-28"/*)*/ ) );
        assertEquals( 2, fs.getCount( new DefaultQuery(tname("dates"),f ) ) );
        
        f = ff.lessOrEqual( ff.property( aname("d") ), 
            ff.literal( new SimpleDateFormat("yyyy-dd-MM").parse("2009-28-06") ) );
        assertEquals( 2, fs.getCount( new DefaultQuery(tname("dates"),f ) ) );
    }
    
    public void testFilterByTimeStamp() throws Exception {
        FeatureSource fs = dataStore.getFeatureSource( tname("dates") );
        
        FilterFactory ff = dataStore.getFilterFactory();
        
        //equal to
        Filter f = ff.equals( ff.property( aname("dt") ), ff.literal( "2009-06-28 15:12:41" ) );
        assertEquals( 1, fs.getCount( new DefaultQuery(tname("dates"),f ) ) );
        
        f = ff.equals( ff.property( aname("dt") ), 
                ff.literal( new SimpleDateFormat("HH:mm:ss,dd-yyyy-MM").parse("15:12:41,28-2009-06") ) );
        assertEquals( 1, fs.getCount( new DefaultQuery(tname("dates"),f ) ) );
    }
    
    public void testFilterByTime() throws Exception {
        FeatureSource fs = dataStore.getFeatureSource( tname("dates") );
        
        FilterFactory ff = dataStore.getFilterFactory();
        
        // greather than or equal to
        Filter f = ff.greaterOrEqual( ff.property( aname("t") ), ff.literal( "13:10:12" ) );
        assertEquals( 3, fs.getCount( new DefaultQuery(tname("dates"),f ) ) );
        
        f = ff.greaterOrEqual( ff.property( aname("t") ), 
                ff.literal( new SimpleDateFormat("ss:HH:mm").parse("12:13:10") ) );
        assertEquals( 3, fs.getCount( new DefaultQuery(tname("dates"),f ) ) );
    }
}

@@ -109,22 +109,16 @@
 va.text.
-Simple
 DateForm
@@ -119,16 +119,78 @@
 teFormat
+;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.TimeZone
 ;%0A%0Aimpor
@@ -1175,32 +1175,155 @@
 ory();%0A        %0A
+        DateFormat df = new SimpleDateFormat(%22yyyy-dd-MM%22);%0A        df.setTimeZone( TimeZone.getTimeZone(%22PST%22));%0A        %0A
         //less t
@@ -1400,18 +1400,16 @@
 iteral( 
-/*
 df.parse
@@ -1413,27 +1413,30 @@
 rse(
-*/
 %222009-
-06-28%22/*)*/
+28-06%22)%0A        
  ) )
@@ -1583,68 +1583,22 @@
 ) ),
- %0A            ff.literal( new SimpleDateFormat(%22yyyy-dd-MM%22)
+ff.literal( df
 .par

Nail the timezone down so the test is stable no matter where in the world you are building