2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
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.timemachine;
22 import org.apache.commons.configuration.PropertiesConfiguration;
23 import org.hamcrest.BaseMatcher;
24 import org.hamcrest.Description;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.sonar.api.database.model.Snapshot;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
33 import static junit.framework.Assert.assertNull;
34 import static org.hamcrest.Matchers.nullValue;
35 import static org.hamcrest.core.Is.is;
36 import static org.hamcrest.core.IsNot.not;
37 import static org.junit.Assert.assertNotNull;
38 import static org.junit.Assert.assertThat;
39 import static org.mockito.Mockito.*;
41 public class PastSnapshotFinderTest {
43 private PastSnapshotFinderByDays finderByDays;
44 private PastSnapshotFinderByDate finderByDate;
45 private PastSnapshotFinderByVersion finderByVersion;
46 private PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis;
47 private PastSnapshotFinder finder;
50 public void initFinders() {
51 finderByDays = mock(PastSnapshotFinderByDays.class);
52 finderByDate = mock(PastSnapshotFinderByDate.class);
53 finderByVersion = mock(PastSnapshotFinderByVersion.class);
54 finderByPreviousAnalysis = mock(PastSnapshotFinderByPreviousAnalysis.class);
55 finder = new PastSnapshotFinder(finderByDays, finderByVersion, finderByDate, finderByPreviousAnalysis);
59 public void shouldFindByNumberOfDays() {
60 when(finderByDays.findFromDays(30)).thenReturn(new PastSnapshot("days", null).setModeParameter("30"));
62 PastSnapshot variationSnapshot = finder.find(1, "30");
64 verify(finderByDays).findFromDays(30);
65 assertNotNull(variationSnapshot);
66 assertThat(variationSnapshot.getIndex(), is(1));
67 assertThat(variationSnapshot.getMode(), is("days"));
68 assertThat(variationSnapshot.getModeParameter(), is("30"));
72 public void shouldNotFindByNumberOfDays() {
73 PastSnapshot variationSnapshot = finder.find(1, "30");
75 verify(finderByDays).findFromDays(30);
76 assertNull(variationSnapshot);
80 public void shouldFindByDate() throws ParseException {
81 final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
82 final Date date = format.parse("2010-05-18");
83 when(finderByDate.findByDate(date)).thenReturn(new PastSnapshot("date", new Snapshot()));
85 PastSnapshot variationSnapshot = finder.find(2, "2010-05-18");
87 verify(finderByDate).findByDate(argThat(new BaseMatcher<Date>() {
88 public boolean matches(Object o) {
89 return o.equals(date);
92 public void describeTo(Description description) {
96 assertThat(variationSnapshot.getIndex(), is(2));
97 assertThat(variationSnapshot.getMode(), is("date"));
98 assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
102 public void shouldNotFindByDate() throws ParseException {
103 when(finderByDate.findByDate((Date) anyObject())).thenReturn(null);
105 PastSnapshot variationSnapshot = finder.find(2, "2010-05-18");
107 verify(finderByDate).findByDate((Date) anyObject());
108 assertNull(variationSnapshot);
112 public void shouldFindByPreviousAnalysis() throws ParseException {
113 final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
114 final Date date = format.parse("2010-05-18");
115 Snapshot snapshot = new Snapshot();
116 snapshot.setCreatedAt(date);
117 when(finderByPreviousAnalysis.findByPreviousAnalysis()).thenReturn(new PastSnapshot(PastSnapshotFinderByPreviousAnalysis.MODE, snapshot));
119 PastSnapshot variationSnapshot = finder.find(2, PastSnapshotFinderByPreviousAnalysis.MODE);
121 verify(finderByPreviousAnalysis).findByPreviousAnalysis();
122 assertThat(variationSnapshot.getIndex(), is(2));
123 assertThat(variationSnapshot.getMode(), is(PastSnapshotFinderByPreviousAnalysis.MODE));
124 assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
128 public void shouldNotFindPreviousAnalysis() {
129 when(finderByPreviousAnalysis.findByPreviousAnalysis()).thenReturn(null);
131 PastSnapshot variationSnapshot = finder.find(2, PastSnapshotFinderByPreviousAnalysis.MODE);
133 verify(finderByPreviousAnalysis).findByPreviousAnalysis();
135 assertNull(variationSnapshot);
139 public void shouldFindByVersion() {
140 when(finderByVersion.findByVersion("1.2")).thenReturn(new PastSnapshot("version", new Snapshot()));
142 PastSnapshot variationSnapshot = finder.find(2, "1.2");
144 verify(finderByVersion).findByVersion("1.2");
145 assertThat(variationSnapshot.getIndex(), is(2));
146 assertThat(variationSnapshot.getMode(), is("version"));
147 assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
151 public void shouldNotFindVersion() {
152 when(finderByVersion.findByVersion("1.2")).thenReturn(null);
154 PastSnapshot variationSnapshot = finder.find(2, "1.2");
156 verify(finderByVersion).findByVersion("1.2");
157 assertNull(variationSnapshot);
161 public void shouldNotFailIfUnknownFormat() {
162 when(finderByPreviousAnalysis.findByPreviousAnalysis()).thenReturn(new PastSnapshot(PastSnapshotFinderByPreviousAnalysis.MODE, new Snapshot())); // should not be called
163 assertNull(finder.find(2, "foooo"));
167 public void shouldGetPropertyValue() {
168 PropertiesConfiguration conf = new PropertiesConfiguration();
169 conf.setProperty("sonar.timemachine.period1", "5");
171 assertThat(PastSnapshotFinder.getPropertyValue(conf, 1), is("5"));
172 assertThat(PastSnapshotFinder.getPropertyValue(conf, 999), nullValue());
176 public void shouldGetDefaultPropertyValue() {
177 PropertiesConfiguration conf = new PropertiesConfiguration();
178 conf.setProperty("sonar.timemachine.period1", "5");
180 assertThat(PastSnapshotFinder.getPropertyValue(conf, 2), is(PastSnapshotFinder.DEFAULT_VALUE_2));