Example #1
0
  @Test
  public void testConstructor() {
    FileLogger loLogger = new FileLogger();
    assertNotNull(loLogger);

    String lcFileName = "./logs/" + Java.getCallingMethodName(false, 0);

    loLogger = new FileLogger(new File(lcFileName + ".log"));
    assertNotNull(loLogger);
  }
Example #2
0
 @SuppressWarnings("unchecked")
 @Override
 protected <K> K onSetProperty(String[] taPath, K toValue) {
   return (K)
       m_oProperties.setProperty(
           denormalise(taPath),
           toValue != null
               ? Java.isPrimitive(toValue) ? toValue.toString() : Utilities.toJSONString(toValue)
               : null);
 }
Example #3
0
 /**
  * JavaPropertyManager stored data as strings at the moment, so need to convert to the correct
  * type here
  *
  * @param taPath the path of the property to get
  * @param toDefault the default value of the property
  * @param <K> the type of the value
  * @return the value or null if there was no value of this type
  */
 @SuppressWarnings("unchecked")
 @Override
 protected <K> K onGetProperty(String[] taPath, K toDefault) {
   K loReturn = onGetProperty(taPath);
   if (loReturn == null) {
     setProperty(taPath, toDefault);
     loReturn = toDefault;
   } else {
     loReturn = (K) Java.stringToPrimitive((String) loReturn, toDefault.getClass());
   }
   return loReturn;
 }
Example #4
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());
  }
Example #5
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();
    }
  }
Example #6
0
  @Test
  public void testConfig() throws Exception {
    String lcFileName = "./logs/" + Java.getCallingMethodName(false, 0);
    final File loFile = new File(lcFileName + ".log");
    loFile.delete();

    TestApplicationConfiguration.executeAsApplicationCore(
        new TestExecutable() {
          @Override
          protected void onRun() {

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

            loLogger.config((IApplicationConfiguration) getApplication().getConfig());

            loLogger.log(lcMessage);
            loLogger.flush();

            assertTrue(loFile.exists());
          }
        });
  }