/*
 * 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$
 *  
 */
// https://issues.jboss.org/browse/TEIID-3392
// https://issues.jboss.org/browse/TEIID-3390
package org.teiid.jdbc;

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

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.teiid.core.util.UnitTestUtil;


@SuppressWarnings("nls")
public class TestResultsCache {

	private Connection conn;
	private static FakeServer server;

	@BeforeAll public static void oneTimeSetup() throws Exception {
		server = new FakeServer(true);
    	server.deployVDB("test", UnitTestUtil.getTestDataPath() + "/TestCase3473/test.vdb");
	}
	
	@AfterAll public static void oneTimeTeardown() {
		server.stop();
	}
	
	@BeforeEach public void setUp() throws Exception {
    	conn = server.createConnection("jdbc:datavirtuality:test"); //$NON-NLS-1$ //$NON-NLS-2$		
    }
	
	@AfterEach public void teardown() throws SQLException {
		conn.close();
	}
	
	@Test public void testCacheHint() throws Exception {
		Statement s = conn.createStatement();
		s.execute("set showplan on");
		ResultSet rs = s.executeQuery("/* cache */ select 1");
		assertTrue(rs.next());
		s.execute("set noexec on");
		rs = s.executeQuery("/* cache */ select 1");
		assertTrue(rs.next());
		rs = s.executeQuery("select 1");
		assertFalse(rs.next());
	}
	
	@Test public void testCacheHintWithMaxRows() throws Exception {
		Statement s = conn.createStatement();
		s.setMaxRows(1);
		ResultSet rs = s.executeQuery("/* cache */ select 1 union all select 2");
		assertTrue(rs.next());
		assertFalse(rs.next());
		s.setMaxRows(2);
		rs = s.executeQuery("/* cache */ select 1 union all select 2");
		assertTrue(rs.next());
		assertTrue(rs.next());
	}
	
	@Test public void testCacheHintTtl() throws Exception {
		Statement s = conn.createStatement();
		s.execute("set showplan on");
		ResultSet rs = s.executeQuery("/* cache(ttl:50) */ select 1");
		assertTrue(rs.next());
		s.execute("set noexec on");
		rs = s.executeQuery("/* cache(ttl:50) */ select 1");
		assertTrue(rs.next());
		Thread.sleep(60);
		rs = s.executeQuery("/* cache(ttl:50) */ select 1");
		assertFalse(rs.next());
	}
	
	@Test public void testExecutionProperty() throws Exception {
		Statement s = conn.createStatement();
		s.execute("set showplan on");
		s.execute("set resultSetCacheMode true");
		ResultSet rs = s.executeQuery("select 1");
		assertTrue(rs.next());
		s.execute("set noexec on");
		rs = s.executeQuery("select 1");
		assertTrue(rs.next());
		s.execute("set resultSetCacheMode false");
		rs = s.executeQuery("select 1");
		assertFalse(rs.next());
	}

	@Test public void testCacheHintWithLargeSQLXML() throws Exception {
		Statement s = conn.createStatement();
		ResultSet rs = s.executeQuery("/* cache */ WITH t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 10000 ) SELECT xmlelement(root, xmlagg(xmlelement(val, n))) FROM t");
		assertTrue(rs.next());
		assertEquals(148907, rs.getString(1).length());
		assertFalse(rs.next());
		rs.close();
		rs = s.executeQuery("/* cache */ WITH t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 10000 ) SELECT xmlelement(root, xmlagg(xmlelement(val, n))) FROM t");
		assertTrue(rs.next());
		assertEquals(148907, rs.getString(1).length());
		assertFalse(rs.next());
	}
	
}
