summaryrefslogtreecommitdiffstats
path: root/container-di/src/test/java/demo/ContainerTestBase.java
blob: fdc739130526a68c08df294aedc08d6da1dda6b0 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package demo;

import com.google.inject.Guice;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.container.bundle.BundleInstantiationSpecification;
import com.yahoo.config.FileReference;
import com.yahoo.container.di.CloudSubscriberFactory;
import com.yahoo.container.di.Container;
import com.yahoo.container.di.ContainerTest;
import com.yahoo.container.di.Osgi;
import com.yahoo.container.di.componentgraph.core.ComponentGraph;
import org.junit.Before;
import org.osgi.framework.Bundle;
import scala.collection.*;
import scala.collection.immutable.*;
import scala.collection.immutable.Set;

import java.util.Collection;
import java.util.List;

/**
 * @author tonytv
 * @author gjoranv
 */
public class ContainerTestBase extends ContainerTest {
    private ComponentGraph componentGraph;

    @Before
    public void createGraph() {
        componentGraph = new ComponentGraph(0);
    }

    public void complete() {
        try {
            Container container = new Container(
                    new CloudSubscriberFactory(dirConfigSource().configSource()),
                    dirConfigSource().configId(),
                    new ContainerTest.TestDeconstructor(),
                    new Osgi() {
                        @SuppressWarnings("unchecked")
                        @Override
                        public Class<Object> resolveClass(BundleInstantiationSpecification spec) {
                            try {
                                return (Class<Object>) Class.forName(spec.classId.getName());
                            } catch (ClassNotFoundException e) {
                                throw new RuntimeException(e);
                            }
                        }

                        @Override
                        public BundleClasses getBundleClasses(ComponentSpecification bundle,
                                                              Set<String> packagesToScan) {
                            throw new UnsupportedOperationException("getBundleClasses not supported");
                        }

                        @Override
                        public void useBundles(Collection<FileReference> bundles) {}

                        @Override
                        public Bundle getBundle(ComponentSpecification spec) {
                            throw new UnsupportedOperationException("getBundle not supported.");
                        }
                    });
            componentGraph = container.runOnce(componentGraph, Guice.createInjector());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

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