forked from pgjdbc/pgjdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseMetaDataCacheTest.java
93 lines (76 loc) · 2.99 KB
/
DatabaseMetaDataCacheTest.java
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
/*
* Copyright (c) 2020, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc2;
import static org.junit.Assert.assertEquals;
import org.postgresql.core.TypeInfo;
import org.postgresql.jdbc.PgConnection;
import org.postgresql.test.TestUtil;
import org.postgresql.util.TestLogHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.regex.Pattern;
/*
* Tests for caching of DatabaseMetadata
*
*/
public class DatabaseMetaDataCacheTest {
private PgConnection con;
private TestLogHandler log;
private Logger driverLogger;
private Level driverLogLevel;
private static final Pattern SQL_TYPE_QUERY_LOG_FILTER = Pattern.compile("querying SQL typecode for pg type");
private static final Pattern SQL_TYPE_CACHE_LOG_FILTER = Pattern.compile("caching all SQL typecodes");
@Before
public void setUp() throws Exception {
con = (PgConnection)TestUtil.openDB();
log = new TestLogHandler();
driverLogger = LogManager.getLogManager().getLogger("org.postgresql");
driverLogger.addHandler(log);
driverLogLevel = driverLogger.getLevel();
driverLogger.setLevel(Level.ALL);
}
@After
public void tearDown() throws Exception {
TestUtil.closeDB(con);
driverLogger.removeHandler(log);
driverLogger.setLevel(driverLogLevel);
log = null;
}
@Test
public void testGetSQLTypeQueryCache() throws SQLException {
TypeInfo ti = con.getTypeInfo();
List<LogRecord> typeQueries = log.getRecordsMatching(SQL_TYPE_QUERY_LOG_FILTER);
assertEquals(0, typeQueries.size());
ti.getSQLType("box"); // this must be a type not in the hardcoded 'types' list
typeQueries = log.getRecordsMatching(SQL_TYPE_QUERY_LOG_FILTER);
assertEquals(1, typeQueries.size());
ti.getSQLType("box"); // this time it should be retrieved from the cache
typeQueries = log.getRecordsMatching(SQL_TYPE_QUERY_LOG_FILTER);
assertEquals(1, typeQueries.size());
}
@Test
public void testGetTypeInfoUsesCache() throws SQLException {
con.getMetaData().getTypeInfo();
List<LogRecord> typeCacheQuery = log.getRecordsMatching(SQL_TYPE_CACHE_LOG_FILTER);
assertEquals("PgDatabaseMetadata.getTypeInfo() did not cache SQL typecodes", 1, typeCacheQuery.size());
List<LogRecord> typeQueries = log.getRecordsMatching(SQL_TYPE_QUERY_LOG_FILTER);
assertEquals("PgDatabaseMetadata.getTypeInfo() resulted in individual queries for SQL typecodes", 0, typeQueries.size());
}
@Test
public void testTypeForAlias() {
TypeInfo ti = con.getTypeInfo();
assertEquals("bool", ti.getTypeForAlias("boolean"));
assertEquals("bool", ti.getTypeForAlias("Boolean"));
assertEquals("bool", ti.getTypeForAlias("Bool"));
assertEquals("bogus", ti.getTypeForAlias("bogus"));
}
}