1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.components;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.CoreProperties;
import org.sonar.api.database.model.Snapshot;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static junit.framework.Assert.assertNull;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class PastSnapshotFinderTest {
private PastSnapshotFinderByDays finderByDays;
private PastSnapshotFinderByDate finderByDate;
private PastSnapshotFinderByVersion finderByVersion;
private PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis;
private PastSnapshotFinder finder;
@Before
public void initFinders() {
finderByDays = mock(PastSnapshotFinderByDays.class);
finderByDate = mock(PastSnapshotFinderByDate.class);
finderByVersion = mock(PastSnapshotFinderByVersion.class);
finderByPreviousAnalysis = mock(PastSnapshotFinderByPreviousAnalysis.class);
finder = new PastSnapshotFinder(finderByDays, finderByVersion, finderByDate, finderByPreviousAnalysis);
}
@Test
public void shouldFindByNumberOfDays() {
when(finderByDays.findFromDays(null, 30)).thenReturn(new PastSnapshot("days", null).setModeParameter("30"));
PastSnapshot variationSnapshot = finder.find(null, 1, "30");
verify(finderByDays).findFromDays(null, 30);
assertNotNull(variationSnapshot);
assertThat(variationSnapshot.getIndex(), is(1));
assertThat(variationSnapshot.getMode(), is("days"));
assertThat(variationSnapshot.getModeParameter(), is("30"));
}
@Test
public void shouldNotFindByNumberOfDays() {
PastSnapshot variationSnapshot = finder.find(null, 1, "30");
verify(finderByDays).findFromDays(null, 30);
assertNull(variationSnapshot);
}
@Test
public void shouldFindByDate() throws ParseException {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
final Date date = format.parse("2010-05-18");
when(finderByDate.findByDate(null, date)).thenReturn(new PastSnapshot("date", date, new Snapshot()));
PastSnapshot variationSnapshot = finder.find(null, 2, "2010-05-18");
verify(finderByDate).findByDate((Snapshot) anyObject(), argThat(new BaseMatcher<Date>() {
public boolean matches(Object o) {
return o.equals(date);
}
public void describeTo(Description description) {
}
}));
assertThat(variationSnapshot.getIndex(), is(2));
assertThat(variationSnapshot.getMode(), is("date"));
assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
}
@Test
public void shouldNotFindByDate() {
when(finderByDate.findByDate((Snapshot) anyObject(), (Date) anyObject())).thenReturn(null);
PastSnapshot variationSnapshot = finder.find(null, 2, "2010-05-18");
verify(finderByDate).findByDate((Snapshot) anyObject(), (Date) anyObject());
assertNull(variationSnapshot);
}
@Test
public void shouldFindByPreviousAnalysis() throws ParseException {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
final Date date = format.parse("2010-05-18");
Snapshot snapshot = new Snapshot();
snapshot.setCreatedAt(date);
when(finderByPreviousAnalysis.findByPreviousAnalysis(null)).thenReturn(new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, date, snapshot));
PastSnapshot variationSnapshot = finder.find(null, 2, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
verify(finderByPreviousAnalysis).findByPreviousAnalysis(null);
assertThat(variationSnapshot.getIndex(), is(2));
assertThat(variationSnapshot.getMode(), is(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS));
assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
}
@Test
public void shouldNotFindPreviousAnalysis() {
when(finderByPreviousAnalysis.findByPreviousAnalysis(null)).thenReturn(null);
PastSnapshot variationSnapshot = finder.find(null, 2, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
verify(finderByPreviousAnalysis).findByPreviousAnalysis(null);
assertNull(variationSnapshot);
}
@Test
public void shouldFindByVersion() {
when(finderByVersion.findByVersion(null, "1.2")).thenReturn(new PastSnapshot("version", new Date(), new Snapshot()));
PastSnapshot variationSnapshot = finder.find(null, 2, "1.2");
verify(finderByVersion).findByVersion(null, "1.2");
assertThat(variationSnapshot.getIndex(), is(2));
assertThat(variationSnapshot.getMode(), is("version"));
assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
}
@Test
public void shouldNotFindVersion() {
when(finderByVersion.findByVersion(null, "1.2")).thenReturn(null);
PastSnapshot variationSnapshot = finder.find(null, 2, "1.2");
verify(finderByVersion).findByVersion(null, "1.2");
assertNull(variationSnapshot);
}
@Test
public void shouldNotFailIfUnknownFormat() {
when(finderByPreviousAnalysis.findByPreviousAnalysis(null)).thenReturn(new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, new Date(), new Snapshot())); // should
// not
// be
// called
assertNull(finder.find(null, 2, "foooo"));
}
@Test
public void shouldGetPropertyValue() {
PropertiesConfiguration conf = new PropertiesConfiguration();
conf.setProperty("sonar.timemachine.period1", "5");
assertThat(PastSnapshotFinder.getPropertyValue(conf, 1), is("5"));
assertThat(PastSnapshotFinder.getPropertyValue(conf, 999), nullValue());
}
@Test
public void shouldGetDefaultPropertyValue() {
PropertiesConfiguration conf = new PropertiesConfiguration();
conf.setProperty("sonar.timemachine.period1", "5");
assertThat(PastSnapshotFinder.getPropertyValue(conf, 2), is(CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_2));
}
}
|