View Javadoc

    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  package org.apache.log4j.xml;
    18
    19  import junit.framework.TestCase;
    20
    21  import java.io.FileNotFoundException;
    22  import java.io.InputStream;
    23  import java.io.InputStreamReader;
    24  import java.nio.CharBuffer;
    25  import java.util.Vector;
    26  import java.net.URL;
    27
    28  /**
    29   * Tests for XMLDecoder.
    30   *
    31   */
    32  public class XMLDecoderTest extends TestCase {
    33
    34
    35    /**
    36     * Constructor for XMLDecoderTest.
    37     * @param arg0 test name.
    38     */
    39    public XMLDecoderTest(String arg0) {
    40      super(arg0);
    41    }
    42
    43    public String getStringFromResource(final String resourceName,
    44                                        final int maxSize) throws Exception {
    45        InputStream is = XMLDecoderTest.class.getResourceAsStream(resourceName);
    46        if (is == null) {
    47            throw new FileNotFoundException(resourceName);
    48        }
    49        InputStreamReader reader = new InputStreamReader(is, "UTF-8");
    50        CharBuffer cb = CharBuffer.allocate(maxSize);
    51        for(int chars = reader.read(cb);
    52            chars != -1;
    53            chars = reader.read(cb));
    54        cb.flip();
    55        return cb.toString();
    56    }
    57
    58      public void testDecodeEventsString1() throws Exception {
    59          String xmlStr = getStringFromResource("xmlLayout.1.xml", 10000);
    60          XMLDecoder decoder = new XMLDecoder();
    61          Vector<org.apache.log4j.spi.LoggingEvent> events = decoder.decodeEvents(xmlStr);
    62          assertEquals(17, events.size());
    63      }
    64
    65    public void testDecodeEventsString2() throws Exception {
    66        String xmlStr = getStringFromResource("xsltLayout.1.xml", 10000);
    67        XMLDecoder decoder = new XMLDecoder();
    68        Vector<org.apache.log4j.spi.LoggingEvent> events = decoder.decodeEvents(xmlStr);
    69        assertEquals(15, events.size());
    70    }
    71
    72      public void testDecodeEventsURL1() throws Exception {
    73          URL resource = XMLDecoderTest.class.getResource("xmlLayout.1.xml");
    74          XMLDecoder decoder = new XMLDecoder();
    75          Vector<org.apache.log4j.spi.LoggingEvent> events = decoder.decode(resource);
    76          assertEquals(17, events.size());
    77      }
    78
    79      public void testDecodeEventsURL2() throws Exception {
    80          URL resource = XMLDecoderTest.class.getResource("xsltLayout.1.xml");
    81          XMLDecoder decoder = new XMLDecoder();
    82          Vector<org.apache.log4j.spi.LoggingEvent> events = decoder.decode(resource);
    83          assertEquals(15, events.size());
    84      }
    85
    86  }