summaryrefslogtreecommitdiff
path: root/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/SimpleFormatterTest.java
blob: ee35d2d7266b38d81e24d11eb9c3af2a00989e5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.harmony.logging.tests.java.util.logging;

import java.util.Calendar;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

import junit.framework.TestCase;

/**
 *
 */
public class SimpleFormatterTest extends TestCase {

    SimpleFormatter sf;

    LogRecord lr;

    private static String MSG = "test msg. pls. ignore it\nadaadasfdasfd\nadfafdadfsa";

    /*
      * @see TestCase#setUp()
      */
    protected void setUp() throws Exception {
        super.setUp();
        sf = new SimpleFormatter();
        lr = new LogRecord(Level.FINE, MSG);
    }

    public void testFormatNull() {
        try {
            sf.format(null);
            fail("should throw nullpointer exception");
        } catch (NullPointerException e) {
        }
        sf.format(new LogRecord(Level.SEVERE, null));
    }

    public void testLocalizedFormat() {
        // if bundle set, should use localized message
        ResourceBundle rb = ResourceBundle
                .getBundle("bundles/java/util/logging/res");
        lr.setResourceBundle(rb);
        lr.setMessage("msg");
        String localeMsg = rb.getString("msg");
        String str = sf.format(lr);
        assertTrue(str.indexOf(localeMsg) > 0);

        // if bundle not set but bundle name set, should use original message
        lr.setResourceBundle(null);
        lr.setResourceBundleName("bundles/java/util/logging/res");
        lr.setMessage("msg");
        str = sf.format(lr);
        localeMsg = rb.getString("msg");
        assertTrue(str.indexOf(localeMsg) < 0);
    }

    public void testFormat() {
        String str = sf.format(lr);
        Throwable t;

        lr.setMessage(MSG + " {0,number}");
        lr.setLoggerName("logger");
        lr.setResourceBundleName("rb name");
        lr.setSourceClassName("class");
        lr.setSourceMethodName("method");
        lr.setParameters(new Object[] { new Integer(100), new Object() });
        lr.setThreadID(1000);
        lr.setThrown(t = new Exception("exception") {
            private static final long serialVersionUID = 1L;

            public String getLocalizedMessage() {
                return "locale";
            }
        });
        lr.setSequenceNumber(12321312);
        lr.setMillis(0);
        str = sf.format(lr);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(12321312);
        assertTrue(str.indexOf(String.valueOf(cal.get(Calendar.YEAR))) >= 0);
        assertTrue(str.indexOf("class") > 0);
        assertTrue(str.indexOf("method") > 0);
        assertTrue(str.indexOf("100") > 0);
        assertTrue(str.indexOf(t.toString()) > 0);
        assertTrue(str.indexOf(Level.FINE.getLocalizedName()) > 0);
    }

    public void testGetHead() {
        assertEquals("", sf.getHead(null));
    }

    public void testGetTail() {
        assertEquals("", sf.getTail(null));
    }
}