|
| 1 | +/* |
| 2 | + * (C) Copyright 2025 Boni Garcia (https://bonigarcia.github.io/) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
1 | 17 | package io.github.bonigarcia.seljup; |
2 | 18 |
|
3 | 19 | import org.junit.jupiter.api.TestInfo; |
|
15 | 31 | import java.util.function.UnaryOperator; |
16 | 32 |
|
17 | 33 | /** |
18 | | - * The annotated method is a <em>test template</em> that should be repeated for each |
19 | | - * <a href="https://bonigarcia.dev/selenium-jupiter/#template-tests">browser scenario</a> |
20 | | - * with a configurable {@linkplain #name display name}. |
| 34 | + * The annotated method is a <em>test template</em> that should be repeated for |
| 35 | + * each |
| 36 | + * <a href="https://bonigarcia.dev/selenium-jupiter/#template-tests">browser |
| 37 | + * scenario</a> with a configurable {@linkplain #name display name}. |
21 | 38 | * |
22 | 39 | * @see org.junit.jupiter.api.TestTemplate |
23 | 40 | * |
|
35 | 52 | String DISPLAY_NAME_PLACEHOLDER = "{displayName}"; |
36 | 53 |
|
37 | 54 | /** |
38 | | - * Placeholder for the {@linkplain BrowsersTemplate.Browser#getType() browser type} of |
39 | | - * a {@code @BrowserScenarioTest} method: <code>{type}</code> |
| 55 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getType() |
| 56 | + * browser type} of a {@code @BrowserScenarioTest} method: |
| 57 | + * <code>{type}</code> |
40 | 58 | */ |
41 | 59 | String TYPE_PLACEHOLDER = "{type}"; |
42 | 60 |
|
43 | 61 | /** |
44 | | - * Placeholder for the {@linkplain BrowsersTemplate.Browser#getVersion() browser version} of |
45 | | - * a {@code @BrowserScenarioTest} method: <code>{version}</code> |
| 62 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getVersion() |
| 63 | + * browser version} of a {@code @BrowserScenarioTest} method: |
| 64 | + * <code>{version}</code> |
46 | 65 | */ |
47 | 66 | String VERSION_PLACEHOLDER = "{version}"; |
48 | 67 |
|
49 | 68 | /** |
50 | | - * Placeholder for the {@linkplain BrowsersTemplate.Browser#getArguments() browser arguments} of |
51 | | - * a {@code @BrowserScenarioTest} method: <code>{arguments}</code> |
| 69 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getArguments() |
| 70 | + * browser arguments} of a {@code @BrowserScenarioTest} method: |
| 71 | + * <code>{arguments}</code> |
52 | 72 | */ |
53 | 73 | String ARGUMENTS_PLACEHOLDER = "{arguments}"; |
54 | 74 |
|
55 | 75 | /** |
56 | | - * Placeholder for the {@linkplain BrowsersTemplate.Browser#getPreferences() browser preferences} of |
57 | | - * a {@code @BrowserScenarioTest} method: <code>{preferences}</code> |
| 76 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getPreferences() |
| 77 | + * browser preferences} of a {@code @BrowserScenarioTest} method: |
| 78 | + * <code>{preferences}</code> |
58 | 79 | */ |
59 | 80 | String PREFERENCES_PLACEHOLDER = "{preferences}"; |
60 | 81 |
|
61 | 82 | /** |
62 | | - * Placeholder for the {@linkplain BrowsersTemplate.Browser#getCapabilities() capabilities} of |
63 | | - * a {@code @BrowserScenarioTest} method: <code>{capabilities}</code> |
| 83 | + * Placeholder for the |
| 84 | + * {@linkplain BrowsersTemplate.Browser#getCapabilities() capabilities} of a |
| 85 | + * {@code @BrowserScenarioTest} method: <code>{capabilities}</code> |
64 | 86 | */ |
65 | 87 | String CAPABILITIES_PLACEHOLDER = "{capabilities}"; |
66 | 88 |
|
67 | 89 | /** |
68 | | - * Placeholder for the {@linkplain BrowsersTemplate.Browser#getRemoteUrl() remoteUrl} of |
69 | | - * a {@code @BrowserScenarioTest} method: <code>{remoteUrl}</code> |
| 90 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getRemoteUrl() |
| 91 | + * remoteUrl} of a {@code @BrowserScenarioTest} method: |
| 92 | + * <code>{remoteUrl}</code> |
70 | 93 | */ |
71 | 94 | String REMOTE_URL_PLACEHOLDER = "{remoteUrl}"; |
72 | 95 |
|
|
89 | 112 | * <li>{@link #REMOTE_URL_PLACEHOLDER}</li> |
90 | 113 | * </ul> |
91 | 114 | * |
92 | | - * <p>Defaults to {@link #DEFAULT_NAME}, resulting in |
93 | | - * names such as {@code "chat button - chrome latest [--window-size=1280,720]"} |
| 115 | + * <p> |
| 116 | + * Defaults to {@link #DEFAULT_NAME}, resulting in names such as |
| 117 | + * {@code "chat button - chrome latest [--window-size=1280,720]"} |
94 | 118 | * |
95 | | - * <p>Alternatively, you can provide a custom display name, optionally |
96 | | - * using the aforementioned placeholders. |
| 119 | + * <p> |
| 120 | + * Alternatively, you can provide a custom display name, optionally using |
| 121 | + * the aforementioned placeholders. |
97 | 122 | * |
98 | 123 | * @return a custom display name; never blank or consisting solely of |
99 | | - * whitespace |
| 124 | + * whitespace |
100 | 125 | */ |
101 | 126 | String name() default DEFAULT_NAME; |
102 | 127 |
|
103 | 128 | /** |
104 | | - * A utility class for formatting the display name of a browser scenario test. |
| 129 | + * A utility class for formatting the display name of a browser scenario |
| 130 | + * test. |
105 | 131 | */ |
106 | 132 | final class NameFormatter { |
107 | 133 |
|
108 | 134 | /** |
109 | 135 | * The constructor is private to prevent instantiation. |
110 | 136 | */ |
111 | | - private NameFormatter() {} |
| 137 | + private NameFormatter() { |
| 138 | + } |
112 | 139 |
|
113 | 140 | /** |
114 | 141 | * Formats the display name of a browser scenario test. |
115 | 142 | * |
116 | | - * @param namePattern the name pattern from {@link BrowserScenarioTest#name()} |
117 | | - * @param displayName the display name from {@link ExtensionContext#getDisplayName()} |
118 | | - * @param browser the {@link BrowsersTemplate.Browser} from the {@link TestTemplateInvocationContext } |
| 143 | + * @param namePattern the name pattern from |
| 144 | + * {@link BrowserScenarioTest#name()} |
| 145 | + * @param displayName the display name from |
| 146 | + * {@link ExtensionContext#getDisplayName()} |
| 147 | + * @param browser the {@link BrowsersTemplate.Browser} from the |
| 148 | + * {@link TestTemplateInvocationContext } |
119 | 149 | * @return the formatted display name |
120 | 150 | */ |
121 | | - public static String format(String namePattern, String displayName, BrowsersTemplate.Browser browser) { |
| 151 | + public static String format(String namePattern, String displayName, |
| 152 | + BrowsersTemplate.Browser browser) { |
122 | 153 | Preconditions.notNull(namePattern, "namePattern must not be null"); |
123 | 154 | Preconditions.notNull(displayName, "displayName must not be null"); |
124 | 155 | Preconditions.notNull(browser, "browser must not be null"); |
125 | 156 | String result = namePattern.trim(); |
126 | | - Preconditions.notBlank(result, "@BrowserScenarioTest must be declared with a non-empty name."); |
127 | | - result = replacePlaceholders(result, DISPLAY_NAME_PLACEHOLDER, displayName); |
128 | | - result = replacePlaceholders(result, TYPE_PLACEHOLDER, browser.getType()); |
129 | | - result = replacePlaceholders(result, VERSION_PLACEHOLDER, browser.getVersion()); |
130 | | - result = replacePlaceholders(result, ARGUMENTS_PLACEHOLDER, browser.getArguments(), Arrays::toString); |
131 | | - result = replacePlaceholders(result, PREFERENCES_PLACEHOLDER, browser.getPreferences(), Arrays::toString); |
132 | | - result = replacePlaceholders(result, CAPABILITIES_PLACEHOLDER, browser.getCapabilities(), Object::toString); |
133 | | - return replacePlaceholders(result, REMOTE_URL_PLACEHOLDER, browser.getRemoteUrl()); |
| 157 | + Preconditions.notBlank(result, |
| 158 | + "@BrowserScenarioTest must be declared with a non-empty name."); |
| 159 | + result = replacePlaceholders(result, DISPLAY_NAME_PLACEHOLDER, |
| 160 | + displayName); |
| 161 | + result = replacePlaceholders(result, TYPE_PLACEHOLDER, |
| 162 | + browser.getType()); |
| 163 | + result = replacePlaceholders(result, VERSION_PLACEHOLDER, |
| 164 | + browser.getVersion()); |
| 165 | + result = replacePlaceholders(result, ARGUMENTS_PLACEHOLDER, |
| 166 | + browser.getArguments(), Arrays::toString); |
| 167 | + result = replacePlaceholders(result, PREFERENCES_PLACEHOLDER, |
| 168 | + browser.getPreferences(), Arrays::toString); |
| 169 | + result = replacePlaceholders(result, CAPABILITIES_PLACEHOLDER, |
| 170 | + browser.getCapabilities(), Object::toString); |
| 171 | + return replacePlaceholders(result, REMOTE_URL_PLACEHOLDER, |
| 172 | + browser.getRemoteUrl()); |
134 | 173 | } |
135 | 174 |
|
136 | 175 | /** |
137 | | - * Null-safe placeholder replacement in the name pattern with the given string value. If the value is null, |
138 | | - * replacement is skipped. |
| 176 | + * Null-safe placeholder replacement in the name pattern with the given |
| 177 | + * string value. If the value is null, replacement is skipped. |
139 | 178 | * |
140 | 179 | * @param namePattern the (not-nullable) pattern for string replacements |
141 | 180 | * @param placeholder the (not-nullable) placeholder to be replaced |
142 | | - * @param value the (nullable) string value to replace with which to replace the placeholder |
143 | | - * @return the (non-null) string pattern with placeholders replaced by the given value |
| 181 | + * @param value the (nullable) string value to replace with which |
| 182 | + * to replace the placeholder |
| 183 | + * @return the (non-null) string pattern with placeholders replaced by |
| 184 | + * the given value |
144 | 185 | */ |
145 | | - private static String replacePlaceholders(String namePattern, String placeholder, String value) { |
146 | | - return replacePlaceholders(namePattern, placeholder, value, UnaryOperator.identity()); |
| 186 | + private static String replacePlaceholders(String namePattern, |
| 187 | + String placeholder, String value) { |
| 188 | + return replacePlaceholders(namePattern, placeholder, value, |
| 189 | + UnaryOperator.identity()); |
147 | 190 | } |
148 | 191 |
|
149 | 192 | /** |
150 | | - * Null-safe placeholder replacement in the name pattern with the given value transformed with a mapping |
151 | | - * function. If the value is null, replacement is skipped. |
| 193 | + * Null-safe placeholder replacement in the name pattern with the given |
| 194 | + * value transformed with a mapping function. If the value is null, |
| 195 | + * replacement is skipped. |
152 | 196 | * |
153 | 197 | * @param namePattern the (not-nullable) pattern for string replacements |
154 | 198 | * @param placeholder the (not-nullable) placeholder to be replaced |
155 | | - * @param value the (nullable) string value to replace with which to replace the placeholder |
156 | | - * @param mapper a mapping function to transform the value into a suitable string |
157 | | - * @return the (non-null) string pattern with placeholders replaced by the given value |
| 199 | + * @param value the (nullable) string value to replace with which |
| 200 | + * to replace the placeholder |
| 201 | + * @param mapper a mapping function to transform the value into a |
| 202 | + * suitable string |
| 203 | + * @return the (non-null) string pattern with placeholders replaced by |
| 204 | + * the given value |
158 | 205 | * @param <T> the generic type of the value |
159 | 206 | */ |
160 | | - private static <T> String replacePlaceholders(String namePattern, String placeholder, T value, Function<T, String> mapper) { |
| 207 | + private static <T> String replacePlaceholders(String namePattern, |
| 208 | + String placeholder, T value, Function<T, String> mapper) { |
161 | 209 | if (value != null) { |
162 | 210 | return namePattern.replace(placeholder, mapper.apply(value)); |
163 | 211 | } else { |
|
0 commit comments