Skip to content

Commit

Permalink
Improved validity checks for Java package and Java identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
gbevin committed Dec 27, 2024
1 parent bf35e98 commit 4f6c294
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/main/java/rife/validation/ValidityChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/rife/validation/TestValidityChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 4f6c294

Please sign in to comment.