-
Notifications
You must be signed in to change notification settings - Fork 1
/
DummyReader.java
115 lines (102 loc) · 3.75 KB
/
DummyReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package it.albertus.routerlogger.reader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.util.LinkedHashMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import it.albertus.routerlogger.resources.Messages;
import it.albertus.util.logging.LoggerFactory;
public class DummyReader extends Reader {
private static final Logger logger = LoggerFactory.getLogger(DummyReader.class);
private static final byte CHARACTERS = 15;
private static final byte COLUMNS = 30;
private static final short LAG_IN_MILLIS = 100;
private static final short CONNECTION_TIME_IN_MILLIS = 1000;
private static final short AUTHENTICATION_TIME_IN_MILLIS = 1000;
private static final double CONNECTION_ERROR_PERCENTAGE = 0.0;
private static final double AUTHENTICATION_ERROR_PERCENTAGE = 0.0;
private static final double READ_ERROR_PERCENTAGE = 0.0;
@Override
public boolean connect() {
logger.info(Messages.get("msg.dummy.connect"));
if (CONNECTION_TIME_IN_MILLIS > 0) {
try {
TimeUnit.MILLISECONDS.sleep(CONNECTION_TIME_IN_MILLIS);
}
catch (final InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
if (Math.random() > (100.0 - CONNECTION_ERROR_PERCENTAGE) / 100.0) {
final Exception e = new ConnectException(Messages.get("msg.dummy.connect.error", CONNECTION_ERROR_PERCENTAGE));
logger.log(Level.WARNING, e.toString(), e);
return false;
}
return true;
}
@Override
public boolean login(final String username, final char[] password) {
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO, "Username: {0}", username);
logger.log(Level.INFO, "Password: {0}", password != null ? String.valueOf(password) : password);
}
if (AUTHENTICATION_TIME_IN_MILLIS > 0) {
try {
TimeUnit.MILLISECONDS.sleep(AUTHENTICATION_TIME_IN_MILLIS);
}
catch (final InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
if (Math.random() > (100.0 - AUTHENTICATION_ERROR_PERCENTAGE) / 100.0) {
throw new SecurityException(Messages.get("msg.dummy.authentication.error", AUTHENTICATION_ERROR_PERCENTAGE));
}
final String message = getClass().getSimpleName() + " - " + Messages.get("msg.test.purposes.only");
final StringBuilder separator = new StringBuilder();
for (int c = 0; c < message.length(); c++) {
separator.append('-');
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintWriter pw = new PrintWriter(baos);
pw.println(separator.toString());
pw.println(message);
pw.print(separator.toString());
pw.close();
logger.log(Level.INFO, LOG_MASK_TELNET, baos);
return true;
}
@Override
public LinkedHashMap<String, String> readInfo() throws IOException {
final LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
for (byte i = 1; i <= COLUMNS; i++) {
StringBuilder field = new StringBuilder();
for (byte j = 1; j <= CHARACTERS; j++) {
field.append((char) (97 + Math.random() * 25));
}
map.put(Messages.get("lbl.column.number", i), field.toString());
}
if (LAG_IN_MILLIS != 0) {
try {
TimeUnit.MILLISECONDS.sleep(LAG_IN_MILLIS);
}
catch (final InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
if (Math.random() > (100.0 - READ_ERROR_PERCENTAGE) / 100.0) {
throw new IOException(Messages.get("msg.dummy.readinfo.error", READ_ERROR_PERCENTAGE));
}
return map;
}
@Override
public void logout() {
logger.info(Messages.get("msg.dummy.logout"));
}
@Override
public void disconnect() {
logger.info(Messages.get("msg.dummy.disconnect"));
}
}