Provide default values for optional cli args
[sig-chat-ex-feb-2019.git] / src / test / java / exercise / nio / chat / server / ServerTest.java
blobdab163c2dfcccd1e07a97fd96fa35e90b74e2a80
1 package exercise.nio.chat.server;
3 import static org.junit.Assert.assertEquals;
5 import java.util.Random;
7 import org.junit.Test;
9 import exercise.nio.chat.server.data.storage.MapDBStorage;
10 import picocli.CommandLine;
11 import picocli.CommandLine.MissingParameterException;
13 public class ServerTest {
14 @Test public void testCliParsing() {
15 final Server.ServerOptions opts = new Server.ServerOptions();
16 final Server s = CommandLine.call(opts, new String[] {"-p", "3336"});
17 assertEquals("opts address should be set to default",
18 Server.TEST_ADDRESS, opts.getAddress());
19 assertEquals("server address should be set to default",
20 Server.TEST_ADDRESS, s.getAddress());
21 assertEquals("opts port should be set to 3336", 3336, opts.getPort());
22 assertEquals("server port should be set to 3336", 3336, s.getPort());
23 assertEquals("opts dbFile should be set to default",
24 MapDBStorage.TESTING_DB_FILE, opts.getDbFile());
25 assertEquals("db file should be set to default",
26 MapDBStorage.TESTING_DB_FILE, MapDBStorage.getDbFile());
29 @Test(expected = MissingParameterException.class)
30 public void testCliParsing_missingPort() {
31 final Server.ServerOptions opts = new Server.ServerOptions();
32 final CommandLine cmd = new CommandLine(opts);
33 cmd.parse(new String[] {});
36 @Test public void testCliParsing_allPopulated() {
37 final Server.ServerOptions opts = new Server.ServerOptions();
38 final String address = "ahost";
39 final Integer port = new Random().nextInt(65536);
40 final String dbFile = "chats.db";
41 final Server s = CommandLine.call(opts,
42 new String[] {"-a", address, "-p", port.toString(),
43 "-f", dbFile});
44 assertEquals("opts address should be set to " + address, address,
45 opts.getAddress());
46 assertEquals("server address should be set to " + address,
47 address, s.getAddress());
48 assertEquals("opts port should be set to " + port, (int) port,
49 opts.getPort());
50 assertEquals("server port should be set to " + port, (int) port,
51 s.getPort());
52 assertEquals("opts dbFile should be set to " + dbFile, dbFile,
53 opts.getDbFile());
54 assertEquals("db file should be set to " + dbFile,
55 dbFile, MapDBStorage.getDbFile());