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.io.UnsupportedEncodingException;
     21 import java.util.logging.Handler;
     22 import java.util.logging.Level;
     23 import java.util.logging.LogRecord;
     24 import java.util.logging.XMLFormatter;
     25 import junit.framework.TestCase;
     26 
     27 public final class OldXMLFormatterTest extends TestCase {
     28 
     29     XMLFormatter formatter = null;
     30     MockHandler handler = null;
     31     LogRecord lr = null;
     32 
     33     @Override protected void setUp() throws Exception {
     34         super.setUp();
     35         formatter = new XMLFormatter();
     36         handler = new MockHandler();
     37         lr = new LogRecord(Level.SEVERE, "pattern");
     38     }
     39 
     40     public void testXMLFormatter() throws SecurityException, UnsupportedEncodingException {
     41         handler.setEncoding("UTF-8");
     42 
     43         String result = formatter.getHead(handler);
     44         int headPos = result
     45                 .indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
     46         int dtdPos = result.indexOf("<!DOCTYPE log SYSTEM \"logger.dtd\">");
     47         int rootPos = result.indexOf("<log>");
     48         assertTrue("head string position should be more or equal zero",
     49                 headPos >= 0);
     50         assertTrue("dtd string position should be more head string position",
     51                 dtdPos > headPos);
     52         assertTrue("root string position should be more dtd string position",
     53                 rootPos > dtdPos);
     54 
     55         assertTrue("Tail string position should be more zero", formatter
     56                 .getTail(handler).indexOf("/log>") > 0);
     57     }
     58 
     59     public void testGetTail() {
     60         assertEquals("Tail string with null handler should be equal expected value",
     61                 "</log>", formatter.getTail(null).trim());
     62         assertEquals("Tail string should be equal expected value", "</log>",
     63                 formatter.getTail(handler).trim());
     64         handler.publish(lr);
     65         assertEquals("Tail string after publish() should be equal expected value",
     66                 "</log>", formatter.getTail(handler).trim());
     67     }
     68 
     69     public static class MockHandler extends Handler {
     70         public void close() {}
     71         public void flush() {}
     72         public void publish(LogRecord record) {}
     73     }
     74 }
     75