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 dalvik.annotation.AndroidOnly;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetNew;
24import dalvik.annotation.TestTargetClass;
25
26import junit.framework.TestCase;
27
28import java.io.UnsupportedEncodingException;
29import java.util.ResourceBundle;
30import java.util.logging.Handler;
31import java.util.logging.Level;
32import java.util.logging.LogRecord;
33import java.util.logging.XMLFormatter;
34
35@TestTargetClass(XMLFormatter.class)
36public class XMLFormatterTest extends TestCase {
37
38    XMLFormatter formatter = null;
39
40    MockHandler handler = null;
41
42    LogRecord lr = null;
43
44    protected void setUp() throws Exception {
45        super.setUp();
46        formatter = new XMLFormatter();
47        handler = new MockHandler();
48        lr = new LogRecord(Level.SEVERE, "pattern");
49    }
50
51    /*
52     * test for constructor public XMLFormatter()
53     *
54     */
55    @TestTargets({
56        @TestTargetNew(
57            level = TestLevel.COMPLETE,
58            notes = "",
59            method = "XMLFormatter",
60            args = {}
61        ),
62        @TestTargetNew(
63            level = TestLevel.PARTIAL_COMPLETE,
64            notes = "",
65            method = "getHead",
66            args = {java.util.logging.Handler.class}
67        ),
68        @TestTargetNew(
69            level = TestLevel.PARTIAL_COMPLETE,
70            notes = "",
71            method = "getTail",
72            args = {java.util.logging.Handler.class}
73        )
74    })
75    public void testXMLFormatter() throws SecurityException,
76            UnsupportedEncodingException {
77
78        handler.setEncoding("UTF-8");
79
80        String result = formatter.getHead(handler);
81        int headPos = result
82                .indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
83        int dtdPos = result.indexOf("<!DOCTYPE log SYSTEM \"logger.dtd\">");
84        int rootPos = result.indexOf("<log>");
85        assertTrue("head string position should be more or equal zero",
86                headPos >= 0);
87        assertTrue("dtd string position should be more head string position",
88                dtdPos > headPos);
89        assertTrue("root string position should be more dtd string position",
90                rootPos > dtdPos);
91
92        assertTrue("Tail string position should be more zero", formatter
93                .getTail(handler).indexOf("/log>") > 0);
94    }
95
96    @TestTargetNew(
97        level = TestLevel.PARTIAL_COMPLETE,
98        notes = "",
99        method = "format",
100        args = {java.util.logging.LogRecord.class}
101    )
102    public void testLocalFormat() {
103        // if set resource bundle, output will use localized message,
104        // but put the original message into the key element
105        // further more, if message pattern has no effect
106        ResourceBundle rb = ResourceBundle
107                .getBundle("bundles/java/util/logging/res");
108        lr.setResourceBundle(rb);
109        lr.setMessage("pattern");
110        String result = formatter.format(lr);
111        assertTrue(result.indexOf("<message>" + rb.getString("pattern")
112                + "</message>") > 0);
113        assertTrue(result.indexOf("<key>pattern</key>") > 0);
114
115        lr.setMessage("msg");
116        result = formatter.format(lr);
117        assertTrue(result.indexOf("<message>" + rb.getString("msg")
118                + "</message>") > 0);
119        assertTrue(result.indexOf("<key>msg</key>") > 0);
120
121        lr.setMessage("pattern {0, number}");
122        result = formatter.format(lr);
123        assertTrue(result.indexOf("<message>pattern {0, number}</message>") > 0);
124        assertTrue(result.indexOf("<key>") < 0);
125
126        // if message has no relevant localized message, use the original
127        lr.setMessage("bad key");
128        result = formatter.format(lr);
129        assertTrue(result.indexOf("<message>bad key</message>") > 0);
130        assertTrue(result.indexOf("<key>") < 0);
131    }
132
133    @TestTargetNew(
134        level = TestLevel.PARTIAL_COMPLETE,
135        notes = "",
136        method = "format",
137        args = {java.util.logging.LogRecord.class}
138    )
139    public void testFullFormat() {
140        lr.setSourceClassName("source class");
141        lr.setSourceMethodName("source method");
142        lr.setLoggerName("logger name");
143        lr.setMillis(0);
144        lr.setThrown(new Throwable("message"));
145        lr.setParameters(new Object[] { "100", "200" });
146        lr.setSequenceNumber(1);
147        ResourceBundle rb = ResourceBundle
148                .getBundle("bundles/java/util/logging/res");
149        lr.setResourceBundle(rb);
150        lr.setResourceBundleName("rbname");
151        String output = formatter.format(lr);
152        // System.out.println(output);
153        assertTrue(output.indexOf("<record>") >= 0);
154        assertTrue(output.indexOf("<date>") >= 0);
155        assertTrue(output.indexOf("<millis>0</millis>") >= 0);
156        assertTrue(output.indexOf("<sequence>") >= 0);
157        assertTrue(output.indexOf("<level>SEVERE</level>") >= 0);
158        assertTrue(output.indexOf("<thread>") >= 0);
159        assertTrue(output.indexOf("<message>" + rb.getString("pattern")
160                + "</message>") >= 0);
161        assertTrue(output.indexOf("<logger>logger name</logger>") > 0);
162        assertTrue(output.indexOf("<class>source class</class>") > 0);
163        assertTrue(output.indexOf("<method>source method</method>") > 0);
164        assertTrue(output.indexOf("<catalog>rbname</catalog>") > 0);
165        assertTrue(output.indexOf("<param>100</param>") > 0);
166        assertTrue(output.indexOf("<param>200</param>") > 0);
167        assertTrue(output.indexOf("<exception>") > 0);
168        assertTrue(output.indexOf("<key>pattern</key>") > 0);
169    }
170
171    @TestTargetNew(
172        level = TestLevel.PARTIAL_COMPLETE,
173        notes = "",
174        method = "format",
175        args = {java.util.logging.LogRecord.class}
176    )
177    public void testFormat() {
178        String output = formatter.format(lr);
179        // System.out.println(output);
180        assertTrue(output.indexOf("<record>") >= 0);
181        assertTrue(output.indexOf("<date>") >= 0);
182        assertTrue(output.indexOf("<millis>") >= 0);
183        assertTrue(output.indexOf("<sequence>") >= 0);
184        assertTrue(output.indexOf("<level>SEVERE</level>") >= 0);
185        assertTrue(output.indexOf("<thread>") >= 0);
186        assertTrue(output.indexOf("<message>pattern</message>") >= 0);
187        assertTrue(output.indexOf("<logger>") < 0);
188        assertTrue(output.indexOf("<class>") < 0);
189        assertTrue(output.indexOf("<method>") < 0);
190        assertTrue(output.indexOf("<catalog>") < 0);
191        assertTrue(output.indexOf("<param>") < 0);
192        assertTrue(output.indexOf("<exception>") < 0);
193        assertTrue(output.indexOf("<key>") < 0);
194    }
195
196    @TestTargetNew(
197        level = TestLevel.PARTIAL_COMPLETE,
198        notes = "",
199        method = "getHead",
200        args = {java.util.logging.Handler.class}
201    )
202    public void testGetHead() throws SecurityException,
203            UnsupportedEncodingException {
204        String result = formatter.getHead(handler);
205        assertNull(handler.getEncoding());
206        // TODO: where do we get the default encoding from?
207        // assertTrue(result.indexOf(defaultEncoding)>0);
208
209        handler.setEncoding("ISO-8859-1");
210        String head = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>";
211        String dtd = "<!DOCTYPE log SYSTEM \"logger.dtd\">";
212        String rootELement = "<log>";
213        result = formatter.getHead(handler);
214        int headPos = result.indexOf(head);
215        int dtdPos = result.indexOf(dtd);
216        int rootPos = result.indexOf(rootELement);
217        assertTrue(headPos >= 0);
218        assertTrue(dtdPos > headPos);
219        assertTrue(rootPos > dtdPos);
220
221        handler.setEncoding(null);
222        result = formatter.getHead(handler);
223        assertNull(handler.getEncoding());
224
225        // make sure no NPE is thrown
226        formatter.getHead(null);
227
228    }
229
230    /*
231     * test for method public String getTail(Handler h)
232     */
233    @TestTargetNew(
234        level = TestLevel.PARTIAL_COMPLETE,
235        notes = "",
236        method = "getTail",
237        args = {java.util.logging.Handler.class}
238    )
239    public void testGetTail() {
240        assertEquals(
241                "Tail string with null handler should be equal expected value",
242                "</log>", formatter.getTail(null).trim());
243        assertEquals("Tail string should be equal expected value", "</log>",
244                formatter.getTail(handler).trim());
245        handler.publish(lr);
246        assertEquals(
247                "Tail string after publish() should be equal expected value",
248                "</log>", formatter.getTail(handler).trim());
249    }
250
251    @TestTargets({
252        @TestTargetNew(
253            level = TestLevel.PARTIAL_COMPLETE,
254            notes = "",
255            method = "format",
256            args = {java.util.logging.LogRecord.class}
257        ),
258        @TestTargetNew(
259            level = TestLevel.COMPLETE,
260            notes = "",
261            method = "getTail",
262            args = {java.util.logging.Handler.class}
263        ),
264        @TestTargetNew(
265            level = TestLevel.COMPLETE,
266            notes = "",
267            method = "XMLFormatter",
268            args = {}
269        )
270    })
271    @AndroidOnly("This test fails on RI. Output doesn't contain " +
272            "<message/>.")
273    public void testInvalidParameter() {
274        formatter.getTail(null);
275        try {
276            formatter.format(null);
277            fail();
278        } catch (NullPointerException e) {
279        }
280
281        formatter = new XMLFormatter();
282        lr = new LogRecord(Level.SEVERE, null);
283        String output = formatter.format(lr);
284        assertTrue(output.indexOf("<message/>") != -1);
285    }
286
287    public static class MockHandler extends Handler {
288        public void close() {
289        }
290
291        public void flush() {
292        }
293
294        public void publish(LogRecord record) {
295        }
296
297    }
298}
299