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

import com.yahoo.config.subscription.ConfigSource;
import com.yahoo.config.subscription.ConfigSourceSet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

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

    private final File tempFolder;
    public final ConfigSource configSource;

    public DirConfigSource(File tmpDir, String testSourcePrefix) {
        this.tempFolder = tmpDir;
        this.configSource = new ConfigSourceSet(testSourcePrefix + new Random().nextLong());
    }

    public void writeConfig(String name, String contents) {
        File file = new File(tempFolder, name + ".cfg");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        printFile(file, contents + "\n");
    }

    public String configId() {
        return "dir:" + tempFolder.getPath();
    }

    public ConfigSource configSource() {
        return configSource;
    }

    public void cleanup() {
        tempFolder.delete();
    }

    private static void printFile(File f, String content) {
        try (OutputStream out = new FileOutputStream(f)) {
            out.write(content.getBytes("UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}