From 59adbb849b1ce3338e6299dea939c454df7d0cdb Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 26 Nov 2020 20:32:28 -0600 Subject: [PATCH] Fix cache of MyBatis configuration extensions in unit tests The cache used a map with an ineffective key, that changed in every call. It now depends on the set of extensions being loaded. --- .../java/org/sonar/db/DbTester.java | 10 ---------- .../java/org/sonar/db/TestDbImpl.java | 17 ++++++++++++----- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java index 33c99ce4421..bab3211c881 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java @@ -28,7 +28,6 @@ import java.util.Map; import java.util.stream.Stream; import javax.annotation.Nullable; import org.apache.commons.dbcp2.BasicDataSource; -import org.apache.commons.lang.StringUtils; import org.picocontainer.containers.TransientPicoContainer; import org.sonar.api.utils.System2; import org.sonar.core.util.SequenceUuidFactory; @@ -174,11 +173,6 @@ public class DbTester extends AbstractDbTester { return this; } - public DbTester enableOrganizations() { - properties().insertInternal("organization.enabled", "true"); - return this; - } - @Override protected void before() { db.start(); @@ -199,10 +193,6 @@ public class DbTester extends AbstractDbTester { } } - public boolean hasDefaultOrganization() { - return defaultOrganization != null; - } - public OrganizationDto getDefaultOrganization() { checkState(defaultOrganization != null, "Default organization has not been created"); return defaultOrganization; diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/TestDbImpl.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/TestDbImpl.java index a8612bcbb25..1f7f2e082ae 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/TestDbImpl.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/TestDbImpl.java @@ -19,11 +19,14 @@ */ package org.sonar.db; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; 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; @@ -35,7 +38,7 @@ 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 defaultSchemaTestDbsWithExtensions = new HashMap<>(); + private static final Map, TestDbImpl> defaultSchemaTestDbsWithExtensions = new HashMap<>(); private boolean isDefault; private MyBatis myBatis; @@ -86,15 +89,19 @@ class TestDbImpl extends CoreTestDb { } static TestDbImpl create(@Nullable String schemaPath, MyBatisConfExtension... confExtensions) { - MyBatisConfExtension[] extensionArray = confExtensions.length == 0 ? null : confExtensions; if (schemaPath == null) { if (defaultSchemaBaseTestDb == null) { defaultSchemaBaseTestDb = new TestDbImpl(null); } - if (extensionArray != null) { + if (confExtensions.length > 0) { + List key = Arrays.stream(confExtensions) + .flatMap(MyBatisConfExtension::getMapperClasses) + .map(Class::getName) + .sorted() + .collect(Collectors.toList()); return defaultSchemaTestDbsWithExtensions.computeIfAbsent( - extensionArray, - extensions -> new TestDbImpl(defaultSchemaBaseTestDb, newMyBatis(defaultSchemaBaseTestDb.getDatabase(), extensions))); + key, + k -> new TestDbImpl(defaultSchemaBaseTestDb, newMyBatis(defaultSchemaBaseTestDb.getDatabase(), confExtensions))); } return defaultSchemaBaseTestDb; } -- 2.39.5