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.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 org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.junit.After;
36  import org.junit.Before;
37  import org.junit.Test;
38  import org.slf4j.impl.JDK14LoggerFactory;
39  import org.slf4j.spi.LocationAwareLogger;
40  
41  public class SerializationTest {
42  
43      ObjectInputStream ois;
44      ByteArrayOutputStream baos = new ByteArrayOutputStream();
45      ObjectOutputStream oos;
46  
47      @Before
48      public void setUp() throws Exception {
49          oos = new ObjectOutputStream(baos);
50      }
51  
52      @After
53      public void tearDown() throws Exception {
54          oos.close();
55      }
56  
57      public void verify() throws IOException, ClassNotFoundException {
58          ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
59          ois = new ObjectInputStream(bis);
60  
61          Log resuscitatedLog = (Log) ois.readObject();
62          // tests that the "private transient Logger logger" field is non-null
63          resuscitatedLog.debug("");
64          resuscitatedLog.isDebugEnabled();
65      }
66  
67      @Test
68      public void testSLF4JLog() throws Exception {
69          JDK14LoggerFactory factory = new JDK14LoggerFactory();
70          SLF4JLog log = new SLF4JLog(factory.getLogger("x"));
71          oos.writeObject(log);
72          verify();
73      }
74  
75      @Test
76      public void testSmoke() throws Exception {
77          Log log = LogFactory.getLog("testing");
78          oos.writeObject(log);
79          verify();
80      }
81  
82      @Test
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  }