@Test
@TestDir
public void services() throws Exception {
String dir = getTestDir().getAbsolutePath();
XConfiguration conf;
Server server;
// no services
ORDER.clear();
conf = new XConfiguration();
server = new Server("server", dir, dir, dir, dir, conf);
server.init();
Assert.assertEquals(ORDER.size(), 0);
// 2 services init/destroy
ORDER.clear();
String services = StringUtils.toString(Arrays.asList(MyService1.class.getName(), MyService3.class.getName()),
",");
conf = new XConfiguration();
conf.set("server.services", services);
server = new Server("server", dir, dir, dir, dir, conf);
server.init();
Assert.assertEquals(server.get(MyService1.class).getInterface(), MyService1.class);
Assert.assertEquals(server.get(MyService3.class).getInterface(), MyService3.class);
Assert.assertEquals(ORDER.size(), 4);
Assert.assertEquals(ORDER.get(0), "s1.init");
Assert.assertEquals(ORDER.get(1), "s3.init");
Assert.assertEquals(ORDER.get(2), "s1.postInit");
Assert.assertEquals(ORDER.get(3), "s3.postInit");
server.destroy();
Assert.assertEquals(ORDER.size(), 6);
Assert.assertEquals(ORDER.get(4), "s3.destroy");
Assert.assertEquals(ORDER.get(5), "s1.destroy");
// 3 services, 2nd one fails on init
ORDER.clear();
services = StringUtils.toString(Arrays.asList(MyService1.class.getName(), MyService2.class.getName(),
MyService3.class.getName()), ",");
conf = new XConfiguration();
conf.set("server.services", services);
server = new Server("server", dir, dir, dir, dir, conf);
try {
server.init();
Assert.fail();
}
catch (ServerException ex) {
Assert.assertEquals(MyService2.class, ex.getError().getClass());
}
catch (Exception ex) {
Assert.fail();
}
Assert.assertEquals(ORDER.size(), 3);
Assert.assertEquals(ORDER.get(0), "s1.init");
Assert.assertEquals(ORDER.get(1), "s2.init");
Assert.assertEquals(ORDER.get(2), "s1.destroy");
// 2 services one fails on destroy
ORDER.clear();
services = StringUtils.toString(Arrays.asList(MyService1.class.getName(), MyService5.class.getName()), ",");
conf = new XConfiguration();
conf.set("server.services", services);
server = new Server("server", dir, dir, dir, dir, conf);
server.init();
Assert.assertEquals(ORDER.size(), 4);
Assert.assertEquals(ORDER.get(0), "s1.init");
Assert.assertEquals(ORDER.get(1), "s5.init");
Assert.assertEquals(ORDER.get(2), "s1.postInit");
Assert.assertEquals(ORDER.get(3), "s5.postInit");
server.destroy();
Assert.assertEquals(ORDER.size(), 6);
Assert.assertEquals(ORDER.get(4), "s5.destroy");
Assert.assertEquals(ORDER.get(5), "s1.destroy");
// service override via ext
ORDER.clear();
services = StringUtils.toString(Arrays.asList(MyService1.class.getName(), MyService3.class.getName()), ",");
String servicesExt = StringUtils.toString(Arrays.asList(MyService1a.class.getName()), ",");
conf = new XConfiguration();
conf.set("server.services", services);
conf.set("server.services.ext", servicesExt);
server = new Server("server", dir, dir, dir, dir, conf);
server.init();
Assert.assertEquals(server.get(MyService1.class).getClass(), MyService1a.class);
Assert.assertEquals(ORDER.size(), 4);
Assert.assertEquals(ORDER.get(0), "s1a.init");
Assert.assertEquals(ORDER.get(1), "s3.init");
Assert.assertEquals(ORDER.get(2), "s1a.postInit");
Assert.assertEquals(ORDER.get(3), "s3.postInit");
server.destroy();
Assert.assertEquals(ORDER.size(), 6);
Assert.assertEquals(ORDER.get(4), "s3.destroy");
Assert.assertEquals(ORDER.get(5), "s1a.destroy");
// service override via setService
ORDER.clear();
services = StringUtils.toString(Arrays.asList(MyService1.class.getName(), MyService3.class.getName()), ",");
conf = new XConfiguration();
conf.set("server.services", services);
server = new Server("server", dir, dir, dir, dir, conf);
server.init();
server.setService(MyService1a.class);
Assert.assertEquals(ORDER.size(), 6);
Assert.assertEquals(ORDER.get(4), "s1.destroy");
Assert.assertEquals(ORDER.get(5), "s1a.init");
Assert.assertEquals(server.get(MyService1.class).getClass(), MyService1a.class);
server.destroy();
Assert.assertEquals(ORDER.size(), 8);
Assert.assertEquals(ORDER.get(6), "s3.destroy");
Assert.assertEquals(ORDER.get(7), "s1a.destroy");
// service add via setService
ORDER.clear();
services = StringUtils.toString(Arrays.asList(MyService1.class.getName(), MyService3.class.getName()), ",");
conf = new XConfiguration();
conf.set("server.services", services);
server = new Server("server", dir, dir, dir, dir, conf);
server.init();
server.setService(MyService5.class);
Assert.assertEquals(ORDER.size(), 5);
Assert.assertEquals(ORDER.get(4), "s5.init");
Assert.assertEquals(server.get(MyService5.class).getClass(), MyService5.class);
server.destroy();
Assert.assertEquals(ORDER.size(), 8);
Assert.assertEquals(ORDER.get(5), "s5.destroy");
Assert.assertEquals(ORDER.get(6), "s3.destroy");
Assert.assertEquals(ORDER.get(7), "s1.destroy");
// service add via setService exception
ORDER.clear();
services = StringUtils.toString(Arrays.asList(MyService1.class.getName(), MyService3.class.getName()), ",");
conf = new XConfiguration();
conf.set("server.services", services);
server = new Server("server", dir, dir, dir, dir, conf);
server.init();
try {
server.setService(MyService7.class);
Assert.fail();
}
catch (ServerException ex) {
Assert.assertEquals(ServerException.ERROR.S09, ex.getError());
}
catch (Exception ex) {
Assert.fail();
}
Assert.assertEquals(ORDER.size(), 6);
Assert.assertEquals(ORDER.get(4), "s3.destroy");
Assert.assertEquals(ORDER.get(5), "s1.destroy");
// service with dependency
ORDER.clear();
services = StringUtils.toString(Arrays.asList(MyService1.class.getName(), MyService6.class.getName()), ",");
conf = new XConfiguration();
conf.set("server.services", services);
server = new Server("server", dir, dir, dir, dir, conf);
server.init();
Assert.assertEquals(server.get(MyService1.class).getInterface(), MyService1.class);
Assert.assertEquals(server.get(MyService6.class).getInterface(), MyService6.class);
server.destroy();