Skip to content

Commit d4658a3

Browse files
mgaffigantonygermano
authored andcommitted
Extracted client command line parsing to tested class
Signed-off-by: Mitch Gaffigan <[email protected]>
1 parent f5211eb commit d4658a3

File tree

3 files changed

+175
-57
lines changed

3 files changed

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

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

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
/*
2-
* Copyright (c) Mirth Corporation. All rights reserved.
3-
*
4-
* http://www.mirthcorp.com
5-
*
6-
* The software in this package is published under the terms of the MPL license a copy of which has
7-
* been included with this distribution in the LICENSE.txt file.
8-
*/
1+
// SPDX-License-Identifier: MPL-2.0
2+
// SPDX-FileCopyrightText: Mirth Corporation
93

104
package com.mirth.connect.client.ui;
115

@@ -259,59 +253,18 @@ public static void initUIManager() {
259253
* String[]
260254
*/
261255
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-
}
256+
CommandLineOptions opts = new CommandLineOptions(args);
306257

307-
if (StringUtils.isNotBlank(protocols)) {
308-
PlatformUI.HTTPS_PROTOCOLS = StringUtils.split(protocols, ',');
258+
if (StringUtils.isNotBlank(opts.getProtocols())) {
259+
PlatformUI.HTTPS_PROTOCOLS = StringUtils.split(opts.getProtocols(), ',');
309260
}
310-
if (StringUtils.isNotBlank(cipherSuites)) {
311-
PlatformUI.HTTPS_CIPHER_SUITES = StringUtils.split(cipherSuites, ',');
261+
if (StringUtils.isNotBlank(opts.getCipherSuites())) {
262+
PlatformUI.HTTPS_CIPHER_SUITES = StringUtils.split(opts.getCipherSuites(), ',');
312263
}
264+
PlatformUI.SERVER_URL = opts.getServer();
265+
PlatformUI.CLIENT_VERSION = opts.getVersion();
313266

314-
start(server, version, username, password);
267+
start(opts.getServer(), opts.getVersion(), opts.getUsername(), opts.getPassword());
315268
}
316269

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

0 commit comments

Comments
 (0)