Source code

    001/**
    002 * Copyright (c) 2004-2011 QOS.ch
    003 * All rights reserved.
    004 *
    005 * Permission is hereby granted, free  of charge, to any person obtaining
    006 * a  copy  of this  software  and  associated  documentation files  (the
    007 * "Software"), to  deal in  the Software without  restriction, including
    008 * without limitation  the rights to  use, copy, modify,  merge, publish,
    009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    010 * permit persons to whom the Software  is furnished to do so, subject to
    011 * the following conditions:
    012 *
    013 * The  above  copyright  notice  and  this permission  notice  shall  be
    014 * included in all copies or substantial portions of the Software.
    015 *
    016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    023 *
    024 */
    025package org.slf4j.issue;
    026
    027import java.io.ByteArrayInputStream;
    028import java.io.ByteArrayOutputStream;
    029import java.io.IOException;
    030import java.io.InputStream;
    031import java.io.ObjectInputStream;
    032import java.io.ObjectOutputStream;
    033import java.io.Serializable;
    034
    035import static org.junit.Assert.assertNotNull;
    036
    037import org.junit.Test;
    038import org.slf4j.Logger;
    039import org.slf4j.LoggerFactory;
    040
    041/**
    042 * See http://jira.qos.ch/browse/SLF4J-252
    043 * @author Thorbjorn Ravn Andersen
    044 */
    045public class LoggerSerializationTest {
    046
    047    static class LoggerHolder implements Serializable {
    048        private static final long serialVersionUID = 1L;
    049
    050        private Logger log = LoggerFactory.getLogger(LoggerHolder.class);
    051
    052        public String toString() {
    053            return "log=" + getLog();
    054        }
    055
    056        public Logger getLog() {
    057            return log;
    058        }
    059    }
    060
    061    @Test
    062    public void testCanLoggerBeSerialized() throws IOException, ClassNotFoundException {
    063
    064        LoggerHolder lh1 = new LoggerHolder();
    065
    066        ByteArrayOutputStream baos = new ByteArrayOutputStream();
    067        ObjectOutputStream out = new ObjectOutputStream(baos);
    068        out.writeObject(lh1);
    069        out.close();
    070
    071        lh1 = null;
    072
    073        byte[] serializedLoggerHolder = baos.toByteArray();
    074
    075        InputStream is = new ByteArrayInputStream(serializedLoggerHolder);
    076        ObjectInputStream in = new ObjectInputStream(is);
    077        LoggerHolder lh2 = (LoggerHolder) in.readObject();
    078
    079        assertNotNull(lh2);
    080        assertNotNull(lh2.getLog());
    081        lh2.getLog().info("You must see this message as a log message");
    082    }
    083
    084}