Home | History | Annotate | Download | only in logging
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package libcore.java.util.logging;
     19 
     20 
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.util.Enumeration;
     24 import java.util.Properties;
     25 import java.util.logging.Handler;
     26 import java.util.logging.LogManager;
     27 import java.util.logging.LogRecord;
     28 import java.util.logging.Logger;
     29 import junit.framework.TestCase;
     30 
     31 public class OldLogManagerTest extends TestCase {
     32 
     33     private static final String FOO = "LogManagerTestFoo";
     34 
     35     LogManager mockManager;
     36 
     37     LogManager manager = LogManager.getLogManager();
     38 
     39     Properties props;
     40 
     41     private static String className = OldLogManagerTest.class.getName();
     42 
     43     static Handler handler = null;
     44 
     45     @Override protected void setUp() throws Exception {
     46         super.setUp();
     47         mockManager = new MockLogManager();
     48         handler = new MockHandler();
     49         props = new Properties();
     50         props.put("handlers", className + "$MockHandler " + className + "$MockHandler");
     51         props.put("java.util.logging.FileHandler.pattern", "%h/java%u.log");
     52         props.put("java.util.logging.FileHandler.limit", "50000");
     53         props.put("java.util.logging.FileHandler.count", "5");
     54         props.put("java.util.logging.FileHandler.formatter", "java.util.logging.XMLFormatter");
     55         props.put(".level", "FINE");
     56         props.put("java.util.logging.ConsoleHandler.level", "OFF");
     57         props.put("java.util.logging.ConsoleHandler.formatter","java.util.logging.SimpleFormatter");
     58         props.put("LogManagerTestFoo.handlers", "java.util.logging.ConsoleHandler");
     59         props.put("LogManagerTestFoo.level", "WARNING");
     60     }
     61 
     62 
     63 
     64     /*
     65      * @see TestCase#tearDown()
     66      */
     67     @Override
     68     protected void tearDown() throws Exception {
     69         super.tearDown();
     70         handler = null;
     71     }
     72 
     73     public void testLogManager() {
     74        class TestLogManager extends LogManager {
     75            public TestLogManager() {
     76                super();
     77            }
     78        }
     79        TestLogManager tlm = new TestLogManager();
     80        assertNotNull(tlm.toString());
     81     }
     82 
     83     /*
     84      * test for method public Logger getLogger(String name)
     85      * test covers following use cases:
     86      * case 1: test default and valid value
     87      * case 2: test throw NullPointerException
     88      * case 3: test bad name
     89      * case 4: check correct tested value
     90      */
     91 
     92     public void testGetLogger() throws Exception {
     93 
     94         // case 1: test default and valid value
     95         Logger log = new MockLogger(FOO, null);
     96         Logger foo = mockManager.getLogger(FOO);
     97         assertNull("Logger should be null", foo);
     98         assertTrue("logger wasn't registered successfully", mockManager.addLogger(log));
     99         foo = mockManager.getLogger(FOO);
    100         assertSame("two loggers not refer to the same object", foo, log);
    101         assertNull("logger foo should not haven parent", foo.getParent());
    102 
    103         // case 2: test throw NullPointerException
    104         try {
    105             mockManager.getLogger(null);
    106             fail("get null should throw NullPointerException");
    107         } catch (NullPointerException e) {
    108         }
    109 
    110         // case 3: test bad name
    111         assertNull("LogManager should not have logger with unforeseen name", mockManager
    112                 .getLogger("bad name"));
    113 
    114         // case 4: check correct tested value
    115         Enumeration<String> enumar = mockManager.getLoggerNames();
    116         int i = 0;
    117         while (enumar.hasMoreElements()) {
    118             String name = enumar.nextElement();
    119             i++;
    120             assertEquals("name logger should be equal to foreseen name", FOO, name);
    121         }
    122         assertEquals("LogManager should contain one element", 1, i);
    123     }
    124 
    125     /*
    126      * test for method public Logger getLogger(String name)
    127      */
    128     public void testGetLogger_duplicateName() throws Exception {
    129         // test duplicate name
    130         // add logger with duplicate name has no effect
    131         mockManager.reset();
    132         Logger foo2 = new MockLogger(FOO, null);
    133         Logger foo3 = new MockLogger(FOO, null);
    134         mockManager.addLogger(foo2);
    135         assertSame(foo2, mockManager.getLogger(FOO));
    136         mockManager.addLogger(foo3);
    137         assertSame(foo2, mockManager.getLogger(FOO));
    138 
    139         Enumeration<String> enumar2 = mockManager.getLoggerNames();
    140         int i = 0;
    141         while (enumar2.hasMoreElements()) {
    142             enumar2.nextElement();
    143             i++;
    144         }
    145         assertEquals(1, i);
    146     }
    147 
    148     /*
    149      * test for method public Logger getLogger(String name)
    150      */
    151     public void testGetLogger_hierarchy() throws Exception {
    152         // test hierarchy
    153         Logger foo = new MockLogger("testGetLogger_hierachy.foo", null);
    154         // but for non-mock LogManager, foo's parent should be root
    155         assertTrue(manager.addLogger(foo));
    156         assertSame(manager.getLogger(""), manager.getLogger("testGetLogger_hierachy.foo")
    157                 .getParent());
    158     }
    159 
    160     /*
    161      * test for method public Logger getLogger(String name)
    162      */
    163     public void testGetLogger_nameSpace() throws Exception {
    164         // test name with space
    165         Logger foo = new MockLogger(FOO, null);
    166         Logger fooBeforeSpace = new MockLogger(FOO + " ", null);
    167         Logger fooAfterSpace = new MockLogger(" " + FOO, null);
    168         Logger fooWithBothSpace = new MockLogger(" " + FOO + " ", null);
    169         assertTrue(mockManager.addLogger(foo));
    170         assertTrue(mockManager.addLogger(fooBeforeSpace));
    171         assertTrue(mockManager.addLogger(fooAfterSpace));
    172         assertTrue(mockManager.addLogger(fooWithBothSpace));
    173 
    174         assertSame(foo, mockManager.getLogger(FOO));
    175         assertSame(fooBeforeSpace, mockManager.getLogger(FOO + " "));
    176         assertSame(fooAfterSpace, mockManager.getLogger(" " + FOO));
    177         assertSame(fooWithBothSpace, mockManager.getLogger(" " + FOO + " "));
    178     }
    179 
    180     /*
    181      * test for method public void checkAccess() throws SecurityException
    182      */
    183     public void testCheckAccess() {
    184         try {
    185             manager.checkAccess();
    186         } catch (SecurityException e) {
    187             fail("securityException should not be thrown");
    188         }
    189     }
    190 
    191     public void testReadConfiguration() throws SecurityException,
    192             IOException {
    193 
    194         MockConfigLogManager lm = new MockConfigLogManager();
    195         assertFalse(lm.isCalled);
    196 
    197         lm.readConfiguration();
    198         assertTrue(lm.isCalled);
    199     }
    200 
    201     public void testReadConfigurationInputStream_IOException_1parm() throws SecurityException {
    202         try {
    203             mockManager.readConfiguration(new MockInputStream());
    204             fail("should throw IOException");
    205         } catch (IOException expected) {
    206         }
    207     }
    208 
    209     public static class MockInputStream extends InputStream {
    210         @Override public int read() throws IOException {
    211             throw new IOException();
    212         }
    213     }
    214 
    215     public static class MockLogger extends Logger {
    216         public MockLogger(String name, String rbName) {
    217             super(name, rbName);
    218         }
    219     }
    220 
    221     public static class MockLogManager extends LogManager {}
    222 
    223     public static class MockConfigLogManager extends LogManager {
    224         public boolean isCalled = false;
    225 
    226         public void readConfiguration(InputStream ins) throws IOException {
    227             isCalled = true;
    228             super.readConfiguration(ins);
    229         }
    230     }
    231 
    232     public static class MockHandler extends Handler {
    233         static int number = 0;
    234 
    235         public MockHandler() {
    236             addNumber();
    237         }
    238 
    239         private synchronized void addNumber() {
    240             number++;
    241         }
    242 
    243         public void close() {
    244             minusNumber();
    245         }
    246 
    247         private synchronized void minusNumber() {
    248             number--;
    249         }
    250 
    251         public void flush() {}
    252 
    253         public void publish(LogRecord record) {}
    254     }
    255 }
    256