-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6257 from pferraro/WFCORE-7065
WFCORE-7065 Migrate JBoss Modules module identifier validator from org.wildfly:wildfly-clustering-common
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...ller/src/main/java/org/jboss/as/controller/operations/validation/ModuleNameValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.jboss.as.controller.operations.validation; | ||
|
||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jboss.as.controller.OperationFailedException; | ||
import org.jboss.as.controller.logging.ControllerLogger; | ||
import org.jboss.dmr.ModelNode; | ||
import org.jboss.dmr.ModelType; | ||
|
||
/** | ||
* Validates that a given parameter is a syntactically valid module name within JBoss Modules. | ||
* N.B. This does not validate that the module actually exists, i.e. can be loaded. | ||
*/ | ||
public class ModuleNameValidator extends ModelTypeValidator { | ||
public static final ParameterValidator INSTANCE = new ModuleNameValidator(); | ||
// Ensure module name is valid with filesystem module repository, permitting deprecated slot, if present | ||
private static final Predicate<String> MODULE_NAME_TESTER = Pattern.compile("(?:^\\w+|\\w+\\.\\w+|\\w+\\Q\\:\\E\\w+)+(?:\\:(?:\\w+|\\w+\\.\\w+))?$").asMatchPredicate(); | ||
|
||
private ModuleNameValidator() { | ||
super(ModelType.STRING); | ||
} | ||
|
||
@Override | ||
public void validateParameter(String parameterName, ModelNode value) throws OperationFailedException { | ||
super.validateParameter(parameterName, value); | ||
if (value.isDefined()) { | ||
String moduleName = value.asString(); | ||
if (!MODULE_NAME_TESTER.test(moduleName)) { | ||
throw ControllerLogger.MGMT_OP_LOGGER.invalidModuleNameParameter(parameterName, moduleName); | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...c/test/java/org/jboss/as/controller/operation/validation/ModuleNameValidatorTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.jboss.as.controller.operation.validation; | ||
|
||
import org.jboss.as.controller.OperationFailedException; | ||
import org.jboss.as.controller.operations.validation.ModuleNameValidator; | ||
import org.jboss.as.controller.operations.validation.ParameterValidator; | ||
import org.jboss.dmr.ModelNode; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Unit test for ModuleNameValidator | ||
*/ | ||
public class ModuleNameValidatorTestCase { | ||
|
||
@Test | ||
public void test() throws OperationFailedException { | ||
ParameterValidator validator = ModuleNameValidator.INSTANCE; | ||
|
||
validator.validateParameter("valid", new ModelNode("org.jboss.modules")); | ||
validator.validateParameter("valid", new ModelNode("org.jboss.modules:main")); | ||
validator.validateParameter("valid", new ModelNode("org.jboss.modules:1.9")); | ||
validator.validateParameter("escaped", new ModelNode("org.jboss.modules.foo\\:bar:main")); | ||
|
||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode(".foo.bar"))); | ||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo..bar"))); | ||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo.bar."))); | ||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo.bar:"))); | ||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo:bar:main"))); | ||
} | ||
} |