View Javadoc

1   /**
2    * Copyright (c) 2004-2016 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.slf4j.helpers;
26  
27  import java.io.PrintStream;
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.List;
31  
32  import org.junit.After;
33  import org.junit.Before;
34  import org.slf4j.LoggerFactoryFriend;
35  import org.slf4j.impl.SimpleLogger;
36  
37  public class SimpleLoggerMultithreadedInitializationTest extends MultithreadedInitializationTest {
38      // final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
39      // private final List<Logger> createdLoggers = Collections.synchronizedList(new ArrayList<Logger>());
40      // private final AtomicLong eventCount = new AtomicLong(0);
41      //
42      // private final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
43      //
44      // final int diff = new Random().nextInt(10000);
45      static int NUM_LINES_IN_SLF4J_REPLAY_WARNING = 3;
46      private final PrintStream oldErr = System.err;
47      final String loggerName = this.getClass().getName();
48      StringPrintStream sps = new StringPrintStream(oldErr, true);
49  
50      @Before
51      public void setup() {
52          System.out.println("THREAD_COUNT=" + THREAD_COUNT);
53          System.setErr(sps);
54          System.setProperty(SimpleLogger.LOG_FILE_KEY, "System.err");
55          LoggerFactoryFriend.reset();
56      }
57  
58      @After
59      public void tearDown() throws Exception {
60          LoggerFactoryFriend.reset();
61          System.clearProperty(SimpleLogger.LOG_FILE_KEY);
62          System.setErr(oldErr);
63      }
64  
65      @Override
66      protected long getRecordedEventCount() {
67          return sps.stringList.size();
68      };
69  
70      @Override
71      protected int extraLogEvents() {
72          return NUM_LINES_IN_SLF4J_REPLAY_WARNING;
73      }
74  
75      static class StringPrintStream extends PrintStream {
76  
77          public static final String LINE_SEP = System.getProperty("line.separator");
78          PrintStream other;
79          boolean duplicate = false;
80  
81          List<String> stringList = Collections.synchronizedList(new ArrayList<String>());
82  
83          public StringPrintStream(PrintStream ps, boolean duplicate) {
84              super(ps);
85              other = ps;
86              this.duplicate = duplicate;
87          }
88  
89          public StringPrintStream(PrintStream ps) {
90              this(ps, false);
91          }
92  
93          public void print(String s) {
94              if (duplicate)
95                  other.print(s);
96              stringList.add(s);
97          }
98  
99          public void println(String s) {
100             if (duplicate)
101                 other.println(s);
102             stringList.add(s);
103         }
104 
105         public void println(Object o) {
106             if (duplicate)
107                 other.println(o);
108             stringList.add(o.toString());
109         }
110     }
111 
112 }