]> source.dussan.org Git - sonarqube.git/blob
127933294d97f913d79ae38aea5157b969d63e52
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2009 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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
19  */
20 package org.sonar.plugins.core.timemachine;
21
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;
28
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32
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.*;
40
41 public class PastSnapshotFinderTest {
42
43   private PastSnapshotFinderByDays finderByDays;
44   private PastSnapshotFinderByDate finderByDate;
45   private PastSnapshotFinderByVersion finderByVersion;
46   private PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis;
47   private PastSnapshotFinder finder;
48
49   @Before
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);
56   }
57
58   @Test
59   public void shouldFindByNumberOfDays() {
60     when(finderByDays.findFromDays(30)).thenReturn(new PastSnapshot("days", null).setModeParameter("30"));
61
62     PastSnapshot variationSnapshot = finder.find(1, "30");
63
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"));
69   }
70
71   @Test
72   public void shouldNotFindByNumberOfDays() {
73     PastSnapshot variationSnapshot = finder.find(1, "30");
74
75     verify(finderByDays).findFromDays(30);
76     assertNull(variationSnapshot);
77   }
78
79   @Test
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()));
84
85     PastSnapshot variationSnapshot = finder.find(2, "2010-05-18");
86
87     verify(finderByDate).findByDate(argThat(new BaseMatcher<Date>() {
88       public boolean matches(Object o) {
89         return o.equals(date);
90       }
91
92       public void describeTo(Description description) {
93
94       }
95     }));
96     assertThat(variationSnapshot.getIndex(), is(2));
97     assertThat(variationSnapshot.getMode(), is("date"));
98     assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
99   }
100
101   @Test
102   public void shouldNotFindByDate() throws ParseException {
103     when(finderByDate.findByDate((Date) anyObject())).thenReturn(null);
104
105     PastSnapshot variationSnapshot = finder.find(2, "2010-05-18");
106
107     verify(finderByDate).findByDate((Date) anyObject());
108     assertNull(variationSnapshot);
109   }
110
111   @Test
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));
118
119     PastSnapshot variationSnapshot = finder.find(2, PastSnapshotFinderByPreviousAnalysis.MODE);
120
121     verify(finderByPreviousAnalysis).findByPreviousAnalysis();
122     assertThat(variationSnapshot.getIndex(), is(2));
123     assertThat(variationSnapshot.getMode(), is(PastSnapshotFinderByPreviousAnalysis.MODE));
124     assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
125   }
126
127   @Test
128   public void shouldNotFindPreviousAnalysis() {
129     when(finderByPreviousAnalysis.findByPreviousAnalysis()).thenReturn(null);
130
131     PastSnapshot variationSnapshot = finder.find(2, PastSnapshotFinderByPreviousAnalysis.MODE);
132
133     verify(finderByPreviousAnalysis).findByPreviousAnalysis();
134
135     assertNull(variationSnapshot);
136   }
137
138   @Test
139   public void shouldFindByVersion() {
140     when(finderByVersion.findByVersion("1.2")).thenReturn(new PastSnapshot("version", new Snapshot()));
141
142     PastSnapshot variationSnapshot = finder.find(2, "1.2");
143
144     verify(finderByVersion).findByVersion("1.2");
145     assertThat(variationSnapshot.getIndex(), is(2));
146     assertThat(variationSnapshot.getMode(), is("version"));
147     assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
148   }
149
150   @Test
151   public void shouldNotFindVersion() {
152     when(finderByVersion.findByVersion("1.2")).thenReturn(null);
153
154     PastSnapshot variationSnapshot = finder.find(2, "1.2");
155
156     verify(finderByVersion).findByVersion("1.2");
157     assertNull(variationSnapshot);
158   }
159
160   @Test
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"));
164   }
165
166   @Test
167   public void shouldGetPropertyValue() {
168     PropertiesConfiguration conf = new PropertiesConfiguration();
169     conf.setProperty("sonar.timemachine.period1", "5");
170
171     assertThat(PastSnapshotFinder.getPropertyValue(conf, 1), is("5"));
172     assertThat(PastSnapshotFinder.getPropertyValue(conf, 999), nullValue());
173   }
174
175   @Test
176   public void shouldGetDefaultPropertyValue() {
177     PropertiesConfiguration conf = new PropertiesConfiguration();
178     conf.setProperty("sonar.timemachine.period1", "5");
179
180     assertThat(PastSnapshotFinder.getPropertyValue(conf, 2), is(PastSnapshotFinder.DEFAULT_VALUE_2));
181   }
182 }