Skip to content

Commit e96e133

Browse files
committed
Code clean-up
* Use finals as and when applicable * delete commented out code * convert inner classes into static
1 parent 3c11fbf commit e96e133

File tree

198 files changed

+442
-1290
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

198 files changed

+442
-1290
lines changed

testng-ant/src/main/java/org/testng/TestNGAntTask.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -678,20 +678,12 @@ private void addStringIfNotBlank(List<String> argv, String name, String value) {
678678
}
679679

680680
private void addXmlFiles(List<String> argv) {
681-
for (String file : getSuiteFileNames()) {
682-
argv.add(file);
683-
}
681+
argv.addAll(getSuiteFileNames());
684682
}
685683

686684
/** @return the list of the XML file names. This method can be overridden by subclasses. */
687685
protected List<String> getSuiteFileNames() {
688-
List<String> result = Lists.newArrayList();
689-
690-
for (String file : getFiles(m_xmlFilesets)) {
691-
result.add(file);
692-
}
693-
694-
return result;
686+
return Lists.newArrayList(getFiles(m_xmlFilesets));
695687
}
696688

697689
private void delegateCommandSystemProperties() {

testng-asserts/src/main/java/org/testng/Assert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,7 @@ public static void assertEqualsNoOrder(Iterator<?> actual, Iterator<?> expected,
18731873
if (actualCollection.size() != expectedCollection.size()) {
18741874
failAssertNoEqual(
18751875
"Iterators do not have the same size: "
1876-
+ +actualCollection.size()
1876+
+ actualCollection.size()
18771877
+ " != "
18781878
+ expectedCollection.size(),
18791879
message);

testng-asserts/src/main/java/org/testng/FileAssert.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.util.Optional;
56

67
/**
78
* Assertion tool for File centric assertions. Conceptually, this is an extension of {@link Assert}.
@@ -80,7 +81,10 @@ public static void assertFile(File tstvalue) {
8081
public static void assertLength(File tstvalue, long expected, String message) {
8182
long actual = -1L;
8283
try {
83-
actual = tstvalue.isDirectory() ? tstvalue.list().length : tstvalue.length();
84+
actual =
85+
tstvalue.isDirectory()
86+
? Optional.ofNullable(tstvalue.list()).orElse(new String[0]).length
87+
: tstvalue.length();
8488
} catch (SecurityException e) {
8589
failSecurity(e, tstvalue, String.valueOf(actual), String.valueOf(expected), message);
8690
}
@@ -284,12 +288,6 @@ private static void failFile(File path, String actual, String expected, String m
284288
+ (expected != null ? "<" + expected + ">" : ""));
285289
}
286290

287-
/**
288-
* @param tstvalue
289-
* @param string
290-
* @param string2
291-
* @param message
292-
*/
293291
private static void failSecurity(
294292
Exception e, File path, String actual, String expected, String message) {
295293
String formatted = "";

testng-asserts/src/test/java/org/testng/AssertTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ public void testAssertNotEqualsWithNull() {
565565
Assert.assertNotEquals(obj, obj);
566566
}
567567

568-
class Contrived {
568+
static class Contrived {
569569

570570
int integer;
571571

testng-asserts/src/test/java/test/assertion/AssertionListContainsTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
public class AssertionListContainsTest {
99

10-
private User userJack = new User("Jack", 22);
11-
private User userJohn = new User("John", 32);
12-
private List<User> users = List.of(userJack, userJohn);
10+
private final User userJack = new User("Jack", 22);
11+
private final User userJohn = new User("John", 32);
12+
private final List<User> users = List.of(userJack, userJohn);
1313

1414
@Test
1515
public void assertListContainsObject() {
@@ -31,10 +31,10 @@ public void testAssertListNotContainsByPredicate() {
3131
Assert.assertListNotContains(users, user -> user.age.equals(19), "user with age 19");
3232
}
3333

34-
private class User {
34+
private static class User {
3535

36-
private String name;
37-
private Integer age;
36+
private final String name;
37+
private final Integer age;
3838

3939
public User(String name, Integer age) {
4040
this.name = name;
@@ -56,11 +56,7 @@ public int hashCode() {
5656

5757
@Override
5858
public String toString() {
59-
final StringBuilder sb = new StringBuilder("User{");
60-
sb.append("name='").append(name).append('\'');
61-
sb.append(", age=").append(age);
62-
sb.append('}');
63-
return sb.toString();
59+
return "User{" + "name='" + name + '\'' + ", age=" + age + '}';
6460
}
6561
}
6662
}

testng-asserts/src/test/java/test/asserttests/ArrayEqualityAssertTest.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import static org.testng.Assert.assertNotEqualsDeep;
77

88
import java.util.ArrayList;
9-
import java.util.Arrays;
109
import java.util.Collections;
1110
import java.util.HashMap;
1211
import java.util.HashSet;
@@ -76,15 +75,15 @@ public void mixedArraysAssertNotEquals() {
7675

7776
@Test(expectedExceptions = AssertionError.class)
7877
public void arrayInsideListAssertEquals() {
79-
List<int[]> list = Arrays.asList(new int[] {42});
80-
List<int[]> listCopy = Arrays.asList(new int[] {42});
78+
List<int[]> list = List.of(new int[] {42});
79+
List<int[]> listCopy = List.of(new int[] {42});
8180
assertEquals(list, listCopy, "arrays inside lists are compared by reference in assertEquals");
8281
}
8382

8483
@Test
8584
public void arrayInsideListAssertNotEquals() {
86-
List<int[]> list = Arrays.asList(new int[] {42});
87-
List<int[]> listCopy = Arrays.asList(new int[] {42});
85+
List<int[]> list = List.of(new int[] {42});
86+
List<int[]> listCopy = List.of(new int[] {42});
8887
assertNotEquals(
8988
list, listCopy, "arrays inside lists are compared by reference in assertNotEquals");
9089
}
@@ -225,8 +224,8 @@ public void arrayInsideSetAssertNotEqualsDeep() {
225224

226225
@Test(expectedExceptions = AssertionError.class)
227226
public void arrayDeepInListsAssertEquals() {
228-
List<List<int[]>> list = Collections.singletonList(Arrays.asList(new int[] {42}));
229-
List<List<int[]>> listCopy = Collections.singletonList(Arrays.asList(new int[] {42}));
227+
List<List<int[]>> list = Collections.singletonList(List.of(new int[] {42}));
228+
List<List<int[]>> listCopy = Collections.singletonList(List.of(new int[] {42}));
230229

231230
assertEquals(
232231
list,
@@ -271,9 +270,9 @@ public void arrayDeepInListAndMapAssertEquals() {
271270
@Test(expectedExceptions = AssertionError.class)
272271
public void arrayDeepInMapAndListAssertEquals() {
273272
Map<String, List<int[]>> map = new HashMap<>();
274-
map.put("list", Arrays.asList(new int[] {42}));
273+
map.put("list", List.of(new int[] {42}));
275274
Map<String, List<int[]>> mapCopy = new HashMap<>();
276-
mapCopy.put("list", Arrays.asList(new int[] {42}));
275+
mapCopy.put("list", List.of(new int[] {42}));
277276

278277
assertEquals(
279278
map,
@@ -283,8 +282,8 @@ public void arrayDeepInMapAndListAssertEquals() {
283282

284283
@Test(expectedExceptions = AssertionError.class)
285284
public void arrayInsideIterableAssertEquals() {
286-
Iterable<int[]> iterable = Arrays.asList(new int[] {42});
287-
Iterable<int[]> iterableCopy = Arrays.asList(new int[] {42});
285+
Iterable<int[]> iterable = List.of(new int[] {42});
286+
Iterable<int[]> iterableCopy = List.of(new int[] {42});
288287
assertEquals(
289288
iterable,
290289
iterableCopy,
@@ -293,8 +292,8 @@ public void arrayInsideIterableAssertEquals() {
293292

294293
@Test(expectedExceptions = AssertionError.class)
295294
public void arrayDeepInIterablesAssertEquals() {
296-
List<List<int[]>> iterable = Collections.singletonList(Arrays.asList(new int[] {42}));
297-
List<List<int[]>> iterableCopy = Collections.singletonList(Arrays.asList(new int[] {42}));
295+
List<List<int[]>> iterable = Collections.singletonList(List.of(new int[] {42}));
296+
List<List<int[]>> iterableCopy = Collections.singletonList(List.of(new int[] {42}));
298297

299298
assertEquals(
300299
iterable,
@@ -323,8 +322,8 @@ public void arrayDeepInArraysAssertEquals() {
323322
@SuppressWarnings("unchecked")
324323
@Test(expectedExceptions = AssertionError.class)
325324
public void arrayDeepInArrayAndListAssertEquals() {
326-
List<int[]>[] array = new List[] {Arrays.asList(new int[] {42})};
327-
List<int[]>[] arrayCopy = new List[] {Arrays.asList(new int[] {42})};
325+
List<int[]>[] array = new List[] {List.of(new int[] {42})};
326+
List<int[]>[] arrayCopy = new List[] {List.of(new int[] {42})};
328327

329328
assertEquals(
330329
array,

testng-core-api/src/main/java/org/testng/annotations/Parameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@Retention(RUNTIME)
1313
@Target({METHOD, CONSTRUCTOR, TYPE})
1414
public @interface Parameters {
15-
public static final String NULL_VALUE = "null";
15+
String NULL_VALUE = "null";
1616

1717
/**
1818
* The list of variables used to fill the parameters of this method. These variables must be

testng-core-api/src/main/java/org/testng/internal/Utils.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public static void writeUtf8File(
7272
final File outDir =
7373
(outputDir != null) ? new File(outputDir) : new File("").getAbsoluteFile();
7474
if (!outDir.exists()) {
75-
outDir.mkdirs();
75+
boolean ignored = outDir.mkdirs();
7676
}
7777
final File file = new File(outDir, fileName);
7878
if (!file.exists()) {
79-
file.createNewFile();
79+
boolean ignored = file.createNewFile();
8080
}
8181
try (final OutputStreamWriter w =
8282
new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
@@ -138,12 +138,12 @@ private static void writeFile(
138138
outDir = new File("").getAbsoluteFile();
139139
}
140140
if (!outDir.exists()) {
141-
outDir.mkdirs();
141+
boolean ignored = outDir.mkdirs();
142142
}
143143

144144
fileName = replaceSpecialCharacters(fileName);
145145
File outputFile = new File(outDir, fileName);
146-
outputFile.delete();
146+
boolean ignored = outputFile.delete();
147147
log(FORMAT, 3, "Attempting to create " + outputFile);
148148
log(FORMAT, 3, " Directory " + outDir + " exists: " + outDir.exists());
149149
outputFile.createNewFile();
@@ -186,18 +186,18 @@ public static BufferedWriter openWriter(@Nullable String outputDir, String fileN
186186
String outDirPath = outputDir != null ? outputDir : "";
187187
File outDir = new File(outDirPath);
188188
if (!outDir.exists()) {
189-
outDir.mkdirs();
189+
boolean ignored = outDir.mkdirs();
190190
}
191191
fileName = replaceSpecialCharacters(fileName);
192192
File outputFile = new File(outDir, fileName);
193-
outputFile.delete();
193+
boolean ignored = outputFile.delete();
194194
return openWriter(outputFile, null);
195195
}
196196

197197
private static BufferedWriter openWriter(File outputFile, @Nullable String encoding)
198198
throws IOException {
199199
if (!outputFile.exists()) {
200-
outputFile.createNewFile();
200+
boolean ignored = outputFile.createNewFile();
201201
}
202202
OutputStreamWriter osw;
203203
if (null != encoding) {
@@ -227,7 +227,7 @@ public static void log(String msg) {
227227
public static void log(String cls, int level, String msg) {
228228
// Why this coupling on a static member of getVerbose()?
229229
if (getVerbose() >= level) {
230-
if (cls.length() > 0) {
230+
if (!cls.isEmpty()) {
231231
LOG.info("[" + cls + "] " + msg);
232232
} else {
233233
LOG.info(msg);
@@ -245,7 +245,7 @@ public static void warn(String warnMsg) {
245245

246246
/* Tokenize the string using the separator. */
247247
public static String[] split(String string, String sep) {
248-
if ((string == null) || (string.length() == 0)) {
248+
if (string == null || string.isEmpty()) {
249249
return new String[0];
250250
}
251251

@@ -275,16 +275,14 @@ public static void writeResourceToFile(File file, String resourceName, Class<?>
275275
LOG.error("Couldn't find resource on the class path: " + resourceName);
276276
return;
277277
}
278-
try {
278+
try (inputStream) {
279279
try (FileOutputStream outputStream = new FileOutputStream(file)) {
280280
int nread;
281281
byte[] buffer = new byte[4096];
282282
while (0 < (nread = inputStream.read(buffer))) {
283283
outputStream.write(buffer, 0, nread);
284284
}
285285
}
286-
} finally {
287-
inputStream.close();
288286
}
289287
}
290288

@@ -293,11 +291,11 @@ public static String defaultIfStringEmpty(String s, String defaultValue) {
293291
}
294292

295293
public static boolean isStringBlank(String s) {
296-
return s == null || "".equals(s.trim());
294+
return s == null || s.trim().isEmpty();
297295
}
298296

299297
public static boolean isStringEmpty(String s) {
300-
return s == null || "".equals(s);
298+
return s == null || s.isEmpty();
301299
}
302300

303301
public static boolean isStringNotBlank(String s) {
@@ -526,7 +524,7 @@ public static String arrayToString(String[] strings) {
526524
*/
527525
public static String replaceSpecialCharacters(String fileNameParameter) {
528526
String fileName = fileNameParameter;
529-
if (fileName == null || fileName.length() == 0) {
527+
if (fileName == null || fileName.isEmpty()) {
530528
return fileName;
531529
}
532530
for (char element : SPECIAL_CHARACTERS) {

testng-core-api/src/main/java/org/testng/internal/objects/InstanceCreator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.reflect.Constructor;
44
import java.lang.reflect.InvocationTargetException;
5+
import java.util.Objects;
56
import org.testng.TestNGException;
67
import org.testng.internal.ClassHelper;
78

@@ -16,6 +17,7 @@ private InstanceCreator() {
1617

1718
public static <T> T newInstance(String className, Object... parameters) {
1819
Class<?> clazz = ClassHelper.forName(className);
20+
Objects.requireNonNull(clazz, "Could not find a valid class");
1921
return (T) newInstance(clazz, parameters);
2022
}
2123

testng-core-api/src/main/java/org/testng/reporters/FileStringBuffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @since Nov 9, 2012
2525
*/
2626
public class FileStringBuffer implements IBuffer {
27-
private static int MAX = 100000;
27+
private static final int MAX = 100000;
2828
private static final boolean VERBOSE = RuntimeBehavior.verboseMode();
2929
private static final Logger LOGGER = Logger.getLogger(FileStringBuffer.class);
3030

0 commit comments

Comments
 (0)