]> source.dussan.org Git - sonarqube.git/blob
ad77611e563133ac519361b7b37731eaec52e22c
[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 package org.sonar.plugins.core.timemachine;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.sonar.api.database.model.RuleFailureModel;
25 import org.sonar.api.database.model.Snapshot;
26 import org.sonar.api.resources.JavaFile;
27 import org.sonar.api.resources.Project;
28 import org.sonar.api.rules.Rule;
29 import org.sonar.api.rules.RulePriority;
30 import org.sonar.api.rules.Violation;
31 import org.sonar.api.utils.DateUtils;
32 import org.sonar.batch.index.ResourcePersister;
33 import org.sonar.core.rule.DefaultRuleFinder;
34 import org.sonar.jpa.test.AbstractDbUnitTestCase;
35
36 import java.util.Arrays;
37
38 import static org.mockito.Matchers.any;
39 import static org.mockito.Matchers.eq;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42
43 public class ViolationPersisterDecoratorTest extends AbstractDbUnitTestCase {
44
45   private ViolationPersisterDecorator decorator;
46   private Rule rule1 = Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck", "Check Header");
47   private Rule rule2 = Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck", "Equals Avoid Null");
48   private JavaFile javaFile = new JavaFile("org.foo.Bar");
49   Project project = new Project("project");
50   private ViolationTrackingDecorator tracker;
51
52   @Before
53   public void before() {
54     setupData("shared");
55     Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", 1000);
56     ResourcePersister resourcePersister = mock(ResourcePersister.class);
57     when(resourcePersister.saveResource(any(Project.class), eq(javaFile))).thenReturn(snapshot);
58     when(resourcePersister.getSnapshot(javaFile)).thenReturn(snapshot);
59     tracker = mock(ViolationTrackingDecorator.class);
60     decorator = new ViolationPersisterDecorator(tracker, resourcePersister, new DefaultRuleFinder(getSessionFactory()), getSession());
61   }
62
63   @Test
64   public void shouldSaveViolations() {
65     Violation violation1a = Violation.create(rule1, javaFile)
66         .setSeverity(RulePriority.CRITICAL).setLineId(20).setCost(55.6).setMessage("the message")
67         .setChecksum("checksum").setCreatedAt(DateUtils.parseDate("2010-12-25"));
68     Violation violation1b = Violation.create(rule1, javaFile)
69         .setSeverity(RulePriority.CRITICAL).setLineId(50).setCost(80.0);
70     Violation violation2 = Violation.create(rule2, javaFile)
71         .setSeverity(RulePriority.MINOR).setSwitchedOff(true);
72
73     decorator.saveViolations(project, Arrays.asList(violation1a, violation1b, violation2));
74
75     checkTables("shouldSaveViolations", "rule_failures");
76   }
77
78   @Test
79   public void shouldCopyPermanentIdFromReferenceViolation() {
80     RuleFailureModel referenceViolation = getSession().getSingleResult(RuleFailureModel.class, "id", 1);
81     Violation violation = Violation.create(rule1, javaFile).setSeverity(RulePriority.MAJOR).setMessage("new message");
82     when(tracker.getReferenceViolation(violation)).thenReturn(referenceViolation);
83
84     decorator.saveViolations(project, Arrays.asList(violation));
85
86     checkTables("shouldCopyPermanentIdFromReferenceViolation", "rule_failures");
87   }
88 }