Skip to content

Commit

Permalink
Fix TestNG overloaded assertEquals method. WrenSecurity#71
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelhoral committed Feb 6, 2023
1 parent 0fd17af commit 8d7d881
Show file tree
Hide file tree
Showing 66 changed files with 327 additions and 337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Copyright 2006-2010 Sun Microsystems, Inc.
* Portions Copyright 2011-2016 ForgeRock AS.
* Portions Copyright 2013 Manuel Gaupp
* Portions Copyright 2023 Wren Security
*/
package org.opends.server;

Expand Down Expand Up @@ -1924,25 +1925,20 @@ public static String generateThreadDump() {
return dump.toString();
}

/** FIXME Replace with {@link Assert#assertNotEquals(Object, Object)} once we upgrade to testng >= 6.1. */
public static void assertNotEquals(Object actual1, Object actual2)
/**
* {@link Object#equals} based assertion to workaround sneaky TestNG overloaded method.
*/
public static void assertObjectEquals(Object actual, Object expected)
{
assertNotEquals(actual1, actual2, null);
Assert.assertEquals(actual, expected);
}

/** FIXME Replace with {@link Assert#assertNotEquals(Object, Object, String)} once we upgrade to testng >= 6.1. */
public static void assertNotEquals(Object actual1, Object actual2, String message)
/**
* {@link Object#equals} based assertion to workaround sneaky TestNG overloaded method.
*/
public static void assertObjectEquals(Object actual, Object expected, String message)
{
try
{
Assert.assertEquals(actual1, actual2);
Assert.fail(message);
}
catch (AssertionError e)
{
// this is good: they are not equals
return;
}
Assert.assertEquals(actual, expected, message);
}

public static int runLdapSearchTrustCertificateForSession(final String[] args)
Expand All @@ -1969,4 +1965,5 @@ public static int runLdapSearchTrustCertificateForSession(final PrintStream out,
System.setIn(stdin);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import static org.forgerock.opendj.ldap.ModificationType.DELETE;
import static org.forgerock.opendj.ldap.ModificationType.REPLACE;
import static org.forgerock.opendj.ldap.requests.Requests.newModifyRequest;
import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.opends.server.TestCaseUtils.getServerLdapPort;
import static org.opends.server.TestCaseUtils.restartServer;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ public void testGetPluginEntryDN()
nullPlugin.initializeInternal(DirectoryServer.getInstance().getServerContext(),
configuration.dn(), pluginTypes,
configuration.isInvokeForInternalOperations());
assertEquals(nullPlugin.getPluginEntryDN(), pluginEntryDN);
TestCaseUtils.assertObjectEquals(nullPlugin.getPluginEntryDN(), pluginEntryDN);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
*/
package org.opends.server.authorization.dseecompat;

import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.opends.server.config.ConfigConstants.ATTR_AUTHZ_GLOBAL_ACI;
import static org.opends.server.util.CollectionUtils.newArrayList;
import static org.opends.server.util.CollectionUtils.newHashSet;
import static org.opends.server.util.ServerConstants.EOL;
import static org.opends.server.util.ServerConstants.OID_PROXIED_AUTH_V2;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/
package org.opends.server.authorization.dseecompat;

import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.opends.server.config.ConfigConstants.ATTR_AUTHZ_GLOBAL_ACI;
import static org.opends.server.util.ServerConstants.OID_GET_EFFECTIVE_RIGHTS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package org.opends.server.authorization.dseecompat;

import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.opends.server.config.ConfigConstants.ATTR_AUTHZ_GLOBAL_ACI;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/
package org.opends.server.authorization.dseecompat;

import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.opends.server.config.ConfigConstants.ATTR_AUTHZ_GLOBAL_ACI;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
package org.opends.server.backends;

import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void testAddNoParent()

AddOperation addOperation = getRootConnection().processAdd(e);
assertEquals(addOperation.getResultCode(), ResultCode.NO_SUCH_OBJECT);
assertEquals(addOperation.getMatchedDN(), DN.valueOf("o=ldif"));
TestCaseUtils.assertObjectEquals(addOperation.getMatchedDN(), DN.valueOf("o=ldif"));
}


