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