Skip to content

Commit ba0e347

Browse files
committed
Validation coordinates and module names
Resolves #46
1 parent d164473 commit ba0e347

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Version 1.4
44
* [New] Minimal Gradle version is now 6.8 for integration with recently added features like the Dependency Version Catalog
5+
* [New] [#46](https://github.com/gradlex-org/extra-java-module-info/issues/46) - Validation coordinates and module names
56
* [New] [#41](https://github.com/gradlex-org/extra-java-module-info/issues/41) - Support version catalog accessors to express dependency coordinates (Thanks [Giuseppe Barbieri](https://github.com/elect86) for suggesting!)
67
* [Fixed] [#47](https://github.com/gradlex-org/extra-java-module-info/issues/47) - requireAllDefinedDependencies() gives error when dependency only appears on runtime path (Thanks [Sola](https://github.com/unlimitedsola) for reporting!)
78
* [Fixed] [#45](https://github.com/gradlex-org/extra-java-module-info/issues/45) - Sub-folders in 'META-INF/services' are not ignored (Thanks [Jonas Beyer](https://github.com/j-beyer) for reporting!)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.gradlex.javamodule.moduleinfo;
2+
3+
class IdValidator {
4+
static private final String COORDINATES_PATTERN = "^[a-zA-Z0-9._-]+:[a-zA-Z0-9._-]+$";
5+
static private final String FILE_NAME_PATTERN = "^[a-zA-Z0-9._-]+\\.(jar|zip)$";
6+
static private final String MODULE_NAME_PATTERN = "^[a-z][a-z0-9_]*(\\.[a-z0-9_]+)*$";
7+
8+
static void validateIdentifier(String identifier) {
9+
if (!identifier.matches(COORDINATES_PATTERN) && !identifier.matches(FILE_NAME_PATTERN)) {
10+
throw new RuntimeException("'" + identifier + "' are not valid coordinates (group:name) / is not a valid file name (name-1.0.jar)");
11+
}
12+
}
13+
14+
static void validateModuleName(String moduleName) {
15+
if (!moduleName.matches(MODULE_NAME_PATTERN)) {
16+
throw new RuntimeException("'" + moduleName + "' is not a valid Java Module name");
17+
}
18+
}
19+
}

src/main/java/org/gradlex/javamodule/moduleinfo/ModuleSpec.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@
2323
import java.util.ArrayList;
2424
import java.util.List;
2525

26+
import static org.gradlex.javamodule.moduleinfo.IdValidator.validateIdentifier;
27+
import static org.gradlex.javamodule.moduleinfo.IdValidator.validateModuleName;
28+
2629
/**
2730
* Details that real Modules and Automatic-Module-Names share.
2831
*/
2932
@SuppressWarnings("unused")
30-
abstract public class ModuleSpec implements Serializable {
33+
public abstract class ModuleSpec implements Serializable {
3134

3235
private final String identifier;
3336
private final String moduleName;
3437
private final List<String> mergedJars = new ArrayList<>();
3538

36-
ModuleSpec(String identifier, String moduleName) {
39+
protected ModuleSpec(String identifier, String moduleName) {
40+
validateIdentifier(identifier);
41+
validateModuleName(moduleName);
3742
this.identifier = identifier;
3843
this.moduleName = moduleName;
3944
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.gradlex.javamodule.moduleinfo.test
2+
3+
import org.gradlex.javamodule.moduleinfo.test.fixture.GradleBuild
4+
import org.gradlex.javamodule.moduleinfo.test.fixture.LegacyLibraries
5+
import spock.lang.Specification
6+
7+
class IdValidationFunctionalTest extends Specification {
8+
9+
@Delegate
10+
GradleBuild build = new GradleBuild()
11+
12+
LegacyLibraries libs = new LegacyLibraries(false)
13+
14+
def setup() {
15+
settingsFile << 'rootProject.name = "test-project"'
16+
buildFile << '''
17+
plugins {
18+
id("application")
19+
id("org.gradlex.extra-java-module-info")
20+
}
21+
'''
22+
}
23+
24+
def "fails for wrong coordinates"() {
25+
given:
26+
buildFile << """
27+
extraJavaModuleInfo {
28+
module("commons-logging:commons-logging:2.0", "apache.commons.logging") {
29+
exportAllPackages()
30+
}
31+
}
32+
"""
33+
34+
expect:
35+
def out = fail()
36+
out.output.contains("'commons-logging:commons-logging:2.0' are not valid coordinates (group:name) / is not a valid file name (name-1.0.jar)")
37+
}
38+
39+
def "fails for wrong file name"() {
40+
given:
41+
buildFile << """
42+
dependencies {
43+
implementation(${libs.commonsCollections})
44+
}
45+
46+
extraJavaModuleInfo {
47+
module("/dummy/some/my.jar", "apache.commons.logging")
48+
}
49+
"""
50+
51+
expect:
52+
def out = fail()
53+
out.output.contains("'/dummy/some/my.jar' are not valid coordinates (group:name) / is not a valid file name (name-1.0.jar)")
54+
}
55+
56+
def "fails for wrong module name"() {
57+
given:
58+
buildFile << """
59+
dependencies {
60+
implementation(${libs.commonsCollections})
61+
}
62+
63+
extraJavaModuleInfo {
64+
module("commons-logging:commons-logging", "apache.commons:logging")
65+
}
66+
"""
67+
68+
expect:
69+
def out = fail()
70+
out.output.contains("'apache.commons:logging' is not a valid Java Module name")
71+
}
72+
73+
}

0 commit comments

Comments
 (0)