aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/application/ServerRepositoryTestCase.java
blob: cb3a81277f3cd04d58743af7b6376167427653b0 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;

import com.yahoo.jdisc.NoopSharedResource;
import com.yahoo.jdisc.service.ServerProvider;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Iterator;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author Simon Thoresen Hult
 */
public class ServerRepositoryTestCase {

    @Test
    void requireThatInstallWorks() {
        ServerRepository servers = newServerRepository();
        MyServer server = new MyServer();
        servers.install(server);

        Iterator<ServerProvider> it = servers.iterator();
        assertTrue(it.hasNext());
        assertSame(server, it.next());
        assertFalse(it.hasNext());
    }

    @Test
    void requireThatInstallAllWorks() {
        ServerRepository servers = newServerRepository();
        ServerProvider foo = new MyServer();
        ServerProvider bar = new MyServer();
        servers.installAll(Arrays.asList(foo, bar));

        Iterator<ServerProvider> it = servers.iterator();
        assertTrue(it.hasNext());
        assertSame(foo, it.next());
        assertTrue(it.hasNext());
        assertSame(bar, it.next());
        assertFalse(it.hasNext());
    }

    @Test
    void requireThatUninstallWorks() {
        ServerRepository servers = newServerRepository();
        ServerProvider server = new MyServer();
        servers.install(server);
        servers.uninstall(server);
        assertFalse(servers.iterator().hasNext());
    }

    @Test
    void requireThatUninstallAllWorks() {
        ServerRepository servers = newServerRepository();
        ServerProvider foo = new MyServer();
        ServerProvider bar = new MyServer();
        ServerProvider baz = new MyServer();
        servers.installAll(Arrays.asList(foo, bar, baz));
        servers.uninstallAll(Arrays.asList(foo, bar));
        Iterator<ServerProvider> it = servers.iterator();
        assertNotNull(it);
        assertTrue(it.hasNext());
        assertSame(baz, it.next());
        assertFalse(it.hasNext());
    }

    private static ServerRepository newServerRepository() {
        return new ServerRepository(new GuiceRepository());
    }

    private static class MyServer extends NoopSharedResource implements ServerProvider {

        @Override
        public void start() {

        }

        @Override
        public void close() {

        }
    }
}