예제 #1
0
  @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();
    }
  }
예제 #2
0
 @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));
 }
예제 #3
0
 @Override
 public void destroy() {
   for (Check c : mAllChecks) {
     c.destroy();
   }
   mCache.destroy();
   super.destroy();
 }
예제 #4
0
  @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);
    }
  }
예제 #5
0
 @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();
 }
예제 #6
0
  /**
   * @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();
  }
예제 #7
0
  @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);
    }
  }