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.apache.commons.logging.test;
026
027import java.io.ByteArrayInputStream;
028import java.io.ByteArrayOutputStream;
029import java.io.IOException;
030import java.io.ObjectInputStream;
031import java.io.ObjectOutputStream;
032
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.apache.commons.logging.impl.SLF4JLocationAwareLog;
036import org.apache.commons.logging.impl.SLF4JLog;
037import org.junit.After;
038import org.junit.Before;
039import org.junit.Test;
040import org.slf4j.jul.JDK14LoggerFactory;
041import org.slf4j.spi.LocationAwareLogger;
042
043public class SerializationTest {
044
045    ObjectInputStream ois;
046    ByteArrayOutputStream baos = new ByteArrayOutputStream();
047    ObjectOutputStream oos;
048
049    @Before
050    public void setUp() throws Exception {
051        oos = new ObjectOutputStream(baos);
052    }
053
054    @After
055    public void tearDown() throws Exception {
056        oos.close();
057    }
058
059    public void verify() throws IOException, ClassNotFoundException {
060        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
061        ois = new ObjectInputStream(bis);
062
063        Log resuscitatedLog = (Log) ois.readObject();
064        // tests that the "private transient Logger logger" field is non-null
065        resuscitatedLog.debug("");
066        resuscitatedLog.isDebugEnabled();
067    }
068
069    @Test
070    public void testSLF4JLog() throws Exception {
071        JDK14LoggerFactory factory = new JDK14LoggerFactory();
072        SLF4JLog log = new SLF4JLog(factory.getLogger("x"));
073        oos.writeObject(log);
074        verify();
075    }
076
077    @Test
078    public void testSmoke() throws Exception {
079        Log log = LogFactory.getLog("testing");
080        oos.writeObject(log);
081        verify();
082    }
083
084    @Test
085    public void testLocationAware() throws Exception {
086        JDK14LoggerFactory factory = new JDK14LoggerFactory();
087        SLF4JLocationAwareLog log = new SLF4JLocationAwareLog((LocationAwareLogger) factory.getLogger("x"));
088        oos.writeObject(log);
089        verify();
090    }
091}