aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java
blob: df8223a6d8659367d70ffb791b6ca0e9d5846890 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2017 Yahoo Holdings. 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 Simon Thoresen Hult
 */
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;
        }
    }
}