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.kodi.internal.utils;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.junit.Assert.assertThat;
19 import java.io.IOException;
20 import java.io.InputStream;
22 import org.junit.After;
23 import org.junit.AfterClass;
24 import org.junit.Before;
25 import org.junit.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.kodi";
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()));
79 assertThat(subject.getFileExtension(
80 "a52fc16cca77ab2bf6abe51e463ce501.jpg?response-content-type=image%2Fjpeg&test=1"), is("jpg"));
84 public void testGetUniqueFileNameIsUnique() {
85 String mp3UniqueFileName = subject.getUniqueFileName(MP3_FILE_NAME);
86 assertThat(mp3UniqueFileName, is(equalTo(subject.getUniqueFileName(MP3_FILE_NAME))));
88 String txtUniqueFileName = subject.getUniqueFileName(TXT_FILE_NAME);
89 assertThat(txtUniqueFileName, is(equalTo(subject.getUniqueFileName(TXT_FILE_NAME))));
91 assertThat(mp3UniqueFileName, is(not(equalTo(txtUniqueFileName))));
95 public void testGet() {
96 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
100 public void testPut() throws IOException {
101 byte[] buffer = readFile();
102 subject.put(MP3_FILE_NAME, buffer);
104 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
108 public void testPutIfAbsent() throws IOException {
109 byte[] buffer = readFile();
110 subject.putIfAbsent(MP3_FILE_NAME, buffer);
112 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
116 public void testPutIfAbsentAndGet() throws IOException {
117 byte[] buffer = readFile();
119 assertThat(subject.putIfAbsentAndGet(MP3_FILE_NAME, buffer), is(equalTo(buffer)));
123 public void testContainsKey() throws IOException {
124 assertThat(subject.containsKey(MP3_FILE_NAME), is(false));
126 subject.put(MP3_FILE_NAME, readFile());
128 assertThat(subject.containsKey(MP3_FILE_NAME), is(true));
132 public void testRemove() throws IOException {
133 subject.put(MP3_FILE_NAME, readFile());
134 subject.remove(MP3_FILE_NAME);
136 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
140 public void testClear() throws IOException {
141 subject.put(MP3_FILE_NAME, readFile());
144 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
148 public void clearExpiredClearsNothing() throws IOException {
149 byte[] buffer = readFile();
150 subject.put(MP3_FILE_NAME, buffer);
151 subject.clearExpired();
153 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
157 public void clearExpired() throws IOException {
158 subject = new ByteArrayFileCache(SERVICE_PID, 1);
160 subject.put(MP3_FILE_NAME, readFile());
162 // manipulate time of last use
163 File fileInCache = subject.getUniqueFile(MP3_FILE_NAME);
164 fileInCache.setLastModified(System.currentTimeMillis() - 2 * ByteArrayFileCache.ONE_DAY_IN_MILLIS);
166 subject.clearExpired();
168 assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
171 private byte[] readFile() throws IOException {
173 try (InputStream is = ByteArrayFileCacheTest.class.getResourceAsStream("/sounds/doorbell.mp3")) {
174 buffer = new byte[is.available()];