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.

DiskCacheTest.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.ce.task.projectanalysis.util.cache;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.TemporaryFolder;
  24. import org.sonar.api.utils.System2;
  25. import org.sonar.core.util.CloseableIterator;
  26. import java.io.ObjectOutputStream;
  27. import java.io.Serializable;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.junit.Assert.fail;
  30. public class DiskCacheTest {
  31. @Rule
  32. public TemporaryFolder temp = new TemporaryFolder();
  33. @Test
  34. public void write_and_read() throws Exception {
  35. DiskCache<String> cache = new DiskCache<>(temp.newFile(), System2.INSTANCE);
  36. try (CloseableIterator<String> traverse = cache.traverse()) {
  37. assertThat(traverse).isExhausted();
  38. }
  39. cache.newAppender()
  40. .append("foo")
  41. .append("bar")
  42. .close();
  43. try (CloseableIterator<String> traverse = cache.traverse()) {
  44. assertThat(traverse).toIterable().containsExactly("foo", "bar");
  45. }
  46. }
  47. @Test
  48. public void fail_if_file_is_not_writable() throws Exception {
  49. try {
  50. new DiskCache<>(temp.newFolder(), System2.INSTANCE);
  51. fail();
  52. } catch (IllegalStateException e) {
  53. assertThat(e).hasMessageContaining("Fail to write into file");
  54. }
  55. }
  56. @Test
  57. public void fail_to_serialize() throws Exception {
  58. class Unserializable implements Serializable {
  59. private void writeObject(ObjectOutputStream out) {
  60. throw new UnsupportedOperationException("expected error");
  61. }
  62. }
  63. DiskCache<Serializable> cache = new DiskCache<>(temp.newFile(), System2.INSTANCE);
  64. try {
  65. cache.newAppender().append(new Unserializable());
  66. fail();
  67. } catch (UnsupportedOperationException e) {
  68. assertThat(e).hasMessage("expected error");
  69. }
  70. }
  71. }