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.text.MessageFormat;
21import java.util.Locale;
22import java.util.ResourceBundle;
23import java.util.logging.Formatter;
24import java.util.logging.Level;
25import java.util.logging.LogRecord;
26
27import junit.framework.TestCase;
28
29public class FormatterTest extends TestCase {
30	Formatter f;
31
32	LogRecord r;
33
34	static String MSG = "msg, pls. ignore it";
35
36	/*
37	 * @see TestCase#setUp()
38	 */
39	protected void setUp() throws Exception {
40		super.setUp();
41		f = new MockFormatter();
42		r = new LogRecord(Level.FINE, MSG);
43	}
44
45	public void testFormat() {
46		assertEquals("format", f.format(r));
47	}
48
49	public void testGetHead() {
50		assertEquals("", f.getHead(null));
51	}
52
53	public void testGetTail() {
54		assertEquals("", f.getTail(null));
55	}
56
57	public void testFormatMessage() {
58		assertEquals(MSG, f.formatMessage(r));
59
60		String pattern = "test formatter {0, number}";
61		r.setMessage(pattern);
62		assertEquals(pattern, f.formatMessage(r));
63
64		Object[] oa = new Object[0];
65		r.setParameters(oa);
66		assertEquals(pattern, f.formatMessage(r));
67
68		oa = new Object[] { new Integer(100), new Float(1.1) };
69		r.setParameters(oa);
70		assertEquals(MessageFormat.format(pattern, oa), f.formatMessage(r));
71
72		r.setMessage(MSG);
73		assertEquals(MSG, f.formatMessage(r));
74
75		pattern = "wrong pattern {0, asdfasfd}";
76		r.setMessage(pattern);
77		assertEquals(pattern, f.formatMessage(r));
78
79		pattern = "pattern without 0 {1, number}";
80		r.setMessage(pattern);
81		assertEquals(pattern, f.formatMessage(r));
82
83		pattern = null;
84		r.setMessage(pattern);
85		assertNull(f.formatMessage(r));
86	}
87
88	public void testLocalizedFormatMessage() {
89		// normal case
90		r.setMessage("msg");
91		ResourceBundle rb = ResourceBundle
92				.getBundle("bundles/java/util/logging/res");
93		r.setResourceBundle(rb);
94		assertEquals(rb.getString("msg"), f.formatMessage(r));
95
96		// local message is a pattern
97		r.setMessage("pattern");
98		Object[] oa = new Object[] { new Integer(3) };
99		r.setParameters(oa);
100		assertEquals(MessageFormat.format(rb.getString("pattern"), oa), f
101				.formatMessage(r));
102
103		// key is a pattern, but local message is not
104		r.setMessage("pattern{0,number}");
105		oa = new Object[] { new Integer(3) };
106		r.setParameters(oa);
107		assertEquals(rb.getString("pattern{0,number}"), f.formatMessage(r));
108
109		// another bundle
110		rb = ResourceBundle.getBundle("bundles/java/util/logging/res",
111				Locale.US);
112		r.setMessage("msg");
113		r.setResourceBundle(rb);
114		assertEquals(rb.getString("msg"), f.formatMessage(r));
115
116		// cannot find local message in bundle
117		r.setMessage("msg without locale");
118		assertEquals("msg without locale", f.formatMessage(r));
119
120		// set bundle name but not bundle
121		r.setResourceBundle(null);
122		r.setResourceBundleName("bundles/java/util/logging/res");
123		r.setMessage("msg");
124		assertEquals("msg", f.formatMessage(r));
125	}
126
127	public static class MockFormatter extends Formatter {
128
129		public String format(LogRecord arg0) {
130			return "format";
131		}
132
133	}
134
135}
136