/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * 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.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */
/*
 * EXTENSION
 * ISSUE: DV-99, DVCORE-2627
 * FIXED BY: Salvatore Raunich
 * DATE: 2013-11-08, 2015-02-05
 * ADOPTED FROM: TEIID-2477, TEIID-3324
 * END EXTENSION
 */
// https://issues.redhat.com/browse/TEIID-5313
// https://issues.redhat.com/browse/TEIID-5480
package org.teiid.translator.jdbc;

import static org.junit.jupiter.api.Assertions.*;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;
import java.util.List;
	
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.teiid.core.types.GeographyType;
import org.teiid.core.types.GeometryType;
import org.teiid.language.Literal;
import org.teiid.translator.TypeFacility;

public class TestJDBCExecutionFactory {

	@Test public void testDatabaseCalender() throws Exception {
		final JDBCExecutionFactory jef = new JDBCExecutionFactory();
		jef.setDatabaseTimeZone("GMT"); //$NON-NLS-1$
		jef.start();
		
		final Calendar[] cals = new Calendar[2];
		
		Thread t1 = new Thread() {
			public void run() {
				cals[0] = jef.getDatabaseCalendar();
			}
		};
		t1.start();
		
		Thread t2 = new Thread() {
			public void run() {
				cals[1] = jef.getDatabaseCalendar();
			}
		};
		t2.start();
		t1.join();
		t2.join();
		
		assertNotSame(cals[0], cals[1]);
	}
	
	@Test public void testVersion() {
        JDBCExecutionFactory jef = new JDBCExecutionFactory();
        jef.setDatabaseVersion("Some db 1.2.3 (some build)");
        assertEquals("1.2.3", jef.getDatabaseVersion().toString());
        assertEquals(new Version(new Integer[] {1, 2, 3}), jef.getVersion());
        
        Version version = Version.getVersion("10.0");
        assertTrue(version.compareTo(Version.getVersion("9.1")) > 0);
        assertTrue(version.compareTo(Version.getVersion("10.0.1")) < 0);
	}
	
	@Test public void testBooleanRetrival() throws SQLException {
         JDBCExecutionFactory jef = new JDBCExecutionFactory();
         ResultSet rs = Mockito.mock(ResultSet.class);
         Mockito.when(rs.getBoolean(1)).thenReturn(false);
         Mockito.when(rs.wasNull()).thenReturn(true);
         assertNull(jef.retrieveValue(rs, 1, TypeFacility.RUNTIME_TYPES.BOOLEAN));
	}

	@Test public void testBindNChar() throws SQLException {
		JDBCExecutionFactory jef = new JDBCExecutionFactory() {
			public boolean useUnicodePrefix() {return true;}
		};
		PreparedStatement ps = Mockito.mock(PreparedStatement.class);
		jef.bindValue(ps, "Hello\u0128World", TypeFacility.RUNTIME_TYPES.STRING, 1);
		Mockito.verify(ps).setObject(1, "Hello\u0128World", Types.NVARCHAR);
	}

	@Test public void testGeospatialLiterals() throws Exception {
		JDBCExecutionFactory jef = new JDBCExecutionFactory();
		List<?> result = jef.translateGeometryLiteral(new Literal(new GeometryType(), TypeFacility.RUNTIME_TYPES.GEOMETRY));
		assertTrue(result.get(0).toString().startsWith("st_geom"));

		GeographyType value = new GeographyType();
		result = jef.translateGeographyLiteral(new Literal(value, TypeFacility.RUNTIME_TYPES.GEOGRAPHY));
		assertTrue(result.get(0).toString().startsWith("st_geog"));

		value.setSrid(4333);
		result = jef.translateGeographyLiteral(new Literal(value, TypeFacility.RUNTIME_TYPES.GEOGRAPHY));
		assertTrue(result.get(0).toString().startsWith("st_setsrid(st_geog"));
		assertTrue(result.get(0).toString().endsWith("4333)"));
	}

}
