blob: 34119163d2f0534dd57d4b2c6a5a75f43b428970 (
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
|
// 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 org.junit.rules.TemporaryFolder;
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
*/
@SuppressWarnings("removal") // TODO Vespa 8: remove
public class DirConfigSource {
private final TemporaryFolder tempFolder = createTemporaryFolder();
public final ConfigSource configSource;
public DirConfigSource(String testSourcePrefix) {
this.configSource = new ConfigSourceSet(testSourcePrefix + new Random().nextLong());
}
public void writeConfig(String name, String contents) {
File file = new File(tempFolder.getRoot(), 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.getRoot().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);
}
}
private static TemporaryFolder createTemporaryFolder() {
TemporaryFolder folder = new TemporaryFolder();
try {
folder.create();
} catch (IOException e) {
throw new RuntimeException(e);
}
return folder;
}
}
|