* test cases.
*
* @return a set of strings that describe the mismatches that occurred
*/
public static NSSet verifyAllSnapshots() {
NSMutableSet mismatches = new NSMutableSet();
NSMutableSet verifiedDatabases = new NSMutableSet();
EOEditingContext editingContext = ERXEC.newEditingContext();
EOModelGroup modelGroup = EOModelGroup.defaultGroup();
Enumeration modelsEnum = modelGroup.models().objectEnumerator();
while (modelsEnum.hasMoreElements()) {
EOModel model = (EOModel) modelsEnum.nextElement();
EODatabaseContext databaseContext = null;
try {
databaseContext = EODatabaseContext.registeredDatabaseContextForModel(model, editingContext);
}
catch (IllegalStateException e) {
log.warn("Model " + model.name() + " failed: " + e.getMessage());
}
if (databaseContext != null) {
databaseContext.lock();
try {
EODatabase database = databaseContext.database();
if (!verifiedDatabases.containsObject(database)) {
Enumeration gidEnum = database.snapshots().keyEnumerator();
while (gidEnum.hasMoreElements()) {
EOGlobalID gid = (EOGlobalID) gidEnum.nextElement();
if (gid instanceof EOKeyGlobalID) {
EOEnterpriseObject eo = null;
EOKeyGlobalID keyGID = (EOKeyGlobalID) gid;
String entityName = keyGID.entityName();
EOEntity entity = modelGroup.entityNamed(entityName);
NSDictionary snapshot = database.snapshotForGlobalID(gid);
if (snapshot != null) {
EOQualifier gidQualifier = entity.qualifierForPrimaryKey(entity.primaryKeyForGlobalID(gid));
EOFetchSpecification gidFetchSpec = new EOFetchSpecification(entityName, gidQualifier, null);
NSMutableDictionary databaseSnapshotClone;
NSMutableDictionary memorySnapshotClone = snapshot.mutableClone();
EOAdaptorChannel channel = databaseContext.availableChannel().adaptorChannel();
channel.openChannel();
channel.selectAttributes(entity.attributesToFetch(), gidFetchSpec, false, entity);
try {
databaseSnapshotClone = channel.fetchRow().mutableClone();
}
finally {
channel.cancelFetch();
}
// gidFetchSpec.setRefreshesRefetchedObjects(true);
// NSArray databaseEOs = editingContext.objectsWithFetchSpecification(gidFetchSpec);
if (databaseSnapshotClone == null) {
mismatches.addObject(gid + " was deleted in the database, but the snapshot still exists: " + memorySnapshotClone);
}
else {
// NSMutableDictionary refreshedSnapshotClone =
// database.snapshotForGlobalID(gid).mutableClone();
ERXDictionaryUtilities.removeMatchingEntries(memorySnapshotClone, databaseSnapshotClone);
if (databaseSnapshotClone.count() > 0 || memorySnapshotClone.count() > 0) {
mismatches.addObject(gid + " doesn't match the database: original = " + memorySnapshotClone + "; database = " + databaseSnapshotClone);
}
eo = (EOEnterpriseObject) editingContext.objectsWithFetchSpecification(gidFetchSpec).objectAtIndex(0);
}
}
if (eo != null) {
Enumeration relationshipsEnum = entity.relationships().objectEnumerator();
while (relationshipsEnum.hasMoreElements()) {
EORelationship relationship = (EORelationship) relationshipsEnum.nextElement();
String relationshipName = relationship.name();
NSArray originalDestinationGIDs = database.snapshotForSourceGlobalID(keyGID, relationshipName);
if (originalDestinationGIDs != null) {
NSMutableArray newDestinationGIDs = new NSMutableArray();
EOQualifier qualifier = relationship.qualifierWithSourceRow(database.snapshotForGlobalID(keyGID));
EOFetchSpecification relationshipFetchSpec = new EOFetchSpecification(entityName, qualifier, null);
EOAdaptorChannel channel = databaseContext.availableChannel().adaptorChannel();
channel.openChannel();
try {
channel.selectAttributes(relationship.destinationEntity().attributesToFetch(), relationshipFetchSpec, false, relationship.destinationEntity());
NSDictionary destinationSnapshot = null;
do {
destinationSnapshot = channel.fetchRow();
if (destinationSnapshot != null) {
EOGlobalID destinationGID = relationship.destinationEntity().globalIDForRow(destinationSnapshot);
newDestinationGIDs.addObject(destinationGID);
}
}
while (destinationSnapshot != null);
}
finally {
channel.cancelFetch();
}
NSArray objectsNotInDatabase = ERXArrayUtilities.arrayMinusArray(originalDestinationGIDs, newDestinationGIDs);
if (objectsNotInDatabase.count() > 0) {
mismatches.addObject(gid + "." + relationshipName + " has entries not in the database: " + objectsNotInDatabase);
}
NSArray objectsNotInMemory = ERXArrayUtilities.arrayMinusArray(newDestinationGIDs, originalDestinationGIDs);
if (objectsNotInMemory.count() > 0) {
mismatches.addObject(gid + "." + relationshipName + " is missing entries in the database: " + objectsNotInMemory);
}
}
}
}
}
}
verifiedDatabases.addObject(database);
}
}
finally {
databaseContext.unlock();
}