Skip to content

Commit e4fe9e6

Browse files
committed
Extracted client command line parsing to tested class
Signed-off-by: Mitch Gaffigan <mitch.gaffigan@comcast.net>
1 parent 7ecf54b commit e4fe9e6

File tree

3 files changed

+167
-49
lines changed

3 files changed

+167
-49
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.mirth.connect.client.ui;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
/**
6+
* Immutable holder for command line options used by the Mirth client.
7+
*/
8+
public class CommandLineOptions {
9+
private final String server;
10+
private final String version;
11+
private final String username;
12+
private final String password;
13+
private final String protocols;
14+
private final String cipherSuites;
15+
16+
/**
17+
* Parse command line arguments for Mirth client.
18+
*/
19+
public CommandLineOptions(String[] args) {
20+
String server = "https://localhost:8443";
21+
String version = "";
22+
String username = "";
23+
String password = "";
24+
String protocols = "";
25+
String cipherSuites = "";
26+
27+
if (args == null) {
28+
args = new String[0];
29+
}
30+
31+
if (args.length > 0) {
32+
server = args[0];
33+
}
34+
if (args.length > 1) {
35+
version = args[1];
36+
}
37+
if (args.length > 2) {
38+
if (StringUtils.equalsIgnoreCase(args[2], "-ssl")) {
39+
// <server> <version> -ssl [<protocols> [<ciphersuites> [<username> [<password>]]]]
40+
if (args.length > 3) {
41+
protocols = args[3];
42+
}
43+
if (args.length > 4) {
44+
cipherSuites = args[4];
45+
}
46+
if (args.length > 5) {
47+
username = args[5];
48+
}
49+
if (args.length > 6) {
50+
password = args[6];
51+
}
52+
} else {
53+
// <server> <version> <username> [<password> [-ssl [<protocols> [<ciphersuites>]]]]
54+
username = args[2];
55+
if (args.length > 3) {
56+
password = args[3];
57+
}
58+
if (args.length > 4 && StringUtils.equalsIgnoreCase(args[4], "-ssl")) {
59+
if (args.length > 5) {
60+
protocols = args[5];
61+
}
62+
if (args.length > 6) {
63+
cipherSuites = args[6];
64+
}
65+
}
66+
}
67+
}
68+
69+
this.server = server;
70+
this.version = version;
71+
this.username = username;
72+
this.password = password;
73+
this.protocols = protocols;
74+
this.cipherSuites = cipherSuites;
75+
}
76+
77+
public String getServer() {
78+
return server;
79+
}
80+
81+
public String getVersion() {
82+
return version;
83+
}
84+
85+
public String getUsername() {
86+
return username;
87+
}
88+
89+
public String getPassword() {
90+
return password;
91+
}
92+
93+
public String getProtocols() {
94+
return protocols;
95+
}
96+
97+
public String getCipherSuites() {
98+
return cipherSuites;
99+
}
100+
}

