Skip to content

Commit 30389c6

Browse files
authored
Improve SAML tests resiliency to auto-formatting (#48517)
Backport of #48452. The SAML tests have large XML documents within which various parameters are replaced. At present, if these test are auto-formatted, the XML documents get strung out over many, many lines, and are basically illegible. Fix this by using named placeholders for variables, and indent the multiline XML documents. The tests in `SamlSpMetadataBuilderTests` deserve a special mention, because they include a number of certificates in Base64. I extracted these into variables, for additional legibility.
1 parent 1ef87c9 commit 30389c6

File tree

4 files changed

+1569
-868
lines changed

4 files changed

+1569
-868
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.util;
21+
22+
import java.util.Map;
23+
import java.util.regex.Matcher;
24+
import java.util.regex.Pattern;
25+
26+
/**
27+
* A formatter that allows named placeholders e.g. "%(param)" to be replaced.
28+
*/
29+
public class NamedFormatter {
30+
private static final Pattern PARAM_REGEX = Pattern
31+
.compile(
32+
// Match either any backlash-escaped characters, or a "%(param)" pattern.
33+
// COMMENTS is specified to allow whitespace in this pattern, for clarity
34+
"\\\\(.) | (% \\( ([^)]+) \\) )",
35+
Pattern.COMMENTS
36+
);
37+
38+
private NamedFormatter() {}
39+
40+
/**
41+
* Replaces named parameters of the form <code>%(param)</code> in format strings. For example:
42+
*
43+
* <ul>
44+
* <li><code>NamedFormatter.format("Hello, %(name)!", Map.of("name", "world"))</code> → <code>"Hello, world!"</code></li>
45+
* <li><code>NamedFormatter.format("Hello, \%(name)!", Map.of("name", "world"))</code> → <code>"Hello, %(world)!"</code></li>
46+
* <li><code>NamedFormatter.format("Hello, %(oops)!", Map.of("name", "world"))</code> → {@link IllegalArgumentException}</li>
47+
* </ul>
48+
*
49+
* @param fmt The format string. Any <code>%(param)</code> is replaced by its corresponding value in the <code>values</code> map.
50+
* Parameter patterns can be escaped by prefixing with a backslash.
51+
* @param values a map of parameter names to values.
52+
* @return The formatted string.
53+
* @throws IllegalArgumentException if a parameter is found in the format string with no corresponding value
54+
*/
55+
public static String format(String fmt, Map<String, Object> values) {
56+
final Matcher matcher = PARAM_REGEX.matcher(fmt);
57+
58+
boolean result = matcher.find();
59+
60+
if (result) {
61+
final StringBuffer sb = new StringBuffer();
62+
do {
63+
String replacement;
64+
65+
// Escaped characters are unchanged
66+
if (matcher.group(1) != null) {
67+
replacement = matcher.group(1);
68+
} else {
69+
final String paramName = matcher.group(3);
70+
if (values.containsKey(paramName) == true) {
71+
replacement = values.get(paramName).toString();
72+
} else {
73+
throw new IllegalArgumentException("No parameter value for %(" + paramName + ")");
74+
}
75+
}
76+
77+
matcher.appendReplacement(sb, replacement);
78+
result = matcher.find();
79+
} while (result);
80+
81+
matcher.appendTail(sb);
82+
return sb.toString();
83+
}
84+
85+
return fmt;
86+
}
87+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.util;
21+
22+
import org.elasticsearch.test.ESTestCase;
23+
import org.junit.Rule;
24+
import org.junit.rules.ExpectedException;
25+
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
import static java.util.Collections.singletonMap;
30+
import static org.hamcrest.Matchers.equalTo;
31+
32+
public class NamedFormatterTests extends ESTestCase {
33+
@Rule
34+
public ExpectedException thrown = ExpectedException.none();
35+
36+
public void testPatternAreFormatted() {
37+
assertThat(NamedFormatter.format("Hello, %(name)!", singletonMap("name", "world")), equalTo("Hello, world!"));
38+
}
39+
40+
public void testDuplicatePatternsAreFormatted() {
41+
assertThat(NamedFormatter.format("Hello, %(name) and %(name)!", singletonMap("name", "world")), equalTo("Hello, world and world!"));
42+
}
43+
44+
public void testMultiplePatternsAreFormatted() {
45+
final Map<String, Object> values = new HashMap<>();
46+
values.put("name", "world");
47+
values.put("second_name", "fred");
48+
49+
assertThat(
50+
NamedFormatter.format("Hello, %(name) and %(second_name)!", values),
51+
equalTo("Hello, world and fred!")
52+
);
53+
}
54+
55+
public void testEscapedPatternsAreNotFormatted() {
56+
assertThat(NamedFormatter.format("Hello, \\%(name)!", singletonMap("name", "world")), equalTo("Hello, %(name)!"));
57+
}
58+
59+
public void testUnknownPatternsThrowException() {
60+
thrown.expect(IllegalArgumentException.class);
61+
thrown.expectMessage("No parameter value for %(name)");
62+
NamedFormatter.format("Hello, %(name)!", singletonMap("foo", "world"));
63+
}
64+
}

0 commit comments

Comments
 (0)