diff options
author | Jim Hahn <jrh3@att.com> | 2020-04-03 09:44:26 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-04-06 09:41:59 -0400 |
commit | a56d3929f2387252525577fb36f9e03933064b8f (patch) | |
tree | 4b671549b5f1c0513c31d77baa19821e422f41e7 /utils/src/main/java/org/onap/policy/common/utils/resources | |
parent | 7da3ddfa40de2f683a2d423d62b78a8d001108eb (diff) |
Address sonar issues in common
Addressed the following sonar issues:
- missing assertion in junit test case
- disable sonars about setAccessible() as it's required for jackson
emulation
- sleep in junit
- don't use wild-cards (e.g., "*") with java.util Pattern
- use re2j instead of java.util Pattern
- use String methods (e.g., startsWith())
- duplicate method bodies
- duplicate code in Coder classes
- string concatenation in logger calls
- UTF-8 encoding
- return primitive instead of boxed primitive
- add assertion to tests
- renamed support methods from doTestXxx to verifyXxx
- cognitive complexity
- use AtomicRef instead of volatile
- use specific Functionals (e.g., IntConsumer)
- function always returns the same value
- serializable vs transient
Issue-ID: POLICY-2305
Change-Id: I08eb7aa495a80bdc1d26827ba17a7946c83b9828
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/resources')
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java | 23 | ||||
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java | 9 |
2 files changed, 20 insertions, 12 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java index 365efabe..58e2baf5 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -100,7 +101,7 @@ public abstract class ResourceUtils { resourceOutputStreamBuffer.write(resourceBuffer, 0, length); } } catch (final IOException e) { - LOGGER.debug("error reading resource stream \"{}\" : " + e.getMessage(), resourceName, e); + LOGGER.debug("error reading resource stream {}", resourceName, e); return null; } @@ -130,7 +131,7 @@ public abstract class ResourceUtils { return urlToResource.openStream(); } catch (final IOException e) { // Any of many IO exceptions such as the resource is a directory - LOGGER.debug("error attaching resource \"{}\" to stream : " + e.getMessage(), resourceName, e); + LOGGER.debug("error attaching resource {}", resourceName, e); return null; } } @@ -164,7 +165,7 @@ public abstract class ResourceUtils { return url; } } catch (final Exception e) { - LOGGER.debug("error getting URL resource \"{}\" : " + e.getMessage(), e); + LOGGER.debug("error getting URL resource {}", resourceName, e); return null; } } @@ -199,7 +200,7 @@ public abstract class ResourceUtils { return null; } } catch (final Exception e) { - LOGGER.debug("error finding resource \"{}\" : " + e.getMessage(), e); + LOGGER.debug("error finding resource {}", resourceName, e); return null; } } @@ -288,6 +289,8 @@ public abstract class ResourceUtils { */ public static Set<String> getDirectoryContentsJar(final URL jarResourceDirectoryUrl, final String resourceDirectoryName) { + String dirNameWithSlash = resourceDirectoryName + "/"; + int minLength = dirNameWithSlash.length() + 1; File jarResourceDirectory = new File(jarResourceDirectoryUrl.getPath()); String jarFileName = jarResourceDirectory.getParent().replaceFirst("^file:", "").replaceFirst("!.*$", ""); @@ -297,10 +300,14 @@ public abstract class ResourceUtils { Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { - JarEntry je = entries.nextElement(); - - if (je.getName().matches("^" + resourceDirectoryName + "\\/.+")) { - localDirectorySet.add(je.getName()); + /* + * Ignore sonar issue, as the entries are not being expanded here. + */ + JarEntry je = entries.nextElement(); // NOSONAR + String jeName = je.getName(); + + if (jeName.length() >= minLength && jeName.startsWith(dirNameWithSlash)) { + localDirectorySet.add(jeName); } } } catch (IOException ioe) { diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java index 01af7fd8..c5b8c981 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +26,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; /** @@ -34,7 +36,6 @@ import java.nio.file.Files; * @author Liam Fallon (liam.fallon@est.tech) */ public abstract class TextFileUtils { - private static final String UTF_8 = "UTF-8"; private static final int READER_CHAR_BUFFER_SIZE_4096 = 4096; private TextFileUtils() { @@ -50,7 +51,7 @@ public abstract class TextFileUtils { */ public static String getTextFileAsString(final String textFilePath) throws IOException { final File textFile = new File(textFilePath); - return new String(Files.readAllBytes(textFile.toPath()), UTF_8); + return Files.readString(textFile.toPath()); } /** @@ -77,7 +78,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static void putStringAsFile(final String outString, final File textFile) throws IOException { - Files.write(textFile.toPath(), outString.getBytes(UTF_8)); + Files.writeString(textFile.toPath(), outString); } /** @@ -88,7 +89,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static String getStreamAsString(final InputStream textStream) throws IOException { - return getReaderAsString(new InputStreamReader(textStream, UTF_8)); + return getReaderAsString(new InputStreamReader(textStream, StandardCharsets.UTF_8)); } /** |