]> git.basschouten.com Git - openhab-addons.git/blob
79d5ef97752783cb53e29b21106e75446a4d3b9c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.kodi.internal.utils;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21
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;
27
28 /**
29  * Test class for the {@link ByteArrayFileCache} class.
30  *
31  * @author Christoph Weitkamp - Initial contribution
32  */
33 public class ByteArrayFileCacheTest {
34
35     private static final String SERVICE_PID = "org.openhab.binding.kodi";
36
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);
40
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";
43
44     private static final byte[] EMPTY_BUFFER = new byte[0];
45
46     private ByteArrayFileCache subject;
47
48     @BeforeEach
49     public void setUp() {
50         subject = new ByteArrayFileCache(SERVICE_PID);
51     }
52
53     @AfterEach
54     public void tearDown() {
55         // delete all files
56         subject.clear();
57     }
58
59     @AfterAll
60     public static void cleanUp() {
61         // delete all folders
62         SERVICE_CACHE_FOLDER.delete();
63         CACHE_FOLDER.delete();
64         USERDATA_FOLDER.delete();
65     }
66
67     @Test
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"));
81     }
82
83     @Test
84     public void testGetUniqueFileNameIsUnique() {
85         String mp3UniqueFileName = subject.getUniqueFileName(MP3_FILE_NAME);
86         assertThat(mp3UniqueFileName, is(equalTo(subject.getUniqueFileName(MP3_FILE_NAME))));
87
88         String txtUniqueFileName = subject.getUniqueFileName(TXT_FILE_NAME);
89         assertThat(txtUniqueFileName, is(equalTo(subject.getUniqueFileName(TXT_FILE_NAME))));
90
91         assertThat(mp3UniqueFileName, is(not(equalTo(txtUniqueFileName))));
92     }
93
94     @Test
95     public void testGet() {
96         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
97     }
98
99     @Test
100     public void testPut() throws IOException {
101         byte[] buffer = readFile();
102         subject.put(MP3_FILE_NAME, buffer);
103
104         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
105     }
106
107     @Test
108     public void testPutIfAbsent() throws IOException {
109         byte[] buffer = readFile();
110         subject.putIfAbsent(MP3_FILE_NAME, buffer);
111
112         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
113     }
114
115     @Test
116     public void testPutIfAbsentAndGet() throws IOException {
117         byte[] buffer = readFile();
118
119         assertThat(subject.putIfAbsentAndGet(MP3_FILE_NAME, buffer), is(equalTo(buffer)));
120     }
121
122     @Test
123     public void testContainsKey() throws IOException {
124         assertThat(subject.containsKey(MP3_FILE_NAME), is(false));
125
126         subject.put(MP3_FILE_NAME, readFile());
127
128         assertThat(subject.containsKey(MP3_FILE_NAME), is(true));
129     }
130
131     @Test
132     public void testRemove() throws IOException {
133         subject.put(MP3_FILE_NAME, readFile());
134         subject.remove(MP3_FILE_NAME);
135
136         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
137     }
138
139     @Test
140     public void testClear() throws IOException {
141         subject.put(MP3_FILE_NAME, readFile());
142         subject.clear();
143
144         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
145     }
146
147     @Test
148     public void clearExpiredClearsNothing() throws IOException {
149         byte[] buffer = readFile();
150         subject.put(MP3_FILE_NAME, buffer);
151         subject.clearExpired();
152
153         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
154     }
155
156     @Test
157     public void clearExpired() throws IOException {
158         subject = new ByteArrayFileCache(SERVICE_PID, 1);
159
160         subject.put(MP3_FILE_NAME, readFile());
161
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);
165
166         subject.clearExpired();
167
168         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
169     }
170
171     private byte[] readFile() throws IOException {
172         byte[] buffer;
173         try (InputStream is = ByteArrayFileCacheTest.class.getResourceAsStream("/sounds/doorbell.mp3")) {
174             buffer = new byte[is.available()];
175             is.read(buffer);
176         }
177         return buffer;
178     }
179 }