JdbcIndexEngine extends JdbcService implements IndexEngine { @Override public <T> Index<T> createIndex(Class<T> model) throws IndexExistsException { LOG.info("Creating Index for Class {}", model); if (indexExists(model)) { throw new IndexExistsException("Index for model " + model.getSimpleName() + " already exists"); } JdbcIndex<T> index = new IndexBuilder(translator).buildIndex(model); schemaMapper.create(index); removeUnmappedFields(index); persist(index); registry.put(index.getName(), index); return index; } JdbcIndexEngine(DataSource dataSource, SchemaMapper schemaMapper); @Override Index<T> createIndex(Class<T> model); @Override boolean indexExists(Class<?> model); @Override boolean indexExists(String name); @SuppressWarnings("unchecked") @Override JdbcIndex<T> getIndex(Class<T> model); @Override JdbcIndex<?> getIndex(String name); @Override void setClassLoader(ClassLoader classLoader); @Override void commit(IndexCommit commit); @Override List<Index<?>> getAll(); @Override void removeIndex(Index<?> index); void install();  }
@Test public void createIndex_works() throws Exception { Index<TestModel> index = indexEngine.createIndex(TestModel.class); assertEquals("org.openengsb.core.edbi.models.TestModel", index.getName()); assertEquals(TestModel.class, index.getModelClass()); assertEquals("INDEX_HEAD", index.getHeadTableName()); assertEquals("INDEX_HISTORY", index.getHistoryTableName()); assertEquals(3, index.getFields().size()); }
@Test(expected = IndexExistsException.class) public void createIndex_twice_throwsException() throws Exception { try { indexEngine.createIndex(TestModel.class); } catch (IndexExistsException e) { fail("IndexExistsException caught on wrong call"); } indexEngine.createIndex(TestModel.class); }
@Test public void load_works() throws Exception { Method loadIndex = JdbcIndexEngine.class.getDeclaredMethod("load", String.class); boolean accessibleFlag = loadIndex.isAccessible(); loadIndex.setAccessible(true); indexEngine.createIndex(TestModel.class); Index index = (Index) loadIndex.invoke(indexEngine, "org.openengsb.core.edbi.models.TestModel"); assertEquals("org.openengsb.core.edbi.models.TestModel", index.getName()); assertEquals(TestModel.class, index.getModelClass()); assertEquals("INDEX_HEAD", index.getHeadTableName()); assertEquals("INDEX_HISTORY", index.getHistoryTableName()); loadIndex.setAccessible(accessibleFlag); }