You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnalysisCacheProviderTest.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-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.scanner.cache;
  21. import org.junit.Test;
  22. import org.sonar.api.batch.sensor.cache.ReadCache;
  23. import org.sonar.scanner.scan.branch.BranchConfiguration;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. import static org.mockito.Mockito.mock;
  26. import static org.mockito.Mockito.verify;
  27. import static org.mockito.Mockito.when;
  28. public class AnalysisCacheProviderTest {
  29. private final AnalysisCacheEnabled analysisCacheEnabled = mock(AnalysisCacheEnabled.class);
  30. private final AnalysisCacheMemoryStorage storage = mock(AnalysisCacheMemoryStorage.class);
  31. private final ReadCache readCache = mock(ReadCache.class);
  32. private final AnalysisCacheProvider cacheProvider = new AnalysisCacheProvider();
  33. private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
  34. @Test
  35. public void provide_noop_reader_cache_when_disable() {
  36. when(analysisCacheEnabled.isEnabled()).thenReturn(false);
  37. var cache = cacheProvider.provideReader(analysisCacheEnabled, storage);
  38. assertThat(cache).isInstanceOf(AnalysisCacheProvider.NoOpReadCache.class);
  39. }
  40. @Test
  41. public void provide_noop_writer_cache_when_disable() {
  42. when(analysisCacheEnabled.isEnabled()).thenReturn(false);
  43. var cache = cacheProvider.provideWriter(analysisCacheEnabled, readCache, branchConfiguration);
  44. assertThat(cache).isInstanceOf(AnalysisCacheProvider.NoOpWriteCache.class);
  45. }
  46. @Test
  47. public void provide_real_reader_cache_when_enable() {
  48. when(analysisCacheEnabled.isEnabled()).thenReturn(true);
  49. var cache = cacheProvider.provideReader(analysisCacheEnabled, storage);
  50. verify(storage).load();
  51. assertThat(cache).isInstanceOf(ReadCacheImpl.class);
  52. }
  53. @Test
  54. public void provide_real_writer_cache_when_enable() {
  55. when(analysisCacheEnabled.isEnabled()).thenReturn(true);
  56. var cache = cacheProvider.provideWriter(analysisCacheEnabled, readCache, branchConfiguration);
  57. assertThat(cache).isInstanceOf(WriteCacheImpl.class);
  58. }
  59. }