cacheFile.put(fileName, timestamp);
@Test public void testCacheRemainsWhenExternalResourceTheSame() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String externalResourcePath = temporaryFolder.newFile().getPath(); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); // pre-populate with cache with resources cache.load(); final Set<String> resources = new HashSet<>(); resources.add(externalResourcePath); cache.putExternalResources(resources); cache.persist(); // test cache with same resources and new file cache.load(); cache.put("myFile", 1); cache.putExternalResources(resources); assertTrue("Should return true in file is in cache", cache.isInCache("myFile", 1)); }
@Test public void testConfigHashRemainsOnResetExternalResources() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); // create cache with one file cache.load(); cache.put("myFile", 1); final String hash = cache.get(PropertyCacheFile.CONFIG_HASH_KEY); assertNotNull("Config hash key should not be null", hash); // apply new external resource to clear cache final Set<String> resources = new HashSet<>(); resources.add("dummy"); cache.putExternalResources(resources); assertEquals("Invalid config hash key", hash, cache.get(PropertyCacheFile.CONFIG_HASH_KEY)); assertFalse("Should return false in file is not in cache", cache.isInCache("myFile", 1)); }
@Test public void testExceptionNoSuchAlgorithmException() throws Exception { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.put("myFile", 1); mockStatic(MessageDigest.class); when(MessageDigest.getInstance("SHA-1")) .thenThrow(NoSuchAlgorithmException.class); final Class<?>[] param = new Class<?>[1]; param[0] = Serializable.class; final Method method = PropertyCacheFile.class.getDeclaredMethod("getHashCodeBasedOnObjectContent", param); method.setAccessible(true); try { method.invoke(cache, config); fail("InvocationTargetException is expected"); } catch (InvocationTargetException ex) { assertTrue("Invalid exception cause", ex.getCause().getCause() instanceof NoSuchAlgorithmException); assertEquals("Invalid exception message", "Unable to calculate hashcode.", ex.getCause().getMessage()); } }
/** * This SuppressWarning("unchecked") required to suppress * "Unchecked generics array creation for varargs parameter" during mock. * @throws IOException when smth wrong with file creation or cache.load */ @Test public void testNonExistentResource() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); // create cache with one file cache.load(); final String myFile = "myFile"; cache.put(myFile, 1); final String hash = cache.get(PropertyCacheFile.CONFIG_HASH_KEY); assertNotNull("Config hash key should not be null", hash); mockStatic(ByteStreams.class); when(ByteStreams.toByteArray(any(BufferedInputStream.class))) .thenThrow(IOException.class); // apply new external resource to clear cache final Set<String> resources = new HashSet<>(); final String resource = getPath("InputPropertyCacheFile.header"); resources.add(resource); cache.putExternalResources(resources); assertFalse("Should return false in file is not in cache", cache.isInCache(myFile, 1)); assertFalse("Should return false in file is not in cache", cache.isInCache(resource, 1)); }
@Test public void testFlushAndCloseCacheFileOutputStream() throws IOException { mockStatic(Closeables.class); doNothing().when(Closeables.class); Closeables.close(any(OutputStream.class), ArgumentMatchers.eq(false)); mockStatic(Flushables.class); doNothing().when(Flushables.class); Flushables.flush(any(OutputStream.class), ArgumentMatchers.eq(false)); final Configuration config = new DefaultConfiguration("myName"); final PropertyCacheFile cache = new PropertyCacheFile(config, temporaryFolder.newFile().getPath()); cache.put("CheckedFileName.java", System.currentTimeMillis()); cache.persist(); verifyStatic(Closeables.class, times(1)); Closeables.close(any(OutputStream.class), ArgumentMatchers.eq(false)); verifyStatic(Flushables.class, times(1)); Flushables.flush(any(OutputStream.class), ArgumentMatchers.eq(false)); }
@Test public void testInCache() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.put("myFile", 1); assertTrue("Should return true when file is in cache", cache.isInCache("myFile", 1)); assertFalse("Should return false when file is not in cache", cache.isInCache("myFile", 2)); assertFalse("Should return false when file is not in cache", cache.isInCache("myFile1", 1)); }
cacheFile.put(fileName, timestamp);