From d0f33a0b67d331658cfea8687f0a509a32dd39cb Mon Sep 17 00:00:00 2001 From: Tigran Mkrtchyan Date: Fri, 23 Apr 2021 15:07:55 +0200 Subject: [PATCH] junit: simplify unit test assertions assertFalse(!value) => assertTrue(value) assertTrue(a.equals(b)) -> assertEquals(a, b) Acked-by: Marina Sahakyan Acked-by: Lea Morschel Target: master --- .../org/dcache/oncrpc4j/rpc/OncRpcProgramTest.java | 4 ++-- .../dcache/oncrpc4j/rpc/OncRpcSvcBuilderTest.java | 6 +++--- .../java/org/dcache/oncrpc4j/rpc/OncRpcSvcTest.java | 10 +++++----- .../java/org/dcache/oncrpc4j/xdr/XdrOpaqueTest.java | 8 ++++---- .../test/java/org/dcache/oncrpc4j/xdr/XdrTest.java | 4 ++-- .../oncrpc4j/rpcgen/BigConstsGenerationTest.java | 12 ++++++------ .../dcache/oncrpc4j/rpcgen/SyncBlobStoreTest.java | 2 +- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcProgramTest.java b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcProgramTest.java index 75c89329..0fed7bd1 100644 --- a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcProgramTest.java +++ b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcProgramTest.java @@ -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 @@ -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); } } \ No newline at end of file diff --git a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcSvcBuilderTest.java b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcSvcBuilderTest.java index 03edaa24..3d205787 100644 --- a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcSvcBuilderTest.java +++ b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcSvcBuilderTest.java @@ -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 @@ -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 @@ -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) diff --git a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcSvcTest.java b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcSvcTest.java index ef743824..a0f5005f 100644 --- a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcSvcTest.java +++ b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/rpc/OncRpcSvcTest.java @@ -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 @@ -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 @@ -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 } } diff --git a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrOpaqueTest.java b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrOpaqueTest.java index e349a1ba..34cb3644 100644 --- a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrOpaqueTest.java +++ b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrOpaqueTest.java @@ -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 @@ -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 @@ -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); } diff --git a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrTest.java b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrTest.java index a22aca70..5884bc57 100644 --- a/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrTest.java +++ b/oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrTest.java @@ -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 @@ -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 diff --git a/oncrpc4j-rpcgen/src/test/java/org/dcache/oncrpc4j/rpcgen/BigConstsGenerationTest.java b/oncrpc4j-rpcgen/src/test/java/org/dcache/oncrpc4j/rpcgen/BigConstsGenerationTest.java index b12b48ca..6a4ca8be 100644 --- a/oncrpc4j-rpcgen/src/test/java/org/dcache/oncrpc4j/rpcgen/BigConstsGenerationTest.java +++ b/oncrpc4j-rpcgen/src/test/java/org/dcache/oncrpc4j/rpcgen/BigConstsGenerationTest.java @@ -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)); } } diff --git a/oncrpc4j-rpcgen/src/test/java/org/dcache/oncrpc4j/rpcgen/SyncBlobStoreTest.java b/oncrpc4j-rpcgen/src/test/java/org/dcache/oncrpc4j/rpcgen/SyncBlobStoreTest.java index 426fbc50..bf0c1003 100644 --- a/oncrpc4j-rpcgen/src/test/java/org/dcache/oncrpc4j/rpcgen/SyncBlobStoreTest.java +++ b/oncrpc4j-rpcgen/src/test/java/org/dcache/oncrpc4j/rpcgen/SyncBlobStoreTest.java @@ -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); }