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
18package libcore.java.util.logging;
19
20import java.io.ByteArrayInputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.IOException;
23import java.io.InputStream;
24import java.util.Properties;
25import java.util.logging.Filter;
26import java.util.logging.Handler;
27import java.util.logging.Level;
28import java.util.logging.LogManager;
29import java.util.logging.LogRecord;
30import java.util.logging.MemoryHandler;
31import junit.framework.TestCase;
32
33public class OldMemoryHandlerTest extends TestCase {
34
35    final static LogManager manager = LogManager.getLogManager();
36    final static Properties props = new Properties();
37    final static String baseClassName = OldMemoryHandlerTest.class.getName();
38    MemoryHandler handler;
39
40    @Override protected void setUp() throws Exception {
41        super.setUp();
42        manager.reset();
43        initProps();
44        manager.readConfiguration(propertiesToInputStream(props));
45        handler = new MemoryHandler();
46    }
47
48    @Override protected void tearDown() throws Exception {
49        super.tearDown();
50        manager.readConfiguration();
51        props.clear();
52    }
53
54    public static InputStream propertiesToInputStream(Properties p) throws IOException {
55        ByteArrayOutputStream bos = new ByteArrayOutputStream();
56        p.store(bos, "");
57        return new ByteArrayInputStream(bos.toByteArray());
58    }
59
60    private void initProps() {
61        props.put("java.util.logging.MemoryHandler.level", "FINE");
62        props.put("java.util.logging.MemoryHandler.filter", baseClassName + "$MockFilter");
63        props.put("java.util.logging.MemoryHandler.size", "2");
64        props.put("java.util.logging.MemoryHandler.push", "WARNING");
65        props.put("java.util.logging.MemoryHandler.target", baseClassName + "$MockHandler");
66        props.put("java.util.logging.MemoryHandler.formatter", baseClassName + "$MockFormatter");
67    }
68
69    public void testIsLoggable() {
70        assertTrue(handler.isLoggable(new LogRecord(Level.INFO, "1")));
71        assertTrue(handler.isLoggable(new LogRecord(Level.WARNING, "2")));
72        assertTrue(handler.isLoggable(new LogRecord(Level.SEVERE, "3")));
73    }
74
75    public void testMemoryHandler() throws IOException {
76        assertNotNull("Filter should not be null", handler.getFilter());
77        assertNotNull("Formatter should not be null", handler.getFormatter());
78        assertNull("character encoding should be null", handler.getEncoding());
79        assertNotNull("ErrorManager should not be null", handler.getErrorManager());
80        assertEquals("Level should be FINE", Level.FINE, handler.getLevel());
81        assertEquals("Level should be WARNING", Level.WARNING, handler.getPushLevel());
82    }
83
84    public static class MockFilter implements Filter {
85        public boolean isLoggable(LogRecord record) {
86            return !record.getMessage().equals("false");
87        }
88    }
89
90    public static class MockHandler extends Handler {
91        public void close() {}
92        public void flush() {}
93        public void publish(LogRecord record) {}
94    }
95}
96