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
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.batch;
import org.mockito.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.SonarIndex;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.PersistenceMode;
import org.sonar.api.resources.Directory;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.Scopes;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
public class DefaultFileLinesContextTest {
private SonarIndex index;
private Resource resource;
private DefaultFileLinesContext fileLineMeasures;
@Before
public void setUp() {
index = mock(SonarIndex.class);
resource = mock(Resource.class);
when(resource.getScope()).thenReturn(Scopes.FILE);
fileLineMeasures = new DefaultFileLinesContext(index, resource);
}
@Test(expected = IllegalArgumentException.class)
public void shouldNotAllowCreationForDirectory() {
new DefaultFileLinesContext(index, new Directory("key"));
}
@Test
public void shouldSave() {
fileLineMeasures.setIntValue("hits", 1, 2);
fileLineMeasures.setIntValue("hits", 3, 4);
fileLineMeasures.save();
ArgumentCaptor<Measure> measureCaptor = ArgumentCaptor.forClass(Measure.class);
verify(index).addMeasure(Matchers.eq(resource), measureCaptor.capture());
Measure measure = measureCaptor.getValue();
assertThat(measure.getMetricKey(), is("hits"));
assertThat(measure.getPersistenceMode(), is(PersistenceMode.DATABASE));
assertThat(measure.getData(), is("1=2;3=4"));
}
@Test
public void shouldSaveSeveral() {
fileLineMeasures.setIntValue("hits", 1, 2);
fileLineMeasures.setIntValue("hits", 3, 4);
fileLineMeasures.setStringValue("author", 1, "simon");
fileLineMeasures.setStringValue("author", 3, "evgeny");
fileLineMeasures.save();
fileLineMeasures.setIntValue("branches", 1, 2);
fileLineMeasures.setIntValue("branches", 3, 4);
fileLineMeasures.save();
verify(index, times(3)).addMeasure(Matchers.eq(resource), Matchers.any(Measure.class));
}
@Test(expected = UnsupportedOperationException.class)
public void shouldNotModifyAfterSave() {
fileLineMeasures.setIntValue("hits", 1, 2);
fileLineMeasures.save();
fileLineMeasures.save();
verify(index).addMeasure(Matchers.eq(resource), Matchers.any(Measure.class));
fileLineMeasures.setIntValue("hits", 1, 2);
}
@Test
public void shouldLoadIntValues() {
when(index.getMeasure(Matchers.any(Resource.class), Matchers.any(Metric.class)))
.thenReturn(new Measure("hits").setData("1=2;3=4"));
assertThat(fileLineMeasures.getIntValue("hits", 1), is(2));
assertThat(fileLineMeasures.getIntValue("hits", 3), is(4));
assertThat("no measure on line", fileLineMeasures.getIntValue("hits", 5), nullValue());
}
@Test
public void shouldLoadStringValues() {
when(index.getMeasure(Matchers.any(Resource.class), Matchers.any(Metric.class)))
.thenReturn(new Measure("author").setData("1=simon;3=evgeny"));
assertThat(fileLineMeasures.getStringValue("author", 1), is("simon"));
assertThat(fileLineMeasures.getStringValue("author", 3), is("evgeny"));
assertThat("no measure on line", fileLineMeasures.getStringValue("author", 5), nullValue());
}
@Test
public void shouldNotSaveAfterLoad() {
when(index.getMeasure(Matchers.any(Resource.class), Matchers.any(Metric.class)))
.thenReturn(new Measure("author").setData("1=simon;3=evgeny"));
fileLineMeasures.getStringValue("author", 1);
fileLineMeasures.save();
verify(index, never()).addMeasure(Matchers.eq(resource), Matchers.any(Measure.class));
}
@Test(expected = UnsupportedOperationException.class)
public void shouldNotModifyAfterLoad() {
when(index.getMeasure(Matchers.any(Resource.class), Matchers.any(Metric.class)))
.thenReturn(new Measure("author").setData("1=simon;3=evgeny"));
fileLineMeasures.getStringValue("author", 1);
fileLineMeasures.setStringValue("author", 1, "evgeny");
}
@Test
public void shouldNotFailIfNoMeasureInIndex() {
assertThat(fileLineMeasures.getIntValue("hits", 1), nullValue());
assertThat(fileLineMeasures.getStringValue("author", 1), nullValue());
}
}
|