### Question:
ApplicationLauncher { public static int exec(String... args) { return exec(new Configuration(), args); } private  ApplicationLauncher(); static void main(String... args); static int exec(String... args); static int exec(Configuration configuration, String... args); static ClassLoader switchContextClassLoader(ClassLoader classLoader); static void disposeClassLoader(ClassLoader classLoader); static final String KEY_LAUNCHER_USED; static final int LAUNCH_ERROR; static final int CLIENT_ERROR; }### Answer:
@Test public void simple() { AtomicBoolean ok = new AtomicBoolean(false); Bootstrap.register(new Callback() { @Override public int run(String[] args) throws Exception { ok.set(true); return 0; } }); int status = ApplicationLauncher.exec(conf, new String[] { Bootstrap.class.getName(), }); assertThat(status, is(0)); assertThat(conf.getBoolean(ApplicationLauncher.KEY_LAUNCHER_USED, false), is(true)); assertThat(ok.get(), is(true)); }
@Test public void w_generic_options() { Bootstrap.register(new Callback() { @Override public int run(String[] args) throws Exception { assertThat(getConf().get("TESTING"), is("OK")); return 0; } }); int status = ApplicationLauncher.exec(conf, new String[] { Bootstrap.class.getName(), "-D", "TESTING=OK", }); assertThat(status, is(0)); }
@Test public void w_libjars() throws Exception { File lib = putFile("dummy.jar"); Bootstrap.register(new Callback() { @Override public int run(String[] args) throws Exception { assertThat(lib, inClasspath(Thread.currentThread().getContextClassLoader())); return 0; } }); int status = ApplicationLauncher.exec(conf, new String[] { Bootstrap.class.getName(), "-libjars", lib.getPath(), }); assertThat(status, is(0)); assertThat(lib, not(inClasspath(Thread.currentThread().getContextClassLoader()))); }
@Test public void invalid_wo_application_class() { int status = ApplicationLauncher.exec(conf, new String[0]); assertThat(status, is(ApplicationLauncher.LAUNCH_ERROR)); }
@Test public void invalid_w_err_ctor() { int status = ApplicationLauncher.exec(conf, new String[] { Invalid.class.getName(), }); assertThat(status, is(ApplicationLauncher.LAUNCH_ERROR)); }
@Test public void body_w_failure() { Bootstrap.register(new Callback() { @Override public int run(String[] args) throws Exception { return 1; } }); int status = ApplicationLauncher.exec(conf, new String[] { Bootstrap.class.getName(), }); assertThat(status, is(1)); }
@Test public void body_w_error() { Bootstrap.register(new Callback() { @Override public int run(String[] args) throws Exception { throw new IOException(); } }); int status = ApplicationLauncher.exec(conf, new String[] { Bootstrap.class.getName(), }); assertThat(status, is(ApplicationLauncher.CLIENT_ERROR)); }