aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/di/ContainerTestBase.java
blob: 93048eefc975f33328eef3234f8ec8fc2cade7cd (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.di;

import com.google.inject.Guice;
import com.yahoo.container.core.config.BundleTestUtil;
import com.yahoo.container.core.config.TestOsgi;
import com.yahoo.container.di.ContainerTest.ComponentTakingConfig;
import com.yahoo.container.di.componentgraph.core.ComponentGraph;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;
import org.osgi.framework.Bundle;

import java.io.File;
import java.util.Collection;
import java.util.List;

/**
 * @author Tony Vaagenes
 * @author gjoranv
 * @author ollivir
 */
public class ContainerTestBase {

    private ComponentGraph componentGraph;
    protected DirConfigSource dirConfigSource = null;
    protected TestOsgi osgi;

    @TempDir
    File tmpDir;

    @BeforeEach
    public void setup() {
        dirConfigSource = new DirConfigSource(tmpDir);
        componentGraph = new ComponentGraph(0);
        osgi = new TestOsgi(BundleTestUtil.testBundles());
    }

    protected Container newContainer(DirConfigSource dirConfigSource,
                                            ComponentDeconstructor deconstructor) {
        return new Container(new CloudSubscriberFactory(dirConfigSource.configSource()),
                             dirConfigSource.configId(),
                             deconstructor,
                             osgi);
    }

    protected Container newContainer(DirConfigSource dirConfigSource) {
        return newContainer(dirConfigSource, new TestDeconstructor(osgi));
    }

    ComponentGraph getNewComponentGraph(Container container, ComponentGraph oldGraph) {
        Container.ComponentGraphResult result = container.waitForNextGraphGeneration(oldGraph, Guice.createInjector(), true);
        result.oldComponentsCleanupTask().run();
        return result.newGraph();
    }

    ComponentGraph getNewComponentGraph(Container container) {
        return container.waitForNextGraphGeneration(new ComponentGraph(), Guice.createInjector(), true).newGraph();
    }


    public <T> T getInstance(Class<T> componentClass) {
        return componentGraph.getInstance(componentClass);
    }

    protected void writeBootstrapConfigsWithBundles(List<String> applicationBundles, List<ComponentEntry> components) {
        writeBootstrapConfigs(components.toArray(new ComponentEntry[0]));
        StringBuilder bundles = new StringBuilder();
        for (int i = 0; i < applicationBundles.size(); i++) {
            bundles.append("bundles[" + i + "] \"" + applicationBundles.get(i) + "\"\n");
        }
        dirConfigSource.writeConfig("application-bundles", String.format("bundles[%s]\n%s", applicationBundles.size(), bundles));
    }

    protected void writeBootstrapConfigs(ComponentEntry... componentEntries) {
        dirConfigSource.writeConfig("platform-bundles", "");
        dirConfigSource.writeConfig("application-bundles", "");
        StringBuilder components = new StringBuilder();
        for (int i = 0; i < componentEntries.length; i++) {
            components.append(componentEntries[i].asConfig(i));
            components.append('\n');
        }
        dirConfigSource.writeConfig("components", String.format("components[%s]\n%s", componentEntries.length, components));
    }

    protected void writeBootstrapConfigs(String componentId, Class<?> classId) {
        writeBootstrapConfigs(new ComponentEntry(componentId, classId));
    }

    protected void writeBootstrapConfigs(String componentId) {
        writeBootstrapConfigs(componentId, ComponentTakingConfig.class);
    }

    protected void writeBootstrapConfigs() {
        writeBootstrapConfigs(ComponentTakingConfig.class.getName(), ComponentTakingConfig.class);
    }

    protected class ComponentEntry {
        private final String componentId;
        private final Class<?> classId;

        ComponentEntry(String componentId, Class<?> classId) {
            this.componentId = componentId;
            this.classId = classId;
        }

        String asConfig(int position) {
            return  "components[" + position + "].id \"" + componentId + "\"\n" +
                    "components[" + position + "].classId \"" + classId.getName() + "\"\n" +
                    "components[" + position + "].configId \"" + dirConfigSource.configId() + "\"\n" ;
        }
    }


    public static class TestDeconstructor implements ComponentDeconstructor {

        final TestOsgi osgi;

        public TestDeconstructor(TestOsgi osgi) {
            this.osgi = osgi;
        }

        @Override
        public void deconstruct(long generation, List<Object> components, Collection<Bundle> bundles) {
            components.forEach(component -> {
                if (component instanceof ContainerTest.DestructableComponent vespaComponent) {
                    vespaComponent.deconstruct();
                }
            });
            bundles.forEach(osgi::removeBundle);
        }
    }

}