public final class ComponentKeys {
- public static final int COMPONENT_KEY_SIZE = 400;
+ public static final int MAX_COMPONENT_KEY_LENGTH = 400;
/*
* Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit
String key = resource.getKey();
if (!StringUtils.equals(Scopes.PROJECT, resource.getScope())) {
// not a project nor a library
- key = new StringBuilder(COMPONENT_KEY_SIZE)
+ key = new StringBuilder(MAX_COMPONENT_KEY_LENGTH)
.append(project.getKey())
.append(':')
.append(resource.getKey())
}
public static String createEffectiveKey(String moduleKey, @Nullable String path) {
- StringBuilder sb = new StringBuilder(COMPONENT_KEY_SIZE);
+ StringBuilder sb = new StringBuilder(MAX_COMPONENT_KEY_LENGTH);
sb.append(moduleKey);
if (path != null) {
sb.append(':').append(path);
package org.sonar.db.component;
import static com.google.common.base.Preconditions.checkArgument;
+import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH;
public class ComponentValidator {
- private static final int MAX_NAME_LENGTH = 2000;
- private static final int MAX_KEY_LENGTH = 400;
- private static final int MAX_QUALIFIER_LENGTH = 10;
+ private static final int MAX_COMPONENT_NAME_LENGTH = 2000;
+ private static final int MAX_COMPONENT_QUALIFIER_LENGTH = 10;
private ComponentValidator() {
// prevent instantiation
}
public static String checkComponentName(String name) {
- checkArgument(name.length() <= MAX_NAME_LENGTH, "Component name length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
- name.length(), MAX_NAME_LENGTH, name);
+ checkArgument(name.length() <= MAX_COMPONENT_NAME_LENGTH, "Component name length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
+ name.length(), MAX_COMPONENT_NAME_LENGTH, name);
return name;
}
public static String checkComponentKey(String key) {
- checkArgument(isComponentKeyValid(key), "Component key length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
- key.length(), MAX_KEY_LENGTH, key);
+ checkArgument(key.length() <= MAX_COMPONENT_KEY_LENGTH, "Component key length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
+ key.length(), MAX_COMPONENT_KEY_LENGTH, key);
return key;
}
- public static boolean isComponentKeyValid(String key) {
- return key.length() <= MAX_KEY_LENGTH;
- }
-
public static String checkComponentQualifier(String qualifier) {
- checkArgument(qualifier.length() <= MAX_QUALIFIER_LENGTH, "Component qualifier length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
- qualifier.length(), MAX_QUALIFIER_LENGTH, qualifier);
+ checkArgument(qualifier.length() <= MAX_COMPONENT_QUALIFIER_LENGTH, "Component qualifier length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
+ qualifier.length(), MAX_COMPONENT_QUALIFIER_LENGTH, qualifier);
return qualifier;
}
}
public void check_key() {
String key = repeat("a", 400);
- assertThat(ComponentValidator.isComponentKeyValid(key)).isTrue();
assertThat(ComponentValidator.checkComponentKey(key)).isEqualTo(key);
}
expectedException.expectMessage("Component key length");
String key = repeat("a", 400 + 1);
- assertThat(ComponentValidator.isComponentKeyValid(key)).isFalse();
ComponentValidator.checkComponentKey(key);
}