Skip to content

Commit be30ab2

Browse files
committed
Improving a bit the coverage
1 parent 6795d78 commit be30ab2

10 files changed

Lines changed: 172 additions & 88 deletions

File tree

.sdkmanrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java=11.0.27-tem

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ The following examples illustrate the CLI usage.
8989
If you want to contribute to this project you can create a fork and a pull request. The project is built with Gradle, so you can import it as Gradle project within Eclipse or IntelliJ IDEA (or any other IDE supporting Gradle).
9090
9191
```bash
92+
sdk env install
9293
./gradlew clean build
94+
./gradlew clean build jacocoTestReport
95+
jwebserver -p 9000 -d "$(pwd)/build/reports/"
9396
./gradlew clean build snapshot
9497
java -jar build/libs/context-mapper-cli-0.1.0-SNAPSHOT.jar
9598
```

build.gradle

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ plugins {
77
id 'nebula.release' version '19.0.10'
88
}
99

10-
group 'org.contextmapper'
10+
group = 'org.contextmapper'
1111

12-
sourceCompatibility = '11'
13-
targetCompatibility = '11'
12+
java {
13+
sourceCompatibility = JavaVersion.VERSION_11
14+
targetCompatibility = JavaVersion.VERSION_11
15+
}
1416

1517
repositories {
1618
mavenCentral()
@@ -25,16 +27,13 @@ dependencies {
2527
testImplementation "org.assertj:assertj-core:${assertJVersion}"
2628
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
2729
testImplementation "org.mockito:mockito-junit-jupiter:${mockitoVersion}"
28-
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${jUnitVersion}"
2930
}
3031

3132
application {
32-
mainClassName = 'org.contextmapper.cli.ContextMapperCLI'
33-
applicationName = 'context-mapper-cli'
34-
}
35-
startScripts {
33+
mainClass = 'org.contextmapper.cli.ContextMapperCLI'
3634
applicationName = 'cm'
3735
}
36+
3837
jar {
3938
manifest {
4039
attributes (
@@ -56,14 +55,12 @@ if (!project.hasProperty('signing.secretKeyRingFile')) {
5655
}
5756

5857
test {
59-
useJUnitPlatform()
60-
6158
testLogging {
62-
showExceptions true
63-
exceptionFormat "full"
59+
showExceptions = true
60+
exceptionFormat = "full"
6461

65-
showCauses true
66-
showStackTraces true
62+
showCauses = true
63+
showStackTraces = true
6764
}
6865
}
6966

@@ -152,14 +149,22 @@ signing {
152149
sign(publishing.publications)
153150
}
154151

152+
testing {
153+
suites {
154+
test(JvmTestSuite) {
155+
useJUnitJupiter(jUnitVersion)
156+
}
157+
}
158+
}
159+
155160
tasks.withType(GenerateModuleMetadata) {
156161
enabled = false
157162
}
158163

159-
tasks.withType(CreateStartScripts).each { task ->
160-
task.doLast {
161-
String text = task.windowsScript.text
164+
tasks.withType(CreateStartScripts).configureEach {
165+
doLast {
166+
String text = windowsScript.text
162167
text = text.replaceFirst(/(set CLASSPATH=%APP_HOME%\\lib\\).*/, { "${it[1]}*" })
163-
task.windowsScript.write text
168+
windowsScript.text = text
164169
}
165170
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ossSnapshotRepository=https://oss.sonatype.org/content/repositories/snapshots/
33
ossReleaseStagingRepository=https://oss.sonatype.org/service/local/staging/deploy/maven2/
44

55
# dependency versions
6-
jUnitVersion=5.9.1
6+
jUnitVersion=5.12.2
77
assertJVersion=3.27.3
88
mockitoVersion=3.9.0
99

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,50 @@
1-
/*
2-
* Copyright 2021 The Context Mapper Project Team
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package org.contextmapper.cli;
172

183
import org.contextmapper.cli.commands.GenerateCommand;
194
import org.contextmapper.cli.commands.ValidateCommand;
205
import picocli.CommandLine;
216
import picocli.CommandLine.Command;
7+
import java.util.function.Supplier;
228

23-
@Command(name = "cm", mixinStandardHelpOptions = true, versionProvider = VersionProvider.class,
24-
description = "Context Mapper CLI",
25-
subcommands = {
26-
ValidateCommand.class,
27-
GenerateCommand.class
28-
})
9+
@Command(
10+
name = "cm",
11+
versionProvider = VersionProvider.class,
12+
description = "Context Mapper CLI",
13+
subcommands = {
14+
ValidateCommand.class,
15+
GenerateCommand.class
16+
},
17+
mixinStandardHelpOptions = true,
18+
usageHelpAutoWidth = true)
2919
public class ContextMapperCLI implements Runnable {
3020

3121
private static final int REQUIRED_JAVA_VERSION = 11;
32-
33-
public static void main(String[] args) {
34-
if (Runtime.version().feature() < REQUIRED_JAVA_VERSION) {
35-
System.err.printf("Invalid Java version '%s' (>=%s is required).%n", Runtime.version().feature(), REQUIRED_JAVA_VERSION);
36-
System.exit(1);
37-
}
38-
int exitCode = new CommandLine(new ContextMapperCLI()).execute(args);
39-
System.exit(exitCode);
40-
}
22+
static Supplier<Integer> javaVersionSupplier = () -> Runtime.version().feature();
4123

4224
@Override
4325
public void run() {
44-
// This is executed if no subcommand is specified.
45-
// Picocli will show the help message by default if mixinStandardHelpOptions = true and no subcommand is given.
46-
// We can add a custom message here if needed, or rely on Picocli's default behavior.
4726
System.out.println("Context Mapper CLI. Use 'cm --help' for usage information.");
4827
}
4928

50-
}
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+
}
5136

52-
class VersionProvider implements CommandLine.IVersionProvider {
53-
@Override
54-
public String[] getVersion() throws Exception {
55-
String implVersion = ContextMapperCLI.class.getPackage().getImplementationVersion();
56-
return new String[]{"Context Mapper CLI " + (implVersion != null ? "v" + implVersion : "DEVELOPMENT VERSION")};
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+
45+
public static void main(String[] args) {
46+
checkJavaVersion();
47+
int exitCode = runCLI(args);
48+
System.exit(exitCode);
5749
}
5850
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.contextmapper.cli;
2+
3+
import java.util.Objects;
4+
5+
import picocli.CommandLine.IVersionProvider;
6+
7+
class VersionProvider implements IVersionProvider {
8+
9+
@Override
10+
public String[] getVersion() throws Exception {
11+
Package programPackage = getPackageToInspect();
12+
String implVersion = null;
13+
if (Objects.nonNull(programPackage)) {
14+
implVersion = programPackage.getImplementationVersion();
15+
}
16+
return new String[]{"Context Mapper CLI " + (implVersion != null ? "v" + implVersion : "DEVELOPMENT VERSION")};
17+
}
18+
19+
//Refactored to help the testing process
20+
protected Package getPackageToInspect() {
21+
return ContextMapperCLI.class.getPackage();
22+
}
23+
}

src/main/java/org/contextmapper/cli/commands/GenerateCommand.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
/*
2-
* Copyright 2021 The Context Mapper Project Team
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package org.contextmapper.cli.commands;
172

183
import org.contextmapper.dsl.cml.CMLResource;

src/main/java/org/contextmapper/cli/commands/ValidateCommand.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
/*
2-
* Copyright 2021 The Context Mapper Project Team
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package org.contextmapper.cli.commands;
172

183
import org.contextmapper.dsl.cml.CMLResource;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,22 @@ 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+
}
109127
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.contextmapper.cli;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
import org.mockito.Mockito;
6+
import org.mockito.junit.jupiter.MockitoExtension;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
@ExtendWith(MockitoExtension.class)
11+
class VersionProviderTest {
12+
13+
static class TestableVersionProvider extends VersionProvider {
14+
private final Package mockPackage;
15+
16+
TestableVersionProvider(Package mockPackage) {
17+
this.mockPackage = mockPackage;
18+
}
19+
20+
@Override
21+
protected Package getPackageToInspect() {
22+
return mockPackage;
23+
}
24+
}
25+
26+
@Test
27+
void getVersion_whenImplVersionIsPresent_returnsVersionString() throws Exception {
28+
// Arrange
29+
String expectedVersion = "1.2.3";
30+
Package mockPackage = Mockito.mock(Package.class);
31+
Mockito.when(mockPackage.getImplementationVersion()).thenReturn(expectedVersion);
32+
VersionProvider versionProvider = new TestableVersionProvider(mockPackage);
33+
34+
// Act
35+
String[] version = versionProvider.getVersion();
36+
37+
// Assert
38+
assertThat(version).isNotNull()
39+
.hasSize(1)
40+
.containsExactly("Context Mapper CLI v" + expectedVersion);
41+
}
42+
43+
@Test
44+
void getVersion_whenImplVersionIsNull_returnsDevelopmentVersionString() throws Exception {
45+
// Arrange
46+
Package mockPackage = Mockito.mock(Package.class);
47+
Mockito.when(mockPackage.getImplementationVersion()).thenReturn(null);
48+
VersionProvider versionProvider = new TestableVersionProvider(mockPackage);
49+
50+
// Act
51+
String[] version = versionProvider.getVersion();
52+
53+
// Assert
54+
assertThat(version).isNotNull()
55+
.hasSize(1)
56+
.containsExactly("Context Mapper CLI DEVELOPMENT VERSION");
57+
}
58+
59+
@Test
60+
void getVersion_whenPackageIsNull_returnsDevelopmentVersionString() throws Exception {
61+
// Arrange
62+
VersionProvider versionProvider = new TestableVersionProvider(null);
63+
64+
// Act
65+
String[] version = versionProvider.getVersion();
66+
67+
// Assert
68+
assertThat(version).isNotNull()
69+
.hasSize(1)
70+
.containsExactly("Context Mapper CLI DEVELOPMENT VERSION");
71+
}
72+
}

0 commit comments

Comments
 (0)