예제 #1
0
 /**
  * Convert the given array of lines into a single source string.
  *
  * @param lines the lines to be merged into a single source string
  * @return the source string composed of the given lines
  */
 public static String createSource(String... lines) {
   PrintStringWriter writer = new PrintStringWriter();
   for (String line : lines) {
     writer.println(line);
   }
   return writer.toString();
 }
예제 #2
0
  /** Assert adding library directive causes sourced file to become analyzed as library */
  public void test_changed_library_directive() throws Exception {
    final String oldContent = FileUtilities.getContents(simpleMoneySrcFile);
    PrintStringWriter writer = new PrintStringWriter();
    writer.println("library \"foobar\";\n");
    writer.append(oldContent);
    final String newContent = writer.toString();

    server.scan(moneyLibFile, null);
    server.start();
    listener.waitForIdle(1, FIVE_MINUTES_MS);
    assertTrackedLibraryFiles(server, moneyLibFile);
    Object lib1 = getCachedLibrary(savedContext, moneyLibFile);
    assertNotNull(lib1);
    assertNull(getCachedLibrary(savedContext, simpleMoneySrcFile));

    server.resetAnalyze();
    final long oldLastModified = simpleMoneySrcFile.lastModified();
    try {
      FileUtilities.setContents(simpleMoneySrcFile, newContent);
      // Ensure marked as modified... lastModified is only accurate to the second
      simpleMoneySrcFile.setLastModified(oldLastModified + 1000);
      server.changed(simpleMoneySrcFile);
      listener.waitForIdle(2, FIVE_MINUTES_MS);
      assertTrackedLibraryFiles(server, moneyLibFile, simpleMoneySrcFile);
      server.assertAnalyze(false, moneyLibFile, simpleMoneySrcFile);
      Object lib2 = getCachedLibrary(savedContext, moneyLibFile);
      assertNotNull(lib2);
      assertNotSame(lib1, lib2);
      lib1 = lib2;
      assertNotNull(getCachedLibrary(savedContext, simpleMoneySrcFile));
    } finally {
      FileUtilities.setContents(simpleMoneySrcFile, oldContent);
      simpleMoneySrcFile.setLastModified(oldLastModified);
    }

    server.resetAnalyze();
    server.changed(simpleMoneySrcFile);
    listener.waitForIdle(3, FIVE_MINUTES_MS);
    assertTrackedLibraryFiles(server, moneyLibFile);
    server.assertAnalyze(false, moneyLibFile, simpleMoneySrcFile);
    Object lib2 = getCachedLibrary(savedContext, moneyLibFile);
    assertNotNull(lib2);
    assertNotSame(lib1, lib2);
    assertNull(getCachedLibrary(savedContext, simpleMoneySrcFile));
  }
예제 #3
0
 /** Assert that the Eclipse .log file does not exist. */
 public static void assertNoLogFile() {
   File logFile = getLogFile();
   if (logFile.exists()) {
     PrintStringWriter writer = new PrintStringWriter();
     try {
       String contents = FileUtilities.getContents(logFile);
       writer.println("Non-empty log file. Log file contents:");
       writer.println();
       writer.print(contents);
     } catch (IOException exception) {
       writer.println("Non-empty log file. Could not access contents of log file.");
       writer.println();
       exception.printStackTrace(writer);
     }
     Assert.fail(writer.toString());
   }
 }
 /**
  * Assert that the number of errors that have been gathered matches the number of errors that are
  * given and that they have the expected error codes. The order in which the errors were gathered
  * is ignored.
  *
  * @param errorCodes the errors that should have been gathered
  * @throws AssertionFailedError with
  */
 private void fail(AnalysisError[] expectedErrors) {
   PrintStringWriter writer = new PrintStringWriter();
   writer.print("Expected ");
   writer.print(expectedErrors.length);
   writer.print(" errors:");
   for (AnalysisError error : expectedErrors) {
     Source source = error.getSource();
     File file = source == null ? null : source.getFile();
     LineInfo lineInfo = lineInfoMap.get(source);
     writer.println();
     if (lineInfo == null) {
       int offset = error.getOffset();
       writer.printf(
           "  %s %s (%d..%d)",
           file == null ? "" : file.getName(),
           error.getErrorCode(),
           offset,
           offset + error.getLength());
     } else {
       LineInfo.Location location = lineInfo.getLocation(error.getOffset());
       writer.printf(
           "  %s %s (%d, %d/%d)",
           source == null ? "" : source.getFile().getName(),
           error.getErrorCode(),
           location.getLineNumber(),
           location.getColumnNumber(),
           error.getLength());
     }
   }
   writer.println();
   writer.print("found ");
   writer.print(errors.size());
   writer.print(" errors:");
   for (AnalysisError error : errors) {
     Source source = error.getSource();
     File file = source == null ? null : source.getFile();
     LineInfo lineInfo = lineInfoMap.get(source);
     writer.println();
     if (lineInfo == null) {
       int offset = error.getOffset();
       writer.printf(
           "  %s %s (%d..%d): %s",
           file == null ? "" : file.getName(),
           error.getErrorCode(),
           offset,
           offset + error.getLength(),
           error.getMessage());
     } else {
       LineInfo.Location location = lineInfo.getLocation(error.getOffset());
       writer.printf(
           "  %s %s (%d, %d/%d): %s",
           source == null ? "" : source.getFile().getName(),
           error.getErrorCode(),
           location.getLineNumber(),
           location.getColumnNumber(),
           error.getLength(),
           error.getMessage());
     }
   }
   Assert.fail(writer.toString());
 }