3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.util.cache;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.TemporaryFolder;
25 import org.sonar.api.utils.System2;
26 import org.sonar.core.util.CloseableIterator;
28 import java.io.ObjectOutputStream;
29 import java.io.Serializable;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.junit.Assert.fail;
34 public class DiskCacheTest {
37 public TemporaryFolder temp = new TemporaryFolder();
40 public void write_and_read() throws Exception {
41 DiskCache<String> cache = new DiskCache<>(temp.newFile(), System2.INSTANCE);
42 try (CloseableIterator<String> traverse = cache.traverse()) {
43 assertThat(traverse).isExhausted();
50 try (CloseableIterator<String> traverse = cache.traverse()) {
51 assertThat(traverse).toIterable().containsExactly("foo", "bar");
56 public void fail_if_file_is_not_writable() throws Exception {
58 new DiskCache<>(temp.newFolder(), System2.INSTANCE);
60 } catch (IllegalStateException e) {
61 assertThat(e).hasMessageContaining("Fail to write into file");
66 public void fail_to_serialize() throws Exception {
67 class Unserializable implements Serializable {
68 private void writeObject(ObjectOutputStream out) {
69 throw new UnsupportedOperationException("expected error");
72 DiskCache<Serializable> cache = new DiskCache<>(temp.newFile(), System2.INSTANCE);
74 cache.newAppender().append(new Unserializable());
76 } catch (UnsupportedOperationException e) {
77 assertThat(e).hasMessage("expected error");