Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AnalysisCacheMemoryStorageTest.java 2.6KB

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 com.google.protobuf.ByteString;
  22. import java.io.IOException;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Optional;
  25. import org.apache.commons.io.IOUtils;
  26. import org.junit.Test;
  27. import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.verify;
  32. import static org.mockito.Mockito.when;
  33. public class AnalysisCacheMemoryStorageTest {
  34. private final AnalysisCacheLoader loader = mock(AnalysisCacheLoader.class);
  35. private final AnalysisCacheMemoryStorage storage = new AnalysisCacheMemoryStorage(loader);
  36. @Test
  37. public void storage_loads_with_loader() throws IOException {
  38. when(loader.load()).thenReturn(Optional.of(AnalysisCacheMsg.newBuilder()
  39. .putMap("key1", ByteString.copyFrom("value1", StandardCharsets.UTF_8))
  40. .build()));
  41. storage.load();
  42. verify(loader).load();
  43. assertThat(IOUtils.toString(storage.get("key1"), StandardCharsets.UTF_8)).isEqualTo("value1");
  44. assertThat(storage.contains("key1")).isTrue();
  45. }
  46. @Test
  47. public void get_throws_IAE_if_doesnt_contain_key() {
  48. when(loader.load()).thenReturn(Optional.of(AnalysisCacheMsg.newBuilder().build()));
  49. storage.load();
  50. assertThat(storage.contains("key1")).isFalse();
  51. assertThatThrownBy(() -> storage.get("key1")).isInstanceOf(IllegalArgumentException.class);
  52. }
  53. @Test
  54. public void get_throws_IAE_if_no_cache() {
  55. when(loader.load()).thenReturn(Optional.empty());
  56. storage.load();
  57. assertThat(storage.contains("key1")).isFalse();
  58. assertThatThrownBy(() -> storage.get("key1")).isInstanceOf(IllegalArgumentException.class);
  59. }
  60. }