java.lang.Objectcom.webobjects.eocontrol.EOObjectStore
com.webobjects.eocontrol.EOEditingContext
org.wounittest.MockEditingContext
public class MockEditingContext
MockEditingContext is a subclass of EOEditingContext
that can be used for fast in-memory testing of business objects.
Unit tests for logic which use fetch specifications or save to an editing context can be relative slow because full roundtrips to a database are being performed. Testing the same logic with a MockEditingContext ensures that nothing is being saved to or read from a database resulting in shorter execution time of the unit test. Also, you don't risk invalidating the persistent data with broken test cases.
Assuming there is an EOCustomObject called Person with the
attributes name, age, nationality and
partner. nationality is of entity Country
for which there is a constant list of objects in the database. We want to write a test for the
static Person method createFromDescription(String, EOEditingContext).
import org.junit.*;
import static org.junit.Assert.*;
import org.wounittest.*;
import static org.wounittest.WOAssert.*;
public class PersonTest extends EOFTest {
Person jane;
@Before
public void setUp() throws Exception {
super.setUp();
mockEditingContext().setEntityNamesToGetFromDatabase(new NSArray("Country"));
jane = (Person)mockEditingContext().createSavedObject("Person");
jane.setName("Jane Doe");
jane.setNationalityRelationship(Country.withName("Canada", mockEditingContext()));
}
@Test
public void creationFromDescriptionWithExistingPartner() {
Person newPerson = Person.createFromDescription("John Doe, 34, Canada, Jane Doe",
mockEditingContext());
assertEquals("John Doe", newPerson.name());
assertEquals(34, newPerson.age());
assertSame(Country.withName("Canada", mockEditingContext()), newPerson.nationality());
assertSame(jane, newPerson.partner());
}
@Test
public void creationFromDescriptionWithUnknownPartner() {
Person newPerson = Person.createFromDescription("Bla Fasel, 12, Germany, Jeniffer Doe",
mockEditingContext());
assertEquals("Blah Fasel", newPerson.name());
assertEquals(12, newPerson.age());
assertSame(Country.withName("Germany", mockEditingContext()), newPerson.nationality());
assertNull(newPerson.partner());
}
@Test(expected = UnknownCountryException.class)
public void creationFromDescriptionWithUnknownCountry() {
Person.createFromDescription("Bla Fasel, 12, Wawaland, Jane Doe",
mockEditingContext());
}
}
Additional information can be found in WOUTMockEditingContextTest.java.
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class com.webobjects.eocontrol.EOEditingContext |
|---|
EOEditingContext._EventLoggingEnabler, EOEditingContext.Delegate, EOEditingContext.EditingContextEvent, EOEditingContext.Editor, EOEditingContext.MessageHandler |
| Nested classes/interfaces inherited from interface com.webobjects.eocontrol.EOKeyValueArchiving |
|---|
EOKeyValueArchiving._NullValueSupport, EOKeyValueArchiving._NumberSupport, EOKeyValueArchiving._TimestampSupport, EOKeyValueArchiving.Awaking, EOKeyValueArchiving.FinishInitialization, EOKeyValueArchiving.Support |
| Field Summary | |
|---|---|
protected NSArray |
entityNamesToGetFromDatabase
|
protected static int |
fakePrimaryKeyCounter
|
protected NSMutableArray |
ignoredObjects
|
| Fields inherited from class com.webobjects.eocontrol.EOObjectStore |
|---|
_doAssertLock, _doAssertLockInitialized, _wasDisposed, DeletedKey, InsertedKey, InvalidatedAllObjectsInStoreNotification, InvalidatedKey, ObjectsChangedInStoreNotification, UpdatedKey |
| Fields inherited from interface com.webobjects.foundation.NSLocking |
|---|
OneCentury, OneDay, OneHour, OneMinute, OneSecond, OneWeek, OneYear |
| Constructor Summary | |
|---|---|
MockEditingContext()
Constructs a MockEditingContext. |
|
| Method Summary | |
|---|---|
protected EOGlobalID |
assignFakeGlobalIDToObject(EOCustomObject anObject)
Internal helper method for insertSavedObject. |
EOCustomObject |
createSavedObject(String anEntityName)
Convenience cover method for insertSavedObject. |
void |
dispose()
Extends the implementation inherited from EOEditingContext to delete ignoredObjects. |
void |
insertSavedObject(EOCustomObject anObject)
Inserts a Custom Object into the receiver and makes it look as if it was fetched from the database. |
NSArray |
objectsWithFetchSpecification(EOFetchSpecification aFetchSpecification,
EOEditingContext anEditingContext)
Overrides the implementation inherited from EOEditingContext to fetch objects from the array of registeredObjects of the receiver instead of going to the database. |
void |
objectWillChange(Object anObject)
Overrides the implementation inherited from EOEditingContext to ignore objects registered with insertSavedObject. |
EOObjectStore |
rootObjectStore()
Overwritten to return the defaultParentObjectStore. |
void |
setEntityNamesToGetFromDatabase(NSArray theEntityNamesToGetFromDatabase)
Defines which entities should be fetched from the rootObjectStore. |
| Methods inherited from class com.webobjects.eocontrol.EOObjectStore |
|---|
_checkAssertLock, _resetAssertLock, _suppressAssertLock |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected static int fakePrimaryKeyCounter
protected NSMutableArray ignoredObjects
protected NSArray entityNamesToGetFromDatabase
| Constructor Detail |
|---|
public MockEditingContext()
MockObjectStore as parent object store.
| Method Detail |
|---|
public void setEntityNamesToGetFromDatabase(NSArray theEntityNamesToGetFromDatabase)
theEntityNamesToGetFromDatabase - array of entity names which should be fetched from the databasepublic EOObjectStore rootObjectStore()
defaultParentObjectStore.
rootObjectStore in class EOEditingContextEOEditingContext.defaultParentObjectStore()
public NSArray objectsWithFetchSpecification(EOFetchSpecification aFetchSpecification,
EOEditingContext anEditingContext)
registeredObjects of the receiver instead of going to the database.
Only entities defined with setEntityNamesToGetFromDatabase are still being fetched from the database using the rootObjectStore.
Throws UnsupportedOperationException if aFetchSpecification is configured to return raw rows.
Hints are ignored.
objectsWithFetchSpecification in class EOEditingContextaFetchSpecification - the criteria specified for fetchanEditingContext - the destination EOEditingContext, needs to be the same as the receiverpublic EOCustomObject createSavedObject(String anEntityName)
insertSavedObject.
Creates a new Custom Object for the specified entity, inserts it into the receiver using insertSavedObject, and returns the new object.
anEntityName - the name of entitypublic void insertSavedObject(EOCustomObject anObject)
anObject - the Custom Objectprotected EOGlobalID assignFakeGlobalIDToObject(EOCustomObject anObject)
insertSavedObject.
public void objectWillChange(Object anObject)
insertSavedObject.
objectWillChange in interface EOObservingobjectWillChange in class EOEditingContextanObject - the object whose state is to be recordedpublic void dispose()
dispose in interface NSDisposabledispose in class EOEditingContext