Skip to content

Commit

Permalink
junit: simplify unit test assertions
Browse files Browse the repository at this point in the history
assertFalse(!value) => assertTrue(value)
assertTrue(a.equals(b)) -> assertEquals(a, b)

Acked-by: Marina Sahakyan
Acked-by: Lea Morschel
Target: master
  • Loading branch information
kofemann committed Apr 27, 2021
1 parent f67311c commit d0f33a0
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void testEqualsTrue() {

OncRpcProgram program1 = new OncRpcProgram(progNum, progVers);
OncRpcProgram program2 = new OncRpcProgram(progNum, progVers);
assertTrue("Equals programs not detected", program1.equals(program2));
assertEquals("Equals programs not detected", program1, program2);
}

@Test
Expand All @@ -61,7 +61,7 @@ public void testNotEqualByProgNumber() {

OncRpcProgram program1 = new OncRpcProgram(progNum, progVers);
OncRpcProgram program2 = new OncRpcProgram(progNum, progVers + 10);
assertFalse("Different programs not detected", program1.equals(program2));
assertNotEquals("Different programs not detected", program1, program2);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void shouldReturnSameThreadExecutorForSameThreadStrategy() {
holder[0] = Thread.currentThread();
});

assertTrue("Executoed in a different thread", thisThread == holder[0]);
assertSame("Executoed in a different thread", thisThread, holder[0]);
}

@Test
Expand All @@ -61,7 +61,7 @@ public void shouldReturnDifferentExecutorForWorkerThreadStrategy() {
holder[0] = Thread.currentThread();
});

assertTrue("Executoed in the same thread", thisThread != holder[0]);
assertNotSame("Executoed in the same thread", thisThread, holder[0]);
}

@Test
Expand All @@ -73,7 +73,7 @@ public void shouldReturnGivenExecutorForWorkerThreadStrategy() {
.withWorkerThreadExecutionService(mockedExecutorService);

ExecutorService executorService = builder.getWorkerThreadExecutorService();
assertTrue("Provided executor service not used", mockedExecutorService == executorService);
assertSame("Provided executor service not used", mockedExecutorService, executorService);
}

