Skip to content

Commit 38b164c

Browse files
Increase test coverage of caching implementations (#1309)
1 parent ae00483 commit 38b164c

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2022 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program 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+
* This program 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 License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.python.caching;
21+
22+
import org.junit.Test;
23+
import org.sonar.api.SonarProduct;
24+
import org.sonar.api.SonarRuntime;
25+
import org.sonar.api.batch.sensor.SensorContext;
26+
import org.sonar.api.utils.Version;
27+
import org.sonar.plugins.python.api.caching.CacheContext;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.when;
32+
33+
public class CacheContextImplTest {
34+
35+
private static final Version VERSION_WITH_CACHING = Version.create(9, 7);
36+
private static final Version VERSION_WITHOUT_CACHING = Version.create(9, 6);
37+
38+
@Test
39+
public void cache_context_of_enabled_cache() {
40+
SensorContext sensorContext = sensorContext(SonarProduct.SONARQUBE, VERSION_WITH_CACHING, true);
41+
42+
CacheContext cacheContext = CacheContextImpl.of(sensorContext);
43+
assertThat(cacheContext.isCacheEnabled()).isTrue();
44+
}
45+
46+
@Test
47+
public void cache_context_of_disabled_cache() {
48+
SensorContext sensorContext = sensorContext(SonarProduct.SONARQUBE, VERSION_WITH_CACHING, false);
49+
50+
CacheContext cacheContext = CacheContextImpl.of(sensorContext);
51+
assertThat(cacheContext.isCacheEnabled()).isFalse();
52+
}
53+
54+
@Test
55+
public void cache_context_on_sonarlint() {
56+
SensorContext sensorContext = sensorContext(SonarProduct.SONARLINT, VERSION_WITH_CACHING, true);
57+
58+
CacheContext cacheContext = CacheContextImpl.of(sensorContext);
59+
assertThat(cacheContext.isCacheEnabled()).isFalse();
60+
}
61+
62+
@Test
63+
public void cache_context_on_old_version() {
64+
SensorContext sensorContext = sensorContext(SonarProduct.SONARQUBE, VERSION_WITHOUT_CACHING, true);
65+
66+
CacheContext cacheContext = CacheContextImpl.of(sensorContext);
67+
assertThat(cacheContext.isCacheEnabled()).isFalse();
68+
}
69+
70+
@Test
71+
public void dummy_cache() {
72+
CacheContext dummyCache = CacheContextImpl.dummyCache();
73+
assertThat(dummyCache.isCacheEnabled()).isFalse();
74+
}
75+
76+
private static SensorContext sensorContext(SonarProduct product, Version version, boolean isCacheEnabled) {
77+
SonarRuntime runtime = mock(SonarRuntime.class);
78+
when(runtime.getProduct()).thenReturn(product);
79+
when(runtime.getApiVersion()).thenReturn(version);
80+
81+
SensorContext sensorContext = mock(SensorContext.class);
82+
when(sensorContext.runtime()).thenReturn(runtime);
83+
when(sensorContext.isCacheEnabled()).thenReturn(isCacheEnabled);
84+
85+
return sensorContext;
86+
}
87+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2022 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program 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+
* This program 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 License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.python.caching;
21+
22+
import java.io.ByteArrayInputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
import org.sonar.api.batch.sensor.cache.ReadCache;
28+
import org.sonar.api.utils.log.LogTester;
29+
import org.sonar.api.utils.log.LoggerLevel;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.when;
34+
35+
public class PythonReadCacheImplTest {
36+
37+
@Rule
38+
public LogTester logTester = new LogTester();
39+
40+
@Test
41+
public void read_bytes() {
42+
byte[] bytes = "hello".getBytes();
43+
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
44+
45+
ReadCache readCache = mock(ReadCache.class);
46+
when(readCache.read("key")).thenReturn(inputStream);
47+
when(readCache.contains("key")).thenReturn(true);
48+
49+
PythonReadCacheImpl pythonReadCache = new PythonReadCacheImpl(readCache);
50+
byte[] result = pythonReadCache.readBytes("key");
51+
52+
assertThat(result).isEqualTo(bytes);
53+
}
54+
55+
@Test
56+
public void read_bytes_no_such_key() {
57+
ReadCache readCache = mock(ReadCache.class);
58+
when(readCache.contains("key")).thenReturn(false);
59+
60+
PythonReadCacheImpl pythonReadCache = new PythonReadCacheImpl(readCache);
61+
byte[] result = pythonReadCache.readBytes("key");
62+
63+
assertThat(result).isNull();
64+
}
65+
66+
@Test
67+
public void read_bytes_io_exception() throws IOException {
68+
InputStream inputStream = mock(InputStream.class);
69+
when(inputStream.readAllBytes()).thenThrow(IOException.class);
70+
71+
ReadCache readCache = mock(ReadCache.class);
72+
when(readCache.read("key")).thenReturn(inputStream);
73+
when(readCache.contains("key")).thenReturn(true);
74+
75+
PythonReadCacheImpl pythonReadCache = new PythonReadCacheImpl(readCache);
76+
byte[] result = pythonReadCache.readBytes("key");
77+
78+
assertThat(result)
79+
.isNull();
80+
81+
assertThat(logTester.logs(LoggerLevel.DEBUG))
82+
.containsExactly("Unable to read data for key: \"key\"");
83+
}
84+
85+
@Test
86+
public void contains() {
87+
ReadCache readCache = mock(ReadCache.class);
88+
when(readCache.contains("exists")).thenReturn(true);
89+
when(readCache.contains("doesNotExist")).thenReturn(false);
90+
91+
PythonReadCacheImpl pythonReadCache = new PythonReadCacheImpl(readCache);
92+
93+
assertThat(pythonReadCache.contains("exists")).isTrue();
94+
assertThat(pythonReadCache.contains("doesNotExists")).isFalse();
95+
}
96+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2022 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program 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+
* This program 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 License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.python.caching;
21+
22+
import org.junit.Test;
23+
import org.mockito.Mockito;
24+
import org.sonar.api.batch.sensor.cache.WriteCache;
25+
26+
public class PythonWriteCacheImplTest {
27+
28+
@Test
29+
public void write() {
30+
byte[] bytes = "hello".getBytes();
31+
WriteCache writeCache = Mockito.spy(WriteCache.class);
32+
33+
PythonWriteCacheImpl pythonWriteCache = new PythonWriteCacheImpl(writeCache);
34+
pythonWriteCache.write("key", bytes);
35+
36+
Mockito.verify(writeCache, Mockito.times(1))
37+
.write("key", bytes);
38+
}
39+
40+
@Test
41+
public void copy_from_previous() {
42+
WriteCache writeCache = Mockito.spy(WriteCache.class);
43+
44+
PythonWriteCacheImpl pythonWriteCache = new PythonWriteCacheImpl(writeCache);
45+
pythonWriteCache.copyFromPrevious("key");
46+
47+
Mockito.verify(writeCache, Mockito.times(1))
48+
.copyFromPrevious("key");
49+
}
50+
}

0 commit comments

Comments
 (0)