*/
package org.sonar.server.issue.index;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
import org.junit.Before;
import org.junit.Rule;
import org.sonar.db.component.SnapshotDto;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.mock;
tuple("PULL_REQUEST", null, "pr_1"));
}
+ @Test
+ public void verify_comparator_transitivity() {
+ Map<String, SnapshotDto> map = new HashMap<>();
+ map.put("A", new SnapshotDto().setCreatedAt(1L));
+ map.put("B", new SnapshotDto().setCreatedAt(2L));
+ map.put("C", new SnapshotDto().setCreatedAt(-1L));
+ List<String> uuids = new ArrayList<>(map.keySet());
+ uuids.add("D");
+ Comparators.verifyTransitivity(AsyncIssueIndexingImpl.compareBySnapshot(map), uuids);
+ }
+
+ @Test
+ public void trigger_with_lot_of_not_analyzed_project_should_not_raise_exception() {
+ for (int i = 0; i < 100; i++) {
+ BranchDto dto = new BranchDto()
+ .setBranchType(BRANCH)
+ .setKey("branch_" + i)
+ .setUuid("branch_uuid" + i)
+ .setProjectUuid("project_uuid" + i);
+ dbClient.branchDao().insert(dbTester.getSession(), dto);
+ dbTester.commit();
+ insertSnapshot("analysis_" + i, "project_uuid" + i, 1);
+ }
+
+ for (int i = 100; i < 200; i++) {
+ BranchDto dto = new BranchDto()
+ .setBranchType(BRANCH)
+ .setKey("branch_" + i)
+ .setUuid("branch_uuid" + i)
+ .setProjectUuid("project_uuid" + i);
+ dbClient.branchDao().insert(dbTester.getSession(), dto);
+ dbTester.commit();
+ }
+
+ assertThatCode(underTest::triggerOnIndexCreation).doesNotThrowAnyException();
+ }
+
private SnapshotDto insertSnapshot(String analysisUuid, String projectUuid, long createdAt) {
SnapshotDto snapshot = new SnapshotDto()
.setUuid(analysisUuid)
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.server.issue.index;
+
+import java.util.Collection;
+import java.util.Comparator;
+
+public final class Comparators {
+
+ /**
+ * Prevent construction.
+ */
+ private Comparators() {
+ }
+
+ /**
+ * Verify that a comparator is transitive.
+ *
+ * @param <T> the type being compared
+ * @param comparator the comparator to test
+ * @param elements the elements to test against
+ * @throws AssertionError if the comparator is not transitive
+ */
+ public static <T> void verifyTransitivity(Comparator<T> comparator, Collection<T> elements) {
+ for (T first : elements) {
+ for (T second : elements) {
+ int result1 = comparator.compare(first, second);
+ int result2 = comparator.compare(second, first);
+ if (result1 != -result2) {
+ // Uncomment the following line to step through the failed case
+ // comparator.compare(first, second);
+ throw new AssertionError("compare(" + first + ", " + second + ") == " + result1 +
+ " but swapping the parameters returns " + result2);
+ }
+ }
+ }
+ for (T first : elements) {
+ for (T second : elements) {
+ int firstGreaterThanSecond = comparator.compare(first, second);
+ if (firstGreaterThanSecond <= 0)
+ continue;
+ for (T third : elements) {
+ int secondGreaterThanThird = comparator.compare(second, third);
+ if (secondGreaterThanThird <= 0)
+ continue;
+ int firstGreaterThanThird = comparator.compare(first, third);
+ if (firstGreaterThanThird <= 0) {
+ throw new AssertionError("compare(" + first + ", " + second + ") > 0, " +
+ "compare(" + second + ", " + third + ") > 0, but compare(" + first + ", " + third + ") == " +
+ firstGreaterThanThird);
+ }
+ }
+ }
+ }
+ }
+
+}