Package org.objectweb.util.monolog

Source Code of org.objectweb.util.monolog.TestLogger

/**
* Copyright (C) 2002
*/

package org.objectweb.util.monolog;

import junit.framework.Assert;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Handler;
import org.objectweb.util.monolog.api.TopicalLogger;

/**
* It verifies a TopicalLogger implementation and a TopicalFactory
* implementation.
*
* @author Sebastien Chassande-Barrioz
*/
public class TestLogger extends TestHelper {

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

  TopicalLogger l = null;

  public TestLogger() {
  }

  public TestLogger(String s) {
    super(s);
  }

  /**
   * 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 TestLogger <logger factory class name>");
      System.exit(1);
    }
    TestHelper.run(TestLogger.class, new String[0],
      new String[0], args[0]);
  }

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

  // ------ TEST METHODS ------ //
  //----------------------------//
  public void testNbLogger() {
    assertEquals("nb initial loggers", 1, lf.getLoggers().length);
  }

  public void testRootLoggerConf() {
    l = (TopicalLogger) lf.getLogger("");
    assertNotNull("Root logger undefined", l);
    String[] ts = l.getTopic();
    assertNotNull("Topic list is null", ts);
    assertEquals("several topic", 1, ts.length);
    assertNotNull("Topic list is null", ts[0]);
    assertEquals("name 'root' not equals to ''", l, lf.getLogger("root"));
    assertTrue("several topic", ts[0].length() == 0 || "root".equals(ts[0]));

  }

  public void testGetOneLoggerByName() {
    l = (TopicalLogger) lf.getLogger("foo");

    assertEquals("same name but 2 instances",
      l, lf.getLogger("foo"));
    String[] topics = ((TopicalLogger) lf.getLogger("foo")).getTopic();
    assertNotNull("Topic list is null", topics);
    assertEquals("several topic", 1, topics.length);
    assertNotNull("Topic elem is null", topics[0]);
    assertTrue("bad name", topics[0].equals("foo"));
  }

  public void testGetAllLoggerConf() {
    int iternumber = 20;
    for (int i = 0; i < iternumber; i++) {
      l = (TopicalLogger) lf.getLogger("foo" + i);
    }
    TopicalLogger[] locs = (TopicalLogger[]) lf.getLoggers();
    assertNotNull("LoggerConf list is null", locs);
    TopicalLogger[] locs2 = new TopicalLogger[iternumber];
    for (int i = 0; i < locs.length; i++) {
      assertNotNull("LoggerConf list element is null", locs[i]);
      String[] ts = locs[i].getTopic();
      assertNotNull("topic list is null", ts);
      assertEquals("bad Topic list size", 1, ts.length);
      assertNotNull("Null topic", ts[0]);
      int j = 0;
      if ((ts[0].length() > 3) && ts[0].startsWith("foo", 0)) {
        try {
          j = Integer.parseInt(ts[0].substring(3, ts[0].length()));
        }
        catch (NumberFormatException e) {
          fail("Bad topic name: " + ts[0]);
        }
        assertNull("duplicate LoggerConf", locs2[j]);
        locs2[j] = locs[i];
        assertEquals("bad topic", "foo" + j, ts[0]);
      }
    }
        boolean allset = true;

        for (int i = 0; i < iternumber && allset; i++) {
            allset = locs2[i] != null;
        }
        if (!allset) {
            for (int i = 0; i < iternumber; i++) {
                if (locs2[i]==null)
                    System.out.println("losc2[" + i + "]=null value");
                else
                    System.out.println("losc2[" + i + "]=" + locs2[i].getTopic()[0]);
            }
            assertTrue("Some logger has not been found", allset);
        }
  }

  public void testMultipleTopic() {
    l = (TopicalLogger) lf.getLogger("foo");

    assertEquals("same name but 2 instances",
      l, lf.getLogger("foo"));
    try {
      l.addTopic("bar");
      l.addTopic("azerty");
      l.addTopic("querty");
    }
    catch (Exception e) {
      fail("does not support multiple topic");
    }
    /*assertEquals("additionnal (1) name but 2 instances",
      l, lf.getLogger("bar"));
    assertEquals("additionnal (2) name but 2 instances",
      l, lf.getLogger("azerty"));
    assertEquals("additionnal (3) name but 2 instances",
      l, lf.getLogger("querty")); */

