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
|
/*
* SonarQube
* Copyright (C) 2009-2021 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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 3 of the License, or (at your option) any later version.
*
* This program 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 program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.AssumptionViolatedException;
import org.sonar.api.config.internal.Settings;
import org.sonar.db.dialect.H2;
import org.sonar.process.logging.LogbackHelper;
class TestDbImpl extends CoreTestDb {
private static TestDbImpl defaultSchemaBaseTestDb;
// instantiating MyBatis objects is costly => we cache them for default schema
private static final Map<Set<String>, TestDbImpl> defaultSchemaTestDbsWithExtensions = new HashMap<>();
private boolean isDefault;
private MyBatis myBatis;
private TestDbImpl(@Nullable String schemaPath, MyBatisConfExtension... confExtensions) {
super();
isDefault = (schemaPath == null);
init(schemaPath, confExtensions);
}
private TestDbImpl(TestDbImpl base, MyBatis myBatis) {
super(base.getDatabase());
this.isDefault = base.isDefault;
this.myBatis = myBatis;
}
private void init(@Nullable String schemaPath, MyBatisConfExtension[] confExtensions) {
Consumer<Settings> loadOrchestratorSettings = settings -> {
OrchestratorSettingsUtils.loadOrchestratorSettings(settings);
};
Function<Settings, Database> databaseCreator = settings -> {
String dialect = settings.getString("sonar.jdbc.dialect");
if (dialect != null && !"h2".equals(dialect)) {
return new DefaultDatabase(new LogbackHelper(), settings);
}
return SQDatabase.newH2Database("h2Tests" + DigestUtils.md5Hex(StringUtils.defaultString(schemaPath)), schemaPath == null);
};
Consumer<Database> schemaPathExecutor = database -> {
if (schemaPath == null) {
return;
}
// scripts are assumed to be using H2 specific syntax, ignore the test if not on H2
if (!database.getDialect().getId().equals("h2")) {
database.stop();
throw new AssumptionViolatedException("This test is intended to be run on H2 only");
}
((SQDatabase) database).executeScript(schemaPath);
};
BiConsumer<Database, Boolean> createMyBatis = (db, created) -> myBatis = newMyBatis(db, confExtensions);
init(loadOrchestratorSettings, databaseCreator, schemaPathExecutor, createMyBatis);
}
private static MyBatis newMyBatis(Database db, MyBatisConfExtension[] extensions) {
MyBatis newMyBatis = new MyBatis(db, extensions);
newMyBatis.start();
return newMyBatis;
}
static TestDbImpl create(@Nullable String schemaPath, MyBatisConfExtension... confExtensions) {
if (schemaPath == null) {
if (defaultSchemaBaseTestDb == null) {
defaultSchemaBaseTestDb = new TestDbImpl(null);
}
if (confExtensions.length > 0) {
Set<String> key = Arrays.stream(confExtensions)
.flatMap(MyBatisConfExtension::getMapperClasses)
.map(Class::getName)
.collect(Collectors.toSet());
return defaultSchemaTestDbsWithExtensions.computeIfAbsent(
key,
k -> new TestDbImpl(defaultSchemaBaseTestDb, newMyBatis(defaultSchemaBaseTestDb.getDatabase(), confExtensions)));
}
return defaultSchemaBaseTestDb;
}
return new TestDbImpl(schemaPath, confExtensions);
}
@Override
public void start() {
if (!isDefault && !H2.ID.equals(getDatabase().getDialect().getId())) {
throw new AssumptionViolatedException("Test disabled because it supports only H2");
}
}
@Override
public void stop() {
if (!isDefault) {
super.stop();
}
}
MyBatis getMyBatis() {
return myBatis;
}
}
|