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 org.apache.harmony.logging.tests.java.util.logging;
19
20import java.io.IOException;
21import java.io.OutputStream;
22import java.io.UnsupportedEncodingException;
23import java.security.Permission;
24import java.util.Properties;
25import java.util.logging.ErrorManager;
26import java.util.logging.Filter;
27import java.util.logging.Formatter;
28import java.util.logging.Handler;
29import java.util.logging.Level;
30import java.util.logging.LogManager;
31import java.util.logging.LogRecord;
32import java.util.logging.LoggingPermission;
33import java.util.logging.SimpleFormatter;
34
35import junit.framework.TestCase;
36import org.apache.harmony.logging.tests.java.util.logging.util.EnvironmentHelper;
37import tests.util.CallVerificationStack;
38
39/**
40 * Test suite for the class java.util.logging.Handler.
41 */
42public class HandlerTest extends TestCase {
43    private static String className = HandlerTest.class.getName();
44
45    /*
46      * @see TestCase#setUp()
47      */
48    protected void setUp() throws Exception {
49        super.setUp();
50    }
51
52    /*
53      * @see TestCase#tearDown()
54      */
55    protected void tearDown() throws Exception {
56        super.tearDown();
57        CallVerificationStack.getInstance().clear();
58    }
59
60    /**
61     * Constructor for HandlerTest.
62     *
63     * @param arg0
64     */
65    public HandlerTest(String arg0) {
66        super(arg0);
67    }
68
69    /*
70      * Test the constructor.
71      */
72    public void testConstructor() {
73        MockHandler h = new MockHandler();
74        assertSame(h.getLevel(), Level.ALL);
75        assertNull(h.getFormatter());
76        assertNull(h.getFilter());
77        assertNull(h.getEncoding());
78        assertTrue(h.getErrorManager() instanceof ErrorManager);
79    }
80
81    /*
82      * Test the constructor, with properties set
83      */
84    public void testConstructor_Properties() throws Exception {
85        Properties p = new Properties();
86        p.put("java.util.logging.MockHandler.level", "FINE");
87        p
88                .put("java.util.logging.MockHandler.filter", className
89                        + "$MockFilter");
90        p.put("java.util.logging.Handler.formatter", className
91                + "$MockFormatter");
92        p.put("java.util.logging.MockHandler.encoding", "utf-8");
93        LogManager.getLogManager().readConfiguration(
94                EnvironmentHelper.PropertiesToInputStream(p));
95
96        assertEquals(LogManager.getLogManager().getProperty(
97                "java.util.logging.MockHandler.level"), "FINE");
98        assertEquals(LogManager.getLogManager().getProperty(
99                "java.util.logging.MockHandler.encoding"), "utf-8");
100        MockHandler h = new MockHandler();
101        assertSame(h.getLevel(), Level.ALL);
102        assertNull(h.getFormatter());
103        assertNull(h.getFilter());
104        assertNull(h.getEncoding());
105        assertTrue(h.getErrorManager() instanceof ErrorManager);
106        LogManager.getLogManager().reset();
107    }
108
109    /*
110      * Abstract method, no test needed.
111      */
112    public void testClose() {
113        MockHandler h = new MockHandler();
114        h.close();
115    }
116
117    /*
118      * Abstract method, no test needed.
119      */
120    public void testFlush() {
121        MockHandler h = new MockHandler();
122        h.flush();
123    }
124
125    /*
126      * Abstract method, no test needed.
127      */
128    public void testPublish() {
129        MockHandler h = new MockHandler();
130        h.publish(null);
131    }
132
133    /*
134      * Test getEncoding & setEncoding methods with supported encoding.
135      */
136    public void testGetSetEncoding_Normal() throws Exception {
137        MockHandler h = new MockHandler();
138        h.setEncoding("iso-8859-1");
139        assertEquals("iso-8859-1", h.getEncoding());
140    }
141
142    /*
143      * Test getEncoding & setEncoding methods with null.
144      */
145    public void testGetSetEncoding_Null() throws Exception {
146        MockHandler h = new MockHandler();
147        h.setEncoding(null);
148        assertNull(h.getEncoding());
149    }
150
151    /*
152      * Test getEncoding & setEncoding methods with unsupported encoding.
153      */
154    public void testGetSetEncoding_Unsupported() {
155        MockHandler h = new MockHandler();
156        try {
157            h.setEncoding("impossible");
158            fail("Should throw UnsupportedEncodingException!");
159        } catch (UnsupportedEncodingException e) {
160        }
161        assertNull(h.getEncoding());
162    }
163
164    /*
165      * Test getErrorManager & setErrorManager methods with non-null value.
166      */
167    public void testGetSetErrorManager_Normal() throws Exception {
168        MockHandler h = new MockHandler();
169        ErrorManager man = new ErrorManager();
170        h.setErrorManager(man);
171        assertSame(man, h.getErrorManager());
172    }
173
174    /*
175      * Test getErrorManager & setErrorManager methods with null.
176      */
177    public void testGetSetErrorManager_Null() throws Exception {
178        MockHandler h = new MockHandler();
179        // test set null
180        try {
181            h.setErrorManager(null);
182            fail("Should throw NullPointerException!");
183        } catch (NullPointerException e) {
184        }
185
186        // test reset null
187        try {
188            h.setErrorManager(new ErrorManager());
189            h.setErrorManager(null);
190            fail("Should throw NullPointerException!");
191        } catch (NullPointerException e) {
192        }
193    }
194
195    /*
196      * Test getFilter & setFilter methods with non-null value.
197      */
198    public void testGetSetFilter_Normal() throws Exception {
199        MockHandler h = new MockHandler();
200        Filter f = new MockFilter();
201        h.setFilter(f);
202        assertSame(f, h.getFilter());
203    }
204
205    /*
206      * Test getFilter & setFilter methods with null.
207      */
208    public void testGetSetFilter_Null() throws Exception {
209        MockHandler h = new MockHandler();
210        // test set null
211        h.setFilter(null);
212
213        // test reset null
214        h.setFilter(new MockFilter());
215        h.setFilter(null);
216    }
217
218    /*
219      * Test getFormatter & setFormatter methods with non-null value.
220      */
221    public void testGetSetFormatter_Normal() throws Exception {
222        MockHandler h = new MockHandler();
223        Formatter f = new SimpleFormatter();
224        h.setFormatter(f);
225        assertSame(f, h.getFormatter());
226    }
227
228    /*
229      * Test getFormatter & setFormatter methods with null.
230      */
231    public void testGetSetFormatter_Null() throws Exception {
232        MockHandler h = new MockHandler();
233        // test set null
234        try {
235            h.setFormatter(null);
236            fail("Should throw NullPointerException!");
237        } catch (NullPointerException e) {
238        }
239
240        // test reset null
241        try {
242            h.setFormatter(new SimpleFormatter());
243            h.setFormatter(null);
244            fail("Should throw NullPointerException!");
245        } catch (NullPointerException e) {
246        }
247    }
248
249    /*
250      * Test getLevel & setLevel methods with non-null value.
251      */
252    public void testGetSetLevel_Normal() throws Exception {
253        MockHandler h = new MockHandler();
254        Level f = Level.CONFIG;
255        h.setLevel(f);
256        assertSame(f, h.getLevel());
257    }
258
259    /*
260      * Test getLevel & setLevel methods with null.
261      */
262    public void testGetSetLevel_Null() throws Exception {
263        MockHandler h = new MockHandler();
264        // test set null
265        try {
266            h.setLevel(null);
267            fail("Should throw NullPointerException!");
268        } catch (NullPointerException e) {
269        }
270
271        // test reset null
272        try {
273            h.setLevel(Level.CONFIG);
274            h.setLevel(null);
275            fail("Should throw NullPointerException!");
276        } catch (NullPointerException e) {
277        }
278    }
279
280    /*
281      * Use no filter
282      */
283    public void testIsLoggable_NoFilter() {
284        MockHandler h = new MockHandler();
285        LogRecord r = new LogRecord(Level.CONFIG, null);
286        assertTrue(h.isLoggable(r));
287
288        h.setLevel(Level.CONFIG);
289        assertTrue(h.isLoggable(r));
290
291        h.setLevel(Level.SEVERE);
292        assertFalse(h.isLoggable(r));
293
294        r.setLevel(Level.OFF);
295        h.setLevel(Level.OFF);
296        assertFalse(h.isLoggable(r));
297    }
298
299    /*
300      * Use a filter
301      */
302    public void testIsLoggable_WithFilter() {
303        MockHandler h = new MockHandler();
304        LogRecord r = new LogRecord(Level.CONFIG, null);
305        h.setFilter(new MockFilter());
306        assertFalse(h.isLoggable(r));
307
308        h.setLevel(Level.CONFIG);
309        assertFalse(h.isLoggable(r));
310        assertSame(r, CallVerificationStack.getInstance().pop());
311
312        h.setLevel(Level.SEVERE);
313        assertFalse(h.isLoggable(r));
314        assertSame(r, CallVerificationStack.getInstance().pop());
315    }
316
317    /**
318     * @tests java.util.logging.Handler#isLoggable(LogRecord)
319     */
320    public void testIsLoggable_Null() {
321        MockHandler h = new MockHandler();
322        try {
323            h.isLoggable(null);
324            fail("should throw NullPointerException");
325        } catch (NullPointerException e) {
326            // expected
327        }
328    }
329
330    /*
331      * Used to enable the testing of Handler because Handler is an abstract
332      * class.
333      */
334    public static class MockHandler extends Handler {
335
336        public void close() {
337        }
338
339        public void flush() {
340        }
341
342        public void publish(LogRecord record) {
343        }
344
345        public void reportError(String msg, Exception ex, int code) {
346            super.reportError(msg, ex, code);
347        }
348    }
349
350    /*
351      * A mock filter, always return false.
352      */
353    public static class MockFilter implements Filter {
354
355        public boolean isLoggable(LogRecord record) {
356            CallVerificationStack.getInstance().push(record);
357            return false;
358        }
359    }
360
361    /*
362      * A mock error manager, used to validate the expected method is called with
363      * the expected parameters.
364      */
365    public static class MockErrorManager extends ErrorManager {
366
367        public void error(String msg, Exception ex, int errorCode) {
368            CallVerificationStack.getInstance().push(msg);
369            CallVerificationStack.getInstance().push(ex);
370            CallVerificationStack.getInstance().push(errorCode);
371        }
372    }
373
374    public static class NullOutputStream extends OutputStream {
375        @Override
376        public void write(int arg0) throws IOException {
377        }
378    }
379
380}
381