Skip to content

Instantly share code, notes, and snippets.

@alan-morey
Created April 6, 2015 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alan-morey/c4c1c0f0562f940c49f6 to your computer and use it in GitHub Desktop.
Save alan-morey/c4c1c0f0562f940c49f6 to your computer and use it in GitHub Desktop.
Salesforce Spring '15 - System.hashCode() producing unexpected result when using same value with different reference types.
// Unexpected results for System.hashCode() when using same values for Salesforce IDs with different reference types.
//
// https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_system.htm?CSHID=apex_methods_system_system.htm#apex_system_system_hashcode
//
// API v33
@isTest
class SystemHashCodeBugTest {
static final String STR_VAL = 'foobar';
static final Id USER_ID_VAL = UserInfo.getUserId();
static final Id LITERAL_ID_VAL = '001000000000001';
static final Account acc;
static {
insert acc = new Account(name = 'SystemHashCodeBugTestAcc');
}
@isTest // PASS
static void hashCode_shouldProduceEqualHashCodesForSameStringValue_whenGivenStringAndObjectReferences() {
String strRef = STR_VAL;
Object objRef = STR_VAL;
System.assertEquals(System.hashCode(strRef), System.hashCode(objRef)); // Works, as expected
}
// Tests with UserId using UserInfo.getUserId()
@isTest // PASS
static void hashCode_shouldProduceEqualHashCodesForSameUserIdValue_whenGivenIdAndObjectReferences() {
Id idRef = USER_ID_VAL;
Object objRef = USER_ID_VAL;
System.assertEquals(System.hashCode(idRef), System.hashCode(objRef));
}
@isTest // FAIL
static void hashCode_shouldProduceEqualHashCodesForSameUserIdValue_whenGivenIdAndStringReferences() {
Id idRef = USER_ID_VAL;
String strRef = USER_ID_VAL;
System.assertEquals(System.hashCode(idRef), System.hashCode(strRef)); // Assertion Failed: Expected: 1126342621, Actual: -1761420659
}
@isTest // FAIL
static void hashCode_shouldProduceEqualHashCodesForSameUserIdValue_whenGivenStringAndObjectReferences() {
String strRef = USER_ID_VAL;
Object objRef = USER_ID_VAL;
System.assertEquals(System.hashCode(strRef), System.hashCode(objRef)); // Assertion Failed: Expected: -1761420659, Actual: 1599584495
}
// Tests with literal Id value
@isTest // PASS
static void hashCode_shouldProduceEqualHashCodesForLiteralIdValue_whenGivenIdAndObjectReferences() {
Id idRef = LITERAL_ID_VAL;
Object objRef = LITERAL_ID_VAL;
System.assertEquals(System.hashCode(idRef), System.hashCode(objRef));
}
@isTest // FAIL
static void hashCode_shouldProduceEqualHashCodesForLiteralIdValue_whenGivenIdAndStringReferences() {
Id idRef = LITERAL_ID_VAL;
String strRef = LITERAL_ID_VAL;
System.assertEquals(System.hashCode(idRef), System.hashCode(strRef)); // Assertion Failed: Expected: 1832992728, Actual: -1002649041
}
@isTest // FAIL
static void hashCode_shouldProduceEqualHashCodesForLiteralIdValue_whenGivenStringAndObjectReferences() {
String strRef = LITERAL_ID_VAL;
Object objRef = LITERAL_ID_VAL;
System.assertEquals(System.hashCode(strRef), System.hashCode(objRef)); //Assertion Failed: Expected: -1002649041, Actual: 194099034
}
// Tests with SObject Id value
@isTest // FAIL
static void hashCode_shouldProduceEqualHashCodesForSobjectIdValue_whenGivenIdAndObjectReferences() {
Id idRef = acc.id;
Object objRef = acc.id;
System.assertEquals(System.hashCode(idRef), System.hashCode(objRef)); // Assertion Failed: Expected: 873699696, Actual: 1925096133
}
@isTest // FAIL
static void hashCode_shouldProduceEqualHashCodesForSobjectIdValue_whenGivenIdAndStringReferences() {
Id idRef = acc.id;
String strRef = acc.id;
System.assertEquals(System.hashCode(idRef), System.hashCode(strRef)); // Assertion Failed: Expected: 1735032254, Actual: -518459028
}
@isTest // FAIL
static void hashCode_shouldProduceEqualHashCodesForSobjectIdValue_whenGivenStringAndObjectReferences() {
String strRef = acc.id;
Object objRef = acc.id;
System.assertEquals(System.hashCode(strRef), System.hashCode(objRef)); // Assertion Failed: Expected: -518429237, Actual: 849992717
}
}
@alan-morey
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment