2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar 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.
11 * Sonar 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.core.sensors;
22 import com.google.common.base.Predicate;
23 import com.google.common.collect.Lists;
24 import org.hamcrest.BaseMatcher;
25 import org.hamcrest.Description;
26 import org.junit.Test;
27 import org.sonar.api.batch.DecoratorContext;
28 import org.sonar.api.resources.Project;
29 import org.sonar.api.rules.Rule;
30 import org.sonar.api.rules.RuleFinder;
31 import org.sonar.api.rules.RulePriority;
32 import org.sonar.api.rules.Violation;
33 import org.sonar.api.utils.DateUtils;
34 import org.sonar.core.review.ReviewDao;
35 import org.sonar.core.review.ReviewDto;
37 import java.util.Date;
39 import static org.mockito.Mockito.*;
41 public class ManualViolationInjectorTest {
44 public void shouldInjectManualViolationsDefinedByReviews() {
45 final Date createdAt = DateUtils.parseDate("2011-12-25");
46 ReviewDto review = new ReviewDto().setRuleId(3).setViolationPermanentId(100).setCreatedAt(createdAt).setSeverity(RulePriority.BLOCKER.toString());
47 ReviewDao dao = mock(ReviewDao.class);
48 when(dao.selectOpenByResourceId(eq(100L), (Predicate<ReviewDto>[])anyVararg())).thenReturn(Lists.newArrayList(review));
49 RuleFinder ruleFinder = mock(RuleFinder.class);
50 when(ruleFinder.findById(3)).thenReturn(new Rule());
51 DecoratorContext context = mock(DecoratorContext.class);
52 ManualViolationInjector injector = new ManualViolationInjector(dao, ruleFinder);
54 injector.decorate(new Project("key").setId(100), context);
56 verify(context, times(1)).saveViolation(argThat(new BaseMatcher<Violation>() {
57 public boolean matches(Object o) {
58 Violation v = (Violation) o;
59 return v.getPermanentId() == 100 && v.getRule() != null && v.isManual() && v.getCreatedAt().equals(createdAt)
60 && v.getSeverity().equals(RulePriority.BLOCKER);
63 public void describeTo(Description description) {