Example #1
0
  public void testReadConfigurationInputStream_root() throws IOException {
    InputStream stream = EnvironmentHelper.PropertiesToInputStream(props);
    manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));

    Logger logger = new MockLogger("testReadConfigurationInputStream_root.foo", null);
    Logger root = manager.getLogger("");
    Logger logger2 = Logger.getLogger("testReadConfigurationInputStream_root.foo2");

    manager.addLogger(logger);
    assertNull(logger.getLevel());
    assertEquals(0, logger.getHandlers().length);
    assertSame(root, logger.getParent());

    assertNull(logger2.getLevel());
    assertEquals(0, logger2.getHandlers().length);
    assertSame(root, logger2.getParent());
    // if (!hasConfigClass) {
    assertEquals(Level.FINE, root.getLevel());
    assertEquals(2, root.getHandlers().length);
    // }

    // after read stream
    manager.readConfiguration(stream);
    assertEquals(Level.FINE, root.getLevel());
    assertEquals(2, root.getHandlers().length);
    assertNull(logger.getLevel());
    assertEquals(0, logger.getHandlers().length);
    stream.close();
  }
Example #2
0
  public void testReadConfigurationUpdatesRootLoggersHandlers() throws IOException {
    Properties properties = new Properties();
    LogManager.getLogManager()
        .readConfiguration(EnvironmentHelper.PropertiesToInputStream(properties));

    Logger root = Logger.getLogger("");
    assertEquals(0, root.getHandlers().length);

    properties.put("handlers", "java.util.logging.ConsoleHandler");
    LogManager.getLogManager()
        .readConfiguration(EnvironmentHelper.PropertiesToInputStream(properties));

    assertEquals(1, root.getHandlers().length);
  }
Example #3
0
  public void testReset() throws SecurityException, IOException {
    // mock LogManager
    mockManager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
    assertNotNull(mockManager.getProperty("handlers"));
    Logger foo = new MockLogger(FOO, null);
    assertNull(foo.getLevel());
    assertEquals(0, foo.getHandlers().length);
    foo.setLevel(Level.ALL);
    foo.addHandler(new ConsoleHandler());
    assertTrue(mockManager.addLogger(foo));
    assertEquals(Level.WARNING, foo.getLevel());
    assertEquals(2, foo.getHandlers().length);

    // reset
    mockManager.reset();

    // properties is cleared
    assertNull(mockManager.getProperty("handlers"));

    // level is null
    assertNull(foo.getLevel());
    // handlers are all closed
    assertEquals(0, foo.getHandlers().length);

    // for root logger
    manager.reset();
    assertNull(manager.getProperty("handlers"));
    Logger root = manager.getLogger("");
    // level reset to info
    assertEquals(Level.INFO, root.getLevel());
    // also close root's handler
    assertEquals(0, root.getHandlers().length);
  }
Example #4
0
  public void testReadConfigurationDoesNotUpdateOtherLoggers() throws IOException {
    Properties properties = new Properties();
    LogManager.getLogManager()
        .readConfiguration(EnvironmentHelper.PropertiesToInputStream(properties));

    Logger logger = Logger.getLogger("testReadConfigurationDoesNotUpdateOtherLoggers");
    assertEquals(0, logger.getHandlers().length);

    properties.put(
        "testReadConfigurationDoesNotUpdateOtherLoggers.handlers",
        "java.util.logging.ConsoleHandler");
    LogManager.getLogManager()
        .readConfiguration(EnvironmentHelper.PropertiesToInputStream(properties));

    assertEquals(0, logger.getHandlers().length);
  }
