SocketWrapper.java

1
package com.renomad.minum.web;
2
3
import com.renomad.minum.logging.ILogger;
4
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.io.OutputStream;
8
import java.net.Socket;
9
import java.net.SocketAddress;
10
import java.nio.charset.Charset;
11
12
/**
13
 * This wraps Sockets to make them more particular to our use case
14
 */
15
final class SocketWrapper implements ISocketWrapper {
16
17
    private final Socket socket;
18
    private final String hostName;
19
    private final InputStream inputStream;
20
    private final OutputStream writer;
21
    private final ILogger logger;
22
    private final IServer server;
23
24
    /**
25
     * Constructor
26
     * @param socket a socket we intend to wrap with methods applicable to our use cases
27
     * @param logger not much more to say on this param
28
     * @param timeoutMillis we'll configure the socket to timeout after this many milliseconds.
29
     */
30
    SocketWrapper(Socket socket, IServer server, ILogger logger, int timeoutMillis, String hostName) throws IOException {
31
        this.socket = socket;
32
        this.hostName = hostName;
33
        logger.logTrace(() -> String.format("Setting timeout of %d milliseconds on socket %s", timeoutMillis, socket));
34 1 1. <init> : removed call to java/net/Socket::setSoTimeout → KILLED
        this.socket.setSoTimeout(timeoutMillis);
35
        this.inputStream = socket.getInputStream();
36
        writer = socket.getOutputStream();
37
        this.logger = logger;
38
        this.server = server;
39
    }
40
41
    @Override
42
    public void send(String msg) throws IOException {
43 1 1. send : removed call to java/io/OutputStream::write → KILLED
        writer.write(msg.getBytes(Charset.defaultCharset()));
44
    }
45
46
    @Override
47
    public void send(byte[] bodyContents) throws IOException {
48 1 1. send : removed call to java/io/OutputStream::write → TIMED_OUT
        writer.write(bodyContents);
49
    }
50
51
    @Override
52
    public void send(byte[] bodyContents, int off, int len) throws IOException {
53 1 1. send : removed call to java/io/OutputStream::write → KILLED
        writer.write(bodyContents, off, len);
54
    }
55
56
    @Override
57
    public void send(int b) throws IOException {
58 1 1. send : removed call to java/io/OutputStream::write → KILLED
        writer.write(b);
59
    }
60
61
    @Override
62
    public void sendHttpLine(String msg) throws IOException {
63
        logger.logTrace(() -> String.format("%s sending: \"%s\"", this, msg));
64 1 1. sendHttpLine : removed call to com/renomad/minum/web/SocketWrapper::send → KILLED
        send(msg + WebEngine.HTTP_CRLF);
65
    }
66
67
    @Override
68
    public int getLocalPort() {
69 1 1. getLocalPort : replaced int return with 0 for com/renomad/minum/web/SocketWrapper::getLocalPort → KILLED
        return socket.getLocalPort();
70
    }
71
72
    @Override
73
    public SocketAddress getRemoteAddrWithPort() {
74 1 1. getRemoteAddrWithPort : replaced return value with null for com/renomad/minum/web/SocketWrapper::getRemoteAddrWithPort → KILLED
        return socket.getRemoteSocketAddress();
75
    }
76
77
    @Override
78
    public String getRemoteAddr() {
79 1 1. getRemoteAddr : replaced return value with "" for com/renomad/minum/web/SocketWrapper::getRemoteAddr → KILLED
        return socket.getInetAddress().getHostAddress();
80
    }
81
82
    @Override
83
    public HttpServerType getServerType() {
84 1 1. getServerType : replaced return value with null for com/renomad/minum/web/SocketWrapper::getServerType → KILLED
        return server.getServerType();
85
    }
86
87
    @Override
88
    public void close() throws IOException {
89
        logger.logTrace(() -> "close called on " + this);
90 1 1. close : removed call to java/net/Socket::close → SURVIVED
        socket.close();
91 2 1. close : negated conditional → KILLED
2. close : removed call to com/renomad/minum/web/IServer::removeMyRecord → KILLED
        if (server != null) server.removeMyRecord(this);
92
    }
93
94
    @Override
95
    public InputStream getInputStream() {
96 1 1. getInputStream : replaced return value with null for com/renomad/minum/web/SocketWrapper::getInputStream → KILLED
        return this.inputStream;
97
    }
98
99
    /**
100
     * Note that since we are indicating just the remote address
101
     * as the unique value, in cases like tests where we are operating as
102
     * sometimes server or client, you might see the server as the remote.
103
     */
104
    @Override
105
    public String toString() {
106 1 1. toString : replaced return value with "" for com/renomad/minum/web/SocketWrapper::toString → KILLED
        return "(SocketWrapper for remote address: " + this.getRemoteAddrWithPort().toString() + ")";
107
    }
108
109
    @Override
110
    public String getHostName() {
111 1 1. getHostName : replaced return value with "" for com/renomad/minum/web/SocketWrapper::getHostName → KILLED
        return hostName;
112
    }
113
}

Mutations

34

1.1
Location : <init>
Killed by : com.renomad.minum.FunctionalTests
removed call to java/net/Socket::setSoTimeout → KILLED

43

1.1
Location : send
Killed by : com.renomad.minum.web.WebTests
removed call to java/io/OutputStream::write → KILLED

48

1.1
Location : send
Killed by : none
removed call to java/io/OutputStream::write → TIMED_OUT

53

1.1
Location : send
Killed by : com.renomad.minum.FunctionalTests
removed call to java/io/OutputStream::write → KILLED

58

1.1
Location : send
Killed by : com.renomad.minum.web.SocketWrapperTests.testSendingSingleByte(com.renomad.minum.web.SocketWrapperTests)
removed call to java/io/OutputStream::write → KILLED

64

1.1
Location : sendHttpLine
Killed by : com.renomad.minum.web.WebTests
removed call to com/renomad/minum/web/SocketWrapper::send → KILLED

69

1.1
Location : getLocalPort
Killed by : com.renomad.minum.FunctionalTests
replaced int return with 0 for com/renomad/minum/web/SocketWrapper::getLocalPort → KILLED

74

1.1
Location : getRemoteAddrWithPort
Killed by : com.renomad.minum.web.WebTests
replaced return value with null for com/renomad/minum/web/SocketWrapper::getRemoteAddrWithPort → KILLED

79

1.1
Location : getRemoteAddr
Killed by : com.renomad.minum.FunctionalTests
replaced return value with "" for com/renomad/minum/web/SocketWrapper::getRemoteAddr → KILLED

84

1.1
Location : getServerType
Killed by : com.renomad.minum.FunctionalTests
replaced return value with null for com/renomad/minum/web/SocketWrapper::getServerType → KILLED

90

1.1
Location : close
Killed by : none
removed call to java/net/Socket::close → SURVIVED
Covering tests

91

1.1
Location : close
Killed by : com.renomad.minum.web.WebTests
negated conditional → KILLED

2.2
Location : close
Killed by : com.renomad.minum.FunctionalTests
removed call to com/renomad/minum/web/IServer::removeMyRecord → KILLED

96

1.1
Location : getInputStream
Killed by : com.renomad.minum.web.WebTests
replaced return value with null for com/renomad/minum/web/SocketWrapper::getInputStream → KILLED

106

1.1
Location : toString
Killed by : com.renomad.minum.FunctionalTests
replaced return value with "" for com/renomad/minum/web/SocketWrapper::toString → KILLED

111

1.1
Location : getHostName
Killed by : com.renomad.minum.FunctionalTests
replaced return value with "" for com/renomad/minum/web/SocketWrapper::getHostName → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0