TestAdditivity.java :  » Development » Monolog » org » objectweb » util » monolog » Java Open Source

Java Open Source » Development » Monolog 
Monolog » org » objectweb » util » monolog » TestAdditivity.java
/**
 * Copyright (C) 2002
 */

package org.objectweb.util.monolog;

import org.objectweb.util.monolog.api.TopicalLogger;
import org.objectweb.util.monolog.api.Handler;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * This test check the additivity flag of the logger.
 *
 * @author Sebastien Chassande-Barrioz
 */
public class TestAdditivity extends TestHelper {

  public static final String LOG_FILE_NAME = "test.log";
  public static final String LOG_PATTERN = "%m%n";

  TopicalLogger l = null;

  /**
   * For running the TestLogger suite standalone.
   */
  public static void main(String args[]) {
    if (args.length < 1) {
      System.out.println("Syntax error !");
      System.out.println("java TestAdditivity <logger factory class name>");
      System.exit(1);
    }
    TestHelper.run(TestAdditivity.class, new String[0],
      new String[0], args[0]);
  }

  public static TestSuite getTestSuite(String lfcn) {
    return TestHelper.getTestSuite(TestAdditivity.class, new String[0],
      new String[0], lfcn);
  }

  // ------ TEST METHODS ------ //
  //----------------------------//

  public void testA() {
    quietRootLogger();
    TopicalLogger l1 = (TopicalLogger) lf.getLogger("test.additivity");
    TopicalLogger l2 = (TopicalLogger) lf.getLogger("test.additivity.foo");
    Handler hc1 = hf.createHandler("myhandler", "file");
    hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '1');
    hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
    hc1.setAttribute("activation", lf);
    Handler hc2 = hf.createHandler("myhandler2", "file");
    hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '2');
    hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
    hc2.setAttribute("activation", lf);
    try {
      l1.addHandler(hc1);
      l2.addHandler(hc2);
    }
    catch (Exception e) {
      fail(e.getMessage());
    }
    l2.setAdditivity(false);
    l2.setIntLevel(BasicLevel.DEBUG);
    l2.log(BasicLevel.DEBUG, "simple additivity");
    // The log message must be found in the file 2
    String[] found2 = getLastLines(LOG_FILE_NAME + '2', 1);
    assertNotNull("TestHelper error", found2);
    assertNotNull("TestHelper error", found2[0]);
    assertTrue("A.1",
      found2[0].endsWith("simple additivity"));

    // The log message must NOT be found in the file 1
    String[] found1 = getLastLines(LOG_FILE_NAME + '1', 1);
    if (found1 !=null && found1.length>0 && found1[0] !=null
      && found1[0].endsWith("simple additivity")) {
      fail("A.2");
    }

    l2 = (TopicalLogger) lf.getLogger("test.additivity.foo.bar");
    l2.log(BasicLevel.DEBUG, "simple additivity2");
    // The log message must be found in the file 2
    found2 = getLastLines(LOG_FILE_NAME + '2', 1);
    assertNotNull("TestHelper error", found2);
    assertNotNull("TestHelper error", found2[0]);
    assertTrue("A.3",
      found2[0].endsWith("simple additivity2"));

    // The log message must NOT be found in the file 1
    found1 = getLastLines(LOG_FILE_NAME + '1', 1);
    if (found1 !=null && found1.length>0 && found1[0] !=null
      && found1[0].endsWith("simple additivity2")) {
      fail("A.4");
    }
  }
  public void testB() {
    quietRootLogger();
    TopicalLogger l1 = (TopicalLogger) lf.getLogger("test.additivity");
    TopicalLogger l2 = (TopicalLogger) lf.getLogger("test.additivity.foo");
    Handler hc1 =
      hf.createHandler("myhandler", "file");
    hc1.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '1');
    hc1.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
    hc1.setAttribute("activation", lf);
    Handler hc2 = hf.createHandler("myhandler2", "file");
    hc2.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME + '2');
    hc2.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
    hc2.setAttribute("activation", lf);
    try {
      l1.addHandler(hc1);
      l2.addHandler(hc2);
    }
    catch (Exception e) {
      fail(e.getMessage());
    }
    l2.setAdditivity(false);
    l2.log(BasicLevel.DEBUG, "simple additivity B");
    // The log message must be found in the file 2
    String[] found2 = getLastLines(LOG_FILE_NAME + '2', 1);
    assertNotNull("TestHelper error", found2);
    assertNotNull("TestHelper error", found2[0]);
    assertTrue("B.1",
      found2[0].endsWith("simple additivity B"));

    // The log message must NOT be found in the file 1
    String[] found1 = getLastLines(LOG_FILE_NAME + '1', 1);
    if (found1 !=null && found1.length>0 && found1[0] !=null
      && found1[0].endsWith("simple additivity B")) {
      fail("B.2");
    }

    l2 = (TopicalLogger) lf.getLogger("test.additivity.foo.bar");
    l2.setIntLevel(BasicLevel.DEBUG);
    l2.log(BasicLevel.DEBUG, "simple additivity B 2");
    // The log message must be found in the file 2
    found2 = getLastLines(LOG_FILE_NAME + '2', 1);
    assertNotNull("TestHelper error", found2);
    assertNotNull("TestHelper error", found2[0]);
    assertTrue("B.3",
      found2[0].endsWith("simple additivity B 2"));

    // The log message must NOT be found in the file 1
    found1 = getFirstLines(LOG_FILE_NAME + '1', 1);
    if (found1 !=null && found1.length>0 && found1[0] !=null
      && found1[0].endsWith("simple additivity B 2")) {
      fail("B.4");
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.