-
Notifications
You must be signed in to change notification settings - Fork 612
/
AbstractSQLStoreFunctionalTest.java
557 lines (495 loc) · 21.4 KB
/
AbstractSQLStoreFunctionalTest.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package org.infinispan.persistence.sql;
import static org.infinispan.persistence.jdbc.common.DatabaseType.H2;
import static org.infinispan.persistence.jdbc.common.DatabaseType.SQLITE;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNull;
import java.io.File;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.TransactionManager;
import org.infinispan.Cache;
import org.infinispan.commons.CacheConfigurationException;
import org.infinispan.commons.dataconversion.MediaType;
import org.infinispan.commons.test.CommonsTestingUtil;
import org.infinispan.commons.test.Exceptions;
import org.infinispan.commons.util.Util;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.marshall.protostream.impl.SerializationContextRegistry;
import org.infinispan.persistence.BaseStoreFunctionalTest;
import org.infinispan.persistence.jdbc.common.DatabaseType;
import org.infinispan.persistence.jdbc.common.UnitTestDatabaseManager;
import org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration;
import org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfigurationBuilder;
import org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfigurationBuilder;
import org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory;
import org.infinispan.persistence.sql.configuration.AbstractSchemaJdbcConfigurationBuilder;
import org.infinispan.protostream.ProtobufUtil;
import org.infinispan.test.TestingUtil;
import org.infinispan.test.data.Address;
import org.infinispan.test.data.Key;
import org.infinispan.test.data.Person;
import org.infinispan.test.data.Sex;
import org.infinispan.transaction.TransactionMode;
import org.mockito.Mockito;
import org.postgresql.Driver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import util.JdbcConnection;
public abstract class AbstractSQLStoreFunctionalTest extends BaseStoreFunctionalTest {
protected final DatabaseType DB_TYPE;
protected final boolean transactionalCache;
protected final boolean transactionalStore;
protected String tmpDirectory;
protected Consumer<AbstractSchemaJdbcConfigurationBuilder<?, ?>> schemaConsumer;
protected static final String DATABASE = System.getProperty("org.infinispan.test.sqlstore.database");
protected static final String JDBC_URL = System.getProperty("org.infinispan.test.sqlstore.jdbc.url");
protected static final String JDBC_USERNAME = System.getProperty("org.infinispan.test.sqlstore.jdbc.username");
protected static final String JDBC_PASSWORD = System.getProperty("org.infinispan.test.sqlstore.jdbc.password");
protected static Map<DatabaseType, JdbcConnection> databasesFromSystemProperty = new HashMap<>();
static {
if(DATABASE != null) {
databasesFromSystemProperty = getDatabases();
}
}
public AbstractSQLStoreFunctionalTest(DatabaseType databaseType, boolean transactionalCache,
boolean transactionalStore) {
this.DB_TYPE = databaseType;
this.transactionalCache = transactionalCache;
this.transactionalStore = transactionalStore;
}
@BeforeClass(alwaysRun = true)
protected void setUpTempDir() {
tmpDirectory = CommonsTestingUtil.tmpDirectory(getClass());
new File(tmpDirectory).mkdirs();
}
@BeforeMethod(alwaysRun = true)
@Override
protected void createBeforeMethod() throws Exception {
schemaConsumer = null;
super.createBeforeMethod();
}
@AfterClass(alwaysRun = true)
protected void clearTempDir() {
Util.recursiveFileRemove(tmpDirectory);
}
// DB table is denormalized when read
@Override
protected Person createEmptyPerson(String name) {
return new Person(name, new Address());
}
@Override
protected String parameters() {
return "[" + DB_TYPE + ", transactionalCache=" + transactionalCache + ", transactionalStore=" + transactionalStore
+ "]";
}
@Override
protected ConfigurationBuilder getDefaultCacheConfiguration() {
ConfigurationBuilder builder = super.getDefaultCacheConfiguration();
builder.encoding().mediaType(MediaType.APPLICATION_PROTOSTREAM_TYPE);
if (transactionalCache) {
builder.transaction().transactionMode(TransactionMode.TRANSACTIONAL);
}
return builder;
}
@Override
public void testPreloadStoredAsBinary() {
schemaConsumer = builder ->
builder.schema()
.embeddedKey(false)
.messageName("Person")
.packageName("org.infinispan.test.core");
super.testPreloadStoredAsBinary();
}
@Test(enabled = false, description = "Expiration not supported")
@Override
public void testPreloadAndExpiry() {
// Expiration not supported
}
@Test(enabled = false, description = "Not applicable")
@Override
public void testTwoCachesSameCacheStore() {
// Stores are always shared
}
@Override
public void testRemoveCacheWithPassivation() {
if (!transactionalStore) {
super.testRemoveCacheWithPassivation();
}
}
public void testRollback() throws SystemException, NotSupportedException {
if (!transactionalCache) {
return;
}
String cacheName = "testRollback";
ConfigurationBuilder cb = getDefaultCacheConfiguration();
createCacheStoreConfig(cb.persistence(), cacheName, false);
TestingUtil.defineConfiguration(cacheManager, cacheName, cb.build());
Cache<String, Object> cache = cacheManager.getCache(cacheName);
String key = "rollback-test";
assertNull(cache.get(key));
TransactionManager manager = cache.getAdvancedCache().getTransactionManager();
String value = "the-value";
manager.begin();
cache.put(key, value);
assertEquals(value, cache.get(key));
manager.rollback();
assertNull(cache.get(key));
}
public void testDBHasMoreKeyColumnsWithKeySchema(Method m) {
schemaConsumer = builder ->
builder.schema()
.embeddedKey(false)
.keyMessageName("Key")
.packageName("org.infinispan.test.core");
Exceptions.expectException(CacheConfigurationException.class, CompletionException.class,
CacheConfigurationException.class,
".*Primary key (?i)(KEYCOLUMN2) was not found.*",
() -> testSimpleGetAndPut(m.getName(), new Key("mykey"), "value1"));
}
public void testDBHasMoreKeyColumnsWithNoKeySchema(Method m) {
Exceptions.expectException(CacheConfigurationException.class, CompletionException.class,
CacheConfigurationException.class,
".*Primary key has multiple columns .*",
() -> testSimpleGetAndPut(m.getName(), "key", "value"));
}
// public void testDBHasLessKeyColumnsWithSchema(Method m) {
// // TODO: add new schema key with 2 columns
// schemaConsumer = builder ->
// builder.schemaJdbcConfigurationBuilder()
// .embeddedKey(false)
// .keyMessageName("Key")
// .packageName("org.infinispan.test.core");
// Exceptions.expectException(CacheConfigurationException.class, CompletionException.class,
// CacheConfigurationException.class,
// "Primary key has multiple columns .*",
// () -> testSimpleGetAndPut(m.getName(), "key", "value"));
// }
public void testDBHasMoreValueColumnsWithValueSchema(Method m) {
schemaConsumer = builder ->
builder.schema()
.embeddedKey(false)
.messageName("Person")
.packageName("org.infinispan.test.core");
Exceptions.expectException(CacheConfigurationException.class, CompletionException.class,
CacheConfigurationException.class,
".*Additional value columns .* found that were not part of the schema,.*",
() -> testSimpleGetAndPut(m.getName(), "key", new Person("man2")));
}
public void testDBHasMoreValueColumnsWithNoValueSchema(Method m) {
Exceptions.expectException(CacheConfigurationException.class, CompletionException.class,
CacheConfigurationException.class,
".*Multiple non key columns but no value message schema defined.*",
() -> testSimpleGetAndPut(m.getName(), "key", "value"));
}
public void testDBHasLessValueColumnsWithSchema(Method m) {
schemaConsumer = builder ->
builder.schema()
.embeddedKey(false)
.messageName("Person")
.packageName("org.infinispan.test.core");
testSimpleGetAndPut(m.getName(), "key", new Person("joe"));
}
public void testEmbeddedKey(Method m) {
schemaConsumer = builder ->
builder.schema()
.embeddedKey(true)
.messageName("Person")
.packageName("org.infinispan.test.core");
testSimpleGetAndPut(m.getName(), "joe", new Person("joe"));
}
public void testEnumForKey(Method m) {
schemaConsumer = builder ->
builder.schema()
.embeddedKey(false)
.keyMessageName("Sex")
.packageName("org.infinispan.test.core");
testSimpleGetAndPut(m.getName(), Sex.FEMALE, "samantha");
}
public void testEnumForValue(Method m) {
schemaConsumer = builder ->
builder.schema()
.embeddedKey(false)
.messageName("Sex")
.packageName("org.infinispan.test.core");
testSimpleGetAndPut(m.getName(), "samantha", Sex.FEMALE);
}
public void testEmbeddedLoadSchemaAfterCreation(Method m) {
schemaConsumer = builder ->
builder.schema()
.embeddedKey(true)
.messageName("Person")
.packageName("org.infinispan.test.core");
final AtomicBoolean loadSchema = new AtomicBoolean(false);
SerializationContextRegistry scr = TestingUtil.extractGlobalComponent(cacheManager, SerializationContextRegistry.class);
SerializationContextRegistry spyScr = Mockito.spy(scr);
Mockito.when(spyScr.getUserCtx()).then(ivk -> {
if (loadSchema.get()) return scr.getUserCtx();
return ProtobufUtil.newSerializationContext();
});
TestingUtil.replaceComponent(cacheManager, SerializationContextRegistry.class, spyScr, true);
String cacheName = m.getName();
ConfigurationBuilder cb = getDefaultCacheConfiguration();
createCacheStoreConfig(cb.persistence(), cacheName, false);
TestingUtil.defineConfiguration(cacheManager, cacheName, cb.build());
// This should fail because the schema does not exist.
Exceptions.expectException(CacheConfigurationException.class, CompletionException.class,
CacheConfigurationException.class,
"ISPN008047: Schema not found for : org.infinispan.test.core.Person",
() -> cacheManager.getCache(cacheName)
);
loadSchema.set(true);
// Schema registered successfully afterwards.
// Should be fully functional now.
Cache<Object, Object> cache = cacheManager.getCache(cacheName);
String key = "joe";
Person value = new Person("joe");
assertNull(cache.get(key));
cache.put(key, value);
assertEquals(value, cache.get(key));
}
private void testSimpleGetAndPut(String cacheName, Object key, Object value) {
ConfigurationBuilder cb = getDefaultCacheConfiguration();
createCacheStoreConfig(cb.persistence(), cacheName, false);
TestingUtil.defineConfiguration(cacheManager, cacheName, cb.build());
Cache<Object, Object> cache = cacheManager.getCache(cacheName);
assertNull(cache.get(key));
cache.put(key, value);
assertEquals(value, cache.get(key));
List<Map.Entry<Object, Object>> entryList = cache.entrySet().stream().collect(Collectors.toList());
assertEquals(1, entryList.size());
Map.Entry<Object, Object> entry = entryList.get(0);
assertEquals(key, entry.getKey());
assertEquals(value, entry.getValue());
}
protected void configureCommonConfiguration(AbstractSchemaJdbcConfigurationBuilder<?, ?> builder) {
if (schemaConsumer != null) {
schemaConsumer.accept(builder);
}
PooledConnectionFactoryConfigurationBuilder<?> connectionPool = null;
if(!(DB_TYPE == SQLITE || DB_TYPE == H2)) {
connectionPool = addJdbcConnection(builder);
}
switch (DB_TYPE) {
case POSTGRES:
connectionPool
.driverClass(Driver.class);
break;
case ORACLE:
connectionPool
.driverClass("oracle.jdbc.OracleDriver");
break;
case MARIA_DB:
connectionPool
.driverClass("org.mariadb.jdbc.Driver");
break;
case DB2:
connectionPool
.driverClass("com.ibm.db2.jcc.DB2Driver");
break;
case MYSQL:
connectionPool
.driverClass("com.mysql.cj.jdbc.Driver");
break;
case SQLITE:
builder.connectionPool()
.driverClass("org.sqlite.JDBC")
.connectionUrl("jdbc:sqlite:" + tmpDirectory + File.separator + "sqllite.data")
.username("sa");
break;
case SQL_SERVER:
connectionPool
.driverClass("com.microsoft.sqlserver.jdbc.SQLServerDriver");
break;
case SYBASE:
connectionPool
.driverClass("com.sybase.jdbc4.jdbc.SybDriver");
break;
case H2:
default:
UnitTestDatabaseManager.configureUniqueConnectionFactory(builder);
}
}
String binaryType() {
switch (DB_TYPE) {
case POSTGRES:
return "BYTEA";
case ORACLE:
return "RAW(255)";
case SQLITE:
return "BINARY";
default:
return "VARBINARY(255)";
}
}
String booleanType() {
switch (DB_TYPE) {
case SQL_SERVER:
return "BIT";
case ORACLE:
case ORACLE_XE:
return "NUMBER(1, 0)";
default:
return "BOOLEAN";
}
}
String dateTimeType() {
switch (DB_TYPE) {
case SYBASE:
case MYSQL:
case MARIA_DB:
return "DATETIME";
case SQL_SERVER:
return "DATETIME2";
case POSTGRES:
case H2:
default:
return "TIMESTAMP";
}
}
protected void createTable(String cacheName, String tableName, ConnectionFactoryConfigurationBuilder<ConnectionFactoryConfiguration> builder) {
String tableCreation;
String upperCaseCacheName = cacheName.toUpperCase();
if (cacheName.equalsIgnoreCase("testPreloadStoredAsBinary")) {
tableCreation = "CREATE TABLE " + tableName + " (" +
"keycolumn VARCHAR(255) NOT NULL, " +
"NAME VARCHAR(255) NOT NULL, " +
"street VARCHAR(255), " +
"city VARCHAR(255), " +
"zip INT, " +
"picture " + binaryType() + ", " +
"accepted_tos " + booleanType() + ", " +
"sex VARCHAR(255), " +
"birthdate " + dateTimeType() + ", " +
"moneyOwned NUMERIC(10, 4), " +
"moneyOwed FLOAT, " +
"decimalField DECIMAL(10, 4), " +
"realField REAL, " +
"PRIMARY KEY (keycolumn))";
} else if (cacheName.equalsIgnoreCase("testStoreByteArrays")) {
tableCreation = "CREATE TABLE " + tableName + " (" +
"keycolumn " + binaryType() + " NOT NULL, " +
"value1 " + binaryType() + " NOT NULL, " +
"PRIMARY KEY (keycolumn))";
} else if (upperCaseCacheName.startsWith("TESTDBHASMOREVALUECOLUMNS")) {
tableCreation = "CREATE TABLE " + tableName + " (" +
"keycolumn VARCHAR(255) NOT NULL, " +
"NAME VARCHAR(255) NOT NULL, " +
"street VARCHAR(255), " +
"city VARCHAR(255), " +
"zip INT, " +
"picture " + binaryType() + ", " +
"sex VARCHAR(255), " +
"birthdate " + dateTimeType() + ", " +
"value2 VARCHAR(255), " +
"value3 VARCHAR(255), " +
"PRIMARY KEY (keycolumn))";
} else if (upperCaseCacheName.startsWith("TESTDBHASMOREKEYCOLUMNS")) {
tableCreation = "CREATE TABLE " + tableName + " (" +
// The name of the field for the Key schema is "value1"
"value1 VARCHAR(255) NOT NULL, " +
"keycolumn2 VARCHAR(255) NOT NULL, " +
"value2 VARCHAR(255) NOT NULL, " +
"PRIMARY KEY (value1, keycolumn2))";
} else if (upperCaseCacheName.startsWith("TESTDBHASLESSVALUECOLUMNS")) {
tableCreation = "CREATE TABLE " + tableName + " (" +
"keycolumn VARCHAR(255) NOT NULL, " +
"NAME VARCHAR(255) NOT NULL, " +
"street VARCHAR(255), " +
"PRIMARY KEY (keycolumn))";
} else if (upperCaseCacheName.startsWith("TESTEMBEDDED")) {
tableCreation = "CREATE TABLE " + tableName + " (" +
"NAME VARCHAR(255) NOT NULL, " +
"street VARCHAR(255), " +
"city VARCHAR(255), " +
"zip INT, " +
"picture " + binaryType() + ", " +
"sex VARCHAR(255), " +
"birthdate " + dateTimeType() + ", " +
"PRIMARY KEY (name))";
} else if (upperCaseCacheName.startsWith("TESTENUMFORVALUE")) {
tableCreation = "CREATE TABLE " + tableName + " (" +
"NAME VARCHAR(255) NOT NULL, " +
"sex VARCHAR(255), " +
"PRIMARY KEY (name))";
} else if (upperCaseCacheName.startsWith("TESTENUMFORKEY")) {
tableCreation = "CREATE TABLE " + tableName + " (" +
"sex VARCHAR(255) NOT NULL, " +
"name VARCHAR(255), " +
"PRIMARY KEY (sex))";
} else {
tableCreation = "CREATE TABLE " + tableName + " (" +
"keycolumn VARCHAR(255) NOT NULL, " +
"value1 VARCHAR(255) NOT NULL, " +
"PRIMARY KEY (keycolumn))";
}
ConnectionFactoryConfiguration config = builder.create();
ConnectionFactory factory = ConnectionFactory.getConnectionFactory(config.connectionFactoryClass());
factory.start(config, getClass().getClassLoader());
Connection connection = null;
try {
connection = factory.getConnection();
String modifiedTableName = tableToSearch(tableName);
try (ResultSet rs = connection.getMetaData().getTables(null, null, modifiedTableName,
new String[]{"TABLE"})) {
if (!rs.next()) {
try (Statement stmt = connection.createStatement()) {
log.debugf("Table: %s doesn't exist, creating via %s%n", modifiedTableName, tableCreation);
stmt.execute(tableCreation);
}
}
}
} catch (SQLException t) {
throw new AssertionError(t);
} finally {
factory.releaseConnection(connection);
factory.stop();
}
}
String tableToSearch(String tableName) {
if (DB_TYPE == DatabaseType.POSTGRES) return tableName.toLowerCase();
return tableName.toUpperCase();
}
private PooledConnectionFactoryConfigurationBuilder<?> addJdbcConnection(AbstractSchemaJdbcConfigurationBuilder<?, ?> builder) {
if(JDBC_URL != null && JDBC_PASSWORD != null && JDBC_USERNAME != null) {
JdbcConnection jdbcConnection = databasesFromSystemProperty.get(DB_TYPE);
return builder.connectionPool()
.connectionUrl(jdbcConnection.getJdbcUrl())
.username(jdbcConnection.getUsername())
.password(jdbcConnection.getPassword());
}
throw new IllegalArgumentException("JDBC connection wasn't provided through System Properties");
}
protected static HashMap<DatabaseType, JdbcConnection> getDatabases() {
Objects.requireNonNull(JDBC_URL);
Objects.requireNonNull(JDBC_USERNAME);
Objects.requireNonNull(JDBC_PASSWORD);
Objects.requireNonNull(DATABASE);
List<DatabaseType> databaseTypes = Arrays.stream(DATABASE.split(",")).map(DatabaseType::guessDialect).collect(Collectors.toList());
HashMap<DatabaseType, JdbcConnection> map = new HashMap<>();
for (int i = 0; i < databaseTypes.size(); i++) {
String jdbcURL = JDBC_URL.split(",")[i];
String username = JDBC_USERNAME.split(",")[i];
String password = JDBC_PASSWORD.split(",")[i];
JdbcConnection jdbcConnection = new JdbcConnection(jdbcURL, username, password);
DatabaseType databaseType = databaseTypes.get(i);
map.put(databaseType, jdbcConnection);
}
return map;
}
}