client/src/com/mirth/connect/client/ui/Mirth.java

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -259,59 +259,18 @@ public static void initUIManager() {
259259
* String[]
260260
*/
261261
public static void main(String[] args) {
262-
String server = "https://localhost:8443";
263-
String version = "";
264-
String username = "";
265-
String password = "";
266-
String protocols = "";
267-
String cipherSuites = "";
268-
269-
if (args.length > 0) {
270-
server = args[0];
271-
}
272-
if (args.length > 1) {
273-
version = args[1];
274-
}
275-
if (args.length > 2) {
276-
if (StringUtils.equalsIgnoreCase(args[2], "-ssl")) {
277-
// <server> <version> -ssl [<protocols> [<ciphersuites> [<username> [<password>]]]]
278-
if (args.length > 3) {
279-
protocols = args[3];
280-
}
281-
if (args.length > 4) {
282-
cipherSuites = args[4];
283-
}
284-
if (args.length > 5) {
285-
username = args[5];
286-
}
287-
if (args.length > 6) {
288-
password = args[6];
289-
}
290-
} else {
291-
// <server> <version> <username> [<password> [-ssl [<protocols> [<ciphersuites>]]]]
292-
username = args[2];
293-
if (args.length > 3) {
294-
password = args[3];
295-
}
296-
if (args.length > 4 && StringUtils.equalsIgnoreCase(args[4], "-ssl")) {
297-
if (args.length > 5) {
298-
protocols = args[5];
299-
}
300-
if (args.length > 6) {
301-
cipherSuites = args[6];
302-
}
303-
}
304-
}
305-
}
262+
CommandLineOptions opts = new CommandLineOptions(args);
306263

307-
if (StringUtils.isNotBlank(protocols)) {
308-
PlatformUI.HTTPS_PROTOCOLS = StringUtils.split(protocols, ',');
264+
if (StringUtils.isNotBlank(opts.getProtocols())) {
265+
PlatformUI.HTTPS_PROTOCOLS = StringUtils.split(opts.getProtocols(), ',');
309266
}
310-
if (StringUtils.isNotBlank(cipherSuites)) {
311-
PlatformUI.HTTPS_CIPHER_SUITES = StringUtils.split(cipherSuites, ',');
267+
if (StringUtils.isNotBlank(opts.getCipherSuites())) {
268+
PlatformUI.HTTPS_CIPHER_SUITES = StringUtils.split(opts.getCipherSuites(), ',');
312269
}
270+
PlatformUI.SERVER_URL = opts.getServer();
271+
PlatformUI.CLIENT_VERSION = opts.getVersion();
313272

314-
start(server, version, username, password);
273+
start(opts.getServer(), opts.getVersion(), opts.getUsername(), opts.getPassword());
315274
}
316275

317276
private static void start(final String server, final String version, final String username, final String password) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.mirth.connect.client.ui;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class CommandLineOptionsTest {
8+
9+
@Test
10+
public void testParseSslForm() {
11+
String[] args = new String[] { "https://example:8443", "1.0", "-ssl", "TLSv1.2,TLSv1.3", "TLS_RSA_WITH_AES_128_GCM_SHA256", "alice", "secret" };
12+
CommandLineOptions opts = new CommandLineOptions(args);
13+
14+
assertEquals("https://example:8443", opts.getServer());
15+
assertEquals("1.0", opts.getVersion());
16+
assertEquals("alice", opts.getUsername());
17+
assertEquals("secret", opts.getPassword());
18+
assertEquals("TLSv1.2,TLSv1.3", opts.getProtocols());
19+
assertEquals("TLS_RSA_WITH_AES_128_GCM_SHA256", opts.getCipherSuites());
20+
}
21+
22+
@Test
23+
public void testParseUsernameFormWithSsl() {
24+
String[] args = new String[] { "https://example:8443", "1.0", "bob", "pw", "-ssl", "TLSv1.2", "CIPHER" };
25+
CommandLineOptions opts = new CommandLineOptions(args);
26+
27+
assertEquals("https://example:8443", opts.getServer());
28+
assertEquals("1.0", opts.getVersion());
29+
assertEquals("bob", opts.getUsername());
30+
assertEquals("pw", opts.getPassword());
31+
assertEquals("TLSv1.2", opts.getProtocols());
32+
assertEquals("CIPHER", opts.getCipherSuites());
33+
}
34+
35+
@Test
36+
public void testNullArgsUsesDefaults() {
37+
CommandLineOptions opts = new CommandLineOptions((String[]) null);
38+
39+
assertEquals("https://localhost:8443", opts.getServer());
40+
assertEquals("", opts.getVersion());
41+
assertEquals("", opts.getUsername());
42+
assertEquals("", opts.getPassword());
43+
assertEquals("", opts.getProtocols());
44+
assertEquals("", opts.getCipherSuites());
45+
}
46+
47+
@Test
48+
public void testNormal() {
49+
String[] args = new String[] { "https://example:8443", "1.0" };
50+
CommandLineOptions opts = new CommandLineOptions(args);
51+
52+
assertEquals("https://example:8443", opts.getServer());
53+
assertEquals("1.0", opts.getVersion());
54+
assertEquals("", opts.getUsername());
55+
assertEquals("", opts.getPassword());
56+
assertEquals("", opts.getProtocols());
57+
assertEquals("", opts.getCipherSuites());
58+
}
59+
}

0 commit comments

Comments
 (0)