Example #5
0
  /*
   * Class under test for void readConfiguration(InputStream)
   */
  public void testReadConfigurationInputStream() throws IOException {
    // mock LogManager
    InputStream stream = EnvironmentHelper.PropertiesToInputStream(props);

    Logger foo = new MockLogger(FOO, null);
    assertNull(foo.getLevel());
    assertTrue(mockManager.addLogger(foo));

    Logger fo = new MockLogger("LogManagerTestFoo2", null);
    fo.setLevel(Level.ALL);
    assertTrue(mockManager.addLogger(fo));

    Handler h = new ConsoleHandler();
    Level l = h.getLevel();
    assertNotSame(Level.OFF, h.getLevel());

    // read configuration from stream
    mockManager.readConfiguration(stream);
    stream.close();

    // level DO has effect
    assertEquals(Level.WARNING, foo.getLevel());

    // for non specified logger, level is reset to null
    assertNull(fo.getLevel());

    // read properties don't affect handler
    assertNotSame(Level.OFF, h.getLevel());
    assertSame(l, h.getLevel());
  }
Example #6
0
 public ConfigClass() throws Exception {
   LogManager man = LogManager.getLogManager();
   Properties props = LogManagerTest.initProps();
   props.put("testConfigClass.foo.level", "OFF");
   props.put("testConfigClass.foo.handlers", "java.util.logging.ConsoleHandler");
   props.put(".level", "FINEST");
   props.remove("handlers");
   InputStream in = EnvironmentHelper.PropertiesToInputStream(props);
   man.readConfiguration(in);
 }
Example #7
0
  public void testAddRemovePropertyChangeListener() throws Exception {
    MockPropertyChangeListener listener1 = new MockPropertyChangeListener();
    MockPropertyChangeListener listener2 = new MockPropertyChangeListener();
    // add same listener1 two times
    mockManager.addPropertyChangeListener(listener1);
    mockManager.addPropertyChangeListener(listener1);
    mockManager.addPropertyChangeListener(listener2);

    assertNull(listener1.getEvent());
    assertNull(listener2.getEvent());
    mockManager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
    // if (!hasConfigClass) {
    assertNotNull(listener1.getEvent());
    assertNotNull(listener2.getEvent());
    // }

    listener1.reset();
    listener2.reset();

    // remove listener1, no effect
    mockManager.removePropertyChangeListener(listener1);
    mockManager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
    assertNotNull(listener1.getEvent());
    assertNotNull(listener2.getEvent());
    listener1.reset();
    listener2.reset();

    // remove listener1 again and it works
    mockManager.removePropertyChangeListener(listener1);
    mockManager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
    assertNull(listener1.getEvent());
    assertNotNull(listener2.getEvent());
    listener2.reset();

    // reset don't produce event
    mockManager.reset();
    assertNull(listener2.getEvent());

    mockManager.removePropertyChangeListener(listener2);
    mockManager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
    assertNull(listener1.getEvent());
    assertNull(listener2.getEvent());
  }
Example #8
0
  public void testGetProperty() throws SecurityException, IOException {
    //      //FIXME: move it to exec
    //        manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
    //		Logger root = manager.getLogger("");
    ////		checkProperty(manager);
    //		assertEquals(Level.FINE, root.getLevel());
    //		assertEquals(2, root.getHandlers().length);

    // but non-mock manager DO read it from the very beginning
    Logger root = manager.getLogger("");
    manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
    checkProperty(manager);
    assertEquals(2, root.getHandlers().length);
    assertEquals(Level.FINE, root.getLevel());

    manager.reset();
    checkPropertyNull(manager);
    assertEquals(0, root.getHandlers().length);
    assertEquals(Level.INFO, root.getLevel());
    manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
  }
Example #9
0
 public void testMockGetProperty() throws Exception {
   // mock manager doesn't read configuration until you call
   // readConfiguration()
   Logger root = new MockLogger("", null);
   assertTrue(mockManager.addLogger(root));
   root = mockManager.getLogger("");
   checkPropertyNull(mockManager);
   assertEquals(0, root.getHandlers().length);
   assertNull(root.getLevel());
   mockManager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
   assertEquals(Level.FINE, root.getLevel());
   checkProperty(mockManager);
   mockManager.reset();
   checkPropertyNull(mockManager);
   assertEquals(Level.INFO, root.getLevel());
   assertEquals(0, mockManager.getLogger("").getHandlers().length);
 }
