@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 public void destroy() { for (Check c : mAllChecks) { c.destroy(); } mCache.destroy(); super.destroy(); }
@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); } }
@Override public void destroy() { for (Check check : ordinaryChecks) { check.destroy(); } for (Check check : commentChecks) { check.destroy(); } if (cache != null) { try { cache.persist(); } catch (IOException e) { throw new IllegalStateException("Unable to persist cache file", e); } } super.destroy(); }
/** * @param fileName the cache file * @throws IOException if there are some problems with file loading */ public void setCacheFile(String fileName) throws IOException { final Configuration configuration = getConfiguration(); cache = new PropertyCacheFile(configuration, fileName); cache.load(); }
@Override protected void processFiltered(File aFile, List<String> aLines) { // check if already checked and passed the file final String fileName = aFile.getPath(); final long timestamp = aFile.lastModified(); if (mCache.alreadyChecked(fileName, timestamp)) { return; } try { final FileText text = FileText.fromLines(aFile, aLines); final FileContents contents = new FileContents(text); final DetailAST rootAST = TreeWalker.parse(contents); walk(rootAST, contents); } catch (final RecognitionException re) { Utils.getExceptionLogger().debug("RecognitionException occured.", re); getMessageCollector() .add( new LocalizedMessage( re.getLine(), re.getColumn(), Defn.CHECKSTYLE_BUNDLE, "general.exception", new String[] {re.getMessage()}, getId(), this.getClass(), null)); } catch (final TokenStreamRecognitionException tre) { Utils.getExceptionLogger().debug("TokenStreamRecognitionException occured.", tre); final RecognitionException re = tre.recog; if (re != null) { getMessageCollector() .add( new LocalizedMessage( re.getLine(), re.getColumn(), Defn.CHECKSTYLE_BUNDLE, "general.exception", new String[] {re.getMessage()}, getId(), this.getClass(), null)); } else { getMessageCollector() .add( new LocalizedMessage( 0, Defn.CHECKSTYLE_BUNDLE, "general.exception", new String[] {"TokenStreamRecognitionException occured."}, getId(), this.getClass(), null)); } } catch (final TokenStreamException te) { Utils.getExceptionLogger().debug("TokenStreamException occured.", te); getMessageCollector() .add( new LocalizedMessage( 0, Defn.CHECKSTYLE_BUNDLE, "general.exception", new String[] {te.getMessage()}, getId(), this.getClass(), null)); } catch (final Throwable err) { Utils.getExceptionLogger().debug("Throwable occured.", err); getMessageCollector() .add( new LocalizedMessage( 0, Defn.CHECKSTYLE_BUNDLE, "general.exception", new String[] {"" + err}, getId(), this.getClass(), null)); } if (getMessageCollector().size() == 0) { mCache.checkedOk(fileName, timestamp); } }