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.server.test;
15  
16  import java.io.IOException;
17  
18  import ch.qos.logback.core.Context;
19  import ch.qos.logback.core.net.server.Client;
20  import ch.qos.logback.core.net.server.ClientVisitor;
21  import ch.qos.logback.core.net.server.ServerRunner;
22  import ch.qos.logback.core.spi.ContextAwareBase;
23  
24  /**
25   * A mock {@link ServerRunner} with instrumentation for unit testing.
26   *
27   * @author Carl Harris
28   */
29  public class MockServerRunner<T extends Client> extends ContextAwareBase implements ServerRunner<T> {
30  
31      private IOException stopException;
32      private int startCount;
33      private boolean contextInjected;
34  
35      @Override
36      public void setContext(Context context) {
37          contextInjected = true;
38          super.setContext(context);
39      }
40  
41      public void run() {
42          startCount++;
43      }
44  
45      public void stop() throws IOException {
46          if (stopException != null) {
47              throw stopException;
48          }
49          startCount--;
50      }
51  
52      public boolean isRunning() {
53          return startCount > 0;
54      }
55  
56      @SuppressWarnings("rawtypes")
57      public void accept(ClientVisitor visitor) {
58          throw new UnsupportedOperationException();
59      }
60  
61      public int getStartCount() {
62          return startCount;
63      }
64  
65      public boolean isContextInjected() {
66          return contextInjected;
67      }
68  
69      public void setStopException(IOException stopException) {
70          this.stopException = stopException;
71      }
72  
73  }