View Javadoc
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.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          // tests that the "private transient Logger logger" field is non-null
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  }