Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-14153 Applied PMD's SimplifiableTestAssertion across the code base and fixed the violations. #9641

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -2560,14 +2561,14 @@ private void verifyEquals(final String expression, final Map<String, String> att
if (expectedResult instanceof Long) {
if (ResultType.NUMBER.equals(result.getResultType())) {
final Number resultNumber = ((NumberQueryResult) result).getValue();
assertTrue(resultNumber instanceof Long);
assertInstanceOf(Long.class, resultNumber);
} else {
assertEquals(ResultType.WHOLE_NUMBER, result.getResultType());
}
} else if (expectedResult instanceof Double) {
if (ResultType.NUMBER.equals(result.getResultType())) {
final Number resultNumber = ((NumberQueryResult) result).getValue();
assertTrue(resultNumber instanceof Double);
assertInstanceOf(Double.class, resultNumber);
} else {
assertEquals(ResultType.DECIMAL, result.getResultType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SuppressWarnings("resource")
Expand Down Expand Up @@ -136,7 +137,7 @@ public void testSelectField() {
assertEquals(1, result.getHitCount());

final Object names = result.nextHit().getValue("PID.5");
assertTrue(names instanceof List);
assertInstanceOf(List.class, names);
final List<Object> nameList = (List) names;
assertEquals(1, nameList.size());
final HL7Field nameField = (HL7Field) nameList.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -315,17 +316,17 @@ void testNestedSchema() {

Map<String, Object> fullConversion = ((MapRecord) record).toMap(true);
assertEquals(FOO_TEST_VAL, fullConversion.get("foo"));
assertTrue(fullConversion.get("nested") instanceof Map);
assertInstanceOf(Map.class, fullConversion.get("nested"));

Map<String, Object> nested = (Map<String, Object>) fullConversion.get("nested");
assertEquals(1, nested.size());
assertEquals(NESTED_RECORD_VALUE, nested.get("test"));

assertTrue(fullConversion.get("list") instanceof List);
assertInstanceOf(List.class, fullConversion.get("list"));
List recordList = (List) fullConversion.get("list");
assertEquals(5, recordList.size());
for (Object rec : recordList) {
assertTrue(rec instanceof Map);
assertInstanceOf(Map.class, rec);
Map<String, Object> map = (Map<String, Object>) rec;
assertEquals(1, map.size());
assertEquals(NESTED_RECORD_VALUE, map.get("test"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -109,7 +109,7 @@ public void testReadExactlyOnceFields() throws IOException {

assertEquals(42, record.getFieldValue("int"));
assertTrue((boolean) record.getFieldValue("boolean"));
assertTrue(Arrays.equals("hello".getBytes(), (byte[]) record.getFieldValue("byte array")));
assertArrayEquals("hello".getBytes(), (byte[]) record.getFieldValue("byte array"));
assertEquals(42L, record.getFieldValue("long"));
assertEquals("hello", record.getFieldValue("string"));
assertEquals("hello", record.getFieldValue("long string"));
Expand Down Expand Up @@ -251,7 +251,7 @@ public void testReadZeroOrOneFields() throws IOException {

assertEquals(42, valueMap.get("int present"));
assertTrue((boolean) valueMap.get("boolean present"));
assertTrue(Arrays.equals("hello".getBytes(), (byte[]) valueMap.get("byte array present")));
assertArrayEquals("hello".getBytes(), (byte[]) valueMap.get("byte array present"));
assertEquals(42L, valueMap.get("long present"));
assertEquals("hello", valueMap.get("string present"));
assertEquals("hello", valueMap.get("long string present"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.apache.nifi.repository.schema.SchemaRecordWriter.MAX_ALLOWED_UTF_LENGTH;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -152,7 +152,7 @@ public void testRoundTrip() throws IOException {
assertEquals(42, record.getFieldValue("int"));
assertEquals(42, record.getFieldValue("int present"));
assertEquals(true, record.getFieldValue("boolean present"));
assertTrue(Arrays.equals("Hello".getBytes(), (byte[]) record.getFieldValue("byte array present")));
assertArrayEquals("Hello".getBytes(), (byte[]) record.getFieldValue("byte array present"));
assertEquals(42L, record.getFieldValue("long present"));
assertEquals("Hello", record.getFieldValue("string present"));
assertEquals("Long Hello", record.getFieldValue("long string present"));
Expand Down Expand Up @@ -197,8 +197,8 @@ public void testUTFLargerThan64k() throws IOException {
values.put(createField("int present", FieldType.INT), 42);
final String utfString = utfStringOneByte + utfStringTwoByte + utfStringThreeByte; // 3 chars and 6 utf8 bytes
final String seventyK = StringUtils.repeat(utfString, 21845); // 65,535 chars and 131070 utf8 bytes
assertTrue(seventyK.length() == 65535);
assertTrue(seventyK.getBytes(StandardCharsets.UTF_8).length == 131070);
assertEquals(65535, seventyK.length());
assertEquals(131070, seventyK.getBytes(StandardCharsets.UTF_8).length);
values.put(createField("string present", FieldType.STRING), seventyK);

final FieldMapRecord originalRecord = new FieldMapRecord(values, schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;


Expand Down Expand Up @@ -86,7 +84,7 @@ public void validateNoDelimiter() throws IOException {
String data = "Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
StreamDemarcator scanner = new StreamDemarcator(is, null, 1000);
assertTrue(Arrays.equals(data.getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertArrayEquals(data.getBytes(StandardCharsets.UTF_8), scanner.nextToken());
// validate that subsequent invocations of nextToken() do not result in exception
assertNull(scanner.nextToken());
assertNull(scanner.nextToken());
Expand All @@ -97,17 +95,17 @@ public void validateNoDelimiterSmallInitialBuffer() throws IOException {
String data = "Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
StreamDemarcator scanner = new StreamDemarcator(is, null, 1000, 1);
assertTrue(Arrays.equals(data.getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertArrayEquals(data.getBytes(StandardCharsets.UTF_8), scanner.nextToken());
}

@Test
public void validateSingleByteDelimiter() throws IOException {
String data = "Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
StreamDemarcator scanner = new StreamDemarcator(is, ",".getBytes(StandardCharsets.UTF_8), 1000);
assertTrue(Arrays.equals("Learn from yesterday".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals(" live for today".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals(" hope for tomorrow. The important thing is not to stop questioning.".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertArrayEquals("Learn from yesterday".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals(" live for today".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals(" hope for tomorrow. The important thing is not to stop questioning.".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertNull(scanner.nextToken());
}

Expand All @@ -116,9 +114,9 @@ public void validateDelimiterAtTheBeginning() throws IOException {
String data = ",Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
StreamDemarcator scanner = new StreamDemarcator(is, ",".getBytes(StandardCharsets.UTF_8), 1000);
assertTrue(Arrays.equals("Learn from yesterday".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals(" live for today".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals(" hope for tomorrow. The important thing is not to stop questioning.".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertArrayEquals("Learn from yesterday".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals(" live for today".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals(" hope for tomorrow. The important thing is not to stop questioning.".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertNull(scanner.nextToken());
}

Expand All @@ -127,9 +125,9 @@ public void validateEmptyDelimiterSegments() throws IOException {
String data = ",,,,,Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
StreamDemarcator scanner = new StreamDemarcator(is, ",".getBytes(StandardCharsets.UTF_8), 1000);
assertTrue(Arrays.equals("Learn from yesterday".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals(" live for today".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals(" hope for tomorrow. The important thing is not to stop questioning.".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertArrayEquals("Learn from yesterday".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals(" live for today".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals(" hope for tomorrow. The important thing is not to stop questioning.".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertNull(scanner.nextToken());
}

Expand All @@ -138,9 +136,9 @@ public void validateSingleByteDelimiterSmallInitialBuffer() throws IOException {
String data = "Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
StreamDemarcator scanner = new StreamDemarcator(is, ",".getBytes(StandardCharsets.UTF_8), 1000, 2);
assertTrue(Arrays.equals("Learn from yesterday".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals(" live for today".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals(" hope for tomorrow. The important thing is not to stop questioning.".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertArrayEquals("Learn from yesterday".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals(" live for today".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals(" hope for tomorrow. The important thing is not to stop questioning.".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertNull(scanner.nextToken());
}

Expand All @@ -149,9 +147,9 @@ public void validateWithMultiByteDelimiter() throws IOException {
String data = "foodaabardaabazzz";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
StreamDemarcator scanner = new StreamDemarcator(is, "daa".getBytes(StandardCharsets.UTF_8), 1000);
assertTrue(Arrays.equals("foo".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals("bar".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals("bazzz".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertArrayEquals("foo".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals("bar".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals("bazzz".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertNull(scanner.nextToken());
}

Expand All @@ -160,9 +158,9 @@ public void validateWithMultiByteDelimiterAtTheBeginning() throws IOException {
String data = "daafoodaabardaabazzz";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
StreamDemarcator scanner = new StreamDemarcator(is, "daa".getBytes(StandardCharsets.UTF_8), 1000);
assertTrue(Arrays.equals("foo".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals("bar".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals("bazzz".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertArrayEquals("foo".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals("bar".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals("bazzz".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertNull(scanner.nextToken());
}

Expand All @@ -171,9 +169,9 @@ public void validateWithMultiByteDelimiterSmallInitialBuffer() throws IOExceptio
String data = "foodaabarffdaabazz";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
StreamDemarcator scanner = new StreamDemarcator(is, "daa".getBytes(StandardCharsets.UTF_8), 1000, 1);
assertTrue(Arrays.equals("foo".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals("barff".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertTrue(Arrays.equals("bazz".getBytes(StandardCharsets.UTF_8), scanner.nextToken()));
assertArrayEquals("foo".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals("barff".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertArrayEquals("bazz".getBytes(StandardCharsets.UTF_8), scanner.nextToken());
assertNull(scanner.nextToken());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,32 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestClassLoaderUtils {

@Test
public void testGetCustomClassLoader() throws MalformedURLException, ClassNotFoundException {
final String jarFilePath = "src/test/resources/TestClassLoaderUtils";
ClassLoader customClassLoader = ClassLoaderUtils.getCustomClassLoader(jarFilePath, this.getClass().getClassLoader(), getJarFilenameFilter());
assertTrue(customClassLoader != null);
assertTrue(customClassLoader.loadClass("TestSuccess") != null);
assertNotNull(customClassLoader);
assertNotNull(customClassLoader.loadClass("TestSuccess"));
}

@Test
public void testGetCustomClassLoaderNoPathSpecified() throws MalformedURLException {
final ClassLoader originalClassLoader = this.getClass().getClassLoader();
ClassLoader customClassLoader = ClassLoaderUtils.getCustomClassLoader(null, originalClassLoader, getJarFilenameFilter());
assertTrue(customClassLoader != null);
assertNotNull(customClassLoader);
ClassNotFoundException cex = assertThrows(ClassNotFoundException.class, () -> customClassLoader.loadClass("TestSuccess"));
assertTrue(cex.getLocalizedMessage().equals("TestSuccess"));
assertEquals("TestSuccess", cex.getLocalizedMessage());
}

@Test
public void testGetCustomClassLoaderWithInvalidPath() {
final String jarFilePath = "src/test/resources/FakeTestClassLoaderUtils/TestSuccess.jar";
MalformedURLException mex = assertThrows(MalformedURLException.class,
() -> ClassLoaderUtils.getCustomClassLoader(jarFilePath, this.getClass().getClassLoader(), getJarFilenameFilter()));
assertTrue(mex.getLocalizedMessage().equals("Path specified does not exist"));
assertEquals("Path specified does not exist", mex.getLocalizedMessage());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class ItemKeysTest {

Expand Down Expand Up @@ -64,7 +64,7 @@ public void testHashNotNullRangeNotNullEquals() {
public void testHashNotNullRangeNotNullForOtherNotEquals() {
ItemKeys ik1 = new ItemKeys(null, string("ab"));
ItemKeys ik2 = new ItemKeys(string("ab"), null);
assertFalse(ik1.equals(ik2));
assertNotEquals(ik1, ik2);
}

private static AttributeValue string(final String s) {
Expand Down
Loading
Loading