Skip to content

Commit

Permalink
Merge pull request #1481 from krmahadevan/krmahadevan-fix-sonarcube-i…
Browse files Browse the repository at this point in the history
…ssues

Sonarcube violations fixed.
  • Loading branch information
cbeust committed Jul 22, 2017
2 parents 08a8039 + a4a7562 commit 971aca1
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 48 deletions.
7 changes: 3 additions & 4 deletions src/main/java/org/testng/SuiteRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ public class SuiteRunner implements ISuite, Serializable, IInvokedMethodListener
private transient Injector parentInjector;

private transient List<ITestListener> testListeners = Lists.newArrayList();
private transient final Map<Class<? extends IClassListener>, IClassListener> classListeners = Maps.newHashMap();
private final transient Map<Class<? extends IClassListener>, IClassListener> classListeners = Maps.newHashMap();
private transient ITestRunnerFactory tmpRunnerFactory;

private transient ITestRunnerFactory runnerFactory;
private transient boolean useDefaultListeners = true;

// The remote host where this suite was run, or null if run locally
Expand Down Expand Up @@ -216,7 +215,7 @@ private void init(IConfiguration configuration,
if (comparator == null) {
throw new IllegalArgumentException("comparator must not be null");
}
this.runnerFactory = buildRunnerFactory(comparator);
ITestRunnerFactory iTestRunnerFactory = buildRunnerFactory(comparator);

// Order the <test> tags based on their order of appearance in testng.xml
List<XmlTest> xmlTests = xmlSuite.getTests();
Expand All @@ -228,7 +227,7 @@ public int compare(XmlTest arg0, XmlTest arg1) {
});

