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.util.Calendar; 21import java.util.ResourceBundle; 22import java.util.logging.Level; 23import java.util.logging.LogRecord; 24import java.util.logging.SimpleFormatter; 25 26import junit.framework.TestCase; 27 28/** 29 * 30 */ 31public class SimpleFormatterTest extends TestCase { 32 33 SimpleFormatter sf; 34 35 LogRecord lr; 36 37 private static String MSG = "test msg. pls. ignore it\nadaadasfdasfd\nadfafdadfsa"; 38 39 /* 40 * @see TestCase#setUp() 41 */ 42 protected void setUp() throws Exception { 43 super.setUp(); 44 sf = new SimpleFormatter(); 45 lr = new LogRecord(Level.FINE, MSG); 46 } 47 48 public void testFormatNull() { 49 try { 50 sf.format(null); 51 fail("should throw nullpointer exception"); 52 } catch (NullPointerException e) { 53 } 54 sf.format(new LogRecord(Level.SEVERE, null)); 55 } 56 57 public void testLocalizedFormat() { 58 // if bundle set, should use localized message 59 ResourceBundle rb = ResourceBundle 60 .getBundle("bundles/com/android/java/util/logging/res"); 61 lr.setResourceBundle(rb); 62 lr.setMessage("msg"); 63 String localeMsg = rb.getString("msg"); 64 String str = sf.format(lr); 65 assertTrue(str.indexOf(localeMsg) > 0); 66 67 // if bundle not set but bundle name set, should use original message 68 lr.setResourceBundle(null); 69 lr.setResourceBundleName("bundles/com/android/java/util/logging/res"); 70 lr.setMessage("msg"); 71 str = sf.format(lr); 72 localeMsg = rb.getString("msg"); 73 assertTrue(str.indexOf(localeMsg) < 0); 74 } 75 76 public void testFormat() { 77 String str = sf.format(lr); 78 Throwable t; 79 80 lr.setMessage(MSG + " {0,number}"); 81 lr.setLoggerName("logger"); 82 lr.setResourceBundleName("rb name"); 83 lr.setSourceClassName("class"); 84 lr.setSourceMethodName("method"); 85 lr.setParameters(new Object[] { new Integer(100), new Object() }); 86 lr.setThreadID(1000); 87 lr.setThrown(t = new Exception("exception") { 88 private static final long serialVersionUID = 1L; 89 90 public String getLocalizedMessage() { 91 return "locale"; 92 } 93 }); 94 lr.setSequenceNumber(12321312); 95 lr.setMillis(0); 96 str = sf.format(lr); 97 Calendar cal = Calendar.getInstance(); 98 cal.setTimeInMillis(12321312); 99 assertTrue(str.indexOf(String.valueOf(cal.get(Calendar.YEAR))) >= 0); 100 assertTrue(str.indexOf("class") > 0); 101 assertTrue(str.indexOf("method") > 0); 102 assertTrue(str.indexOf("100") > 0); 103 assertTrue(str.indexOf(t.toString()) > 0); 104 assertTrue(str.indexOf(Level.FINE.getLocalizedName()) > 0); 105 } 106 107 public void testGetHead() { 108 assertEquals("", sf.getHead(null)); 109 } 110 111 public void testGetTail() { 112 assertEquals("", sf.getTail(null)); 113 } 114} 115