FieldNameValidator { @SuppressWarnings("Slf4jLogsafeArgs") public static void validate(FieldName fieldName) { Preconditions.checkArgument( CaseConverter.CAMEL_CASE_PATTERN.matcher(fieldName.get()).matches() || CaseConverter.KEBAB_CASE_PATTERN .matcher(fieldName.get()) .matches() || CaseConverter.SNAKE_CASE_PATTERN .matcher(fieldName.get()) .matches(), "FieldName \"%s\" must follow one of the following patterns: %s", fieldName, Arrays.toString(CaseConverter.Case.values())); if (!CaseConverter.CAMEL_CASE_PATTERN.matcher(fieldName.get()).matches()) { log.warn( "{} should be specified in lowerCamelCase. kebab-case and snake_case are supported for " + "legacy endpoints only: {}", FieldName.class, fieldName.get()); } } private  FieldNameValidator(); static String capitalize(FieldName fieldName); static FieldName toCase(FieldName fieldName, CaseConverter.Case targetCase); @SuppressWarnings("Slf4jLogsafeArgs") static void validate(FieldName fieldName);  }
@Test public void testInvalidNames() throws Exception { for (String invalid : new String[] { "UpperCamelCase", "Upper-Kebab-Case", "Upper_Snake_Case", "ABC", "$special", "special%", "snake_123_case", "kebab-123-case", "mixed-kebab_snake-case", "defaultDNSName" }) { assertThatThrownBy(() -> FieldNameValidator.validate(FieldName.of(invalid))) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining( String.format("FieldName \"%s\" must follow one of the following patterns", invalid)); } }
@Test public void testValidNames() { FieldNameValidator.validate(FieldName.of("camel")); FieldNameValidator.validate(FieldName.of("camelCase")); FieldNameValidator.validate(FieldName.of("camelCase1")); FieldNameValidator.validate(FieldName.of("camel1Case2")); FieldNameValidator.validate(FieldName.of("kebab-case")); FieldNameValidator.validate(FieldName.of("kebab1-case123")); FieldNameValidator.validate(FieldName.of("snake_case")); FieldNameValidator.validate(FieldName.of("snake1_case123")); FieldNameValidator.validate(FieldName.of("xCoordinate")); FieldNameValidator.validate(FieldName.of("defaultXPosition")); FieldNameValidator.validate(FieldName.of("defaultX")); }