for (XmlTest test : xmlTests) {
TestRunner tr = this.runnerFactory.newTestRunner(this, test, invokedMethodListeners.values(), Lists.newArrayList(
TestRunner tr = iTestRunnerFactory.newTestRunner(this, test, invokedMethodListeners.values(), Lists.newArrayList(
this.classListeners.values()));

//
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/testng/internal/PackageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
Expand All @@ -31,7 +32,7 @@ public class PackageUtils {
private static String[] testClassPaths;

/** The additional class loaders to find classes in. */
private static final List<ClassLoader> classLoaders = new Vector<>();
private static final Collection<ClassLoader> classLoaders = new ConcurrentLinkedDeque<>();

private PackageUtils() {
//Utility class. Defeat instantiation.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/testng/internal/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class Parameters {
*/
static {
List<Class<?>> ctxTest = Arrays.asList(new Class<?>[]{ITestContext.class, XmlTest.class});
List<Class<?>> ctxTest = Arrays.<Class<?>>asList(ITestContext.class, XmlTest.class);
List<Class<?>> beforeAfterMethod = Arrays.asList(ITestContext.class, XmlTest.class, Method.class,
Object[].class, ITestResult.class);
mapping.put(BeforeSuite.class.getSimpleName(), ctxTest);
Expand All @@ -102,7 +102,7 @@ public class Parameters {

mapping.put(BeforeMethod.class.getSimpleName(), beforeAfterMethod);
mapping.put(AfterMethod.class.getSimpleName(), beforeAfterMethod);
mapping.put(Test.class.getSimpleName(), Arrays.asList(new Class<?>[] {ITestContext.class}));
mapping.put(Test.class.getSimpleName(), Collections.<Class<?>>singletonList(ITestContext.class));

}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/testng/internal/TestMethodWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

/**
* FIXME: reduce contention when this class is used through parallel invocation due to
Expand Down
42 changes: 22 additions & 20 deletions src/main/java/org/testng/internal/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,22 @@ public final class Utils {

private static final String LINE_SEP = System.getProperty("line.separator");

public static final char[] SPECIAL_CHARACTERS =
{'*','/','\\','?','%',':',';','<','>','&','~','|'};
private static final char[] SPECIAL_CHARACTERS = {'*','/','\\','?','%',':',';','<','>','&','~','|'};
public static final char CHAR_REPLACEMENT = '_';
public static final char UNICODE_REPLACEMENT = 0xFFFD;
private static final String FORMAT = String.format("[%s]", Utils.class.getSimpleName());

private static final Logger LOG = Logger.getLogger(Utils.class);

private static final Map<Character, String> ESCAPES = new HashMap<Character, String>() {
private static final long serialVersionUID = 1285607660247157523L;
private static final Map<Character, String> ESCAPES = new HashMap<>();

{
put('<', "&lt;");
put('>', "&gt;");
put('\'', "&apos;");
put('"', "&quot;");
put('&', "&amp;");
}};
static {
ESCAPES.put('<', "&lt;");
ESCAPES.put('>', "&gt;");
ESCAPES.put('\'', "&apos;");
ESCAPES.put('"', "&quot;");
ESCAPES.put('&', "&amp;");
}

/**
* Hide constructor for utility class.
Expand Down Expand Up @@ -155,11 +153,13 @@ public static void writeFile(@Nullable String outputDir, String fileName, String
* Writes the content of the sb string to the file named filename in outDir. If
* outDir does not exist, it is created.
*
* @param outDir the output directory (may not exist). If <tt>null</tt> then current directory is used.
* @param fileName the filename
* @param outputFolder the output directory (may not exist). If <tt>null</tt> then current directory is used.
* @param fileNameParameter the filename
* @param sb the file content
*/
private static void writeFile(@Nullable File outDir, String fileName, String sb, @Nullable String encoding) {
private static void writeFile(@Nullable File outputFolder, String fileNameParameter, String sb, @Nullable String encoding) {
File outDir = outputFolder;
String fileName = fileNameParameter;
try {
if (outDir == null) {
outDir = new File("").getAbsoluteFile();
Expand Down Expand Up @@ -219,10 +219,11 @@ private static void writeFile(File outputFile, String sb, @Nullable String encod
* exist, it is created. If the output file exists, it is deleted. The output file is
* created in any case.
* @param outputDir output directory. If <tt>null</tt>, then current directory is used
* @param fileName file name
* @param fileNameParameter file name
* @throws IOException if anything goes wrong while creating files.
*/
public static BufferedWriter openWriter(@Nullable String outputDir, String fileName) throws IOException {
public static BufferedWriter openWriter(@Nullable String outputDir, String fileNameParameter) throws IOException {
String fileName = fileNameParameter;
String outDirPath= outputDir != null ? outputDir : "";
File outDir= new File(outDirPath);
if (!outDir.exists()) {
Expand Down Expand Up @@ -719,10 +720,11 @@ public static String arrayToString(String[] strings) {
* In order to have the same behavior of testng on the all platforms, characters like * will
* be replaced on all platforms whether they are causing the problem or not.
*
* @param fileName file name that could contain special characters.
* @param fileNameParameter file name that could contain special characters.
* @return fileName with special characters replaced
*/
public static String replaceSpecialCharacters(String fileName) {
public static String replaceSpecialCharacters(String fileNameParameter) {
String fileName = fileNameParameter;
if (fileName == null || fileName.length() == 0) {
return fileName;
}
Expand Down Expand Up @@ -808,14 +810,14 @@ public static void checkReturnType(Method method, Class<?>... returnTypes) {

private static String toString(Class<?>[] classes) {
StringBuilder sb = new StringBuilder("[ ");
for (int i=0; i<classes.length;) {
for (int i=0; i<classes.length; i++) {
Class<?> clazz = classes[i];
if (clazz.isArray()) {
sb.append(clazz.getComponentType().getName()).append("[]");
} else {
sb.append(clazz.getName());
}
if (++i < classes.length) { // increment and compare
if ( (i+1) < classes.length) { // increment and compare
sb.append(" or ");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class AnnotationHelper {
IBeforeGroups.class, IAfterGroups.class
};

public static final Class[] CONFIGURATION_CLASSES = new Class[] {
private static final Class[] CONFIGURATION_CLASSES = new Class[] {
IConfigurationAnnotation.class,
IBeforeSuite.class, IAfterSuite.class,
IBeforeTest.class, IAfterTest.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static Class<?>[] classesFromParameters(final Parameter[] parameters) {
*/
public static Parameter[] getMethodParameters(final Method method) {
if (method == null) {
return null;
return new Parameter[]{};
}
return getParameters(method.getParameterTypes(), method.getParameterAnnotations());
}
Expand All @@ -158,7 +158,7 @@ public static Parameter[] getMethodParameters(final Method method) {
*/
public static Parameter[] getConstructorParameters(final Constructor constructor) {
if (constructor == null) {
return null;
return new Parameter[]{};
}
return getParameters(constructor.getParameterTypes(), constructor.getParameterAnnotations());
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/testng/reporters/JUnitReportReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
testCases.add(createIgnoredTestTagFor(eachMethod));
}

p1.setProperty(XMLConstants.ATTR_FAILURES, "" + failures);
p1.setProperty(XMLConstants.ATTR_ERRORS, "" + errors);
p1.setProperty(XMLConstants.SKIPPED, "" + (skipped + ignored));
p1.setProperty(XMLConstants.ATTR_FAILURES, Integer.toString(failures));
p1.setProperty(XMLConstants.ATTR_ERRORS, Integer.toString(errors));
p1.setProperty(XMLConstants.SKIPPED, Integer.toString(skipped + ignored));
p1.setProperty(XMLConstants.ATTR_NAME, cls.getName());
p1.setProperty(XMLConstants.ATTR_TESTS, "" + (testCount + ignored));
p1.setProperty(XMLConstants.ATTR_TESTS, Integer.toString(testCount + ignored));
p1.setProperty(XMLConstants.ATTR_TIME, "" + formatTime(totalTime));
try {
p1.setProperty(XMLConstants.ATTR_HOSTNAME, InetAddress.getLocalHost().getHostName());
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/org/testng/reporters/SuiteHTMLReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class SuiteHTMLReporter implements IReporter {
public static final String REPORTER_OUTPUT = "reporter-output.html";
public static final String METHODS_NOT_RUN = "methods-not-run.html";
public static final String TESTNG_XML = "testng.xml.html";
private static final String TD_A_TARGET_MAIN_FRAME_HREF = "<td><a target='mainFrame' href='";
private static final String CLOSE_TD = "</td>";

private Map<String, ITestClass> m_classes = Maps.newHashMap();
private String m_outputDirectory;
Expand Down Expand Up @@ -145,9 +147,9 @@ private void generateIndex(List<ISuite> suites) {
suiteBuf.append("<tr align='center' class='").append(cls).append("'>")
.append("<td><a href='").append(name).append("/index.html'>")
.append(name).append("</a></td>\n");
suiteBuf.append("<td>").append(passedTests).append("</td>")
.append("<td>").append(failedTests).append("</td>")
.append("<td>").append(skippedTests).append("</td>")
suiteBuf.append("<td>").append(passedTests).append(CLOSE_TD)
.append("<td>").append(failedTests).append(CLOSE_TD)
.append("<td>").append(skippedTests).append(CLOSE_TD)
.append("<td><a href='").append(name).append("/").append(TESTNG_XML).append("'>Link").append("</a></td>")
.append("</tr>");

Expand Down Expand Up @@ -441,7 +443,7 @@ else if (s.startsWith(AFTER)) {
result.append("</td> \n");
}
else {
result.append("<td>").append(SP).append("</td>");
result.append("<td>").append(SP).append(CLOSE_TD);
}

return result.toString();
Expand All @@ -465,7 +467,7 @@ private void generateMethodsAndGroups(XmlSuite xmlSuite, ISuite suite) {
Arrays.sort(groupNames);
for (String group : groupNames) {
Collection<ITestNGMethod> methods = groups.get(group);
sb.append("<tr><td>").append(group).append("</td>");
sb.append("<tr><td>").append(group).append(CLOSE_TD);
StringBuilder methodNames = new StringBuilder();
Map<ITestNGMethod, ITestNGMethod> uniqueMethods = Maps.newHashMap();
for (ITestNGMethod tm : methods) {
Expand All @@ -484,7 +486,7 @@ private void generateMethodsAndGroups(XmlSuite xmlSuite, ISuite suite) {

private void generateIndex(XmlSuite xmlSuite, ISuite sr) {

String index = String.format("<html><head><title>Results for %s</title></head>\n", sr.getName())
String index = String.format("<html><head><title>Results for %s</title></head>%n", sr.getName())
+ "<frameset cols=\"26%,74%\">\n"
+ "<frame src=\"toc.html\" name=\"navFrame\">\n"
+ "<frame src=\"main.html\" name=\"mainFrame\">\n"
Expand All @@ -499,7 +501,7 @@ private String makeTitle(ISuite suite) {
}

private void generateMain(XmlSuite xmlSuite, ISuite sr) {
String index = String.format("<html><head><title>Results for %s</title></head>\n", sr.getName())
String index = String.format("<html><head><title>Results for %s</title></head>%n", sr.getName())
+ "<body>Select a result on the left-hand pane.</body>"
+ "</html>\n";

Expand Down Expand Up @@ -560,9 +562,9 @@ private void generateTableOfContents(XmlSuite xmlSuite, ISuite suite) {
.append("</tr>\n")

.append("<tr>\n")
.append("<td><a target='mainFrame' href='").append(GROUPS).append("'>").append(groupCount).append(pluralize(groupCount, " group")).append("</a></td>\n")
.append("<td><a target='mainFrame' href='").append(REPORTER_OUTPUT).append("'>reporter output</a></td>\n")
.append("<td><a target='mainFrame' href='").append(TESTNG_XML).append("'>testng.xml</a></td>\n")
.append(TD_A_TARGET_MAIN_FRAME_HREF).append(GROUPS).append("'>").append(groupCount).append(pluralize(groupCount, " group")).append("</a></td>\n")
.append(TD_A_TARGET_MAIN_FRAME_HREF).append(REPORTER_OUTPUT).append("'>reporter output</a></td>\n")
.append(TD_A_TARGET_MAIN_FRAME_HREF).append(TESTNG_XML).append("'>testng.xml</a></td>\n")
.append("</tr>")
.append("</table>");

Expand Down Expand Up @@ -655,10 +657,10 @@ private void generateSuiteResult(String suiteName,
.append("<table style='width: 100%'><tr>")
.append("<td valign='top'>")
.append(suiteName).append(" (").append(passed).append("/").append(failed).append("/").append(skipped).append(")")
.append("</td>")
.append(CLOSE_TD)
.append("<td valign='top' align='right'>\n")
.append(" <a href='").append(baseFile).append(".html' target='mainFrame'>Results</a>\n")
.append("</td>")
.append(CLOSE_TD)
.append("</tr></table>\n")
.append("</td></tr><p/>\n")
;
Expand Down

0 comments on commit 971aca1

Please sign in to comment.