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

import com.yahoo.config.test.ThreadPoolConfig;
import com.yahoo.container.di.componentgraph.Provider;
import org.junit.Test;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.junit.Assert.assertNotNull;


/**
 * @author tonytv
 * @author gjoranv
 */
public class ComponentConfigTest extends Base {
    public static class ThreadPoolExecutorProvider implements Provider<Executor> {
        private ExecutorService executor;

        public ThreadPoolExecutorProvider(ThreadPoolConfig config) {
            executor = Executors.newFixedThreadPool(config.numThreads());
        }

        @Override
        public Executor get() {
            return executor;
        }

        @Override
        public void deconstruct() {
            executor.shutdown();
        }
    }

    @Test
    public void require_that_non_components_can_be_configured() {
        register(ThreadPoolExecutorProvider.class);
        addConfig(new ThreadPoolConfig(new ThreadPoolConfig.Builder().numThreads(4)),
                toId(ThreadPoolExecutorProvider.class));
        complete();

        Executor executor = getInstance(Executor.class);
        assertNotNull(executor);
    }
}