@Test public void testException_NoSuchAlgorithmException() throws Exception { Configuration config = new DefaultConfiguration("myname"); final String filePath = temporaryFolder.newFile().getPath(); PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.put("myFile", 1); mockStatic(MessageDigest.class); when(MessageDigest.getInstance("SHA-1")).thenThrow(NoSuchAlgorithmException.class); Class<?>[] param = new Class<?>[1]; param[0] = Serializable.class; Method method = PropertyCacheFile.class.getDeclaredMethod("getConfigHashCode", param); method.setAccessible(true); try { method.invoke(cache, config); fail(); } catch (InvocationTargetException e) { assertTrue(e.getCause().getCause() instanceof NoSuchAlgorithmException); assertEquals("Unable to calculate hashcode.", e.getCause().getMessage()); } catch (Exception e) { fail(); } }
@Test public void testInCache() throws IOException { Configuration config = new DefaultConfiguration("myname"); final String filePath = temporaryFolder.newFile().getPath(); PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.put("myFile", 1); assertTrue(cache.inCache("myFile", 1)); assertFalse(cache.inCache("myFile", 2)); assertFalse(cache.inCache("myFile1", 1)); }
@Override protected void processFiltered(File file, List<String> lines) { // check if already checked and passed the file final String fileName = file.getPath(); final long timestamp = file.lastModified(); if (cache != null && (cache.inCache(fileName, timestamp) || !Utils.fileExtensionMatches(file, getFileExtensions()))) { return; } final String msg = "%s occurred during the analysis of file %s."; try { final FileText text = FileText.fromLines(file, lines); final FileContents contents = new FileContents(text); final DetailAST rootAST = parse(contents); getMessageCollector().reset(); walk(rootAST, contents, AstState.ORDINARY); final DetailAST astWithComments = appendHiddenCommentNodes(rootAST); walk(astWithComments, contents, AstState.WITH_COMMENTS); } catch (final TokenStreamRecognitionException tre) { final String exceptionMsg = String.format(msg, "TokenStreamRecognitionException", fileName); LOG.error(exceptionMsg); final RecognitionException re = tre.recog; final String message = re.getMessage(); getMessageCollector().add(createLocalizedMessage(message)); } // RecognitionException and any other (need to check if needed) catch (Throwable ex) { final String exceptionMsg = String.format(msg, ex.getClass().getSimpleName(), fileName); LOG.error(exceptionMsg, ex); getMessageCollector().add(createLocalizedMessage(ex.getMessage())); } if (cache != null && getMessageCollector().size() == 0) { cache.put(fileName, timestamp); } }