Test type no. 1 - test of PAT aspect itself
When applying PAT to any software I have to test if PAT works correctly. To do
that I need to write tests of the aspect.
And PAT is a persistence aspect, so what I need to
test is if the data is really stored on the disk after some persistent
operation. And it means I must check if the database I use (Prevayler)
behaves correctly: stores data changes on the file system,
when recovering does it read
files in correct order and does it restore the data after failure.
And persistent operation is PAT's transaction invoked on business
object (BO).
So here is sample code, of how do I test my PAT aspect:
public void testModifyingRootAndResetingSystem()
{
assertEquals(0, getFilesNumberInRepo());
TestRoot root = new TestRoot();
assertEquals(1, getFilesNumberInRepo());
((Persisteble)root).takeSnapshot();
assertEquals(1, getFilesNumberInRepo());
assertEquals("000000000000000000000.snapshot",
getLastFileFromRepo());
root.newUser("user 1");
assertEquals(1, root.getUsers().size());
assertEquals(2, getFilesNumberInRepo());
assertEquals("000000000000000000001.commandLog",
getLastFileFromRepo());
//Emulation of database restart
Pat.unload();
TestRoot rootReloaded = new TestRoot();
if (rootReloaded == root)
throw new AssertionError("Roots' references should differ");
assertEquals(2, getFilesNumberInRepo());
assertEquals("000000000000000000001.commandLog",
getLastFileFromRepo());
assertEquals("Should be 1 user", 1, rootReloaded.getUsers().size());
}
Test type no. 2 - testing Ilybra's business methods
It is trivial to test Ilybra application and its business methods. Due to the fact, any business class created with PAT is plain Java class without any dependency on external resources, I can just invoke the object's methods without any "mocks fun".Code of the test case - again - without any cheating it is one of my original test cases. There is no external configuration to the test case. None of aspects is applied here, no custom advice here except PAT's annotations on transaction methods (lendCopy).
Note: I test core functionality here. And this is the most unique. PAT lets you create such simple test cases for an applicationpublic void setUp()
{
Pat.unload();
removeDatabaseFiles();
world = new World();
library = world.getLibrary();
}
public void testLendBookToReader()
{
Book book = TestObjectFactory.addBookWithAuthor(world, "Java", "Author");
Copy copy = TestObjectFactory.addCopy(world, book, "S.2409");
Reader reader = TestObjectFactory.addStudent(world, "Nazar", "Tomasz");
assertEquals(0, reader.getCopies().getSize());
library.lendCopy(copy, reader, Dates.now(), Dates.nowPlus(14));
assertEquals(1, reader.getCopies().getSize());
assertEquals(reader, copy.getReader());
assertEquals(Dates.now(), copy.getLendDate());
assertEquals(Dates.nowPlus(14), copy.getReturnDate());
}
With those two examples above I think I've just exhausted my "AOP & testing" topic.
Have fun and take care!
Tomasz Nazar