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
|
/*
* SonarQube
* Copyright (C) 2009-2017 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.purge;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
public class PurgeCommandsTest {
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
private PurgeProfiler profiler = new PurgeProfiler();
/**
* Test that SQL queries execution do not fail with a huge number of parameter
*/
@Test
public void should_not_fail_when_deleting_huge_number_of_analyses() {
new PurgeCommands(dbTester.getSession(), profiler).deleteAnalyses(getHugeNumberOfIdUuidPairs());
// The goal of this test is only to check that the query do no fail, not to check result
}
/**
* Test that all related data is purged.
*/
@Test
public void shouldPurgeAnalysis() {
dbTester.prepareDbUnit(getClass(), "shouldPurgeAnalysis.xml");
new PurgeCommands(dbTester.getSession(), profiler).purgeAnalyses(singletonList(new IdUuidPair(1, "u1")));
dbTester.assertDbUnit(getClass(), "shouldPurgeAnalysis-result.xml", "snapshots", "project_measures", "duplications_index", "events");
}
@Test
public void delete_wasted_measures_when_purging_analysis() {
dbTester.prepareDbUnit(getClass(), "shouldDeleteWastedMeasuresWhenPurgingAnalysis.xml");
new PurgeCommands(dbTester.getSession(), profiler).purgeAnalyses(singletonList(new IdUuidPair(1, "u1")));
dbTester.assertDbUnit(getClass(), "shouldDeleteWastedMeasuresWhenPurgingAnalysis-result.xml", "project_measures");
}
/**
* Test that SQL queries execution do not fail with a huge number of parameter
*/
@Test
public void should_not_fail_when_purging_huge_number_of_analyses() {
new PurgeCommands(dbTester.getSession(), profiler).purgeAnalyses(getHugeNumberOfIdUuidPairs());
// The goal of this test is only to check that the query do no fail, not to check result
}
@Test
public void shouldDeleteComponentsAndChildrenTables() {
dbTester.prepareDbUnit(getClass(), "shouldDeleteResource.xml");
PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler);
purgeCommands.deleteComponents(newArrayList(new IdUuidPair(1L, "uuid_1")));
assertThat(dbTester.countRowsOfTable("projects")).isZero();
assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
assertThat(dbTester.countRowsOfTable("events")).isEqualTo(2);
assertThat(dbTester.countRowsOfTable("issues")).isZero();
assertThat(dbTester.countRowsOfTable("issue_changes")).isZero();
assertThat(dbTester.countRowsOfTable("authors")).isZero();
}
@Test
public void shouldDeleteAnalyses() {
dbTester.prepareDbUnit(getClass(), "shouldDeleteResource.xml");
PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler);
purgeCommands.deleteAnalyses("uuid_1");
assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(1);
assertThat(dbTester.countRowsOfTable("snapshots")).isZero();
assertThat(dbTester.countRowsOfTable("events")).isEqualTo(0);
assertThat(dbTester.countRowsOfTable("issues")).isEqualTo(1);
assertThat(dbTester.countRowsOfTable("issue_changes")).isEqualTo(1);
assertThat(dbTester.countRowsOfTable("authors")).isEqualTo(2);
}
/**
* Test that SQL queries execution do not fail with a huge number of parameter
*/
@Test
public void should_not_fail_when_deleting_huge_number_of_resources() {
new PurgeCommands(dbTester.getSession(), profiler).deleteComponents(getHugeNumberOfIdUuids());
// The goal of this test is only to check that the query do no fail, not to check result
}
private List<IdUuidPair> getHugeNumberOfIdUuids() {
List<IdUuidPair> hugeNbOfSnapshotIds = newArrayList();
for (long i = 0; i < 4500; i++) {
hugeNbOfSnapshotIds.add(new IdUuidPair(i, String.valueOf(i)));
}
return hugeNbOfSnapshotIds;
}
private List<IdUuidPair> getHugeNumberOfIdUuidPairs() {
List<IdUuidPair> hugeNbOfSnapshotIds = newArrayList();
for (long i = 0; i < 4500; i++) {
hugeNbOfSnapshotIds.add(new IdUuidPair(i, "uuid_" + i));
}
return hugeNbOfSnapshotIds;
}
}
|