This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
/
XargsFileLauncherPropertiesTest.java
174 lines (152 loc) · 5.94 KB
/
XargsFileLauncherPropertiesTest.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
* Copyright (c) 2014 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Jochen Hiller
*******************************************************************************/
package org.eclipse.concierge.compat.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.concierge.test.util.TestUtils;
import org.junit.Assert;
import org.junit.Test;
/**
* Tests the XargsLauncher with its pattern replacement and wildcard support.
*/
public class XargsFileLauncherPropertiesTest {
@Test
public void testConstructor() {
new XargsFileLauncher();
}
@Test
public void testReplaceVariable() {
XargsFileLauncher l = new XargsFileLauncher();
Map<String, String> p = new HashMap<String, String>();
Assert.assertEquals("${prop1}", l.replaceVariable("${prop1}", p));
Assert.assertEquals("XXX${prop1}YYY",
l.replaceVariable("XXX${prop1}YYY", p));
p.put("prop1", "value1");
Assert.assertEquals("value1", l.replaceVariable("${prop1}", p));
Assert.assertEquals("XXXvalue1YYY",
l.replaceVariable("XXX${prop1}YYY", p));
}
@Test
public void testReplaceMultipleVariables() {
XargsFileLauncher l = new XargsFileLauncher();
Map<String, String> p = new HashMap<String, String>();
Assert.assertEquals("${p1}", l.replaceVariable("${p1}", p));
Assert.assertEquals("XXX${p1}YYY", l.replaceVariable("XXX${p1}YYY", p));
p.put("p1", "v1");
p.put("p2", "v2");
Assert.assertEquals("v1v2", l.replaceVariable("${p1}${p2}", p));
Assert.assertEquals("XXXv1YYYv2ZZZ",
l.replaceVariable("XXX${p1}YYY${p2}ZZZ", p));
Assert.assertEquals("v1v1", l.replaceVariable("${p1}${p1}", p));
Assert.assertEquals("XXXv1YYYv1ZZZ",
l.replaceVariable("XXX${p1}YYY${p1}ZZZ", p));
}
@Test
public void testReplaceVariablesPerformance() {
XargsFileLauncher l = new XargsFileLauncher();
Map<String, String> p = new HashMap<String, String>();
p.put("p1", "v1");
p.put("p2", "v2");
int N = 500000;
System.gc();
long startTime = System.currentTimeMillis();
for (int i = 0; i < N; i++) {
Assert.assertEquals("v1v2", l.replaceVariable("${p1}${p2}", p));
Assert.assertEquals("XXXv1YYYv2ZZZ",
l.replaceVariable("XXX${p1}YYY${p2}ZZZ", p));
}
long endTime = System.currentTimeMillis();
System.out.println("test10ReplaceVariablesPerformance: " + N
+ " runs in " + (endTime - startTime) + " ms.");
}
@Test
public void testGetPropertiesFromXargsFileEmpty() throws IOException {
Map<String, String> props = processProperties("");
Assert.assertEquals(0, props.size());
}
@Test
public void testGetPropertiesFromXargsFileOneLine() throws IOException {
Map<String, String> props = processProperties("-Dprop=value");
Assert.assertEquals(1, props.size());
Assert.assertEquals("value", props.get("prop"));
}
@Test
public void testGetPropertiesFromXargsFileTwoLines() throws IOException {
Map<String, String> props = processProperties("-Dprop1=value1\n-Dprop2=value2");
Assert.assertEquals(2, props.size());
Assert.assertEquals("value1", props.get("prop1"));
Assert.assertEquals("value2", props.get("prop2"));
}
@Test
public void testGetPropertiesFromXargsSplitOverMultipleLines()
throws IOException {
Map<String, String> props = processProperties("-Dprop=value1\\\n value2");
Assert.assertEquals(1, props.size());
Assert.assertEquals("value1value2", props.get("prop"));
}
@Test
public void testGetPropertiesFromXargsSplitOverTwoLinesWithComments()
throws IOException {
Map<String, String> props = processProperties("-Dprop=value1\\ # comment \n value2 # comment");
Assert.assertEquals(1, props.size());
Assert.assertEquals("value1value2", props.get("prop"));
}
@Test
public void testGetPropertiesFromXargsSplitOverMultipleLinesWithComments()
throws IOException {
Map<String, String> props = processProperties("-Dprop=v1\\ # c \n v2 \\ # c \n\t\tv3 \\ #\n v4 \\ # comment \n v5 # c");
Assert.assertEquals(1, props.size());
Assert.assertEquals("v1v2v3v4v5", props.get("prop"));
}
@Test
public void testGetPropertiesFromXargsAddedByPlus() throws IOException {
Map<String, String> props = processProperties("-Dprop=v1\n-Dprop+=v2\n-Dprop+=v3");
Assert.assertEquals(1, props.size());
Assert.assertEquals("v1v2v3", props.get("prop"));
}
@Test
public void testGetPropertiesFromXargsAddedByPlusNotInitialized()
throws IOException {
Map<String, String> props = processProperties("-Dprop+=v1\n-Dprop+=v2\n-Dprop+=v3");
Assert.assertEquals(1, props.size());
Assert.assertEquals("v1v2v3", props.get("prop"));
}
@Test
public void testGetPropertiesFromXargsNoEquals() throws IOException {
Map<String, String> props = processProperties("-Dprop_v");
Assert.assertEquals(0, props.size());
}
@Test
public void testGetPropertiesFromXargsNoName() throws IOException {
Map<String, String> props = processProperties("-D=v");
Assert.assertEquals(0, props.size()); // no name
props = processProperties("-D+=v");
Assert.assertEquals(0, props.size());
}
@Test
public void testGetPropertiesFromXargsNoValue() throws IOException {
Map<String, String> props = processProperties("-Dprop=");
Assert.assertEquals(1, props.size());
Assert.assertEquals("", props.get("prop"));
}
// private helper methods
private Map<String, String> processProperties(String s) throws IOException {
XargsFileLauncher l = new XargsFileLauncher();
InputStream is = TestUtils.createInputStreamFromString(s);
Map<String, String> props = l.getPropertiesFromXargsInputStream(is);
return props;
}
}