19
19
import java .util .HashSet ;
20
20
import java .util .List ;
21
21
import java .util .Locale ;
22
+ import java .util .stream .Stream ;
22
23
23
24
import org .htmlunit .BrowserVersion ;
24
25
import org .htmlunit .MockWebConnection ;
25
26
import org .htmlunit .WebClient ;
26
27
import org .htmlunit .WebTestCase ;
27
28
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 ;
32
32
33
33
/**
34
34
* <p>Tests for all the generated attribute accessors. This test case will
46
46
* @author Ronald Brill
47
47
* @author Frank Danek
48
48
*/
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 {
54
50
55
51
private static final List <String > EXCLUDED_METHODS = new ArrayList <>();
56
52
static {
@@ -61,13 +57,13 @@ public class AttributesTest extends TestCase {
61
57
}
62
58
63
59
/**
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.
65
61
*
66
- * @return the test suite
62
+ * @return stream of dynamic tests
67
63
* @throws Exception if the tests cannot be created
68
64
*/
69
- public static Test suite () throws Exception {
70
- final TestSuite suite = new TestSuite ();
65
+ @ TestFactory
66
+ Stream < DynamicTest > attributeTests () throws Exception {
71
67
final String [] classesToTest = {
72
68
"HtmlAbbreviated" , "HtmlAcronym" ,
73
69
"HtmlAnchor" , "HtmlAddress" , "HtmlArea" ,
@@ -123,10 +119,11 @@ public static Test suite() throws Exception {
123
119
};
124
120
125
121
final HashSet <String > supportedTags = new HashSet <>(DefaultElementFactory .SUPPORTED_TAGS_ );
122
+ final List <DynamicTest > tests = new ArrayList <>();
126
123
127
124
for (final String testClass : classesToTest ) {
128
125
final Class <?> clazz = Class .forName ("org.htmlunit.html." + testClass );
129
- addTestsForClass ( clazz , suite );
126
+ tests . addAll ( createTestsForClass ( clazz ) );
130
127
131
128
String tag ;
132
129
if (DomComment .class == clazz ) {
@@ -150,72 +147,78 @@ public static Test suite() throws Exception {
150
147
if (!supportedTags .isEmpty ()) {
151
148
throw new RuntimeException ("Missing tag class(es) " + supportedTags );
152
149
}
153
- return suite ;
150
+ return tests . stream () ;
154
151
}
155
152
156
153
/**
157
- * Adds all the tests for a given class.
154
+ * Creates all the tests for a given class.
158
155
*
159
156
* @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
162
157
* @throws Exception if the tests cannot be created
163
158
*/
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 <>();
167
161
final Method [] methods = clazz .getMethods ();
162
+
168
163
for (final Method method : methods ) {
169
164
final String methodName = method .getName ();
170
165
if (methodName .startsWith ("get" )
171
166
&& methodName .endsWith ("Attribute" )
172
167
&& !EXCLUDED_METHODS .contains (methodName )) {
173
168
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
+ }));
197
177
}
198
178
}
179
+ return tests ;
199
180
}
200
181
201
182
/**
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.
207
184
*/
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
+ }
212
197
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
+ }
217
219
}
218
220
221
+
219
222
/**
220
223
* Creates a name for this particular test that reflects the attribute being tested.
221
224
* @param clazz the class containing the attribute
@@ -230,51 +233,29 @@ private static String createTestName(final Class<?> clazz, final Method method)
230
233
return "testAttributes_" + className + '_' + method .getName ();
231
234
}
232
235
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
-
256
236
/**
257
237
* Creates a new instance of the class being tested.
258
238
* @return the new instance
259
239
* @throws Exception if the new object cannot be created
260
240
*/
261
- private DomElement getNewInstanceForClassUnderTest (final HtmlPage page ) throws Exception {
241
+ private static DomElement getNewInstanceForClassUnderTest (
242
+ final Class <?> classUnderTest , final HtmlPage page ) throws Exception {
262
243
final HTMLParser htmlParser = page .getWebClient ().getPageCreator ().getHtmlParser ();
263
244
final DomElement newInstance ;
264
- if (classUnderTest_ == HtmlTableRow .class ) {
245
+ if (classUnderTest == HtmlTableRow .class ) {
265
246
newInstance = htmlParser .getFactory (HtmlTableRow .TAG_NAME )
266
247
.createElement (page , HtmlTableRow .TAG_NAME , null );
267
248
}
268
- else if (classUnderTest_ == HtmlTableHeaderCell .class ) {
249
+ else if (classUnderTest == HtmlTableHeaderCell .class ) {
269
250
newInstance = htmlParser .getFactory (HtmlTableHeaderCell .TAG_NAME )
270
251
.createElement (page , HtmlTableHeaderCell .TAG_NAME , null );
271
252
}
272
- else if (classUnderTest_ == HtmlTableDataCell .class ) {
253
+ else if (classUnderTest == HtmlTableDataCell .class ) {
273
254
newInstance = htmlParser .getFactory (HtmlTableDataCell .TAG_NAME )
274
255
.createElement (page , HtmlTableDataCell .TAG_NAME , null );
275
256
}
276
257
else {
277
- final String tagName = (String ) classUnderTest_ .getField ("TAG_NAME" ).get (null );
258
+ final String tagName = (String ) classUnderTest .getField ("TAG_NAME" ).get (null );
278
259
newInstance = htmlParser .getFactory (tagName ).createElement (page , tagName , null );
279
260
}
280
261
0 commit comments