    String[] tcs = l.getTopic();
    assertNotNull("Topic list is null", tcs);
    assertEquals("Wrong topic number", 4, tcs.length);
    String[] tcs2 = new String[4];
    for (int i = 0; i < tcs.length; i++) {
      assertNotNull("Null topic", tcs[i]);
      if (tcs[i].equals("foo")) {
        assertNull("duplicate first name", tcs2[0]);
        tcs2[0] = tcs[i];
      }
      else if (tcs[i].equals("bar")) {
        assertNull("duplicate name", tcs2[1]);
        tcs2[1] = tcs[i];
      }
      else if (tcs[i].equals("azerty")) {
        assertNull("duplicate name", tcs2[2]);
        tcs2[2] = tcs[i];
      }
      else if (tcs[i].equals("querty")) {
        assertNull("duplicate name", tcs2[3]);
        tcs2[3] = tcs[i];
      }
    }
  }

  public void testSimpleInheritanceLevel() {
    l = (TopicalLogger) lf.getLogger("test.simple.inheritance.level.toto");
    l.setIntLevel(BasicLevel.WARN);
    l = (TopicalLogger) lf.getLogger("test.simple.inheritance.level.toto.titi");
    assertTrue("wrong isLoggable return 1", l.isLoggable(BasicLevel.WARN));
    assertTrue("wrong isLoggable return 2", !l.isLoggable(BasicLevel.DEBUG));

    l.setIntLevel(BasicLevel.DEBUG);
    assertTrue("wrong isLoggable return 3", l.isLoggable(BasicLevel.WARN));
    assertTrue("wrong isLoggable return 4", l.isLoggable(BasicLevel.DEBUG));
  }

  public void testTopicsInheritanceLevel() {
    l = (TopicalLogger) lf.getLogger("test.topic.inheritance.level.toto");
    l.setIntLevel(BasicLevel.WARN);
    l = (TopicalLogger) lf.getLogger("test.topic.inheritance.level.toto.titi");
    assertTrue("wrong isLoggable return 1", l.isLoggable(BasicLevel.WARN));
    assertTrue("wrong isLoggable return 2",
      !l.isLoggable(BasicLevel.DEBUG));

    try {
      l.addTopic("test.topic.inheritance.level.tutu.titi");
    }
    catch (Exception e) {
      Assert.fail("Multiple topic error: " + e.getMessage());
    }
    l = (TopicalLogger) lf.getLogger("test.topic.inheritance.level.tutu");
    l.setIntLevel(BasicLevel.DEBUG);
    assertTrue("wrong isLoggable return 3", l.isLoggable(BasicLevel.WARN));
    assertTrue("wrong isLoggable return 4", l.isLoggable(BasicLevel.DEBUG));
  }

  public void testLogInCollocatedHandler() {
    quietRootLogger();
    l = (TopicalLogger) lf.getLogger("test.simple.log");
    Handler hc =
      hf.createHandler("myhandler", "file");
    hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
    hc.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
    hc.setAttribute("activation", lf);
    try {
      l.addHandler(hc);
    }
    catch (Exception e) {
      fail(e.getMessage());
    }
    l.setIntLevel(BasicLevel.DEBUG);
    l.log(BasicLevel.DEBUG, "collocated Handler bar");
    String[] found = getFirstLines(LOG_FILE_NAME, 1);
    assertNotNull("TestHelper error", found);
    assertNotNull("TestHelper error", found[0]);
    assertTrue("no log in collocated Handler", found[0].endsWith("collocated Handler bar"));
  }

  public void testLogInSeveralCollocatedHandler() {
    quietRootLogger();
    l = (TopicalLogger) lf.getLogger("test.simple.log");
    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", hf);

    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", hf);

    try {
      l.addHandler(hc1);
      l.addHandler(hc2);
    }
    catch (Exception e) {
      fail(e.getMessage());
    }

    l.setIntLevel(BasicLevel.DEBUG);
    l.log(BasicLevel.DEBUG, "several collocated Handler bar");

    String[] found = getFirstLines(LOG_FILE_NAME + "1", 1);
    assertNotNull("TestHelper error", found);
    assertNotNull("TestHelper error", found[0]);
    assertTrue("no log in collocated Handler",
      found[0].endsWith("several collocated Handler bar"));

    found = getFirstLines(LOG_FILE_NAME + "2", 1);
    assertNotNull("TestHelper error", found);
    assertNotNull("TestHelper error", found[0]);
    assertTrue("no log in collocated Handler",
      found[0].endsWith("several collocated Handler bar"));
  }

  public void testLogInInheritedHandler() {
    quietRootLogger();
    l = (TopicalLogger) lf.getLogger("test.simple.log");
    Handler hc = hf.createHandler("myhandler", "file");
    hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, LOG_FILE_NAME);
    hc.setAttribute(Handler.PATTERN_ATTRIBUTE, LOG_PATTERN);
    hc.setAttribute("activation", hf);
    try {
      l.addHandler(hc);
    }
    catch (Exception e) {
      fail(e.getMessage());
    }
    l.setIntLevel(BasicLevel.DEBUG);
    l = (TopicalLogger) lf.getLogger("test.simple.log.foo");
    l.log(BasicLevel.DEBUG, "inherited Handler bar");
    String[] found = getLastLines(LOG_FILE_NAME, 1);
    assertNotNull("TestHelper error", found);
    assertNotNull("TestHelper error", found[0]);
    assertTrue("no log in inherited Handler", found[0].endsWith("inherited Handler bar"));
  }

  public void testLogInSeveralInheritedHandler() {
    quietRootLogger();
    l = (TopicalLogger) lf.getLogger("test.simple.log");
    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", hf);

    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", hf);

    try {
      l.addHandler(hc1);
      l = (TopicalLogger) lf.getLogger("test.simple.log.foo");
      l.addHandler(hc2);
    }
    catch (Exception e) {
      fail(e.getMessage());
    }

    l = (TopicalLogger) lf.getLogger("test.simple.log.foo.bar");
    l.setIntLevel(BasicLevel.DEBUG);
    l.log(BasicLevel.DEBUG, "several collocated Handler bar");

    String[] found = getFirstLines(LOG_FILE_NAME + "1", 1);
    assertNotNull("TestHelper error", found);
    assertNotNull("TestHelper error", found[0]);
    assertTrue("no log in collocated Handler",
      found[0].endsWith("several collocated Handler bar"));

    found = getFirstLines(LOG_FILE_NAME + "2", 1);
    assertNotNull("TestHelper error", found);
    assertNotNull("TestHelper error", found[0]);
    assertTrue("no log in collocated Handler",
      found[0].endsWith("several collocated Handler bar"));
  }
}
TOP

Related Classes of org.objectweb.util.monolog.TestLogger

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.