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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.config.testutil;
import com.yahoo.container.core.config.HandlersConfigurerDi;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author gjoranv
*/
public class TestUtil {
public static void createComponentsConfig(String configFile,
String componentsFile,
String componentType) throws IOException {
createComponentsConfig(configFile, componentsFile, componentType, false);
}
/**
* Copies the component ids from another config, e.g. 'handlers' to a 'components' array in a new components file,
* to avoid a manually written 'components' file for tests where the bundle spec is given by the component id.
* @param configFile Full path to the original config file, e.g. 'handlers'
* @param componentsFile Full path to the new 'components' file
* @param componentType The type of component, e.g. 'handler'
* @param append 'true' will append to an already existing 'componentsFile'
*/
public static void createComponentsConfig(String configFile,
String componentsFile,
String componentType,
boolean append) throws IOException {
StringBuilder buf = new StringBuilder();
String line;
int i = 0;
if (append) {
final Pattern p = Pattern.compile("^[a-z]+" + "\\[\\d+\\]\\.id (.+)");
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(componentsFile)), "UTF-8"));
while ((line = reader.readLine()) != null) {
Matcher m = p.matcher(line);
if (m.matches() && !m.group(1).equals(HandlersConfigurerDi.RegistriesHack.class.getName())) {
buf.append("components[").append(i).append("].id ").append(m.group(1)).append("\n");
i++;
}
}
reader.close();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(configFile)), "UTF-8"));
final Pattern component = Pattern.compile("^" + componentType + "\\[\\d+\\]\\.id (.+)");
while ((line = reader.readLine()) != null) {
Matcher m = component.matcher(line);
if (m.matches()) {
buf.append("components[").append(i).append("].id ").append(m.group(1)).append("\n");
i++;
}
}
buf.append("components[").append(i).append("].id ").
append(HandlersConfigurerDi.RegistriesHack.class.getName()).append("\n");
i++;
reader.close();
buf.insert(0, "components["+i+"]\n");
Writer writer = new OutputStreamWriter(new FileOutputStream(new File(componentsFile)), "UTF-8");
writer.write(buf.toString());
writer.flush();
writer.close();
}
/**
* Copies src file to dst file. If the dst file does not exist, it is created.
*/
public static void copyFile(String srcName, File dstFile) throws IOException {
InputStream src = new FileInputStream(new File(srcName));
OutputStream dst = new FileOutputStream(dstFile);
byte[] buf = new byte[1024];
int len;
while ((len = src.read(buf)) > 0) {
dst.write(buf, 0, len);
}
src.close();
dst.close();
}
}
|