2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.darksky.internal.utils;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
19 import java.io.IOException;
20 import java.io.InputStream;
22 import org.junit.jupiter.api.AfterAll;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.core.config.core.ConfigConstants;
29 * Test class for the {@link ByteArrayFileCache} class.
31 * @author Christoph Weitkamp - Initial contribution
33 public class ByteArrayFileCacheTest {
35 private static final String SERVICE_PID = "org.openhab.binding.darksky";
37 private static final File USERDATA_FOLDER = new File(ConfigConstants.getUserDataFolder());
38 private static final File CACHE_FOLDER = new File(USERDATA_FOLDER, ByteArrayFileCache.CACHE_FOLDER_NAME);
39 private static final File SERVICE_CACHE_FOLDER = new File(CACHE_FOLDER, SERVICE_PID);
41 private static final String MP3_FILE_NAME = SERVICE_CACHE_FOLDER.getAbsolutePath() + "doorbell.mp3";
42 private static final String TXT_FILE_NAME = SERVICE_CACHE_FOLDER.getAbsolutePath() + "doorbell.txt";
44 private static final byte[] EMPTY_BUFFER = new byte[0];
46 private ByteArrayFileCache subject;
50 subject = new ByteArrayFileCache(SERVICE_PID);
54 public void tearDown() {
60 public static void cleanUp() {
62 SERVICE_CACHE_FOLDER.delete();
63 CACHE_FOLDER.delete();
64 USERDATA_FOLDER.delete();
68 public void testGetFileExtension() {
69 assertThat(subject.getFileExtension("/var/log/openhab2/"), is(nullValue()));
70 assertThat(subject.getFileExtension("/var/log/foo.bar/"), is(nullValue()));
71 assertThat(subject.getFileExtension("doorbell.mp3"), is(equalTo("mp3")));
72 assertThat(subject.getFileExtension("/tmp/doorbell.mp3"), is(equalTo("mp3")));
73 assertThat(subject.getFileExtension(MP3_FILE_NAME), is(equalTo("mp3")));
74 assertThat(subject.getFileExtension(TXT_FILE_NAME), is(equalTo("txt")));
75 assertThat(subject.getFileExtension("/var/log/openhab2/.."), is(""));
76 assertThat(subject.getFileExtension(".hidden"), is(equalTo("hidden")));
77 assertThat(subject.getFileExtension("C:\\Program Files (x86)\\java\\bin\\javaw.exe"), is(equalTo("exe")));
78 assertThat(subject.getFileExtension("https://www.youtube.com/watch?v=qYrpPrLY868"), is(nullValue()));
82 public void testGetUniqueFileName() {
83 String mp3UniqueFileName = subject.getUniqueFileName(MP3_FILE_NAME);
84 assertThat(mp3UniqueFileName, is(equalTo(subject.getUniqueFileName(MP3_FILE_NAME))));
86 String txtUniqueFileName = subject.getUniqueFileName(TXT_FILE_NAME);
87 assertThat(txtUniqueFileName, is(equalTo(subject.getUniqueFileName(TXT_FILE_NAME))));
89 assertThat(mp3UniqueFileName, is(not(equalTo(txtUniqueFileName))));
93 public void testGet() {
94 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
98 public void testPut() throws IOException {
99 byte[] buffer = readFile();
100 subject.put(MP3_FILE_NAME, buffer);
102 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
106 public void testPutIfAbsent() throws IOException {
107 byte[] buffer = readFile();
108 subject.putIfAbsent(MP3_FILE_NAME, buffer);
110 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
114 public void testPutIfAbsentAndGet() throws IOException {
115 byte[] buffer = readFile();
117 assertThat(subject.putIfAbsentAndGet(MP3_FILE_NAME, buffer), is(equalTo(buffer)));
121 public void testContainsKey() throws IOException {
122 assertThat(subject.containsKey(MP3_FILE_NAME), is(false));
124 subject.put(MP3_FILE_NAME, readFile());
126 assertThat(subject.containsKey(MP3_FILE_NAME), is(true));
130 public void testRemove() throws IOException {
131 subject.put(MP3_FILE_NAME, readFile());
132 subject.remove(MP3_FILE_NAME);
134 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
138 public void testClear() throws IOException {
139 subject.put(MP3_FILE_NAME, readFile());
142 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
146 public void clearExpiredClearsNothing() throws IOException {
147 byte[] buffer = readFile();
148 subject.put(MP3_FILE_NAME, buffer);
149 subject.clearExpired();
151 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
155 public void clearExpired() throws IOException {
156 subject = new ByteArrayFileCache(SERVICE_PID, 1);
158 subject.put(MP3_FILE_NAME, readFile());
160 // manipulate time of last use
161 File fileInCache = subject.getUniqueFile(MP3_FILE_NAME);
162 fileInCache.setLastModified(System.currentTimeMillis() - 2 * ByteArrayFileCache.ONE_DAY_IN_MILLIS);
164 subject.clearExpired();
166 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
169 private byte[] readFile() throws IOException {
171 try (InputStream is = ByteArrayFileCacheTest.class.getResourceAsStream("/sounds/doorbell.mp3")) {
172 buffer = new byte[is.available()];