Skip to content

Commit 4b77ee5

Browse files
committed
junit4 is gone
1 parent 0d446ea commit 4b77ee5

File tree

5 files changed

+101
-145
lines changed

5 files changed

+101
-145
lines changed

src/test/java/org/htmlunit/ErrorOutputChecker.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
import java.util.regex.Pattern;
2121

2222
import org.apache.commons.lang3.StringUtils;
23-
import org.junit.rules.MethodRule;
24-
import org.junit.runners.model.FrameworkMethod;
25-
import org.junit.runners.model.Statement;
26-
23+
import org.junit.jupiter.api.extension.AfterEachCallback;
24+
import org.junit.jupiter.api.extension.BeforeEachCallback;
25+
import org.junit.jupiter.api.extension.ExtensionContext;
2726
/**
2827
* JUnit 4 {@link org.junit.Rule} verifying that nothing is printed to {@link System#err}
2928
* during test execution. If this is the case, the rule generates a failure for
@@ -34,7 +33,7 @@
3433
* @author Ahmed Ashour
3534
* @author Frank Danek
3635
*/
37-
public class ErrorOutputChecker implements MethodRule {
36+
public class ErrorOutputChecker implements BeforeEachCallback, AfterEachCallback {
3837
private PrintStream originalErr_;
3938
private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream();
4039
private static final Pattern[] PATTERNS = {
@@ -50,31 +49,19 @@ public class ErrorOutputChecker implements MethodRule {
5049
Pattern.compile(".*Quercus finished initialization in \\d*ms\r?\n"),
5150
};
5251

53-
/**
54-
* {@inheritDoc}
55-
*/
5652
@Override
57-
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
58-
if (target instanceof WebDriverTestCase) {
59-
final WebDriverTestCase testCase = (WebDriverTestCase) target;
60-
if (testCase.useRealBrowser()) {
61-
return base;
62-
}
63-
}
53+
public void beforeEach(final ExtensionContext context) throws Exception {
54+
wrapSystemErr();
55+
}
6456

65-
return new Statement() {
66-
@Override
67-
public void evaluate() throws Throwable {
68-
wrapSystemErr();
69-
try {
70-
base.evaluate();
71-
verifyNoOutput();
72-
}
73-
finally {
74-
restoreSystemErr();
75-
}
76-
}
77-
};
57+
@Override
58+
public void afterEach(final ExtensionContext context) throws Exception {
59+
try {
60+
verifyNoOutput();
61+
}
62+
finally {
63+
restoreSystemErr();
64+
}
7865
}
7966

8067
void verifyNoOutput() {

src/test/java/org/htmlunit/WebTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
*/
7373
@ClassTemplate
7474
@ExtendWith({BrowserVersionClassTemplateInvocationContextProvider.class,
75-
SetExpectedAlertsBeforeTestExecutionCallback.class})
75+
SetExpectedAlertsBeforeTestExecutionCallback.class,
76+
ErrorOutputChecker.class})
7677
@TestMethodOrder(MethodOrderer.DisplayName.class)
7778
public abstract class WebTestCase {
7879

src/test/java/org/htmlunit/html/AttributesTest.java

Lines changed: 66 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
import java.util.HashSet;
2020
import java.util.List;
2121
import java.util.Locale;
22+
import java.util.stream.Stream;
2223

2324
import org.htmlunit.BrowserVersion;
2425
import org.htmlunit.MockWebConnection;
2526
import org.htmlunit.WebClient;
2627
import org.htmlunit.WebTestCase;
2728
import org.htmlunit.html.parser.HTMLParser;
28-
29-
import junit.framework.Test;
30-
import junit.framework.TestCase;
31-
import junit.framework.TestSuite;
29+
import org.junit.jupiter.api.Assertions;
30+
import org.junit.jupiter.api.DynamicTest;
31+
import org.junit.jupiter.api.TestFactory;
3232

3333
/**
3434
* <p>Tests for all the generated attribute accessors. This test case will
@@ -46,11 +46,7 @@
4646
* @author Ronald Brill
4747
* @author Frank Danek
4848
*/
49-
public class AttributesTest extends TestCase {
50-
51-
private final Class<?> classUnderTest_;
52-
private final Method method_;
53-
private final String attributeName_;
49+
public class AttributesTest {
5450

5551
private static final List<String> EXCLUDED_METHODS = new ArrayList<>();
5652
static {
@@ -61,13 +57,13 @@ public class AttributesTest extends TestCase {
6157
}
6258

6359
/**
64-
* Returns a test suite containing a separate test for each attribute on each element.
60+
* Creates dynamic tests for each attribute on each element.
6561
*
66-
* @return the test suite
62+
* @return stream of dynamic tests
6763
* @throws Exception if the tests cannot be created
6864
*/
69-
public static Test suite() throws Exception {
70-
final TestSuite suite = new TestSuite();
65+
@TestFactory
66+
Stream<DynamicTest> attributeTests() throws Exception {
7167
final String[] classesToTest = {
7268
"HtmlAbbreviated", "HtmlAcronym",
7369
"HtmlAnchor", "HtmlAddress", "HtmlArea",
@@ -123,10 +119,11 @@ public static Test suite() throws Exception {
123119
};
124120

125121
final HashSet<String> supportedTags = new HashSet<>(DefaultElementFactory.SUPPORTED_TAGS_);
122+
final List<DynamicTest> tests = new ArrayList<>();
126123

127124
for (final String testClass : classesToTest) {
128125
final Class<?> clazz = Class.forName("org.htmlunit.html." + testClass);
129-
addTestsForClass(clazz, suite);
126+
tests.addAll(createTestsForClass(clazz));
130127

131128
String tag;
132129
if (DomComment.class == clazz) {
@@ -150,72 +147,78 @@ public static Test suite() throws Exception {
150147
if (!supportedTags.isEmpty()) {
151148
throw new RuntimeException("Missing tag class(es) " + supportedTags);
152149
}
153-
return suite;
150+
return tests.stream();
154151
}
155152

156153
/**
157-
* Adds all the tests for a given class.
154+
* Creates all the tests for a given class.
158155
*
159156
* @param clazz the class to create tests for
160-
* @param page the page that will be passed into the constructor of the objects to be tested
161-
* @param suite the suite that all the tests will be placed inside
162157
* @throws Exception if the tests cannot be created
163158
*/
164-
private static void addTestsForClass(final Class<?> clazz, final TestSuite suite)
165-
throws Exception {
166-
159+
private List<DynamicTest> createTestsForClass(final Class<?> clazz) throws Exception {
160+
final List<DynamicTest> tests = new ArrayList<>();
167161
final Method[] methods = clazz.getMethods();
162+
168163
for (final Method method : methods) {
169164
final String methodName = method.getName();
170165
if (methodName.startsWith("get")
171166
&& methodName.endsWith("Attribute")
172167
&& !EXCLUDED_METHODS.contains(methodName)) {
173168

174-
String attributeName = methodName.substring(3, methodName.length() - 9).toLowerCase(Locale.ROOT);
175-
if ("xmllang".equals(attributeName)) {
176-
attributeName = "xml:lang";
177-
}
178-
else if ("columns".equals(attributeName)) {
179-
attributeName = "cols";
180-
}
181-
else if ("columnspan".equals(attributeName)) {
182-
attributeName = "colspan";
183-
}
184-
else if ("textdirection".equals(attributeName)) {
185-
attributeName = "dir";
186-
}
187-
else if ("httpequiv".equals(attributeName)) {
188-
attributeName = "http-equiv";
189-
}
190-
else if ("acceptcharset".equals(attributeName)) {
191-
attributeName = "accept-charset";
192-
}
193-
else if ("htmlfor".equals(attributeName)) {
194-
attributeName = "for";
195-
}
196-
suite.addTest(new AttributesTest(attributeName, clazz, method));
169+
final String attributeName =
170+
normalizeAttributeName(
171+
methodName.substring(3, methodName.length() - 9).toLowerCase(Locale.ROOT));
172+
173+
final String testName = createTestName(clazz, method);
174+
tests.add(DynamicTest.dynamicTest(testName, () -> {
175+
executeAttributeTest(clazz, method, attributeName);
176+
}));
197177
}
198178
}
179+
return tests;
199180
}
200181

201182
/**
202-
* Creates an instance of the test. This will test one specific attribute
203-
* on one specific class.
204-
* @param attributeName the name of the attribute to test
205-
* @param classUnderTest the class containing the attribute
206-
* @param method the "getter" method for the specified attribute
183+
* Normalizes attribute names for special cases.
207184
*/
208-
public AttributesTest(
209-
final String attributeName,
210-
final Class<?> classUnderTest,
211-
final Method method) {
185+
private static String normalizeAttributeName(final String attributeName) {
186+
switch (attributeName) {
187+
case "xmllang": return "xml:lang";
188+
case "columns": return "cols";
189+
case "columnspan": return "colspan";
190+
case "textdirection": return "dir";
191+
case "httpequiv": return "http-equiv";
192+
case "acceptcharset": return "accept-charset";
193+
case "htmlfor": return "for";
194+
default: return attributeName;
195+
}
196+
}
212197

213-
super(createTestName(classUnderTest, method));
214-
classUnderTest_ = classUnderTest;
215-
method_ = method;
216-
attributeName_ = attributeName;
198+
/**
199+
* Executes the actual test for a specific attribute.
200+
* @param classUnderTest the class under test
201+
* @param method the getter method
202+
* @param attributeName the attribute name
203+
* @throws Exception if the test fails
204+
*/
205+
private static void executeAttributeTest(final Class<?> classUnderTest,
206+
final Method method, final String attributeName) throws Exception {
207+
try (WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED)) {
208+
final MockWebConnection connection = new MockWebConnection();
209+
connection.setDefaultResponse("<html><head><title>foo</title></head><body></body></html>");
210+
webClient.setWebConnection(connection);
211+
final HtmlPage page = webClient.getPage(WebTestCase.URL_FIRST);
212+
final String value = "value";
213+
final DomElement objectToTest = getNewInstanceForClassUnderTest(classUnderTest, page);
214+
objectToTest.setAttribute(attributeName, value);
215+
final Object[] noObjects = new Object[0];
216+
final Object result = method.invoke(objectToTest, noObjects);
217+
Assertions.assertSame(value, result);
218+
}
217219
}
218220

221+
219222
/**
220223
* Creates a name for this particular test that reflects the attribute being tested.
221224
* @param clazz the class containing the attribute
@@ -230,51 +233,29 @@ private static String createTestName(final Class<?> clazz, final Method method)
230233
return "testAttributes_" + className + '_' + method.getName();
231234
}
232235

233-
/**
234-
* Runs the actual test.
235-
* @throws Exception if the test fails
236-
*/
237-
@Override
238-
protected void runTest() throws Exception {
239-
try (WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED)) {
240-
final MockWebConnection connection = new MockWebConnection();
241-
connection.setDefaultResponse("<html><head><title>foo</title></head><body></body></html>");
242-
webClient.setWebConnection(connection);
243-
final HtmlPage page = webClient.getPage(WebTestCase.URL_FIRST);
244-
245-
final String value = "value";
246-
247-
final DomElement objectToTest = getNewInstanceForClassUnderTest(page);
248-
objectToTest.setAttribute(attributeName_, value);
249-
250-
final Object[] noObjects = new Object[0];
251-
final Object result = method_.invoke(objectToTest, noObjects);
252-
assertSame(value, result);
253-
}
254-
}
255-
256236
/**
257237
* Creates a new instance of the class being tested.
258238
* @return the new instance
259239
* @throws Exception if the new object cannot be created
260240
*/
261-
private DomElement getNewInstanceForClassUnderTest(final HtmlPage page) throws Exception {
241+
private static DomElement getNewInstanceForClassUnderTest(
242+
final Class<?> classUnderTest, final HtmlPage page) throws Exception {
262243
final HTMLParser htmlParser = page.getWebClient().getPageCreator().getHtmlParser();
263244
final DomElement newInstance;
264-
if (classUnderTest_ == HtmlTableRow.class) {
245+
if (classUnderTest == HtmlTableRow.class) {
265246
newInstance = htmlParser.getFactory(HtmlTableRow.TAG_NAME)
266247
.createElement(page, HtmlTableRow.TAG_NAME, null);
267248
}
268-
else if (classUnderTest_ == HtmlTableHeaderCell.class) {
249+
else if (classUnderTest == HtmlTableHeaderCell.class) {
269250
newInstance = htmlParser.getFactory(HtmlTableHeaderCell.TAG_NAME)
270251
.createElement(page, HtmlTableHeaderCell.TAG_NAME, null);
271252
}
272-
else if (classUnderTest_ == HtmlTableDataCell.class) {
253+
else if (classUnderTest == HtmlTableDataCell.class) {
273254
newInstance = htmlParser.getFactory(HtmlTableDataCell.TAG_NAME)
274255
.createElement(page, HtmlTableDataCell.TAG_NAME, null);
275256
}
276257
else {
277-
final String tagName = (String) classUnderTest_.getField("TAG_NAME").get(null);
258+
final String tagName = (String) classUnderTest.getField("TAG_NAME").get(null);
278259
newInstance = htmlParser.getFactory(tagName).createElement(page, tagName, null);
279260
}
280261

0 commit comments

Comments
 (0)