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