harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r443320 - in /incubator/harmony/enhanced/classlib/trunk/modules/logging/src: main/java/java/util/logging/LogManager.java main/java/java/util/logging/Logger.java test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java
Date Thu, 14 Sep 2006 11:34:51 GMT
Author: pyang
Date: Thu Sep 14 04:34:50 2006
New Revision: 443320

URL: http://svn.apache.org/viewvc?view=rev&rev=443320
Log:
More refactory, including: 
1. decoupling Logger and LogManager
2. more tests updated to be independent on default properties

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java?view=diff&rev=443320&r1=443319&r2=443320
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java
Thu Sep 14 04:34:50 2006
@@ -24,6 +24,7 @@
 import java.io.InputStream;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.Collection;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -195,10 +196,8 @@
 				}
 
 				// if global logger has been initialized, set root as its parent
-                Logger root = manager.getLogger(""); //$NON-NLS-1$
-				if (null != Logger.global && null != root) {
-					Logger.global.setParent(root);
-				}
+                Logger root = Logger.getLogger(""); //$NON-NLS-1$
+                Logger.global.setParent(root);
 				return null;
 			}
 		});
@@ -283,28 +282,10 @@
         }
         addToFamilyTree(logger, name);
         loggers.put(name, logger);
-
-        setLoggerLevel(logger, name, false);
-        logger.manager = this;
+        logger.setManager(this);
         return true;
     }
 
-    private void setLoggerLevel(Logger logger, String loggerName,
-                                boolean inherit) {
-        String configedLevel = getProperty(loggerName + ".level"); //$NON-NLS-1$
-        if (null != configedLevel) {
-            try {
-                logger.setLevel(Level.parse(configedLevel));
-            } catch (IllegalArgumentException e) {
-                // Print invalid level setting to the screen
-                // logging.1=Invalid level name: {0}.
-                System.err
-                        .print(Messages.getString("logging.1", configedLevel)); //$NON-NLS-1$
-            }
-        } else if (inherit) {
-            logger.setLevel(null);
-        }
-    }
 
     private void addToFamilyTree(Logger logger, String name) {
         Logger parent = null;
@@ -323,8 +304,8 @@
         }
 
         // find children
