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.period;
22 import org.apache.commons.lang.ObjectUtils;
23 import org.hamcrest.BaseMatcher;
24 import org.hamcrest.Description;
25 import org.junit.Test;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
28 import org.sonar.api.config.Settings;
29 import org.sonar.core.purge.PurgeDao;
30 import org.sonar.core.purge.PurgeSnapshotQuery;
31 import org.sonar.core.purge.PurgeableSnapshotDto;
33 import java.util.Arrays;
34 import java.util.Date;
36 import static org.mockito.Matchers.argThat;
37 import static org.mockito.Mockito.*;
39 public class DefaultPeriodCleanerTest {
43 public void doClean() {
44 PurgeDao dao = mock(PurgeDao.class);
45 when(dao.selectPurgeableSnapshots(123L)).thenReturn(Arrays.asList(
46 new PurgeableSnapshotDto().setSnapshotId(999L).setDate(new Date())));
47 Filter filter1 = newLazyFilter();
48 Filter filter2 = newLazyFilter();
50 DefaultPeriodCleaner cleaner = new DefaultPeriodCleaner(dao, mock(Settings.class));
51 cleaner.doClean(123L, Arrays.asList(filter1, filter2));
53 verify(filter1).log();
54 verify(filter2).log();
55 verify(dao, times(2)).deleteSnapshots(argThat(newRootSnapshotQuery()));
56 verify(dao, times(2)).deleteSnapshots(argThat(newSnapshotIdQuery()));
59 private BaseMatcher<PurgeSnapshotQuery> newRootSnapshotQuery() {
60 return new BaseMatcher<PurgeSnapshotQuery>() {
61 public boolean matches(Object o) {
62 PurgeSnapshotQuery query = (PurgeSnapshotQuery) o;
63 return ObjectUtils.equals(query.getRootSnapshotId(), 999L);
66 public void describeTo(Description description) {
71 private BaseMatcher<PurgeSnapshotQuery> newSnapshotIdQuery() {
72 return new BaseMatcher<PurgeSnapshotQuery>() {
73 public boolean matches(Object o) {
74 PurgeSnapshotQuery query = (PurgeSnapshotQuery) o;
75 return ObjectUtils.equals(query.getId(), 999L);
78 public void describeTo(Description description) {
83 private Filter newLazyFilter() {
84 Filter filter1 = mock(Filter.class);
85 when(filter1.filter(anyListOf(PurgeableSnapshotDto.class))).thenAnswer(new Answer<Object>() {
86 public Object answer(InvocationOnMock invocation) throws Throwable {
87 return invocation.getArguments()[0];