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.

WriteCacheImplTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 java.io.ByteArrayInputStream;
  22. import java.io.InputStream;
  23. import org.junit.Test;
  24. import org.sonar.scanner.scan.branch.BranchConfiguration;
  25. import static org.assertj.core.api.Assertions.assertThat;
  26. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  27. import static org.assertj.core.data.MapEntry.entry;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.when;
  30. public class WriteCacheImplTest {
  31. private final ReadCacheImpl readCache = mock(ReadCacheImpl.class);
  32. private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
  33. private final WriteCacheImpl writeCache = new WriteCacheImpl(readCache, branchConfiguration);
  34. @Test
  35. public void write_bytes_adds_entries() {
  36. byte[] b1 = new byte[] {1, 2, 3};
  37. byte[] b2 = new byte[] {3, 4};
  38. writeCache.write("key", b1);
  39. writeCache.write("key2", b2);
  40. assertThat(writeCache.getCache()).containsOnly(entry("key", b1), entry("key2", b2));
  41. }
  42. @Test
  43. public void dont_write_if_its_pull_request() {
  44. byte[] b1 = new byte[] {1, 2, 3};
  45. when(branchConfiguration.isPullRequest()).thenReturn(true);
  46. writeCache.write("key1", b1);
  47. writeCache.write("key2", new ByteArrayInputStream(b1));
  48. assertThat(writeCache.getCache()).isEmpty();
  49. }
  50. @Test
  51. public void write_inputStream_adds_entries() {
  52. byte[] b1 = new byte[] {1, 2, 3};
  53. byte[] b2 = new byte[] {3, 4};
  54. writeCache.write("key", new ByteArrayInputStream(b1));
  55. writeCache.write("key2", new ByteArrayInputStream(b2));
  56. assertThat(writeCache.getCache()).containsOnly(entry("key", b1), entry("key2", b2));
  57. }
  58. @Test
  59. public void write_throws_IAE_if_writing_same_key_twice() {
  60. byte[] b1 = new byte[] {1};
  61. byte[] b2 = new byte[] {2};
  62. writeCache.write("key", b1);
  63. assertThatThrownBy(() -> writeCache.write("key", b2))
  64. .isInstanceOf(IllegalArgumentException.class)
  65. .hasMessage("Cache already contains key 'key'");
  66. }
  67. @Test
  68. public void copyFromPrevious_throws_IAE_if_read_cache_doesnt_contain_key() {
  69. assertThatThrownBy(() -> writeCache.copyFromPrevious("key"))
  70. .isInstanceOf(IllegalArgumentException.class)
  71. .hasMessage("Previous cache doesn't contain key 'key'");
  72. }
  73. @Test
  74. public void copyFromPrevious_reads_from_readCache() {
  75. byte[] b = new byte[] {1};
  76. InputStream value = new ByteArrayInputStream(b);
  77. when(readCache.contains("key")).thenReturn(true);
  78. when(readCache.read("key")).thenReturn(value);
  79. writeCache.copyFromPrevious("key");
  80. assertThat(writeCache.getCache()).containsOnly(entry("key", b));
  81. }
  82. }