]> source.dussan.org Git - sonarqube.git/blob
a32ea0bb51dc72b00d03192958d8b32722ae5736
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2013 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
6  * SonarQube is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * SonarQube is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20
21 package org.sonar.plugins.core.issue;
22
23 import java.util.Collections;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.ClassRule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.sonar.api.CoreProperties;
30 import org.sonar.batch.bootstrap.BootstrapProperties;
31 import org.sonar.batch.bootstrap.BootstrapSettings;
32 import org.sonar.batch.bootstrap.TempFolderProvider;
33 import org.sonar.batch.index.Caches;
34 import org.sonar.core.issue.db.IssueChangeDto;
35 import org.sonar.core.issue.db.IssueDto;
36
37 import java.io.IOException;
38 import java.util.List;
39
40 import static org.fest.assertions.Assertions.assertThat;
41
42 public class InitialOpenIssuesStackTest {
43
44   @ClassRule
45   public static TemporaryFolder temp = new TemporaryFolder();
46
47   public static Caches createCacheOnTemp(TemporaryFolder temp) {
48     BootstrapSettings bootstrapSettings = new BootstrapSettings(
49       new BootstrapProperties(Collections.<String,String>emptyMap())
50     );
51     try {
52       bootstrapSettings.properties().put(CoreProperties.WORKING_DIRECTORY, temp.newFolder().getAbsolutePath());
53     } catch (IOException e) {
54       throw new RuntimeException(e);
55     }
56     return new Caches(new TempFolderProvider().provide(bootstrapSettings));
57   }
58
59   InitialOpenIssuesStack stack;
60   Caches caches;
61
62   @Before
63   public void setUp() throws Exception {
64     caches = createCacheOnTemp(temp);
65     caches.start();
66     stack = new InitialOpenIssuesStack(caches);
67   }
68
69   @After
70   public void tearDown() {
71     caches.stop();
72   }
73
74   @Test
75   public void get_and_remove_issues() {
76     IssueDto issueDto = new IssueDto().setComponentKey_unit_test_only("org.struts.Action").setKee("ISSUE-1");
77     stack.addIssue(issueDto);
78
79     List<IssueDto> issueDtos = stack.selectAndRemoveIssues("org.struts.Action");
80     assertThat(issueDtos).hasSize(1);
81     assertThat(issueDtos.get(0).getKee()).isEqualTo("ISSUE-1");
82
83     assertThat(stack.selectAllIssues()).isEmpty();
84   }
85
86   @Test
87   public void get_and_remove_with_many_issues_on_same_resource() {
88     stack.addIssue(new IssueDto().setComponentKey_unit_test_only("org.struts.Action").setKee("ISSUE-1"));
89     stack.addIssue(new IssueDto().setComponentKey_unit_test_only("org.struts.Action").setKee("ISSUE-2"));
90
91     List<IssueDto> issueDtos = stack.selectAndRemoveIssues("org.struts.Action");
92     assertThat(issueDtos).hasSize(2);
93
94     assertThat(stack.selectAllIssues()).isEmpty();
95   }
96
97   @Test
98   public void get_and_remove_do_nothing_if_resource_not_found() {
99     stack.addIssue(new IssueDto().setComponentKey_unit_test_only("org.struts.Action").setKee("ISSUE-1"));
100
101     List<IssueDto> issueDtos = stack.selectAndRemoveIssues("Other");
102     assertThat(issueDtos).hasSize(0);
103
104     assertThat(stack.selectAllIssues()).hasSize(1);
105   }
106
107   @Test
108   public void select_changelog() {
109     stack.addChangelog(new IssueChangeDto().setKey("CHANGE-1").setIssueKey("ISSUE-1"));
110     stack.addChangelog(new IssueChangeDto().setKey("CHANGE-2").setIssueKey("ISSUE-1"));
111
112     List<IssueChangeDto> issueChangeDtos = stack.selectChangelog("ISSUE-1");
113     assertThat(issueChangeDtos).hasSize(2);
114     assertThat(issueChangeDtos.get(0).getKey()).isEqualTo("CHANGE-1");
115     assertThat(issueChangeDtos.get(1).getKey()).isEqualTo("CHANGE-2");
116   }
117
118   @Test
119   public void return_empty_changelog() {
120     assertThat(stack.selectChangelog("ISSUE-1")).isEmpty();
121   }
122
123   @Test
124   public void clear_issues() {
125     stack.addIssue(new IssueDto().setComponentKey_unit_test_only("org.struts.Action").setKee("ISSUE-1"));
126
127     assertThat(stack.selectAllIssues()).hasSize(1);
128
129     // issues are not removed
130     assertThat(stack.selectAllIssues()).hasSize(1);
131
132     stack.clear();
133     assertThat(stack.selectAllIssues()).isEmpty();
134   }
135
136   @Test
137   public void clear_issues_changelog() {
138     stack.addChangelog(new IssueChangeDto().setKey("CHANGE-1").setIssueKey("ISSUE-1"));
139
140     assertThat(stack.selectChangelog("ISSUE-1")).hasSize(1);
141
142     stack.clear();
143     assertThat(stack.selectChangelog("ISSUE-1")).isEmpty();
144   }
145 }