1 /** 2 * Copyright (c) 2004-2011 QOS.ch 3 * All rights reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 * 24 */ 25 package org.apache.commons.logging.impl; 26 27 import java.io.ByteArrayInputStream; 28 import java.io.ByteArrayOutputStream; 29 import java.io.IOException; 30 import java.io.ObjectInputStream; 31 import java.io.ObjectOutputStream; 32 33 import junit.framework.TestCase; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.slf4j.impl.JDK14LoggerFactory; 38 import org.slf4j.spi.LocationAwareLogger; 39 40 public class SerializationTest extends TestCase { 41 42 ObjectInputStream ois; 43 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 44 ObjectOutputStream oos; 45 46 public SerializationTest(String name) { 47 super(name); 48 } 49 50 protected void setUp() throws Exception { 51 oos = new ObjectOutputStream(baos); 52 super.setUp(); 53 } 54 55 protected void tearDown() throws Exception { 56 super.tearDown(); 57 oos.close(); 58 } 59 60 public void verify() throws IOException, ClassNotFoundException { 61 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 62 ois = new ObjectInputStream(bis); 63 64 Log resuscitatedLog = (Log) ois.readObject(); 65 // tests that the "private transient Logger logger" field is non-null 66 resuscitatedLog.debug(""); 67 resuscitatedLog.isDebugEnabled(); 68 } 69 70 public void testSLF4JLog() throws Exception { 71 JDK14LoggerFactory factory = new JDK14LoggerFactory(); 72 SLF4JLog log = new SLF4JLog(factory.getLogger("x")); 73 oos.writeObject(log); 74 verify(); 75 } 76 77 public void testSmoke() throws Exception { 78 Log log = LogFactory.getLog("testing"); 79 oos.writeObject(log); 80 verify(); 81 } 82 83 public void testLocationAware() throws Exception { 84 JDK14LoggerFactory factory = new JDK14LoggerFactory(); 85 SLF4JLocationAwareLog log = new SLF4JLocationAwareLog((LocationAwareLogger) factory.getLogger("x")); 86 oos.writeObject(log); 87 verify(); 88 } 89 } 90