示例#1
0
  @Test
  public void testStopwatch() throws Exception {
    String lcFileName = "./logs/" + Java.getCallingMethodName(false, 0);
    File loFile = new File(lcFileName + ".log");
    loFile.delete();

    String lcMessage = "This is the message that is to be logged";
    FileLogger loLogger = new FileLogger(3, 10, loFile);

    for (int i = 0; i < 10; i++) {
      loLogger.log(lcMessage);
    }

    Thread.sleep(1500);

    assertTrue(loFile.exists());
  }
示例#2
0
  @Test
  public void testLog() throws Exception {
    String lcFileName = "./logs/" + Java.getCallingMethodName(false, 0);
    new File(lcFileName + ".log").delete();

    for (int i = 0; i < 3; i++) {
      new File(lcFileName + "_" + (i + 1) + ".log").delete();
    }

    String lcMessage = "This is the message that is to be logged";
    FileLogger loLogger = new FileLogger(3, 10, new File(lcFileName + ".log"));

    for (int i = 0; i < 4; i++) {
      loLogger.log(lcMessage + " i = " + i);
      // Force Flush
      loLogger.flush();
    }

    loLogger.log("final write");
    loLogger.flush();

    // Test the file contents
    BufferedReader loReader = new BufferedReader(new FileReader(lcFileName + ".log"));

    String lcLine = loReader.readLine();
    assertTrue(lcLine.contains("final write"));
    loReader.close();

    for (int i = 0; i < 3; i++) {
      loReader = new BufferedReader(new FileReader(lcFileName + "_" + (i + 1) + ".log"));
      lcLine = loReader.readLine();
      if (i == 0) {
        assertTrue(lcLine.contains("This is the message that is to be logged" + " i = " + 3));
      } else {
        assertTrue(lcLine.contains("This is the message that is to be logged" + " i = " + i));
      }
      loReader.close();
    }
  }