BackgroundMusicManager { File downloadAndGetFromCache(URL resourceURL, String resourceUrlExternalForm) { final Charset utf8 = StandardCharsets.UTF_8; byte[] encodedUrl = Base64.getEncoder().encode(resourceUrlExternalForm.getBytes(utf8)); final String localCacheFileName = new String(encodedUrl, utf8); File musicCacheFolder = new File(GazePlayDirectories.getGazePlayFolder(), "cache/music"); File outputFile = new File(musicCacheFolder, localCacheFileName); if (!outputFile.exists()) { File tempOutputFile = new File(outputFile.getAbsolutePath() + ".downloading"); if (tempOutputFile.exists()) { boolean tempOutputFileDeleted = tempOutputFile.delete(); log.trace("tempOutputFileDeleted = {}", tempOutputFileDeleted); } try { log.info("Downloading music file {}", resourceURL); FileUtils.copyURLToFile(resourceURL, tempOutputFile, 10000, 10000); } catch (IOException e) { throw new RuntimeException(e); } boolean renamed = tempOutputFile.renameTo(outputFile); log.debug("renamed = {}", renamed); log.info("Finished downloading music file {}", resourceURL); } else { log.info("Found music file in cache for {}", resourceURL); } return outputFile; } BackgroundMusicManager(); static void onConfigurationChanged(); void onEndGame(); void getAudioFromFolder(String folderPath); boolean isPlaying(); void changeMusic(int newMusicIndex); void emptyPlaylist(); void backupPlaylist(); void restorePlaylist(); void pause(); void play(); void stop(); void next(); void previous(); void setVolume(double value); void playMusicAlone(String resourceUrlAsString); static String getMusicTitle(final MediaPlayer music); static String getMusicTitle(final String musicPath); ReadOnlyBooleanProperty getIsMusicChanging();  }
@Test void shouldDownloadAndGetFromCache() throws IOException { File testFile = new File("testFolder"); new MockUp<GazePlayDirectories>() { @mockit.Mock public File getGazePlayFolder() { return testFile; } }; URL resource = new URL("https: File result = musicManagerSpy.downloadAndGetFromCache(resource, resource.toExternalForm()); File expected = new File(testFile, "cache/music/" + new String(Base64.getEncoder().encode(resource.toExternalForm().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)); assertEquals(expected, result); FileUtils.deleteDirectory(testFile); }
@Test void shouldFindExistingInCache() throws IOException { File testFile = new File("testFolder"); new MockUp<GazePlayDirectories>() { @mockit.Mock public File getGazePlayFolder() { return testFile; } }; URL resource = new URL("https: File folder = new File(testFile, "cache/music"); folder.mkdirs(); File expected = new File(folder, new String(Base64.getEncoder().encode(resource.toExternalForm().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)); expected.createNewFile(); File result = musicManagerSpy.downloadAndGetFromCache(resource, resource.toExternalForm()); assertEquals(expected, result); FileUtils.deleteDirectory(testFile); }