Expand Down Expand Up @@ -548,7 +548,7 @@ public void testBaseSearchNoSuchEntry() throws Exception
SearchRequest request = newSearchRequest(dn, SearchScope.BASE_OBJECT);
InternalSearchOperation searchOperation = getRootConnection().processSearch(request);
assertEquals(searchOperation.getResultCode(), ResultCode.NO_SUCH_OBJECT);
assertEquals(searchOperation.getMatchedDN(), DN.valueOf("o=ldif"));
TestCaseUtils.assertObjectEquals(searchOperation.getMatchedDN(), DN.valueOf("o=ldif"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import static org.forgerock.opendj.ldap.schema.CoreSchema.getObjectClassesAttributeType;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.opends.server.TestCaseUtils.getServerContext;
import static org.opends.server.protocols.internal.InternalClientConnection.getRootConnection;
import static org.opends.server.protocols.internal.InternalClientConnection.nextMessageID;
Expand All @@ -43,6 +42,7 @@
import static org.opends.server.util.StaticUtils.createEntry;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -163,7 +163,7 @@ public void testGetValidEntry()
DN schemaDN = DN.valueOf("cn=schema");
Entry schemaEntry = schemaBackend.getEntry(schemaDN);
assertNotNull(schemaEntry);
assertEquals(schemaEntry.getName(), schemaDN);
TestCaseUtils.assertObjectEquals(schemaEntry.getName(), schemaDN);

assertTrue(schemaEntry.hasAttribute(getAttributeTypesAttributeType()));
assertTrue(schemaEntry.hasAttribute(getObjectClassesAttributeType()));
Expand Down Expand Up @@ -202,7 +202,7 @@ public void testGetSchemaEntry() throws Exception
DN schemaDN = DN.valueOf("cn=schema");
Entry schemaEntry = schemaBackend.getSchemaEntry(schemaDN);
assertNotNull(schemaEntry);
assertEquals(schemaEntry.getName(), schemaDN);
TestCaseUtils.assertObjectEquals(schemaEntry.getName(), schemaDN);

assertTrue(schemaEntry.hasAttribute(getAttributeTypesAttributeType()));
assertTrue(schemaEntry.hasAttribute(getObjectClassesAttributeType()));
Expand All @@ -212,7 +212,7 @@ public void testGetSchemaEntry() throws Exception
schemaDN = DN.valueOf("cn=subschema");
schemaEntry = schemaBackend.getSchemaEntry(schemaDN);
assertNotNull(schemaEntry);
assertEquals(schemaEntry.getName(), schemaDN);
TestCaseUtils.assertObjectEquals(schemaEntry.getName(), schemaDN);

assertTrue(schemaEntry.hasAttribute(getAttributeTypesAttributeType()));
assertTrue(schemaEntry.hasAttribute(getObjectClassesAttributeType()));
Expand Down Expand Up @@ -4849,7 +4849,7 @@ public void testGetComponentEntryDN() throws Exception
{
DN configEntryDN =
DN.valueOf("ds-cfg-backend-id=schema,cn=Backends,cn=config");
assertEquals(schemaBackend.getComponentEntryDN(), configEntryDN);
TestCaseUtils.assertObjectEquals(schemaBackend.getComponentEntryDN(), configEntryDN);
}

/** Tests the {@code getClassName} method. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,12 +733,12 @@ public void testRenameEntry() throws Exception
renameEntry.setDN(newDN);
backend.renameEntry(prevDN, renameEntry, op);
Entry dbEntry = backend.getEntry(newDN);
assertEquals(dbEntry.getName(), newDN, "Renamed entry is missing.");
TestCaseUtils.assertObjectEquals(dbEntry.getName(), newDN, "Renamed entry is missing.");

renameEntry.setDN(prevDN);
backend.renameEntry(newDN, renameEntry, op);
dbEntry = backend.getEntry(prevDN);
assertEquals(dbEntry.getName(), prevDN, "Original entry has not been renamed");
TestCaseUtils.assertObjectEquals(dbEntry.getName(), prevDN, "Original entry has not been renamed");
}

@Test(description = "OPENDJ-2404")
Expand Down Expand Up @@ -797,7 +797,7 @@ private void baseSearch(boolean useInternalConnection) throws Exception
List<SearchResultEntry> result = runSearch(request, useInternalConnection);

assertEquals(result.size(), 1, "Base Search should return only one Entry");
assertEquals(result.get(0).getName(), testBaseDN, "Base Search on the suffix should return the suffix itself");
TestCaseUtils.assertObjectEquals(result.get(0).getName(), testBaseDN, "Base Search on the suffix should return the suffix itself");
}

@Test
Expand All @@ -814,8 +814,7 @@ private void oneLevelSearch(boolean useInternalConnection) throws Exception

assertEquals(result.size(), 1, "One Level search should return a single child entry");
SearchResultEntry resEntry = result.get(0);
assertEquals(topEntries.get(1).getName(), resEntry.getName(),
"One Level search should return the expected child");
TestCaseUtils.assertObjectEquals(topEntries.get(1).getName(), resEntry.getName(), "One Level search should return the expected child");
}

@Test
Expand Down Expand Up @@ -914,7 +913,7 @@ public void testUserEntrySearch(boolean useInternalConnection, SearchScope scope
+ " child entry");
if (expectedEntryCount > 0)
{
assertEquals(searchDN, result.get(0).getName(), "User entry search should return the expected entry");
TestCaseUtils.assertObjectEquals(searchDN, result.get(0).getName(), "User entry search should return the expected entry");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public void testEntryToAndFromDatabase() throws Exception {

// check DN and number of attributes
assertThat(entryBefore.getAllAttributes()).hasSameSizeAs(entryAfter.getAllAttributes());
assertEquals(entryBefore.getName(), entryAfter.getName());
TestCaseUtils.assertObjectEquals(entryBefore.getName(), entryAfter.getName());

// check the object classes were not changed
for (String ocBefore : entryBefore.getObjectClasses().values()) {
Expand All @@ -359,7 +359,7 @@ public void testEntryToAndFromDatabase() throws Exception {
for (Attribute attrAfter : listAfter) {
if (attrAfter.getAttributeDescription().equals(attrBefore.getAttributeDescription())) {
// Found the corresponding attribute
assertEquals(attrBefore, attrAfter);
TestCaseUtils.assertObjectEquals(attrBefore, attrAfter);
found = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package org.opends.server.backends.task;

import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.opends.server.util.ServerConstants.DATE_FORMAT_GMT_TIME;
import static org.opends.server.util.ServerConstants.TIME_ZONE_UTC;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;

import java.text.SimpleDateFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static org.forgerock.opendj.ldap.requests.Requests.newModifyRequest;
import static org.forgerock.opendj.ldap.requests.Requests.newSearchRequest;
import static org.forgerock.opendj.ldap.requests.Requests.newSimpleBindRequest;
import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.opends.server.TestCaseUtils.getServerLdapPort;
import static org.opends.server.controls.PasswordPolicyErrorType.ACCOUNT_LOCKED;
import static org.opends.server.controls.PasswordPolicyErrorType.CHANGE_AFTER_RESET;
Expand All @@ -38,6 +37,7 @@
import static org.opends.server.controls.PasswordPolicyErrorType.PASSWORD_TOO_YOUNG;
import static org.opends.server.util.ServerConstants.OID_PASSWORD_POLICY_CONTROL;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.forgerock.opendj.ldap.requests.Requests.newModifyRequest;
import static org.opends.server.TestCaseUtils.assertNotEquals;
import static org.opends.server.controls.PersistentSearchChangeType.ADD;
import static org.opends.server.controls.PersistentSearchChangeType.DELETE;
import static org.opends.server.controls.PersistentSearchChangeType.MODIFY;
Expand All @@ -29,6 +28,7 @@
import static org.opends.server.util.ServerConstants.OID_ENTRY_CHANGE_NOTIFICATION;
import static org.opends.server.util.ServerConstants.OID_PERSISTENT_SEARCH;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -420,7 +420,7 @@ public void checkEntryChangeNotificationControlTest(
assertEquals(OID_ENTRY_CHANGE_NOTIFICATION, ecnc.getOID());
assertEquals(changeNumber, ecnc.getChangeNumber());
assertEquals(type, ecnc.getChangeType());
assertEquals(dn, ecnc.getPreviousDN());
TestCaseUtils.assertObjectEquals(dn, ecnc.getPreviousDN());
assertEquals(false, ecnc.isCritical()) ;
checkEntryChangeNotificationControlToString(ecnc);

Expand All @@ -435,7 +435,7 @@ public void checkEntryChangeNotificationControlTest(
assertEquals(ecnc.getOID(),newEcnc.getOID());
assertEquals(ecnc.getChangeNumber(),newEcnc.getChangeNumber());
assertEquals(ecnc.getChangeType(),newEcnc.getChangeType());
assertEquals(ecnc.getPreviousDN(),newEcnc.getPreviousDN());
TestCaseUtils.assertObjectEquals(ecnc.getPreviousDN(), newEcnc.getPreviousDN());
assertEquals(ecnc.isCritical(),newEcnc.isCritical()) ;
}
catch (DirectoryException e)
Expand All @@ -457,7 +457,7 @@ public void checkEntryChangeNotificationControlTest(
assertEquals(OID_ENTRY_CHANGE_NOTIFICATION, ecnc.getOID());
assertEquals(changeNumber, ecnc.getChangeNumber());
assertEquals(type, ecnc.getChangeType());
assertEquals(dn, ecnc.getPreviousDN());
TestCaseUtils.assertObjectEquals(dn, ecnc.getPreviousDN());
assertEquals(isCritical, ecnc.isCritical()) ;
checkEntryChangeNotificationControlToString(ecnc);

Expand All @@ -472,7 +472,7 @@ public void checkEntryChangeNotificationControlTest(
assertEquals(ecnc.getOID(),newEcnc.getOID());
assertEquals(ecnc.getChangeNumber(),newEcnc.getChangeNumber());
assertEquals(ecnc.getChangeType(),newEcnc.getChangeType());
assertEquals(ecnc.getPreviousDN(),newEcnc.getPreviousDN());
TestCaseUtils.assertObjectEquals(ecnc.getPreviousDN(), newEcnc.getPreviousDN());
assertEquals(ecnc.isCritical(),newEcnc.isCritical()) ;
}
catch (DirectoryException e)
Expand Down
Loading

0 comments on commit 8d7d881

Please sign in to comment.