@Test(expected = IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public void testBindToInterface() throws IOException {

InetSocketAddress tcpSocketAddresses = svc.getInetSocketAddress(IpProtocolType.TCP);
InetSocketAddress udpSocketAddresses = svc.getInetSocketAddress(IpProtocolType.UDP);
assertTrue(!tcpSocketAddresses.getAddress().isAnyLocalAddress());
assertTrue(!udpSocketAddresses.getAddress().isAnyLocalAddress());
assertFalse(tcpSocketAddresses.getAddress().isAnyLocalAddress());
assertFalse(udpSocketAddresses.getAddress().isAnyLocalAddress());
}

@Test
Expand Down Expand Up @@ -98,13 +98,13 @@ public void testPortmapSetApplication() throws IOException, TimeoutException {
try ( OncRpcClient rpcClient = new OncRpcClient(InetAddress.getByName(null), IpProtocolType.UDP, svc.getInetSocketAddress(IpProtocolType.UDP).getPort() ) ) {
OncPortmapClient portmapClient = new GenericPortmapClient(rpcClient.connect()); // init portmapper (only v2 atm)
assertTrue(portmapClient.ping()); // ping portmap
assertTrue( portmapClient.getPort(OncRpcPortmap.PORTMAP_PROGRAMM, OncRpcPortmap.PORTMAP_V2, "tcp").equals("127.0.0.1.0.111") ); // check port
assertEquals("127.0.0.1.0.111", portmapClient.getPort(OncRpcPortmap.PORTMAP_PROGRAMM, OncRpcPortmap.PORTMAP_V2, "tcp")); // check port
String addr = InetSocketAddresses.uaddrOf(new InetSocketAddress("127.0.0.1",1234));
assertTrue( portmapClient.setPort(TEST_PROG, TEST_PROG_VER, IpProtocolType.toString(IpProtocolType.TCP),addr, TEST_PROG_OWNER) ); // reg app with tcp and udp
assertTrue( portmapClient.setPort(TEST_PROG, TEST_PROG_VER, IpProtocolType.toString(IpProtocolType.UDP),addr, TEST_PROG_OWNER) ); // reg app with udp and udp
assertFalse( portmapClient.setPort(TEST_PROG, TEST_PROG_VER, IpProtocolType.toString(IpProtocolType.TCP),addr, TEST_PROG_OWNER) ); // try again app with tcp
assertFalse( portmapClient.setPort(TEST_PROG, TEST_PROG_VER, IpProtocolType.toString(IpProtocolType.UDP),addr, TEST_PROG_OWNER) ); // try again app with udp
assertTrue( addr.equals( portmapClient.getPort(TEST_PROG,TEST_PROG_VER, IpProtocolType.toString(IpProtocolType.TCP) ) ) ); // check tcp address match
assertEquals(addr, portmapClient.getPort(TEST_PROG, TEST_PROG_VER, IpProtocolType.toString(IpProtocolType.TCP))); // check tcp address match
assertTrue( portmapClient.unsetPort(TEST_PROG, TEST_PROG_VER, TEST_PROG_OWNER) ); // remove app
assertFalse( portmapClient.unsetPort(TEST_PROG, TEST_PROG_VER,TEST_PROG_OWNER) ); // remove app again
// do dump lookup test
Expand All @@ -114,7 +114,7 @@ public void testPortmapSetApplication() throws IOException, TimeoutException {
found = true;
}
}
assertTrue(!found); // we should not find one anymore
assertFalse(found); // we should not find one anymore
svc.unregister(portMapProg); // just remove portmap
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testSameMustBeEqual() throws IOException {

XdrOpaque opaque = new XdrOpaque("some bytes".getBytes(StandardCharsets.UTF_8));

assertTrue("opaques with equal values must be equal", opaque.equals(opaque));
assertEquals("opaques with equal values must be equal", opaque, opaque);
}

@Test
Expand All @@ -58,8 +58,8 @@ public void testEqualsByValue() throws IOException {
XdrOpaque opaque1 = new XdrOpaque("some bytes".getBytes(StandardCharsets.UTF_8));
XdrOpaque opaque2 = new XdrOpaque("some bytes".getBytes(StandardCharsets.UTF_8));

assertTrue("opaques with equal values must be equal", opaque1.equals(opaque2));
assertTrue("equal objects must have the same hashcode", opaque1.hashCode() == opaque2.hashCode());
assertEquals("opaques with equal values must be equal", opaque1, opaque2);
assertEquals("equal objects must have the same hashcode", opaque1.hashCode(), opaque2.hashCode());
}

@Test
Expand All @@ -68,7 +68,7 @@ public void testDifferentTypesCantBeEqual() throws IOException {
byte[] data = "some bytes".getBytes(StandardCharsets.UTF_8);
XdrOpaque opaque = new XdrOpaque(data);

assertFalse("opaques with equal values must be equal", opaque.equals(data));
assertNotEquals("opaques with equal values must be equal", opaque, data);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testEncodeDecodeOpaque() throws BadXdrOncRpcException {
decoder.beginDecoding();
byte[] decoded = decoder.xdrDecodeDynamicOpaque();

assertTrue("encoded/decoded data do not match", Arrays.equals(data, decoded));
assertArrayEquals("encoded/decoded data do not match", data, decoded);
}

@Test
Expand All @@ -85,7 +85,7 @@ public void testEncodeDecodeOpaque2() throws BadXdrOncRpcException {
decoder.beginDecoding();
byte[] decoded = decoder.xdrDecodeOpaque(data.length);

assertTrue("encoded/decoded data do not match", Arrays.equals(data, decoded));
assertArrayEquals("encoded/decoded data do not match", data, decoded);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public void testBigCostsGeneration() throws Exception{
Assert.assertTrue(Calculator.HUGE_CONST.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0);
//the code below is jdk8's compareUnsigned (back ported because project is jdk7)
//noinspection NumericOverflow
Assert.assertTrue(Integer.compare(maxInt + Integer.MIN_VALUE, Calculator.UNSIGNED_INT_OCT_CONST + Integer.MIN_VALUE) == 0);
Assert.assertEquals(0, Integer.compare(maxInt + Integer.MIN_VALUE, Calculator.UNSIGNED_INT_OCT_CONST + Integer.MIN_VALUE));
//noinspection NumericOverflow
Assert.assertTrue(Integer.compare(maxInt + Integer.MIN_VALUE, Calculator.UNSIGNED_INT_HEX_CONST + Integer.MIN_VALUE) == 0);
Assert.assertEquals(0, Integer.compare(maxInt + Integer.MIN_VALUE, Calculator.UNSIGNED_INT_HEX_CONST + Integer.MIN_VALUE));
//noinspection NumericOverflow
Assert.assertTrue(Integer.compare(maxInt + Integer.MIN_VALUE, ((int) Calculator.UNSIGNED_INT_DEC_CONST) + Integer.MIN_VALUE) == 0);
Assert.assertEquals(0, Integer.compare(maxInt + Integer.MIN_VALUE, ((int) Calculator.UNSIGNED_INT_DEC_CONST) + Integer.MIN_VALUE));
//noinspection NumericOverflow
Assert.assertTrue(Long.compare(maxLong + Long.MIN_VALUE, Calculator.UNSIGNED_LONG_OCT_CONST + Long.MIN_VALUE) == 0);
Assert.assertEquals(0, Long.compare(maxLong + Long.MIN_VALUE, Calculator.UNSIGNED_LONG_OCT_CONST + Long.MIN_VALUE));
//noinspection NumericOverflow
Assert.assertTrue(Long.compare(maxLong + Long.MIN_VALUE, Calculator.UNSIGNED_LONG_HEX_CONST + Long.MIN_VALUE) == 0);
Assert.assertTrue(Long.compare(maxLong + Long.MIN_VALUE, Calculator.UNSIGNED_LONG_DEC_CONST.longValue() + Long.MIN_VALUE) == 0);
Assert.assertEquals(0, Long.compare(maxLong + Long.MIN_VALUE, Calculator.UNSIGNED_LONG_HEX_CONST + Long.MIN_VALUE));
Assert.assertEquals(0, Long.compare(maxLong + Long.MIN_VALUE, Calculator.UNSIGNED_LONG_DEC_CONST.longValue() + Long.MIN_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void testSimpleScenario() throws Exception {
client.put_1(key, value, 0, null, null);
Value returned = client.get_1(key, 0, null, null);
Assert.assertNotNull(returned);
Assert.assertTrue(returned != value);
Assert.assertNotSame(returned, value);
byte[] returnedValue = returned.data;
Assert.assertArrayEquals(new byte[] {4,5,6}, returnedValue);
}
Expand Down

0 comments on commit d0f33a0

Please sign in to comment.