1   package org.apache.commons.logging.impl;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.IOException;
6   import java.io.ObjectInputStream;
7   import java.io.ObjectOutputStream;
8   
9   import junit.framework.TestCase;
10  
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  import org.slf4j.impl.JDK14LoggerFactory;
14  import org.slf4j.spi.LocationAwareLogger;
15  
16  public class SerializationTest extends TestCase {
17  
18    ObjectInputStream ois;
19    ByteArrayOutputStream baos = new ByteArrayOutputStream();
20    ObjectOutputStream oos;
21  
22    public SerializationTest(String name) {
23      super(name);
24    }
25  
26    protected void setUp() throws Exception {
27      oos = new ObjectOutputStream(baos);
28      super.setUp();
29    }
30  
31    protected void tearDown() throws Exception {
32      super.tearDown();
33      oos.close();
34    }
35  
36    public void verify() throws IOException, ClassNotFoundException {
37      ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
38      ois = new ObjectInputStream(bis);
39  
40      Log readLog = (Log) ois.readObject();
41      // tests that the "private transient Logger logger" field is non-null
42      readLog.debug("");
43    }
44  
45    public void testSLF4JLog() throws Exception {
46      JDK14LoggerFactory factory = new JDK14LoggerFactory();
47      SLF4JLog log = new SLF4JLog(factory.getLogger("x"));
48      oos.writeObject(log);
49      verify();
50    }
51  
52    public void testSmoke() throws Exception {
53      Log log = LogFactory.getLog("testing");
54      oos.writeObject(log);
55      verify();
56    }
57  
58    public void testLocationAware() throws Exception {
59      JDK14LoggerFactory factory = new JDK14LoggerFactory();
60      SLF4JLocationAwareLog log = new SLF4JLocationAwareLog(
61          (LocationAwareLogger) factory.getLogger("x"));
62      oos.writeObject(log);
63      verify();
64    }
65  }