Example #10
0
 public MockValidInitClass() {
   Properties p = new Properties();
   p.put("handlers", className + "$MockHandler");
   p.put(".level", "OFF");
   InputStream in = null;
   try {
     in = EnvironmentHelper.PropertiesToInputStream(p);
     LogManager manager = LogManager.getLogManager();
     manager.readConfiguration(in);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     try {
       in.close();
     } catch (Exception e) {
     }
   }
 }
Example #11
0
  public void testDefaultLoggerProperties() throws Exception {
    // mock LogManager has no default logger
    assertNull(mockManager.getLogger(""));
    assertNull(mockManager.getLogger("global"));

    // non-mock LogManager has two default logger
    Logger global = manager.getLogger("global");
    Logger root = manager.getLogger("");

    assertSame(global, Logger.global);
    assertSame(root, global.getParent());

    // root properties
    manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
    assertNull(root.getFilter());
    assertEquals(2, root.getHandlers().length);
    assertEquals(Level.FINE, root.getLevel());
    assertEquals("", root.getName());
    assertSame(root.getParent(), null);
    assertNull(root.getResourceBundle());
    assertNull(root.getResourceBundleName());
    assertTrue(root.getUseParentHandlers());
  }
Example #12
0
  public void testGlobalPropertyConfig() throws Exception {
    PrintStream err = System.err;
    try {
      System.setErr(new PrintStream(new NullOutputStream()));
      // before add config property, root has two handler
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
      assertEquals(2, manager.getLogger("").getHandlers().length);

      // one valid config class
      props.setProperty("config", className + "$MockValidConfig");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
      assertEquals(3, manager.getLogger("").getHandlers().length);

      // two config class take effect orderly
      props.setProperty(
          "config", className + "$MockValidConfig " + className + "$MockValidConfig2");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
      assertEquals(2, manager.getLogger("").getHandlers().length);

      props.setProperty(
          "config", className + "$MockValidConfig2 " + className + "$MockValidConfig");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
      assertEquals(3, manager.getLogger("").getHandlers().length);

      // invalid config class which throw exception, just print exception
      // and
      // message
      props.setProperty("config", className + "$MockInvalidConfigException");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));

      // invalid config class without default constructor, just print
      // exception and message
      props.setProperty("config", className + "$MockInvalidConfigNoDefaultConstructor");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));

      // bad config class name, just print exception and message
      props.setProperty("config", "badname");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));

      // invalid separator, nothing happened
      props.setProperty(
          "config", className + "$MockValidConfig2;" + className + "$MockValidConfig");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
      assertEquals(2, manager.getLogger("").getHandlers().length);
      props.setProperty(
          "config",
          className
              + "$MockValidConfig2;"
              + className
              + "$MockValidConfig "
              + className
              + "$MockValidConfig");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
      assertEquals(3, manager.getLogger("").getHandlers().length);

      // duplicate config class, take effect twice
      props.setProperty("config", className + "$MockValidConfig " + className + "$MockValidConfig");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
      assertEquals(4, manager.getLogger("").getHandlers().length);

      // invalid config classes mixed with valid config classes, valid
      // config
      // classes take effect
      props.setProperty(
          "config",
          "badname "
              + className
              + "$MockValidConfig "
              + className
              + "$MockInvalidConfigNoDefaultConstructor "
              + className
              + "$MockValidConfig");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
      assertEquals(4, manager.getLogger("").getHandlers().length);

      // global property take effect before logger specified property
      props.setProperty("config", className + "$MockValidConfig");
      manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
      assertEquals(Level.FINE, manager.getLogger("").getLevel());
    } finally {
      System.setErr(err);
    }
  }