-        Logger[] values = loggers.values().toArray(new Logger[0]);
-        for (Logger child : values) {
+        Collection<Logger> allLoggers = loggers.values();
+        for (Logger child : allLoggers) {
             Logger oldParent = child.getParent();
             if (parent == oldParent
                     && (name.length() == 0 || child.getName().startsWith(
@@ -477,9 +458,11 @@
             if (property.endsWith(".level")) { //$NON-NLS-1$
                 String loggerName = property.substring(0,
                         property.length() - ".level".length()); //$NON-NLS-1$
-                Logger l = createLoggers?Logger.getLogger(loggerName):loggers.get(loggerName);
+                Logger l = loggers.get(loggerName);
                 if (null != l) {
-                    setLoggerLevel(l, loggerName, true);
+                    l.setManager(this);
+                } else if (createLoggers) {
+                    Logger.getLogger(loggerName);
                 }
             }else if(createLoggers && property.endsWith(".handlers")){ //$NON-NLS-1$
                 String loggerName = property.substring(0,
@@ -540,19 +523,7 @@
         Iterator<Logger> it = loggers.values().iterator();
         while (it.hasNext()) {
             Logger l = (Logger) it.next();
-            l.setLevel(null);
-            if(l.handlers != null){
-                for (Handler element : l.handlers) {
-                    // close all handlers, when unknown exceptions happen,
-                    // ignore them and go on
-                    try {
-                        element.close();
-                    } catch (Exception e) {
-                        // Ignored.
-                    }
-                }
-                l.handlers = null;
-            }
+            l.reset();
         }
         Logger root = loggers.get(""); //$NON-NLS-1$
         if (null != root) {

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java?view=diff&rev=443320&r1=443319&r2=443320
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java
Thu Sep 14 04:34:50 2006
@@ -129,7 +129,7 @@
     private ResourceBundle resBundle;
 
     // the handlers attached to this logger
-    List<Handler> handlers = null;
+    private List<Handler> handlers = null;
 
     /*
      * flag indicating whether to notify parent's handlers on receiving a log
@@ -142,7 +142,9 @@
 
     private List<Logger> childs;
     
-    LogManager manager = null;
+    private LogManager manager = null;
+
+    private boolean handlerInited = false;
 
     /*
      * -------------------------------------------------------------------
@@ -436,12 +438,14 @@
     }
     
     private void initHandler() {
-      if(handlers == null){
-          if(manager == null){
+      if(!handlerInited){
+          handlerInited = true;
+          if(handlers == null){
               handlers = new ArrayList<Handler>();
+          }
+          if(manager == null){
               return;
           }
-          handlers = new ArrayList<Handler>();
           String handlerStr = manager.getProperty("".equals(name)?"handlers":name+".handlers");
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
           if (null == handlerStr) {
               return;
@@ -1389,5 +1393,38 @@
         }
     }
 
+    void setManager(LogManager manager) {
+        if(this.manager != manager){
+            this.manager = manager;
+            handlerInited  = false;
+        }
+        //init level here, but let handlers be for lazy loading
+        String configedLevel = manager.getProperty(name+ ".level"); //$NON-NLS-1$
+        if (null != configedLevel) {
+            try {
+                setLevel(Level.parse(configedLevel));
+            } catch (IllegalArgumentException e) {
+                //ignore
+            }
+        }        
+    }
+
+    void reset() {
+        levelObjVal = null;
+        levelIntVal = Level.INFO.intValue();
+        if(handlers != null){
+            for (Handler element : handlers) {
+                // close all handlers, when unknown exceptions happen,
+                // ignore them and go on
+                try {
+                    element.close();
+                } catch (Exception e) {
+                    // Ignored.
+                }
+            }
+            handlers.clear();
+            handlerInited = false;
+        }
+    }
 }
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java?view=diff&rev=443320&r1=443319&r2=443320
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java
Thu Sep 14 04:34:50 2006
@@ -41,7 +41,9 @@
  */
 public class LogManagerTest extends TestCase {
 
-	LogManager mockManager;
+	private static final String FOO = "LogManagerTestFoo";
+
+    LogManager mockManager;
 
 	LogManager manager = LogManager.getLogManager();
 
@@ -88,8 +90,8 @@
 		props.put("java.util.logging.ConsoleHandler.level", "OFF");
 		props.put("java.util.logging.ConsoleHandler.formatter",
 				"java.util.logging.SimpleFormatter");
-		props.put("foo.handlers", "java.util.logging.ConsoleHandler");
-		props.put("foo.level", "WARNING");
+		props.put("LogManagerTestFoo.handlers", "java.util.logging.ConsoleHandler");
+		props.put("LogManagerTestFoo.level", "WARNING");
 	}
 
 	/*
@@ -101,11 +103,11 @@
 	}
 
 	public void testAddGetLogger() {
-		Logger log = new MockLogger("foo", null);
-		Logger foo = mockManager.getLogger("foo");
+		Logger log = new MockLogger(FOO, null);
+		Logger foo = mockManager.getLogger(FOO);
 		assertNull(foo);
 		assertTrue(mockManager.addLogger(log));
-		foo = mockManager.getLogger("foo");
+		foo = mockManager.getLogger(FOO);
 		assertSame(foo, log);
 		assertNull(foo.getParent());
 
@@ -128,19 +130,19 @@
 		while (enumar.hasMoreElements()) {
 			String name = (String) enumar.nextElement();
 			i++;
-			assertEquals("foo", name);
+			assertEquals(FOO, name);
 		}
 		assertEquals(i, 1);
 	}
 
 	public void testAddGetLogger_duplicateName() {
 		// add logger with duplicate name has no effect
-		Logger foo = new MockLogger("foo", null);
-		Logger foo2 = new MockLogger("foo", null);
+		Logger foo = new MockLogger(FOO, null);
+		Logger foo2 = new MockLogger(FOO, null);
 		assertTrue(mockManager.addLogger(foo));
-		assertSame(foo, mockManager.getLogger("foo"));
+		assertSame(foo, mockManager.getLogger(FOO));
 		assertFalse(mockManager.addLogger(foo2));
-		assertSame(foo, mockManager.getLogger("foo"));
+		assertSame(foo, mockManager.getLogger(FOO));
 		Enumeration<String> enumar = mockManager.getLoggerNames();
 		int i = 0;
 		while (enumar.hasMoreElements()) {
@@ -263,24 +265,24 @@
 	}
 
 	public void testAddGetLogger_nameWithSpace() {
-		Logger foo = new MockLogger("foo", null);
-		Logger fooBeforeSpace = new MockLogger("foo ", null);
-		Logger fooAfterSpace = new MockLogger(" foo", null);
-		Logger fooWithBothSpace = new MockLogger(" foo ", null);
+		Logger foo = new MockLogger(FOO, null);
+		Logger fooBeforeSpace = new MockLogger(FOO+" ", null);
+		Logger fooAfterSpace = new MockLogger(" "+FOO, null);
+		Logger fooWithBothSpace = new MockLogger(" "+FOO+" ", null);
 		assertTrue(mockManager.addLogger(foo));
 		assertTrue(mockManager.addLogger(fooBeforeSpace));
 		assertTrue(mockManager.addLogger(fooAfterSpace));
 		assertTrue(mockManager.addLogger(fooWithBothSpace));
 
-		assertSame(foo, mockManager.getLogger("foo"));
-		assertSame(fooBeforeSpace, mockManager.getLogger("foo "));
-		assertSame(fooAfterSpace, mockManager.getLogger(" foo"));
-		assertSame(fooWithBothSpace, mockManager.getLogger(" foo "));
+		assertSame(foo, mockManager.getLogger(FOO));
+		assertSame(fooBeforeSpace, mockManager.getLogger(FOO+" "));
+		assertSame(fooAfterSpace, mockManager.getLogger(" "+FOO));
+		assertSame(fooWithBothSpace, mockManager.getLogger(" "+FOO+" "));
 	}
 
-	public void testAddGetLogger_addRoot() {
-		Logger foo = new MockLogger("foo", null);
-		Logger fooChild = new MockLogger("foo.child", null);
+	public void testAddGetLogger_addRoot() throws IOException {
+		Logger foo = new MockLogger(FOO, null);
+		Logger fooChild = new MockLogger(FOO+".child", null);
 		Logger other = new MockLogger("other", null);
 		Logger root = new MockLogger("", null);
 		assertNull(foo.getParent());
@@ -321,7 +323,7 @@
 		}
 	}
 	
-	public void testDefaultLoggerProperties() {
+	public void testDefaultLoggerProperties() throws Exception{
 		// mock LogManager has no default logger
 		assertNull(mockManager.getLogger(""));
 		assertNull(mockManager.getLogger("global"));
@@ -334,9 +336,10 @@
 		assertSame(root, global.getParent());
 
 		// root properties
+        manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
 		assertNull(root.getFilter());
-		assertEquals(1, root.getHandlers().length);
-		assertEquals(root.getLevel(), Level.INFO);
+		assertEquals(2, root.getHandlers().length);
+		assertEquals(Level.FINE, root.getLevel());
 		assertEquals("", root.getName());
 		assertSame(root.getParent(), null);
 		assertNull(root.getResourceBundle());
@@ -416,13 +419,15 @@
 	}
 
 	public void testGetProperty() throws SecurityException, IOException {
-		// but non-mock manager DO read it from the very beginning
-		Logger root = manager.getLogger("");
-        //FIXME: move it to exec
-//		checkProperty(manager);
-		assertEquals(Level.INFO, root.getLevel());
-		assertEquals(1, root.getHandlers().length);
+//      //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);
@@ -432,7 +437,7 @@
 		checkPropertyNull(manager);
 		assertEquals(0, root.getHandlers().length);
 		assertEquals(Level.INFO, root.getLevel());
-		manager.readConfiguration();
+		manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
 	}
 
 	public void testReadConfiguration_null() throws SecurityException,
@@ -467,7 +472,7 @@
 		// assertEquals(m.getProperty("handlers"),
 		// "java.util.logging.ConsoleHandler");
 		assertEquals(m.getProperty("java.util.logging.FileHandler.count"), "5");
-		assertEquals(m.getProperty("foo.level"), "WARNING");
+		assertEquals(m.getProperty("LogManagerTestFoo.level"), "WARNING");
 		assertEquals(m.getProperty("java.util.logging.FileHandler.formatter"),
 				"java.util.logging.XMLFormatter");
 		assertEquals(m.getProperty("java.util.logging.ConsoleHandler.level"),
@@ -510,11 +515,11 @@
 		// mock LogManager
 		InputStream stream = EnvironmentHelper.PropertiesToInputStream(props);
 
-		Logger foo = new MockLogger("foo", null);
+		Logger foo = new MockLogger(FOO, null);
 		assertEquals(null, foo.getLevel());
 		assertTrue(mockManager.addLogger(foo));
 
-		Logger fo = new MockLogger("foo2", null);
+		Logger fo = new MockLogger("LogManagerTestFoo2", null);
 		fo.setLevel(Level.ALL);
 		assertTrue(mockManager.addLogger(fo));
 
@@ -549,7 +554,7 @@
 
 	public void testReadConfigurationInputStream_root() throws IOException {
 		InputStream stream = EnvironmentHelper.PropertiesToInputStream(props);
-		manager.readConfiguration();
+		manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
 
 		Logger logger = new MockLogger(
 				"testReadConfigurationInputStream_root.foo", null);
@@ -566,8 +571,8 @@
 		assertEquals(0, logger2.getHandlers().length);
 		assertSame(root, logger2.getParent());
 		// if (!hasConfigClass) {
-		assertEquals(Level.INFO, root.getLevel());
-		assertEquals(1, root.getHandlers().length);
+		assertEquals(Level.FINE, root.getLevel());
+		assertEquals(2, root.getHandlers().length);
 		// }
 
 		// after read stream
@@ -589,7 +594,7 @@
 
 		assertNull(listener1.getEvent());
 		assertNull(listener2.getEvent());
-		mockManager.readConfiguration();
+		mockManager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
 		// if (!hasConfigClass) {
 		assertNotNull(listener1.getEvent());
 		assertNotNull(listener2.getEvent());
@@ -620,7 +625,7 @@
 		assertNull(listener2.getEvent());
 
 		mockManager.removePropertyChangeListener(listener2);
-		mockManager.readConfiguration();
+		mockManager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
 		assertNull(listener1.getEvent());
 		assertNull(listener2.getEvent());
 	}
@@ -637,15 +642,16 @@
 
 	public void testReset() throws SecurityException, IOException {
 		// mock LogManager
-		mockManager.readConfiguration();
+		mockManager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
 		assertNotNull(mockManager.getProperty("handlers"));
-		Logger foo = new MockLogger("foo", null);
+		Logger foo = new MockLogger(FOO, null);
 		assertEquals(null, foo.getLevel());
+        assertEquals(0, foo.getHandlers().length);
 		foo.setLevel(Level.ALL);
 		foo.addHandler(new ConsoleHandler());
 		assertTrue(mockManager.addLogger(foo));
-		assertEquals(Level.ALL, foo.getLevel());
-		assertEquals(1, foo.getHandlers().length);
+		assertEquals(Level.WARNING, foo.getLevel());
+		assertEquals(2, foo.getHandlers().length);
 
 		// reset
 		mockManager.reset();



Mime
View raw message