diff --git a/src/main/java/rife/validation/ValidityChecks.java b/src/main/java/rife/validation/ValidityChecks.java index 31fe116..7680c0d 100644 --- a/src/main/java/rife/validation/ValidityChecks.java +++ b/src/main/java/rife/validation/ValidityChecks.java @@ -512,21 +512,26 @@ public static boolean checkJavaPackage(Object value) { return false; } - if (!(value instanceof String name)) { - return false; - } - - if (name.startsWith(".") || name.endsWith(".")) { + if (!(value instanceof String name) || name.isBlank() || name.startsWith(".") || name.endsWith(".")) { return false; } var identifier = true; var it = new StringCharacterIterator(name); + var first = true; for (var c = it.first(); c != CharacterIterator.DONE; c = it.next()) { - if (!Character.isJavaIdentifierPart(c) && c != '.') { - identifier = false; - break; + if ((first && !Character.isJavaIdentifierStart(c)) || + (!first && !Character.isJavaIdentifierPart(c))) { + if (c == '.') { + first = true; + continue; + } + else { + identifier = false; + break; + } } + first = false; } return identifier; @@ -537,17 +542,20 @@ public static boolean checkJavaIdentifier(Object value) { return false; } - if (!(value instanceof String name)) { + if (!(value instanceof String name) || name.isBlank()) { return false; } var identifier = true; + var first = true; var it = new StringCharacterIterator(name); for (var c = it.first(); c != CharacterIterator.DONE; c = it.next()) { - if (!Character.isJavaIdentifierPart(c)) { + if ((first && !Character.isJavaIdentifierStart(c)) || + (!first && !Character.isJavaIdentifierPart(c))) { identifier = false; break; } + first = false; } return identifier; diff --git a/src/test/java/rife/validation/TestValidityChecks.java b/src/test/java/rife/validation/TestValidityChecks.java index 9c5916c..1f97712 100644 --- a/src/test/java/rife/validation/TestValidityChecks.java +++ b/src/test/java/rife/validation/TestValidityChecks.java @@ -501,4 +501,34 @@ void testCheckFormat() { assertFalse(ValidityChecks.checkFormat("31/02/2004", RifeConfig.tools().getSimpleDateFormat("dd/MM/yyyy"))); assertFalse(ValidityChecks.checkFormat("testing", RifeConfig.tools().getSimpleDateFormat("dd/MM/yyyy"))); } + + @Test + void testJavaPackage() { + assertFalse(ValidityChecks.checkJavaPackage(null)); + assertFalse(ValidityChecks.checkJavaPackage(new Object())); + assertFalse(ValidityChecks.checkJavaPackage("")); + assertFalse(ValidityChecks.checkJavaPackage(".")); + assertFalse(ValidityChecks.checkJavaPackage("a.")); + assertFalse(ValidityChecks.checkJavaPackage(".a")); + assertFalse(ValidityChecks.checkJavaPackage("a.b.")); + assertFalse(ValidityChecks.checkJavaPackage(".a.b")); + assertFalse(ValidityChecks.checkJavaPackage("1.a.b")); + assertFalse(ValidityChecks.checkJavaPackage("a.1b")); + assertTrue(ValidityChecks.checkJavaPackage("a")); + assertTrue(ValidityChecks.checkJavaPackage("a.b")); + assertTrue(ValidityChecks.checkJavaPackage("com.A1.bB")); + } + + @Test + void testJavaIdentifier() { + assertFalse(ValidityChecks.checkJavaIdentifier(null)); + assertFalse(ValidityChecks.checkJavaIdentifier(new Object())); + assertFalse(ValidityChecks.checkJavaIdentifier("")); + assertFalse(ValidityChecks.checkJavaIdentifier(".")); + assertFalse(ValidityChecks.checkJavaIdentifier("1")); + assertFalse(ValidityChecks.checkJavaIdentifier("1b")); + assertTrue(ValidityChecks.checkJavaIdentifier("a")); + assertTrue(ValidityChecks.checkJavaIdentifier("A1")); + assertTrue(ValidityChecks.checkJavaIdentifier("bB")); + } }