001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.net;
015
016import java.io.IOException;
017import java.io.ObjectOutputStream;
018
019import org.junit.Before;
020import org.junit.Test;
021import org.mockito.InOrder;
022import static org.mockito.Mockito.inOrder;
023import static org.mockito.Mockito.spy;
024import static org.mockito.Mockito.verify;
025
026/**
027 * Unit tests for {@link ch.qos.logback.core.net.AutoFlushingObjectWriter}.
028 *
029 * @author Sebastian Gröbler
030 */
031public class AutoFlushingObjectWriterTest {
032
033    private InstrumentedObjectOutputStream objectOutputStream;
034
035    @Before
036    public void beforeEachTest() throws IOException {
037        objectOutputStream = spy(new InstrumentedObjectOutputStream());
038    }
039
040    @Test
041    public void writesToUnderlyingObjectOutputStream() throws IOException {
042
043        // given
044        ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
045        String object = "foo";
046
047        // when
048        objectWriter.write(object);
049
050        // then
051        verify(objectOutputStream).writeObjectOverride(object);
052    }
053
054    @Test
055    public void flushesAfterWrite() throws IOException {
056
057        // given
058        ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
059        String object = "foo";
060
061        // when
062        objectWriter.write(object);
063
064        // then
065        InOrder inOrder = inOrder(objectOutputStream);
066        inOrder.verify(objectOutputStream).writeObjectOverride(object);
067        inOrder.verify(objectOutputStream).flush();
068    }
069
070    @Test
071    public void resetsObjectOutputStreamAccordingToGivenResetFrequency() throws IOException {
072
073        // given
074        ObjectWriter objectWriter = new AutoFlushingObjectWriter(objectOutputStream, 2);
075        String object = "foo";
076
077        // when
078        objectWriter.write(object);
079        objectWriter.write(object);
080        objectWriter.write(object);
081        objectWriter.write(object);
082
083        // then
084        InOrder inOrder = inOrder(objectOutputStream);
085        inOrder.verify(objectOutputStream).writeObjectOverride(object);
086        inOrder.verify(objectOutputStream).writeObjectOverride(object);
087        inOrder.verify(objectOutputStream).reset();
088        inOrder.verify(objectOutputStream).writeObjectOverride(object);
089        inOrder.verify(objectOutputStream).writeObjectOverride(object);
090        inOrder.verify(objectOutputStream).reset();
091    }
092
093    private static class InstrumentedObjectOutputStream extends ObjectOutputStream {
094
095        protected InstrumentedObjectOutputStream() throws IOException, SecurityException {
096        }
097
098        @Override
099        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}