1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.apache.commons.logging.test;
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 org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.commons.logging.impl.SLF4JLocationAwareLog;
36 import org.apache.commons.logging.impl.SLF4JLog;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.slf4j.jul.JDK14LoggerFactory;
41 import org.slf4j.spi.LocationAwareLogger;
42
43 public class SerializationTest {
44
45 ObjectInputStream ois;
46 ByteArrayOutputStream baos = new ByteArrayOutputStream();
47 ObjectOutputStream oos;
48
49 @Before
50 public void setUp() throws Exception {
51 oos = new ObjectOutputStream(baos);
52 }
53
54 @After
55 public void tearDown() throws Exception {
56 oos.close();
57 }
58
59 public void verify() throws IOException, ClassNotFoundException {
60 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
61 ois = new ObjectInputStream(bis);
62
63 Log resuscitatedLog = (Log) ois.readObject();
64
65 resuscitatedLog.debug("");
66 resuscitatedLog.isDebugEnabled();
67 }
68
69 @Test
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 @Test
78 public void testSmoke() throws Exception {
79 Log log = LogFactory.getLog("testing");
80 oos.writeObject(log);
81 verify();
82 }
83
84 @Test
85 public void testLocationAware() throws Exception {
86 JDK14LoggerFactory factory = new JDK14LoggerFactory();
87 SLF4JLocationAwareLog log = new SLF4JLocationAwareLog((LocationAwareLogger) factory.getLogger("x"));
88 oos.writeObject(log);
89 verify();
90 }
91 }