Skip to content

Commit 4562162

Browse files
committed
use our own ArrayUtils at more places and add/fix unit tests for ArrayUtils
1 parent f21a60e commit 4562162

File tree

8 files changed

+88
-9
lines changed

8 files changed

+88
-9
lines changed

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
<body>
1010
<release version="4.19.0" date="November xx, 2025" description="Chrome/Edge 142, Firefox 145, Bugfixes">
11+
<action type="update" dev="rbri">
12+
Use our own ArrayUtils at more places.
13+
</action>
1114
<action type="update" dev="rbri">
1215
Upgrade Apache commons-lang3 to 3.20.0.
1316
</action>

src/main/java/org/htmlunit/html/HtmlAnchor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Locale;
2323
import java.util.Map;
2424

25-
import org.apache.commons.lang3.ArrayUtils;
2625
import org.apache.commons.logging.Log;
2726
import org.apache.commons.logging.LogFactory;
2827
import org.htmlunit.BrowserVersion;
@@ -36,6 +35,7 @@
3635
import org.htmlunit.javascript.host.event.Event;
3736
import org.htmlunit.javascript.host.html.HTMLElement;
3837
import org.htmlunit.protocol.javascript.JavaScriptURLConnection;
38+
import org.htmlunit.util.ArrayUtils;
3939
import org.htmlunit.util.StringUtils;
4040
import org.htmlunit.util.UrlUtils;
4141

src/main/java/org/htmlunit/html/HtmlForm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.function.Predicate;
3333
import java.util.regex.Pattern;
3434

35-
import org.apache.commons.lang3.ArrayUtils;
3635
import org.apache.commons.lang3.StringUtils;
3736
import org.apache.commons.logging.Log;
3837
import org.apache.commons.logging.LogFactory;
@@ -52,6 +51,7 @@
5251
import org.htmlunit.javascript.host.event.Event;
5352
import org.htmlunit.javascript.host.event.SubmitEvent;
5453
import org.htmlunit.protocol.javascript.JavaScriptURLConnection;
54+
import org.htmlunit.util.ArrayUtils;
5555
import org.htmlunit.util.EncodingSniffer;
5656
import org.htmlunit.util.NameValuePair;
5757
import org.htmlunit.util.UrlUtils;

src/main/java/org/htmlunit/javascript/host/css/CSSStyleDeclaration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.Map;
3838
import java.util.Set;
3939

40-
import org.apache.commons.lang3.ArrayUtils;
4140
import org.htmlunit.BrowserVersion;
4241
import org.htmlunit.corejs.javascript.Scriptable;
4342
import org.htmlunit.corejs.javascript.ScriptableObject;
@@ -57,6 +56,7 @@
5756
import org.htmlunit.javascript.configuration.JsxSetter;
5857
import org.htmlunit.javascript.configuration.JsxSymbol;
5958
import org.htmlunit.javascript.host.Element;
59+
import org.htmlunit.util.ArrayUtils;
6060
import org.htmlunit.util.StringUtils;
6161

