diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java |
Publish
Diffstat (limited to 'jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java')
-rw-r--r-- | jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java new file mode 100644 index 00000000000..bc80358b760 --- /dev/null +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java @@ -0,0 +1,154 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jdisc.core; + +import org.apache.commons.daemon.DaemonContext; +import org.apache.commons.daemon.DaemonController; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class BootstrapDaemonTestCase { + + @Test + public void requireThatPrivilegedLifecycleWorks() throws Exception { + MyLoader loader = new MyLoader(); + BootstrapDaemon daemon = new BootstrapDaemon(loader, true); + daemon.init(new MyContext("foo")); + assertTrue(loader.hasState(true, false, false, false)); + assertTrue(loader.privileged); + daemon.start(); + assertTrue(loader.hasState(true, true, false, false)); + daemon.stop(); + assertTrue(loader.hasState(true, true, true, false)); + daemon.destroy(); + assertTrue(loader.hasState(true, true, true, true)); + } + + @Test + public void requireThatNonPrivilegedLifecycleWorks() throws Exception { + MyLoader loader = new MyLoader(); + BootstrapDaemon daemon = new BootstrapDaemon(loader, false); + daemon.init(new MyContext("foo")); + assertTrue(loader.hasState(false, false, false, false)); + daemon.start(); + assertTrue(loader.hasState(true, true, false, false)); + assertFalse(loader.privileged); + daemon.stop(); + assertTrue(loader.hasState(true, true, true, false)); + daemon.destroy(); + assertTrue(loader.hasState(true, true, true, true)); + } + + @Test + public void requireThatBundleLocationIsRequired() throws Exception { + MyLoader loader = new MyLoader(); + BootstrapDaemon daemon = new BootstrapDaemon(loader, true); + try { + daemon.init(new MyContext((String[])null)); + fail(); + } catch (IllegalArgumentException e) { + assertNull(loader.bundleLocation); + } + try { + daemon.init(new MyContext()); + fail(); + } catch (IllegalArgumentException e) { + assertNull(loader.bundleLocation); + } + try { + daemon.init(new MyContext((String)null)); + fail(); + } catch (IllegalArgumentException e) { + assertNull(loader.bundleLocation); + } + try { + daemon.init(new MyContext("foo", "bar")); + fail(); + } catch (IllegalArgumentException e) { + assertNull(loader.bundleLocation); + } + + daemon.init(new MyContext("foo")); + daemon.start(); + + assertNotNull(loader.bundleLocation); + assertEquals("foo", loader.bundleLocation); + + daemon.stop(); + daemon.destroy(); + } + + @Test + public void requireThatEnvironmentIsRequired() { + try { + new BootstrapDaemon(); + fail(); + } catch (IllegalStateException e) { + + } + } + + private static class MyLoader implements BootstrapLoader { + + String bundleLocation = null; + boolean privileged = false; + boolean initCalled = false; + boolean startCalled = false; + boolean stopCalled = false; + boolean destroyCalled = false; + + boolean hasState(boolean initCalled, boolean startCalled, boolean stopCalled, boolean destroyCalled) { + return this.initCalled == initCalled && this.startCalled == startCalled && + this.stopCalled == stopCalled && this.destroyCalled == destroyCalled; + } + + @Override + public void init(String bundleLocation, boolean privileged) throws Exception { + this.bundleLocation = bundleLocation; + this.privileged = privileged; + initCalled = true; + } + + @Override + public void start() throws Exception { + startCalled = true; + } + + @Override + public void stop() throws Exception { + stopCalled = true; + } + + @Override + public void destroy() { + destroyCalled = true; + } + } + + private static class MyContext implements DaemonContext { + + final String[] args; + + MyContext(String... args) { + this.args = args; + } + + @Override + public DaemonController getController() { + return null; + } + + @Override + public String[] getArguments() { + return args; + } + } +} |