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.jupiter.api.BeforeEach;
20  import org.junit.jupiter.api.Disabled;
21  import org.junit.jupiter.api.Test;
22  import org.mockito.InOrder;
23  import static org.mockito.Mockito.inOrder;
24  import static org.mockito.Mockito.spy;
25  import static org.mockito.Mockito.verify;
26  
27  /**
28   * Unit tests for {@link ch.qos.logback.core.net.AutoFlushingObjectWriter}.
29   *
30   * @author Sebastian Gröbler
31   */
32  @Disabled
33  public class AutoFlushingObjectWriterTest {
34  
35      private InstrumentedObjectOutputStream objectOutputStream;
36  
37      @BeforeEach
38      public void beforeEachTest() throws IOException {
39          objectOutputStream = spy(new InstrumentedObjectOutputStream());
40      }
41  
42      @Test
43      public void writesToUnderlyingObjectOutputStream() throws IOException {
44  
45          // given
46          ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
47          String object = "foo";
48  
49          // when
50          objectWriter.write(object);
51  
52          // then
53          verify(objectOutputStream).writeObjectOverride(object);
54      }
55  
56      @Test
57      public void flushesAfterWrite() throws IOException {
58  
59          // given
60          ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
61          String object = "foo";
62  
63          // when
64          objectWriter.write(object);
65  
66          // then
67          InOrder inOrder = inOrder(objectOutputStream);
68          inOrder.verify(objectOutputStream).writeObjectOverride(object);
69          inOrder.verify(objectOutputStream).flush();
70      }
71  
72      @Test
73      public void resetsObjectOutputStreamAccordingToGivenResetFrequency() throws IOException {
74  
75          // given
76          ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
77          String object = "foo";
78  
79          // when
80          objectWriter.write(object);
81          objectWriter.write(object);
82          objectWriter.write(object);
83          objectWriter.write(object);
84  
85          // then
86          InOrder inOrder = inOrder(objectOutputStream);
87          inOrder.verify(objectOutputStream).writeObjectOverride(object);
88          inOrder.verify(objectOutputStream).writeObjectOverride(object);
89          inOrder.verify(objectOutputStream).reset();
90          inOrder.verify(objectOutputStream).writeObjectOverride(object);
91          inOrder.verify(objectOutputStream).writeObjectOverride(object);
92          inOrder.verify(objectOutputStream).reset();
93      }
94  
95      private static class InstrumentedObjectOutputStream extends ObjectOutputStream {
96  
97          protected InstrumentedObjectOutputStream() throws IOException, SecurityException {
98              super();
99          }
100 
101         @Override
102         protected void writeObjectOverride(final Object obj) throws IOException {
103             // nop
104         }
105 
106         @Override
107         public void flush() throws IOException {
108             // nop
109         }
110 
111         @Override
112         public void reset() throws IOException {
113             // nop
114         }
115     }
116 }