Skip to content

Commit a9f9bfb

Browse files
committed
Simplify main
1 parent 54bb2a6 commit a9f9bfb

File tree

9 files changed

+112
-97
lines changed

9 files changed

+112
-97
lines changed

.editorconfig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# File generated by jbang setup@jabrena
4+
5+
# top-most EditorConfig file
6+
root = true
7+
8+
[*]
9+
charset = utf-8
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.html]
15+
indent_style = space
16+
indent_size = 4
17+
18+
[*.json]
19+
indent_style = space
20+
indent_size = 4
21+
22+
[*.xml]
23+
indent_style = space
24+
indent_size = 4
25+
26+
[*.java]
27+
indent_style = space
28+
indent_size = 4
29+
30+
[*.yml,*.yaml]
31+
indent_style = space
32+
indent_size = 2
33+
34+
[*.dsl]
35+
indent_style = space
36+
indent_size = 4

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ sdk env install
9595
jwebserver -p 9000 -d "$(pwd)/build/reports/"
9696
./gradlew clean build snapshot
9797
java -jar build/libs/context-mapper-cli-0.1.0-SNAPSHOT.jar
98+
99+
sdk use java 11.0.27-tem
100+
java -jar build/libs/context-mapper-cli-0.1.0-SNAPSHOT.jar
98101
```
99102
100103
## Contributing

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ application {
3434
applicationName = 'cm'
3535
}
3636

37+
javadoc {
38+
options.addStringOption('Xdoclint:none', '-quiet')
39+
failOnError false
40+
}
41+
3742
jar {
3843
manifest {
3944
attributes (

src/main/java/org/contextmapper/cli/ContextMapperCLI.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.function.Supplier;
88

99
@Command(
10-
name = "cm",
10+
name = "cm",
1111
versionProvider = VersionProvider.class,
1212
description = "Context Mapper CLI",
1313
subcommands = {
@@ -18,33 +18,14 @@
1818
usageHelpAutoWidth = true)
1919
public class ContextMapperCLI implements Runnable {
2020

21-
private static final int REQUIRED_JAVA_VERSION = 17;
22-
static Supplier<Integer> javaVersionSupplier = () -> Runtime.version().feature();
23-
2421
@Override
2522
public void run() {
2623
System.out.println("Context Mapper CLI. Use 'cm --help' for usage information.");
2724
}
2825

29-
static void checkJavaVersion(Runnable exiter) {
30-
int currentVersion = javaVersionSupplier.get();
31-
if (currentVersion < REQUIRED_JAVA_VERSION) {
32-
System.err.printf("Invalid Java version '%s' (>=%s is required).%n", currentVersion, REQUIRED_JAVA_VERSION);
33-
exiter.run();
34-
}
35-
}
36-
37-
private static void checkJavaVersion() {
38-
checkJavaVersion(() -> System.exit(1));
39-
}
40-
41-
static int runCLI(String[] args) {
42-
return new CommandLine(new ContextMapperCLI()).execute(args);
43-
}
44-
4526
public static void main(String[] args) {
46-
checkJavaVersion();
47-
int exitCode = runCLI(args);
27+
CommandLine commandLine = new CommandLine(new ContextMapperCLI());
28+
int exitCode = commandLine.execute(args);
4829
System.exit(exitCode);
4930
}
5031
}

src/test/java/org/contextmapper/cli/ContextMapperCLITest.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,25 @@ void main_WhenCalledWithoutCommand_ThenPrintsTopLevelHelp() {
4949
@DisplayName("main() should print top-level help when called with --help option")
5050
void main_WhenCalledWithHelpOption_ThenPrintsTopLevelHelp() {
5151
// Given
52-
String[] args = {"--help"};
52+
String[] args = { "--help" };
5353

5454
// When
5555
int exitCode = cmd.execute(args);
5656

5757
// Then
5858
assertThat(exitCode).isEqualTo(0);
5959
assertThat(outContent.toString())
60-
.contains("Usage: cm [-hV] [COMMAND]")
61-
.contains("Commands:")
62-
.contains("validate Validates a CML file.")
63-
.contains("generate Generates output from a CML file.");
60+
.contains("Usage: cm [-hV] [COMMAND]")
61+
.contains("Commands:")
62+
.contains("validate Validates a CML file.")
63+
.contains("generate Generates output from a CML file.");
6464
}
6565

6666
@Test
6767
@DisplayName("main() should print version when called with --version option")
6868
void main_WhenCalledWithVersionOption_ThenPrintsVersion() {
6969
// Given
70-
String[] args = {"--version"};
70+
String[] args = { "--version" };
7171

7272
// When
7373
int exitCode = cmd.execute(args);
@@ -81,23 +81,23 @@ void main_WhenCalledWithVersionOption_ThenPrintsVersion() {
8181
@DisplayName("main() should print error and usage when called with an invalid option")
8282
void main_WhenCalledWithInvalidOption_ThenPrintsErrorAndUsage() {
8383
// Given
84-
String[] args = {"--invalid-option"};
84+
String[] args = { "--invalid-option" };
8585

8686
// When
8787
int exitCode = cmd.execute(args);
8888

8989
// Then
9090
assertThat(exitCode).isNotEqualTo(0);
9191
assertThat(errContent.toString())
92-
.contains("Unknown option: '--invalid-option'")
93-
.contains("Usage: cm [-hV] [COMMAND]");
92+
.contains("Unknown option: '--invalid-option'")
93+
.contains("Usage: cm [-hV] [COMMAND]");
9494
}
9595

9696
@Test
9797
@DisplayName("main() should print error and usage when called with an invalid subcommand")
9898
void main_WhenCalledWithInvalidSubcommand_ThenPrintsErrorAndUsage() {
9999
// Given
100-
String[] args = {"invalid-command"};
100+
String[] args = { "invalid-command" };
101101

102102
// When
103103
int exitCode = cmd.execute(args);
@@ -106,22 +106,4 @@ void main_WhenCalledWithInvalidSubcommand_ThenPrintsErrorAndUsage() {
106106
assertThat(exitCode).isNotEqualTo(0);
107107
assertThat(errContent.toString()).contains("Unmatched argument at index 0: 'invalid-command'");
108108
}
109-
110-
@Test
111-
@DisplayName("runCLI() should return 0 and print help when called with --help option")
112-
void runCLI_WhenCalledWithHelpOption_ThenReturnsZeroAndPrintsHelp() {
113-
// Given
114-
String[] args = {"--help"};
115-
116-
// When
117-
int exitCode = ContextMapperCLI.runCLI(args);
118-
119-
// Then
120-
assertThat(exitCode).isEqualTo(0);
121-
assertThat(outContent.toString())
122-
.contains("Usage: cm [-hV] [COMMAND]")
123-
.contains("Commands:")
124-
.contains("validate Validates a CML file.")
125-
.contains("generate Generates output from a CML file.");
126-
}
127109
}

src/test/java/org/contextmapper/cli/VersionProviderTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ void getVersion_whenImplVersionIsPresent_returnsVersionString() throws Exception
3636

3737
// Assert
3838
assertThat(version).isNotNull()
39-
.hasSize(1)
40-
.containsExactly("Context Mapper CLI v" + expectedVersion);
39+
.hasSize(1)
40+
.containsExactly("Context Mapper CLI v" + expectedVersion);
4141
}
4242

4343
@Test
@@ -52,8 +52,8 @@ void getVersion_whenImplVersionIsNull_returnsDevelopmentVersionString() throws E
5252

5353
// Assert
5454
assertThat(version).isNotNull()
55-
.hasSize(1)
56-
.containsExactly("Context Mapper CLI DEVELOPMENT VERSION");
55+
.hasSize(1)
56+
.containsExactly("Context Mapper CLI DEVELOPMENT VERSION");
5757
}
5858

5959
@Test
@@ -66,7 +66,7 @@ void getVersion_whenPackageIsNull_returnsDevelopmentVersionString() throws Excep
6666

6767
// Assert
6868
assertThat(version).isNotNull()
69-
.hasSize(1)
70-
.containsExactly("Context Mapper CLI DEVELOPMENT VERSION");
69+
.hasSize(1)
70+
.containsExactly("Context Mapper CLI DEVELOPMENT VERSION");
7171
}
72-
}
72+
}

src/test/java/org/contextmapper/cli/commands/ContextMapperGeneratorTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class ContextMapperGeneratorTest {
1818
"PLANT_UML, plantuml",
1919
"GENERIC, generic"
2020
})
21-
void getName_WhenUsingGeneratorEnumValue_ThenCanGetGeneratorName(ContextMapperGenerator generator, String expectedName) {
21+
void getName_WhenUsingGeneratorEnumValue_ThenCanGetGeneratorName(ContextMapperGenerator generator,
22+
String expectedName) {
2223
// Given
2324
// generator and expectedName are provided by @CsvSource
2425

@@ -30,7 +31,7 @@ void getName_WhenUsingGeneratorEnumValue_ThenCanGetGeneratorName(ContextMapperGe
3031
}
3132

3233
@ParameterizedTest
33-
@ValueSource(strings = {"CONTEXT_MAP", "PLANT_UML", "GENERIC"})
34+
@ValueSource(strings = { "CONTEXT_MAP", "PLANT_UML", "GENERIC" })
3435
void getDescription_WhenUsingGeneratorEnumValue_ThenCanGetGeneratorDescription(final String enumValueAsString) {
3536
// Given
3637
final ContextMapperGenerator generator = ContextMapperGenerator.valueOf(enumValueAsString);
@@ -62,7 +63,7 @@ void toString_ReturnsName(ContextMapperGenerator generator, String expectedName)
6263
}
6364

6465
@ParameterizedTest
65-
@ValueSource(strings = {"context-map", "plantuml", "generic", "CONTEXT-MAP", "PlantUML", "GeNeRiC"})
66+
@ValueSource(strings = { "context-map", "plantuml", "generic", "CONTEXT-MAP", "PlantUML", "GeNeRiC" })
6667
void byName_WhenWithValidName_ThenReturnGenerator(final String validGeneratorKey) {
6768
// Given
6869
// validGeneratorKey is provided by @ValueSource
@@ -80,8 +81,7 @@ void byName_WhenWithoutName_ThenThrowIllegalArgumentException() {
8081
// No specific setup needed
8182

8283
// When & Then
83-
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() ->
84-
ContextMapperGenerator.byName(null))
84+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> ContextMapperGenerator.byName(null))
8585
.withMessageContaining("Please provide a name for the generator.");
8686
}
8787

@@ -91,8 +91,7 @@ void byName_WhenWithEmptyName_ThenThrowIllegalArgumentException() {
9191
// No specific setup needed
9292

9393
// When & Then
94-
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() ->
95-
ContextMapperGenerator.byName(""))
94+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> ContextMapperGenerator.byName(""))
9695
.withMessageContaining("Please provide a name for the generator.");
9796
}
9897

@@ -102,13 +101,14 @@ void byName_WhenWithInvalidName_ThenThrowIllegalArgumentException() {
102101
// No specific setup needed
103102

104103
// When & Then
105-
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() ->
106-
ContextMapperGenerator.byName("just a string"))
107-
.withMessageContaining("No generator found for the name 'just a string'. Valid values are: context-map, plantuml, generic");
104+
assertThatExceptionOfType(IllegalArgumentException.class)
105+
.isThrownBy(() -> ContextMapperGenerator.byName("just a string"))
106+
.withMessageContaining(
107+
"No generator found for the name 'just a string'. Valid values are: context-map, plantuml, generic");
108108
}
109109

110110
@ParameterizedTest
111-
@ValueSource(strings = {"CONTEXT_MAP", "PLANT_UML", "GENERIC"})
111+
@ValueSource(strings = { "CONTEXT_MAP", "PLANT_UML", "GENERIC" })
112112
void getGenerator_WhenCalled_ThenReturnGeneratorImplementation(final String enumValueAsString) {
113113
// Given
114114
final ContextMapperGenerator generator = ContextMapperGenerator.valueOf(enumValueAsString);
@@ -133,7 +133,7 @@ void getDisplayName_ReturnsCorrectFormat(ContextMapperGenerator generator, Strin
133133

134134
// When
135135
String displayName = generator.getDisplayName();
136-
136+
137137
// Then
138138
assertThat(displayName).isEqualTo(expectedDisplayName);
139139
}

0 commit comments

Comments
 (0)