*/
public class DecoratorTest {
@Test
public void test() throws Exception {
LilyProxy lilyProxy = createLilyProxy();
LilyClient client = lilyProxy.getLilyServerProxy().getClient();
RepositoryModelImpl repositoryModel = new RepositoryModelImpl(lilyProxy.getLilyServerProxy().getZooKeeper());
repositoryModel.create("repo1");
assertTrue(repositoryModel.waitUntilRepositoryInState("repo1", RepositoryLifecycleState.ACTIVE, 60000L));
repositoryModel.create("repo2");
assertTrue(repositoryModel.waitUntilRepositoryInState("repo2", RepositoryLifecycleState.ACTIVE, 60000L));
repositoryModel.close();
// Make a schema
TypeManager typeMgr = client.getDefaultRepository().getTypeManager();
QName field1 = new QName("ns", "f1");
FieldType fieldType1 = typeMgr.newFieldType(typeMgr.getValueType("STRING"), field1, Scope.NON_VERSIONED);
fieldType1 = typeMgr.createFieldType(fieldType1);
QName field2 = new QName("ns", "f2");
FieldType fieldType2 = typeMgr.newFieldType(typeMgr.getValueType("STRING"), field2, Scope.NON_VERSIONED);
fieldType2 = typeMgr.createFieldType(fieldType2);
QName typeName = new QName("ns", "rt1");
RecordType recordType = typeMgr.newRecordType(typeName);
recordType.addFieldTypeEntry(fieldType1.getId(), false);
recordType.addFieldTypeEntry(fieldType2.getId(), false);
recordType = typeMgr.createRecordType(recordType);
DecoratingRepositoryManager repositoryMgr = (DecoratingRepositoryManager)lilyProxy.getLilyServerProxy()
.getLilyServerTestingUtility().getRuntime().getModuleById("repository")
.getApplicationContext().getBean("repositoryManager");
IdentityHashMap<Object, Object> chains = new IdentityHashMap<Object, Object>();
// Test the decorator is applied for each repository and each table
for (String repositoryName : new String[] {"default", "repo1", "repo2"}) {
LRepository repository = client.getRepository(repositoryName);
repository.getTableManager().createTable("table1");
repository.getTableManager().createTable("table2");
for (String tableName : new String[] {"record", "table1", "table2"}) {
LTable table = repository.getTable(tableName);
Record record = table.newRecord();
record.setRecordType(typeName);
record.setField(field1, "foobar");
record = table.create(record);
assertEquals("foo", record.getField(field2));
assertEquals("foo", table.read(record.getId()).getField(field2));
// Test we can get access to our test decorator: this is something that is occasionally useful
// in test cases to check certain conditions, e.g. if the decorator would have async side effects.
RepositoryDecoratorChain chain = repositoryMgr.getRepositoryDecoratorChain(repositoryName, tableName);
assertNotNull(chain);
assertNotNull(chain.getDecorator(TestRepositoryDecoratorFactory.NAME));
chains.put(chain.getDecorator(TestRepositoryDecoratorFactory.NAME), null);
assertEquals(2, chain.getEntries().size());
}
}
// There should be one instance of the decorator created for each repository-table combination (that
// was accessed)
assertEquals(3 * 3, chains.size());
// Check that if we ask the same table twice, we get the same instance (verifies cache works)
assertTrue(client.getRepository("repo1").getTable("table1") == client.getRepository("repo1").getTable("table1"));
lilyProxy.stop();
// Check each of the decorators was properly closed
assertEquals(9, TestRepositoryDecoratorFactory.CLOSE_COUNT.get());
}