Home | History | Annotate | Download | only in logging
      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 
     18 package libcore.java.util.logging;
     19 
     20 import java.util.logging.Formatter;
     21 import java.util.logging.Handler;
     22 import java.util.logging.Level;
     23 import java.util.logging.LogRecord;
     24 import java.util.logging.StreamHandler;
     25 import junit.framework.TestCase;
     26 
     27 public class OldFormatterTest extends TestCase {
     28     static String MSG = "msg, pls. ignore it";
     29 
     30     Formatter f = new MockFormatter();
     31     LogRecord r = new LogRecord(Level.FINE, MSG);
     32     Handler h;
     33 
     34     @Override protected void setUp() throws Exception {
     35         super.setUp();
     36         h = new StreamHandler();
     37     }
     38 
     39     public void testFormatter() {
     40         assertEquals("head string is not empty", "", f.getHead(null));
     41         assertEquals("tail string is not empty", "", f.getTail(null));
     42 
     43     }
     44 
     45     public void testGetHead() {
     46         assertEquals("head string is not empty", "", f.getHead(null));
     47         assertEquals("head string is not empty", "", f.getHead(h));
     48         h.publish(r);
     49         assertEquals("head string is not empty", "", f.getHead(h));
     50     }
     51 
     52     public void testGetTail() {
     53         assertEquals("tail string is not empty", "", f.getTail(null));
     54         assertEquals("tail string is not empty", "", f.getTail(h));
     55         h.publish(r);
     56         assertEquals("tail string is not empty", "", f.getTail(h));
     57     }
     58 
     59     public void testFormatMessage() {
     60         // The RI fails in this test because it uses a MessageFormat to format
     61         // the message even though it doesn't contain "{0". The spec says that
     62         // this would indicate that a MessageFormat should be used and else no
     63         // formatting should be done.
     64         String pattern = "pattern without 0 {1, number}";
     65         r.setMessage(pattern);
     66         assertEquals(pattern, f.formatMessage(r));
     67     }
     68 
     69     public static class MockFormatter extends Formatter {
     70         public String format(LogRecord arg0) {
     71             return "format";
     72         }
     73     }
     74 }
     75