]> git.basschouten.com Git - openhab-addons.git/blob
eb1ab1075cd70a1cefefef55c696aef6bd5a24b8
[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.darksky.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.darksky";
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     }
80
81     @Test
82     public void testGetUniqueFileName() {
83         String mp3UniqueFileName = subject.getUniqueFileName(MP3_FILE_NAME);
84         assertThat(mp3UniqueFileName, is(equalTo(subject.getUniqueFileName(MP3_FILE_NAME))));
85
86         String txtUniqueFileName = subject.getUniqueFileName(TXT_FILE_NAME);
87         assertThat(txtUniqueFileName, is(equalTo(subject.getUniqueFileName(TXT_FILE_NAME))));
88
89         assertThat(mp3UniqueFileName, is(not(equalTo(txtUniqueFileName))));
90     }
91
92     @Test
93     public void testGet() {
94         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
95     }
96
97     @Test
98     public void testPut() throws IOException {
99         byte[] buffer = readFile();
100         subject.put(MP3_FILE_NAME, buffer);
101
102         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
103     }
104
105     @Test
106     public void testPutIfAbsent() throws IOException {
107         byte[] buffer = readFile();
108         subject.putIfAbsent(MP3_FILE_NAME, buffer);
109
110         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
111     }
112
113     @Test
114     public void testPutIfAbsentAndGet() throws IOException {
115         byte[] buffer = readFile();
116
117         assertThat(subject.putIfAbsentAndGet(MP3_FILE_NAME, buffer), is(equalTo(buffer)));
118     }
119
120     @Test
121     public void testContainsKey() throws IOException {
122         assertThat(subject.containsKey(MP3_FILE_NAME), is(false));
123
124         subject.put(MP3_FILE_NAME, readFile());
125
126         assertThat(subject.containsKey(MP3_FILE_NAME), is(true));
127     }
128
129     @Test
130     public void testRemove() throws IOException {
131         subject.put(MP3_FILE_NAME, readFile());
132         subject.remove(MP3_FILE_NAME);
133
134         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
135     }
136
137     @Test
138     public void testClear() throws IOException {
139         subject.put(MP3_FILE_NAME, readFile());
140         subject.clear();
141
142         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
143     }
144
145     @Test
146     public void clearExpiredClearsNothing() throws IOException {
147         byte[] buffer = readFile();
148         subject.put(MP3_FILE_NAME, buffer);
149         subject.clearExpired();
150
151         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(buffer)));
152     }
153
154     @Test
155     public void clearExpired() throws IOException {
156         subject = new ByteArrayFileCache(SERVICE_PID, 1);
157
158         subject.put(MP3_FILE_NAME, readFile());
159
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);
163
164         subject.clearExpired();
165
166         assertThat(subject.get(MP3_FILE_NAME), is(equalTo(EMPTY_BUFFER)));
167     }
168
169     private byte[] readFile() throws IOException {
170         byte[] buffer;
171         try (InputStream is = ByteArrayFileCacheTest.class.getResourceAsStream("/sounds/doorbell.mp3")) {
172             buffer = new byte[is.available()];
173             is.read(buffer);
174         }
175         return buffer;
176     }
177 }