DefaultPersistenceIndex implements PersistenceIndex { @Override public List<ObjectInfo> findIndexObject(Class<?> beanClass) { LOGGER.trace("Looking for bean class {} in index {}", beanClass.getName(), indexFile.toString()); List<ObjectInfo> retVal = Lists.newArrayList(); synchronized (index) { for (ObjectInfo info : index) { if (info.containsClass(beanClass)) { retVal.add(info); LOGGER.debug("Looking for {} in index {} and found " + info.toString(), beanClass.getName(), indexFile.toString()); } } } LOGGER.debug("Found {} objects of type bean class {} in index: " + indexFile.toString(), retVal.size(), beanClass.getName()); return retVal; } DefaultPersistenceIndex(File indexDirectory, ObjectPersistenceBackend persistenceBackend); @Override @SuppressWarnings("unchecked") void indexObject(Class<?> bean, File beanLocation); @Override void removeIndexObject(ObjectInfo info); @Override void updateIndex(); @Override List<ObjectInfo> findIndexObject(Class<?> beanClass);  }
@Test public void testIndexObject_shouldPersistIndexForObjectAndMakeItRetrievableAgainFromLoadedIndex() throws Exception { File tmpDir = FileHelper.createTempDirectory(); persistBean(tmpDir, B.class); DefaultPersistenceIndex defaultPersistenceIndex = new DefaultPersistenceIndex(tmpDir, new DefaultObjectPersistenceBackend()); List<ObjectInfo> findIndexObject = defaultPersistenceIndex.findIndexObject(B.class); assertEquals(1, findIndexObject.size()); }
@Test public void testSearch_shouldAlsoFindObjectBySubclass() throws Exception { File tmpDir = FileHelper.createTempDirectory(); persistBean(tmpDir, B.class); DefaultPersistenceIndex defaultPersistenceIndex = new DefaultPersistenceIndex(tmpDir, new DefaultObjectPersistenceBackend()); List<ObjectInfo> findIndexObject = defaultPersistenceIndex.findIndexObject(A.class); assertEquals(1, findIndexObject.size()); }
@Test public void testSearch_shouldAlsoFindObjectByInterface() throws Exception { File tmpDir = FileHelper.createTempDirectory(); persistBean(tmpDir, B.class); DefaultPersistenceIndex defaultPersistenceIndex = new DefaultPersistenceIndex(tmpDir, new DefaultObjectPersistenceBackend()); List<ObjectInfo> findIndexObject = defaultPersistenceIndex.findIndexObject(Z.class); assertEquals(1, findIndexObject.size()); }
@Test public void testSearch_shouldAlsoFindObjectBySubInterface() throws Exception { File tmpDir = FileHelper.createTempDirectory(); persistBean(tmpDir, B.class); DefaultPersistenceIndex defaultPersistenceIndex = new DefaultPersistenceIndex(tmpDir, new DefaultObjectPersistenceBackend()); List<ObjectInfo> findIndexObject = defaultPersistenceIndex.findIndexObject(X.class); assertEquals(1, findIndexObject.size()); }
@Test public void testPersistingMultibleObjectsAtDifferentLvls_shouldReturnByA() throws Exception { File tmpDir = FileHelper.createTempDirectory(); persistBean(tmpDir, B.class, A.class); DefaultPersistenceIndex defaultPersistenceIndex = new DefaultPersistenceIndex(tmpDir, new DefaultObjectPersistenceBackend()); List<ObjectInfo> findIndexObject = defaultPersistenceIndex.findIndexObject(A.class); assertEquals(2, findIndexObject.size()); }
@Test public void testPersistingMultibleObjectsAtDifferentLvls_shouldReturnByB() throws Exception { File tmpDir = FileHelper.createTempDirectory(); persistBean(tmpDir, B.class, A.class); DefaultPersistenceIndex defaultPersistenceIndex = new DefaultPersistenceIndex(tmpDir, new DefaultObjectPersistenceBackend()); List<ObjectInfo> findIndexObject = defaultPersistenceIndex.findIndexObject(B.class); assertEquals(1, findIndexObject.size()); }