File: SQLiteJDBCLoaderTest.java

package info (click to toggle)
xerial-sqlite-jdbc 3.50.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,284 kB
  • sloc: java: 18,781; sh: 2,890; ansic: 2,830; xml: 463; makefile: 199
file content (140 lines) | stat: -rw-r--r-- 5,347 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*--------------------------------------------------------------------------
 *  Copyright 2007 Taro L. Saito
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *--------------------------------------------------------------------------*/
// --------------------------------------
// sqlite-jdbc Project
//
// SQLiteJDBCLoaderTest.java
// Since: Oct 15, 2007
//
// $URL$
// $Author$
// --------------------------------------
package org.sqlite;

import static org.assertj.core.api.Assertions.assertThat;
//import static org.assertj.core.api.Assertions.assertThatNoException;

import java.nio.file.Path;
import java.sql.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class SQLiteJDBCLoaderTest {

    private Connection connection = null;

    @BeforeEach
    public void setUp() throws Exception {
        connection = null;
        // create a database connection
        connection = DriverManager.getConnection("jdbc:sqlite::memory:");
    }

    @AfterEach
    public void tearDown() throws Exception {
        if (connection != null) {
            connection.close();
        }
    }

    /*@Test
    public void query() {
        // if e.getMessage() is "out of memory", it probably means no
        // database file is found
        assertThatNoException()
                .isThrownBy(
                        () -> {
                            Statement statement = connection.createStatement();
                            statement.setQueryTimeout(30); // set timeout to 30 sec.

                            statement.executeUpdate(
                                    "create table person ( id integer, name string)");
                            statement.executeUpdate("insert into person values(1, 'leo')");
                            statement.executeUpdate("insert into person values(2, 'yui')");

                            ResultSet rs =
                                    statement.executeQuery("select * from person order by id");
                            while (rs.next()) {
                                // read the result set
                                rs.getInt(1);
                                rs.getString(2);
                            }
                        });
    }*/

    @Test
    public void function() throws SQLException {
        Function.create(
                connection,
                "total",
                new Function() {
                    @Override
                    protected void xFunc() throws SQLException {
                        int sum = 0;
                        for (int i = 0; i < args(); i++) {
                            sum += value_int(i);
                        }
                        result(sum);
                    }
                });

        ResultSet rs = connection.createStatement().executeQuery("select total(1, 2, 3, 4, 5)");
        assertThat(rs.next()).isTrue();
        assertThat(rs.getInt(1)).isEqualTo(1 + 2 + 3 + 4 + 5);
    }

    @Test
    public void version() {
        assertThat(SQLiteJDBCLoader.getVersion()).isNotEqualTo("unknown");
    }

    @Test
    public void test(@TempDir Path tmpDir) throws Throwable {
        final AtomicInteger completedThreads = new AtomicInteger(0);
        ExecutorService pool = Executors.newFixedThreadPool(32);
        for (int i = 0; i < 32; i++) {
            final String connStr = "jdbc:sqlite:" + tmpDir.resolve("sample-" + i + ".db");
            final int sleepMillis = i;
            pool.execute(
                    () -> {
                        try {
                            Thread.sleep(sleepMillis * 10);
                        } catch (InterruptedException ignored) {
                        }
                        /*assertThatNoException()
                                .isThrownBy(
                                        () -> {
                                            // Uncomment the synchronized block and everything
                                            // works.
                                            // synchronized (TestSqlite.class) {
                                            Connection conn = DriverManager.getConnection(connStr);
                                            conn.close();
                                            // }
                                        });
                        completedThreads.incrementAndGet();*/
                    });
        }
        pool.shutdown();
        pool.awaitTermination(3, TimeUnit.SECONDS);
        //assertThat(completedThreads.get()).isEqualTo(32);
    }
}