GUACAMOLE-579: Change fromAttribute to canonicalize

This commit is contained in:
Virtually Nick
2019-06-21 12:54:42 -04:00
parent d8db630dbd
commit a6601a2bfd
5 changed files with 41 additions and 74 deletions

View File

@@ -28,13 +28,13 @@ import java.util.regex.Pattern;
public abstract class TokenName {
/**
* Pattern which matches logical groupings of words within an
* attribute name. This pattern is intended to match logical groupings
* Pattern which matches logical groupings of words within a
* string. This pattern is intended to match logical groupings
* regardless of the naming convention used: "CamelCase",
* "headlessCamelCase", "lowercase_with_underscores",
* "lowercase-with-dashes" or even "aVery-INCONSISTENTMix_ofAllStyles".
*/
private static final Pattern ATTRIBUTE_NAME_GROUPING = Pattern.compile(
private static final Pattern STRING_NAME_GROUPING = Pattern.compile(
// "Camel" word groups
"\\p{javaUpperCase}\\p{javaLowerCase}+"
@@ -61,29 +61,29 @@ public abstract class TokenName {
/**
* Generates the name of the parameter token that should be populated with
* the value of the given attribute. The name of the attribute will
* automatically be transformed from "CamelCase", "headlessCamelCase",
* "lowercase_with_underscores", and "mixes_ofBoth_Styles" to consistent
* "UPPERCASE_WITH_UNDERSCORES". Each returned attribute will be prefixed
* with value provided in the prefix. The value provided in prefix will
* be prepended to the attribute name, but will itself not be transformed.
* the given string. The provided string will be automatically transformed
* from "CamelCase", "headlessCamelCase", "lowercase_with_underscores",
* and "mixes_ofBoth_Styles" to consistent "UPPERCASE_WITH_UNDERSCORES".
* Each returned attribute will be prefixed with the string value provided
* in the prefix. The value provided in prefix will be prepended to the
* attribute name, but will itself not be transformed.
*
* @param name
* The name of the attribute to use to generate the token name.
* The string to be used to generate the token name.
*
* @param prefix
* The prefix to prepend to the generated token name.
*
* @return
* The name of the parameter token that should be populated with the
* value of the attribute having the given name.
* given string.
*/
public static String fromAttribute(final String name, final String prefix) {
public static String canonicalize(final String name, final String prefix) {
// If even one logical word grouping cannot be found, default to
// simply converting the attribute to uppercase and adding the
// simply converting the string to uppercase and adding the
// prefix
Matcher groupMatcher = ATTRIBUTE_NAME_GROUPING.matcher(name);
Matcher groupMatcher = STRING_NAME_GROUPING.matcher(name);
if (!groupMatcher.find())
return prefix + name.toUpperCase();
@@ -102,19 +102,19 @@ public abstract class TokenName {
}
/**
* Generate the name of a parameter from the value of the given attribute,
* and with a blank prefix such that the token name will simply be the
* transformed version of the attribute name.
* Generate the name of a parameter from the given string, and with a blank
* prefix such that the token name will simply be the transformed version
* of the string.
*
* @param name
* The name of the attribute to use to generate the token name.
* The string to use to generate the token name.
*
* @return
* The name of the parameter token that should be populated with the
* value of the attribute having the given name.
* given string.
*/
public static String fromAttribute(final String name) {
return fromAttribute(name, "");
public static String canonicalize(final String name) {
return canonicalize(name, "");
}
}

View File

@@ -29,28 +29,28 @@ import org.junit.Test;
public class TokenNameTest {
/**
* Verifies that TokenName.fromAttribute() generates token names as
* Verifies that TokenName.canonicalize() generates token names as
* specified, regardless of the naming convention of the attribute.
*/
@Test
public void testFromAttribute() {
assertEquals("A", TokenName.fromAttribute("a"));
assertEquals("B", TokenName.fromAttribute("b"));
assertEquals("1", TokenName.fromAttribute("1"));
assertEquals("SOME_URL", TokenName.fromAttribute("someURL"));
assertEquals("LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes"));
assertEquals("HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase"));
assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase"));
assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase"));
assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores"));
assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES"));
assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles"));
assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("abc123def456"));
assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("ABC123DEF456"));
assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord"));
public void testCanonicalize() {
assertEquals("A", TokenName.canonicalize("a"));
assertEquals("B", TokenName.canonicalize("b"));
assertEquals("1", TokenName.canonicalize("1"));
assertEquals("SOME_URL", TokenName.canonicalize("someURL"));
assertEquals("LOWERCASE_WITH_DASHES", TokenName.canonicalize("lowercase-with-dashes"));
assertEquals("HEADLESS_CAMEL_CASE", TokenName.canonicalize("headlessCamelCase"));
assertEquals("CAMEL_CASE", TokenName.canonicalize("CamelCase"));
assertEquals("CAMEL_CASE", TokenName.canonicalize("CamelCase"));
assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.canonicalize("lowercase_with_underscores"));
assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.canonicalize("UPPERCASE_WITH_UNDERSCORES"));
assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.canonicalize("aVery-INCONSISTENTMix_ofAllStyles"));
assertEquals("ABC_123_DEF_456", TokenName.canonicalize("abc123def456"));
assertEquals("ABC_123_DEF_456", TokenName.canonicalize("ABC123DEF456"));
assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.canonicalize("WordAWordABWordABCWord"));
assertEquals("AUTH_ATTRIBUTE", TokenName.fromAttribute("Attribute", "AUTH_"));
assertEquals("auth_SOMETHING", TokenName.fromAttribute("Something", "auth_"));
assertEquals("AUTH_ATTRIBUTE", TokenName.canonicalize("Attribute", "AUTH_"));
assertEquals("auth_SOMETHING", TokenName.canonicalize("Something", "auth_"));
}
}