6262
/**

src/main/java/org/htmlunit/javascript/host/html/HTMLElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.Locale;
3131
import java.util.Set;
3232

33-
import org.apache.commons.lang3.ArrayUtils;
3433
import org.htmlunit.SgmlPage;
3534
import org.htmlunit.corejs.javascript.Function;
3635
import org.htmlunit.corejs.javascript.ScriptableObject;
@@ -109,6 +108,7 @@
109108
import org.htmlunit.javascript.host.event.Event;
110109
import org.htmlunit.javascript.host.event.EventHandler;
111110
import org.htmlunit.javascript.host.event.MouseEvent;
111+
import org.htmlunit.util.ArrayUtils;
112112
import org.htmlunit.util.StringUtils;
113113

114114
/**

src/main/java/org/htmlunit/util/ArrayUtils.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,48 @@ private ArrayUtils() {
2828
// Empty.
2929
}
3030

31+
/**
32+
* @param strings the string[] to check
33+
* @param expected the string that we expect
34+
* @return true if at least one element of the array equals to the expected string
35+
*/
36+
public static boolean contains(final String[] strings, final String expected) {
37+
if (expected == null) {
38+
throw new IllegalArgumentException("Expected string can't be null");
39+
}
40+
41+
if (strings == null) {
42+
return false;
43+
}
44+
45+
for (final String s : strings) {
46+
if (expected.equals(s)) {
47+
return true;
48+
}
49+
}
50+
51+
return false;
52+
}
53+
54+
/**
55+
* @param bytes the byte[] to check
56+
* @param expected the byte that we expect
57+
* @return true if at least one element of the array equals to the expected byte
58+
*/
59+
public static boolean contains(final byte[] bytes, final byte expected) {
60+
if (bytes == null) {
61+
return false;
62+
}
63+
64+
for (final byte b : bytes) {
65+
if (expected == b) {
66+
return true;
67+
}
68+
}
69+
70+
return false;
71+
}
72+
3173
/**
3274
* @param strings the string[] to check
3375
* @param expected the string that we expect

src/main/java/org/htmlunit/util/EncodingSniffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static boolean matches(final byte[] bytes, final int i, final byte[][] sought) {
589589
static int skipToAnyOf(final byte[] bytes, final int startFrom, final byte[] targets) {
590590
int i = startFrom;
591591
for ( ; i < bytes.length; i++) {
592-
if (ArrayUtils.contains(targets, bytes[i])) {
592+
if (org.htmlunit.util.ArrayUtils.contains(targets, bytes[i])) {
593593
break;
594594
}
595595
}

src/test/java/org/htmlunit/util/ArrayUtilsTest.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,34 @@
1414
*/
1515
package org.htmlunit.util;
1616

17+
import static org.junit.jupiter.api.Assertions.assertFalse;
1718
import static org.junit.jupiter.api.Assertions.assertThrows;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
1820

19-
import org.htmlunit.SimpleWebTestCase;
2021
import org.junit.jupiter.api.Test;
2122

2223
/**
2324
* Tests for {@link ArrayUtils}.
2425
*
2526
* @author Ronald Brill
2627
*/
27-
public class ArrayUtilsTest extends SimpleWebTestCase {
28+
public class ArrayUtilsTest {
29+
30+
/**
31+
* @throws Exception if the test fails
32+
*/
33+
@Test
34+
public void contains() throws Exception {
35+
assertTrue(ArrayUtils.contains(new String[] {"ab"}, "ab"));
36+
assertTrue(ArrayUtils.contains(new String[] {"o", "ab", "cd"}, "ab"));
37+
assertTrue(ArrayUtils.contains(new String[] {"cd", "ab"}, "ab"));
38+
39+
assertFalse(ArrayUtils.contains(null, "ab"));
40+
assertFalse(ArrayUtils.contains(new String[] {}, "ab"));
41+
assertFalse(ArrayUtils.contains(new String[] {"cd", "ab"}, "x"));
42+
43+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.contains(new String[] {}, null));
44+
}
2845

2946
/**
3047
* @throws Exception if the test fails
@@ -35,11 +52,28 @@ public void containsIgnoreCase() throws Exception {
3552
assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"o", "ab", "cd"}, "ab"));
3653
assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"cd", "ab"}, "ab"));
3754

55+
assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"ab"}, "aB"));
56+
assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"o", "ab", "cd"}, "Ab"));
57+
assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"cd", "ab"}, "AB"));
58+
3859
assertFalse(ArrayUtils.containsIgnoreCase(null, "ab"));
3960
assertFalse(ArrayUtils.containsIgnoreCase(new String[] {}, "ab"));
4061
assertFalse(ArrayUtils.containsIgnoreCase(new String[] {"cd", "ab"}, "x"));
4162

42-
assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase("AB", null));
43-
assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase(null, null));
63+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.containsIgnoreCase(new String[] {}, null));
64+
}
65+
66+
/**
67+
* @throws Exception if the test fails
68+
*/
69+
@Test
70+
public void containsByte() throws Exception {
71+
assertTrue(ArrayUtils.contains(new byte[] {1}, (byte) 1));
72+
assertTrue(ArrayUtils.contains(new byte[] {7, 1, 9}, (byte) 1));
73+
assertTrue(ArrayUtils.contains(new byte[] {5, 2}, (byte) 2));
74+
75+
assertFalse(ArrayUtils.contains(null, (byte) 7));
76+
assertFalse(ArrayUtils.contains(new byte[] {}, (byte) 1));
77+
assertFalse(ArrayUtils.contains(new byte[] {7, 9}, (byte) 4));
4478
}
4579
}

0 commit comments

Comments
 (0)