View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.net;
15  
16  import java.io.IOException;
17  import java.io.ObjectOutputStream;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.mockito.InOrder;
22  import static org.mockito.Mockito.inOrder;
23  import static org.mockito.Mockito.spy;
24  import static org.mockito.Mockito.verify;
25  
26  /**
27   * Unit tests for {@link ch.qos.logback.core.net.AutoFlushingObjectWriter}.
28   *
29   * @author Sebastian Gröbler
30   */
31  public class AutoFlushingObjectWriterTest {
32  
33      private InstrumentedObjectOutputStream objectOutputStream;
34  
35      @Before
36      public void beforeEachTest() throws IOException {
37          objectOutputStream = spy(new InstrumentedObjectOutputStream());
38      }
39  
40      @Test
41      public void writesToUnderlyingObjectOutputStream() throws IOException {
42  
43          // given
44          ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
45          String object = "foo";
46  
47          // when
48          objectWriter.write(object);
49  
50          // then
51          verify(objectOutputStream).writeObjectOverride(object);
52      }
53  
54      @Test
55      public void flushesAfterWrite() throws IOException {
56  
57          // given
58          ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
59          String object = "foo";
60  
61          // when
62          objectWriter.write(object);
63  
64          // then
65          InOrder inOrder = inOrder(objectOutputStream);
66          inOrder.verify(objectOutputStream).writeObjectOverride(object);
67          inOrder.verify(objectOutputStream).flush();
68      }
69  
70      @Test
71      public void resetsObjectOutputStreamAccordingToGivenResetFrequency() throws IOException {
72  
73          // given
74          ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
75          String object = "foo";
76  
77          // when
78          objectWriter.write(object);
79          objectWriter.write(object);
80          objectWriter.write(object);
81          objectWriter.write(object);
82  
83          // then
84          InOrder inOrder = inOrder(objectOutputStream);
85          inOrder.verify(objectOutputStream).writeObjectOverride(object);
86          inOrder.verify(objectOutputStream).writeObjectOverride(object);
87          inOrder.verify(objectOutputStream).reset();
88          inOrder.verify(objectOutputStream).writeObjectOverride(object);
89          inOrder.verify(objectOutputStream).writeObjectOverride(object);
90          inOrder.verify(objectOutputStream).reset();
91      }
92  
93      private static class InstrumentedObjectOutputStream extends ObjectOutputStream {
94  
95          protected InstrumentedObjectOutputStream() throws IOException, SecurityException {
96          }
97  
98          @Override
99          protected void writeObjectOverride(final Object obj) throws IOException {
100             // nop
101         }
102 
103         @Override
104         public void flush() throws IOException {
105             // nop
106         }
107 
108         @Override
109         public void reset() throws IOException {
110             // nop
111         }
112     }
113 }