1
2
3
4
5
6
7
8
9
10
11
12
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
29
30
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
46 ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
47 String object = "foo";
48
49
50 objectWriter.write(object);
51
52
53 verify(objectOutputStream).writeObjectOverride(object);
54 }
55
56 @Test
57 public void flushesAfterWrite() throws IOException {
58
59
60 ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
61 String object = "foo";
62
63
64 objectWriter.write(object);
65
66
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
76 ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
77 String object = "foo";
78
79
80 objectWriter.write(object);
81 objectWriter.write(object);
82 objectWriter.write(object);
83 objectWriter.write(object);
84
85
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
104 }
105
106 @Override
107 public void flush() throws IOException {
108
109 }
110
111 @Override
112 public void reset() throws IOException {
113
114 }
115 }
116 }