Skip to content

Commit

Permalink
Eliminate SonarQube warnings (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert authored Jan 22, 2024
1 parent cb0b964 commit 265bb47
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private ContextPropertiesBuilder() {
* @param variableStringResolver Variable string resolver
* @return Context variables map
*/
@SuppressWarnings("java:S3776") // ignore complexity
public static Map<String, Object> buildEnvironmentContextVariables(String environmentName,
Environment environment, String version,
VariableObjectTreeResolver variableObjectTreeResolver, VariableStringResolver variableStringResolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ private void generateNode(Node node) {
mergedConfig.putAll(ContextPropertiesBuilder.buildCurrentContextVariables(node, nodeRole));

// collect role and tenant information for export model
ExportNodeRoleData exportNodeRoleData = exportModelGenerator.addRole(roleName, variants, mergedConfig,
role.getSensitiveConfigParameters());
ExportNodeRoleData exportNodeRoleData = exportModelGenerator.addRole(roleName, variants, mergedConfig);

// generate files
List<GeneratedFileContext> allFiles = new ArrayList<>();
Expand Down Expand Up @@ -319,6 +318,7 @@ private String getEscapingStrategy(RoleFile roleFile) {
.getName();
}

@SuppressWarnings("java:S107") // allow many parameters
private void multiplyFiles(Role role, RoleFile roleFile, Map<String, Object> config, File nodeDir, Template template,
String roleName, List<String> roleVariantNames, String templateName, List<GeneratedFileContext> generatedFiles) {
MultiplyPlugin multiplyPlugin = defaultMultiplyPlugin;
Expand Down Expand Up @@ -365,7 +365,10 @@ private void multiplyFiles(Role role, RoleFile roleFile, Map<String, Object> con
}
}

@SuppressWarnings("PMD.PreserveStackTrace")
@SuppressWarnings({
"PMD.PreserveStackTrace",
"java:S107" // allow many parameters
})
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
private Collection<GeneratedFileContext> generateFile(RoleFile roleFile, String dir,
String fileName, String url, String symlinkTarget,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ class FileGenerator {
static final String POSTPROCESSOR_KEY_FILE_HEADER = "postProcessor.fileHeader";
static final String POSTPROCESSOR_KEY_VALIDATORS = "postProcessor.validators";

//CHECKSTYLE:OFF
@SuppressWarnings({ "java:S107", "checkstyle:ParameterNumberCheck" }) // allow many parameters
FileGenerator(GeneratorOptions options, String environmentName,
String roleName, List<String> roleVariantNames, String templateName,
File nodeDir, File file, String url, String symlinkTarget,
RoleFile roleFile, Map<String, Object> config, Template template,
VariableMapResolver variableMapResolver, UrlFileManager urlFileManager, PluginContextOptions pluginContextOptions,
Collection<String> dependencyVersions) {
//CHECKSTYLE:ON
this.environmentName = environmentName;
this.roleName = roleName;
this.roleVariantNames = roleVariantNames;
Expand Down Expand Up @@ -206,6 +205,10 @@ private List<String> formatFileHeaderCommentLines(List<String> lines) {
* @return List of files that where generated directly or indirectly (by post processors).
*/
@SuppressFBWarnings({ "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE" })
@SuppressWarnings({
"java:S3776", // ignore complexity
"java:S2696" // static variable set by intention
})
public Collection<GeneratedFileContext> generate() throws IOException {
File dir = file.getParentFile();
if (!dir.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void generate(String[] environmentNames) {
* @param nodeNames Node names to generate. If none specified all nodes are generated.
*/
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
@SuppressWarnings("java:S3776") // ignore complexity
public void generate(String[] environmentNames, String[] nodeNames) {
Map<String, Environment> selectedEnvironments = new HashMap<>();
if (environmentNames == null || environmentNames.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public final class NodeModelExport {
* environment.
* @param yamlRepresenter YAML representer
*/
@SuppressWarnings("java:S107") // allow many parameters
public NodeModelExport(File nodeDir, Node node, Environment environment, ModelExport modelExport,
VariableStringResolver variableStringResolver, VariableMapResolver variableMapResolver,
Map<String, String> containerVersionInfo, PluginContextOptions pluginContextOptions,
Expand Down Expand Up @@ -105,11 +106,9 @@ private boolean isActive() {
* @param role Role name
* @param roleVariants Role variant name
* @param config Merged configuration (unresolved)
* @param sensitiveConfigParametersList List of configuration parameter names that contain sensitive data
* @return Node role data
*/
public ExportNodeRoleData addRole(String role, List<String> roleVariants, Map<String, Object> config,
List<String> sensitiveConfigParametersList) {
public ExportNodeRoleData addRole(String role, List<String> roleVariants, Map<String, Object> config) {
if (!isActive()) {
return new ExportNodeRoleData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ protected final FileHeaderContext extractFileHeaderBetweenBlockStartEnd(FileCont
* @param file File File
* @return File header or null
*/
@SuppressWarnings("java:S3776") // ignore complexity
protected final FileHeaderContext extractFileHeaderWithLinePrefixes(FileContext file) {
try {
if (StringUtils.isNotEmpty(getLineBreak()) && StringUtils.isNotEmpty(getCommentLinePrefix())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public String getFileName(String url, UrlFilePluginContext context) {
public InputStream getFile(String url, UrlFilePluginContext context) throws IOException {
File file = getFileInternal(url, context);
if (!file.exists()) {
throw new FileNotFoundException("File does not exist: " + FileUtil.getCanonicalPath(file));
throwFileNotFoundException(file);
}
return new BufferedInputStream(new FileInputStream(file));
}
Expand All @@ -79,7 +79,7 @@ public InputStream getFile(String url, UrlFilePluginContext context) throws IOEx
public URL getFileUrl(String url, UrlFilePluginContext context) throws IOException {
File file = getFileInternal(url, context);
if (!file.exists()) {
throw new FileNotFoundException("File does not exist: " + FileUtil.getCanonicalPath(file));
throwFileNotFoundException(file);
}
return file.toURI().toURL();
}
Expand All @@ -89,7 +89,7 @@ public URL getFileUrl(String url, UrlFilePluginContext context) throws IOExcepti
public void deleteFile(String url, UrlFilePluginContext context) throws IOException {
File file = getFileInternal(url, context);
if (!file.exists()) {
throw new FileNotFoundException("File does not exist: " + FileUtil.getCanonicalPath(file));
throwFileNotFoundException(file);
}
Files.delete(file.toPath());
}
Expand All @@ -115,4 +115,8 @@ else if (StringUtils.startsWith(url, PREFIX_NODE)) {
}
}

private static void throwFileNotFoundException(File file) throws FileNotFoundException {
throw new FileNotFoundException("File does not exist: " + FileUtil.getCanonicalPath(file));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public final class VariableStringResolver {
* ${provider::var1}
* ${provider::Var1:defaultValue}
*/
@SuppressWarnings("java:S125") // no commented out code
private static final String NAME_PATTERN_STRING = "[^\\}\\{\\$\\:()'\"/\\#,;\\+\\*@!\\^\\s]";
private static final String NAME_PATTERN_STRING_NOT_EMPTY = NAME_PATTERN_STRING + "+";
private static final String NAME_PATTERN_STRING_OR_EMPTY = NAME_PATTERN_STRING + "*";
Expand Down Expand Up @@ -172,6 +173,7 @@ private Object resolve(String value, Map<String, Object> variables, int iteratio
}
}

@SuppressWarnings("java:S3776") // ignore complexity
private Object resolveSingle(Matcher matcher, Map<String, Object> variables, int iterationCount) {
boolean escapedVariable = StringUtils.equals(matcher.group(EXPRESSION_POS_DOLLAR_SIGN), "\\$");
String expression = matcher.group(EXPRESSION_POS_EXPRESSION);
Expand Down Expand Up @@ -224,6 +226,7 @@ private Object resolveSingle(Matcher matcher, Map<String, Object> variables, int
}
}

@SuppressWarnings("java:S3776") // ignore complexity
private Object resolveMulti(Matcher matcher, Map<String, Object> variables, int iterationCount) {
StringBuffer sb = new StringBuffer();
boolean replacedAny = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void setUp(TestInfo testInfo) throws IOException {
}

@Test
@SuppressWarnings("java:S5961") // number of asserts
void testAllEnvironments() {
File node1Dir = assertDirectory(destDir, "env1/node1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ private MapMerger() {
* @param map2 Map 2
* @return Merged map
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({
"unchecked",
"java:S3776", // ignore complexity
"java:S2234" // parameter arguments switched by intention
})
public static <K> Map<K, Object> merge(Map<K, Object> map1, Map<K, Object> map2) {
Map<K, Object> merged = new HashMap<>();
if (map1 == null || map2 == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ private MapSplitter() {
* @return Result with the first map (matching) with all matching values, and the second map (unmatching) with all
* values that do not match.
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({
"unchecked",
"java:S135" // multiple continue statements
})
public static @NotNull SplitResult splitMap(Map<String, Object> map,
@NotNull Predicate<Map.Entry<String, Object>> matcher) {
Map<String, Object> matching = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ void testTenant() {
}

@Test
void testEnvironmentWithNullTenant() {
assertThrows(ConstructorException.class, () -> {
EnvironmentReader reader = new EnvironmentReader();
try (InputStream is = getClass().getResourceAsStream("/environment_null_tenant.yaml")) {
void testEnvironmentWithNullTenant() throws IOException {
EnvironmentReader reader = new EnvironmentReader();
try (InputStream is = getClass().getResourceAsStream("/environment_null_tenant.yaml")) {
assertThrows(ConstructorException.class, () -> {
reader.read(is);
}
});
});
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ private File buildJarFile(File contentDirectory) throws MojoExecutionException {
return jarFile;
}

@SuppressWarnings("java:S1168") // null array is allowed
private String[] toArray(List<String> values) {
if (values == null || values.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}


@SuppressWarnings("PMD.UseStringBufferForStringAppends")
@SuppressWarnings({
"PMD.UseStringBufferForStringAppends",
"java:S3776" // ignore complexity
})
private void buildGeneratedConfigurationAttachments() throws MojoExecutionException, MojoFailureException {
Set<String> selectedEnvironments;
if (environments != null && environments.length > 0) {
Expand Down Expand Up @@ -167,6 +170,7 @@ private File buildZipFile(File contentDirectory, String classifier) throws MojoE
* @param basePath Base path
* @param directory Directory to include
*/
@SuppressWarnings("java:S3776") // ignore complexity
private void addZipDirectory(String basePath, File directory) throws MojoExecutionException {
String directoryPath = toZipDirectoryPath(directory);
if (StringUtils.startsWith(directoryPath, basePath)) {
Expand Down

0 comments on commit 265bb47

Please sign in to comment.