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 junit.framework.ComparisonFailure;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.notifications.Notification;
26 import org.sonar.api.notifications.NotificationManager;
27 import org.sonar.api.resources.Project;
28 import org.sonar.api.security.UserFinder;
29 import org.sonar.jpa.entity.Review;
30 import org.sonar.jpa.test.AbstractDbUnitTestCase;
32 import static org.hamcrest.Matchers.is;
33 import static org.junit.Assert.*;
34 import static org.mockito.Matchers.any;
35 import static org.mockito.Mockito.*;
37 public class CloseReviewsDecoratorTest extends AbstractDbUnitTestCase {
39 private NotificationManager notificationManager;
40 private CloseReviewsDecorator reviewsDecorator;
44 Project project = mock(Project.class);
45 when(project.getRoot()).thenReturn(project);
46 notificationManager = mock(NotificationManager.class);
47 reviewsDecorator = new CloseReviewsDecorator(project, null, getSession(), notificationManager, mock(UserFinder.class));
51 public void testShouldExecuteOnProject() throws Exception {
52 Project project = mock(Project.class);
53 when(project.isLatestAnalysis()).thenReturn(true);
54 assertTrue(reviewsDecorator.shouldExecuteOnProject(project));
58 public void shouldCloseReviewWithoutCorrespondingViolation() throws Exception {
61 int count = reviewsDecorator.closeReviewsOnResolvedViolations(null, 666, 222);
63 assertThat(count, is(3));
64 verify(notificationManager, times(3)).scheduleForSending(any(Notification.class));
65 checkTablesWithExcludedColumns("shouldCloseReviewWithoutCorrespondingViolation", new String[]{"updated_at"}, "reviews");
68 checkTables("shouldCloseReviewWithoutCorrespondingViolation", "reviews");
69 fail("'updated_at' columns are identical whereas they should be different.");
70 } catch (ComparisonFailure e) {
71 // "updated_at" column must be different, so the comparison should raise this exception
76 public void shouldReopenResolvedReviewWithNonFixedViolation() throws Exception {
79 // First we close the reviews for which the violations have been fixed (this is because we use the same "fixture"...)
80 reviewsDecorator.closeReviewsOnResolvedViolations(null, 666, 222);
82 // And now we reopen the reviews that still have a violation
83 int count = reviewsDecorator.reopenReviewsOnUnresolvedViolations(null, 666);
85 assertThat(count, is(1));
86 verify(notificationManager, times(4)).scheduleForSending(any(Notification.class));
87 checkTablesWithExcludedColumns("shouldReopenResolvedReviewWithNonFixedViolation", new String[]{"updated_at"}, "reviews");
90 checkTables("shouldReopenResolvedReviewWithNonFixedViolation", "reviews");
91 fail("'updated_at' columns are identical whereas they should be different.");
92 } catch (ComparisonFailure e) {
93 // "updated_at" column must be different, so the comparison should raise this exception
98 public void shouldCloseResolvedManualViolations() throws Exception {
99 setupData("shouldCloseResolvedManualViolations");
101 int count = reviewsDecorator.closeReviewsOnResolvedViolations(null, 555, 11);
102 assertThat(count, is(1));
104 verify(notificationManager, times(1)).scheduleForSending(any(Notification.class));
106 getSession().commit();
107 assertThat(getSession().getSingleResult(Review.class, "id", 1L).getStatus(), is("CLOSED")); // automatic violation not changed
108 assertThat(getSession().getSingleResult(Review.class, "id", 2L).getStatus(), is("CLOSED")); // manual violation resolved -> closed
109 assertThat(getSession().getSingleResult(Review.class, "id", 3L).getStatus(), is("OPEN")); // manual violation not changed