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.server.test;
015
016import java.io.IOException;
017
018import ch.qos.logback.core.Context;
019import ch.qos.logback.core.net.server.Client;
020import ch.qos.logback.core.net.server.ClientVisitor;
021import ch.qos.logback.core.net.server.ServerRunner;
022import ch.qos.logback.core.spi.ContextAwareBase;
023
024/**
025 * A mock {@link ServerRunner} with instrumentation for unit testing.
026 *
027 * @author Carl Harris
028 */
029public class MockServerRunner<T extends Client> extends ContextAwareBase implements ServerRunner<T> {
030
031    private IOException stopException;
032    private int startCount;
033    private boolean contextInjected;
034
035    @Override
036    public void setContext(Context context) {
037        contextInjected = true;
038        super.setContext(context);
039    }
040
041    public void run() {
042        startCount++;
043    }
044
045    public void stop() throws IOException {
046        if (stopException != null) {
047            throw stopException;
048        }
049        startCount--;
050    }
051
052    public boolean isRunning() {
053        return startCount > 0;
054    }
055
056    @SuppressWarnings("rawtypes")
057    public void accept(ClientVisitor visitor) {
058        throw new UnsupportedOperationException();
059    }
060
061    public int getStartCount() {
062        return startCount;
063    }
064
065    public boolean isContextInjected() {
066        return contextInjected;
067    }
068
069    public void setStopException(IOException stopException) {
070        this.stopException = stopException;
071    }
072
073}