package com.vaadin.tests.components.table;

import java.sql.SQLException;

import com.vaadin.data.util.sqlcontainer.DataGenerator;
import com.vaadin.data.util.sqlcontainer.SQLContainer;
import com.vaadin.data.util.sqlcontainer.SQLTestsConstants;
import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
import com.vaadin.data.util.sqlcontainer.query.QueryDelegate;
import com.vaadin.data.util.sqlcontainer.query.TableQuery;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class TableScrollingWithSQLContainer extends UI {

    /** Table should never end up calling indexOfId in this case */
    private class LimitedSQLContainer extends SQLContainer {

        public LimitedSQLContainer(QueryDelegate delegate) throws SQLException {
            super(delegate);
        }

        @Override
        public int indexOfId(Object itemId) {
            throw new RuntimeException("This function should not be called");
        }
    }

    static final String TABLE = "table";

    @Override
    public void init(VaadinRequest request) {
        try {
            SimpleJDBCConnectionPool connectionPool = new SimpleJDBCConnectionPool(
                    SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL,
                    SQLTestsConstants.dbUser, SQLTestsConstants.dbPwd, 2, 2);
            DataGenerator.addPeopleToDatabase(connectionPool);
            DataGenerator.addFiveThousandPeople(connectionPool);

            TableQuery query = new TableQuery("people", connectionPool,
                    SQLTestsConstants.sqlGen);

            SQLContainer container = new LimitedSQLContainer(query);

            final VerticalLayout rootLayout = new VerticalLayout();

            final Table table = new Table();
            table.setContainerDataSource(container);
            table.setCurrentPageFirstItemIndex(300);
            rootLayout.addComponent(table);

            table.setImmediate(true);

            rootLayout.addComponent(new Button("GOTO 200", new ClickListener() {

                @Override
                public void buttonClick(ClickEvent event) {
                    table.setCurrentPageFirstItemIndex(200);
                }
            }));

            setContent(rootLayout);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
@@ -37,16 +37,44 @@
 table;%0A%0A
+import java.sql.Connection;%0A
 import j
@@ -99,64 +99,35 @@
 on;%0A
-%0A
 import 
-com.vaadin.data.util.sqlcontainer.DataGenerator;
+java.sql.Statement;%0A
 %0Aimp
@@ -219,33 +219,45 @@
 ntainer.
-SQLTestsConstants
+connection.JDBCConnectionPool
 ;%0Aimport
@@ -449,16 +449,94 @@
 eQuery;%0A
+import com.vaadin.data.util.sqlcontainer.query.generator.DefaultSQLGenerator;%0A
 import c
@@ -1262,24 +1262,1002 @@
    %7D%0A    %7D%0A%0A
+    private void generateTestData(JDBCConnectionPool connectionPool)%0A            throws SQLException %7B%0A        Connection conn = connectionPool.reserveConnection();%0A        Statement statement = conn.createStatement();%0A        try %7B%0A            statement.execute(%22drop table PEOPLE%22);%0A        %7D catch (SQLException e) %7B%0A            // Will fail if table doesn't exist, which is OK.%0A            conn.rollback();%0A        %7D%0A        statement%0A                .execute(%22create table people (id integer generated always as identity,%22%0A                        + %22 name varchar(32), AGE INTEGER)%22);%0A        statement.execute(%22alter table people add primary key (id)%22);%0A        for (int i = 0; i %3C 5000; i++) %7B%0A            statement%0A                    .executeUpdate(%22insert into people values(default, 'Person %22%0A                            + i + %22', '%22 + i %25 99 + %22')%22);%0A        %7D%0A        statement.close();%0A        conn.commit();%0A        connectionPool.releaseConnection(conn);%0A    %7D%0A%0A
     static f
@@ -2468,59 +2468,36 @@
     
-SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL
+%22org.hsqldb.jdbc.JDBCDriver%22
 ,%0A  
@@ -2518,63 +2518,55 @@
     
-SQLTestsC
+%22jdbc:hsqldb:mem:sqlc
 on
-s
 ta
+i
 n
-ts.dbUs
 er
+%22
 , 
-SQLTestsConstants.dbPwd
+%22SA%22, %22%22
 , 2, 2
+0
 );%0A 
@@ -2580,106 +2580,24 @@
     
-DataG
+g
 enerat
-or.addPeopleToDatabase(connectionPool);%0A            DataGenerator.addFiveThousandPeople
+eTestData
 (con
@@ -2707,32 +2707,33 @@
     
-SQLTestsConstants.sqlGen
+new DefaultSQLGenerator()
 );%0A%0A

Fix broken TableScrollingWithSQLContainer test UI