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

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.name.Names;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

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

    @Test
    void requireThatEntriesAreBoundWithLowerCaseKeys() {
        Map<String, String> config = new HashMap<>();
        config.put("foo_key", "foo");
        config.put("BAR_key", "bar");
        config.put("BAZ_KEY", "baz");

        Injector injector = Guice.createInjector(new ApplicationConfigModule(config));
        assertBinding(injector, "foo_key", "foo");
        assertBinding(injector, "bar_key", "bar");
        assertBinding(injector, "baz_key", "baz");
    }

    @Test
    void requireThatEntriesAreBoundWithUnmodifiedValue() {
        Map<String, String> config = new HashMap<>();
        config.put("foo", "foo_val");
        config.put("bar", "BAR_val");
        config.put("baz", "BAZ_VAL");

        Injector injector = Guice.createInjector(new ApplicationConfigModule(config));
        assertBinding(injector, "foo", "foo_val");
        assertBinding(injector, "bar", "BAR_val");
        assertBinding(injector, "baz", "BAZ_VAL");
    }

    @Test
    void requireThatUpperCaseKeysPrecedeLowerCaseKeys() {
        Map<String, String> config = new HashMap<>();
        config.put("foo", "lower-case");
        assertBinding(config, "foo", "lower-case");

        config.put("Foo", "mixed-case 1");
        assertBinding(config, "foo", "mixed-case 1");

        config.put("FOO", "upper-case");
        assertBinding(config, "foo", "upper-case");

        config.put("FOo", "mixed-case 2");
        assertBinding(config, "foo", "upper-case");
    }

    @Test
    void requireThatNullFileNameThrowsException() throws IOException {
        try {
            ApplicationConfigModule.newInstanceFromFile(null);
            fail();
        } catch (NullPointerException e) {

        }
    }

    @Test
    void requireThatFileNotFoundThrowsException() throws IOException {
        try {
            ApplicationConfigModule.newInstanceFromFile("/file/not/found");
            fail();
        } catch (FileNotFoundException e) {

        }
    }

    @Test
    void requireThatPropertieFilesCanBeRead() throws IOException {
        Properties props = new Properties();
        props.put("foo_key", "foo_val");

        File file = File.createTempFile("config-", ".properties");
        file.deleteOnExit();
        FileOutputStream out = new FileOutputStream(file);
        props.store(out, null);
        out.close();

        assertBinding(ApplicationConfigModule.newInstanceFromFile(file.getAbsolutePath()), "foo_key", "foo_val");
    }

    private static void assertBinding(Map<String, String> config, String stringName, String expected) {
        assertBinding(new ApplicationConfigModule(config), stringName, expected);
    }

    private static void assertBinding(Module module, String stringName, String expected) {
        assertBinding(Guice.createInjector(module), stringName, expected);
    }

    private static void assertBinding(Injector injector, String stringName, String expected) {
        assertEquals(expected, injector.getInstance(Key.get(String.class, Names.named(stringName))));
    }
}