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.dbcleaner.runner;
22 import org.hamcrest.BaseMatcher;
23 import org.hamcrest.Description;
24 import org.junit.Test;
25 import org.sonar.api.database.model.Snapshot;
26 import org.sonar.api.resources.Project;
27 import org.sonar.jpa.test.AbstractDbUnitTestCase;
28 import org.sonar.plugins.dbcleaner.api.Purge;
29 import org.sonar.plugins.dbcleaner.api.PurgeContext;
31 import javax.persistence.PersistenceException;
33 import static org.junit.Assert.assertFalse;
34 import static org.junit.Assert.assertTrue;
35 import static org.mockito.Matchers.argThat;
36 import static org.mockito.Mockito.*;
38 public class DeprecatedPurgePostJobTest extends AbstractDbUnitTestCase {
41 public void shouldExecutePurges() {
43 final int currentSID = 400;
44 final int previousSID = 300;
45 Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", currentSID);
47 Purge purge1 = mock(Purge.class);
48 Purge purge2 = mock(Purge.class);
49 Purge[] purges = new Purge[]{purge1, purge2};
51 new DeprecatedPurgePostJob(getSession(), new Project("key"), snapshot, purges).purge();
53 verify(purge1).purge(argThat(new BaseMatcher<PurgeContext>() {
54 public boolean matches(Object o) {
55 PurgeContext context = (PurgeContext) o;
56 return context.getSnapshotId() == currentSID && context.getPreviousSnapshotId() == previousSID;
59 public void describeTo(Description description) {
65 public void shouldExecutePurgesEvenIfSingleAnalysis() {
67 final int currentSID = 1000;
68 Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", currentSID);
70 Purge purge1 = mock(Purge.class);
71 Purge purge2 = mock(Purge.class);
72 Purge[] purges = new Purge[]{purge1, purge2};
74 new DeprecatedPurgePostJob(getSession(), new Project("key"), snapshot, purges).purge();
76 verify(purge1).purge(argThat(new BaseMatcher<PurgeContext>() {
77 public boolean matches(Object o) {
78 PurgeContext context = (PurgeContext) o;
79 return context.getSnapshotId() == currentSID && context.getPreviousSnapshotId() == null;
82 public void describeTo(Description description) {
88 public void shouldExecuteOnlyOnRootProjects() {
89 Project project = mock(Project.class);
90 when(project.isRoot()).thenReturn(true);
91 assertTrue(DeprecatedPurgePostJob.shouldExecuteOn(project));
93 when(project.isRoot()).thenReturn(false);
94 assertFalse(DeprecatedPurgePostJob.shouldExecuteOn(project));
98 * See https://jira.codehaus.org/browse/SONAR-2961
99 * Temporarily ignore MySQL deadlocks
102 public void shouldIgnoreDeadlocks() {
103 Purge purge = mock(Purge.class);
104 doThrow(new PersistenceException()).when(purge).purge((PurgeContext) anyObject());
106 DeprecatedPurgePostJob runner = new DeprecatedPurgePostJob(getSession(), new Project(""), new Snapshot(), new Purge[]{purge});
107 runner.purge();// must not raise any exceptions
109 verify(purge).purge((PurgeContext) anyObject());