Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

WriteCacheImplTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.StreamSupport;
  28. import org.junit.Before;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.TemporaryFolder;
  32. import org.sonar.core.util.Protobuf;
  33. import org.sonar.scanner.protocol.internal.ScannerInternal.SensorCacheEntry;
  34. import org.sonar.scanner.protocol.output.FileStructure;
  35. import org.sonar.scanner.scan.branch.BranchConfiguration;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  38. import static org.mockito.Mockito.mock;
  39. import static org.mockito.Mockito.when;
  40. public class WriteCacheImplTest {
  41. private final ReadCacheImpl readCache = mock(ReadCacheImpl.class);
  42. private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
  43. private FileStructure fileStructure;
  44. private WriteCacheImpl writeCache;
  45. @Rule
  46. public TemporaryFolder temp = new TemporaryFolder();
  47. @Before
  48. public void setUp() throws IOException {
  49. fileStructure = new FileStructure(temp.newFolder());
  50. writeCache = new WriteCacheImpl(readCache, fileStructure);
  51. }
  52. @Test
  53. public void write_bytes_adds_entries() throws IOException {
  54. byte[] b1 = new byte[] {1, 2, 3};
  55. byte[] b2 = new byte[] {3, 4};
  56. writeCache.write("key", b1);
  57. writeCache.write("key2", b2);
  58. assertThatCacheContains(Map.of("key", b1, "key2", b2));
  59. }
  60. @Test
  61. public void dont_write_if_its_pull_request() throws IOException {
  62. byte[] b1 = new byte[] {1, 2, 3};
  63. when(branchConfiguration.isPullRequest()).thenReturn(true);
  64. writeCache.write("key1", b1);
  65. writeCache.write("key2", new ByteArrayInputStream(b1));
  66. assertThatCacheContains(Map.of());
  67. }
  68. @Test
  69. public void write_inputStream_adds_entries() throws IOException {
  70. byte[] b1 = new byte[] {1, 2, 3};
  71. byte[] b2 = new byte[] {3, 4};
  72. writeCache.write("key", new ByteArrayInputStream(b1));
  73. writeCache.write("key2", new ByteArrayInputStream(b2));
  74. assertThatCacheContains(Map.of("key", b1, "key2", b2));
  75. }
  76. @Test
  77. public void write_throws_IAE_if_writing_same_key_twice() {
  78. byte[] b1 = new byte[] {1};
  79. byte[] b2 = new byte[] {2};
  80. writeCache.write("key", b1);
  81. assertThatThrownBy(() -> writeCache.write("key", b2))
  82. .isInstanceOf(IllegalArgumentException.class)
  83. .hasMessage("Cache already contains key 'key'");
  84. }
  85. @Test
  86. public void copyFromPrevious_throws_IAE_if_read_cache_doesnt_contain_key() {
  87. assertThatThrownBy(() -> writeCache.copyFromPrevious("key"))
  88. .isInstanceOf(IllegalArgumentException.class)
  89. .hasMessage("Previous cache doesn't contain key 'key'");
  90. }
  91. @Test
  92. public void copyFromPrevious_reads_from_readCache() throws IOException {
  93. byte[] b = new byte[] {1};
  94. InputStream value = new ByteArrayInputStream(b);
  95. when(readCache.contains("key")).thenReturn(true);
  96. when(readCache.read("key")).thenReturn(value);
  97. writeCache.copyFromPrevious("key");
  98. assertThatCacheContains(Map.of("key", b));
  99. }
  100. private void assertThatCacheContains(Map<String, byte[]> expectedData) {
  101. writeCache.close();
  102. File cacheFile = fileStructure.analysisCache();
  103. Iterable<SensorCacheEntry> it = () -> Protobuf.readGzipStream(cacheFile, SensorCacheEntry.parser());
  104. Map<String, byte[]> data = StreamSupport.stream(it.spliterator(), false)
  105. .collect(Collectors.toMap(SensorCacheEntry::getKey, e -> e.getData().toByteArray()));
  106. assertThat(data).containsAllEntriesOf(expectedData);
  107. }
  108. }