|
| 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 | +} |
0 commit comments