Skip to content

Commit b023399

Browse files
laurentschoelenslukasj
authored andcommitted
[#240] fixes parseBoolean according spec
1 parent 10f8e1a commit b023399

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

api/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
<groupId>jakarta.activation</groupId>
3636
<artifactId>jakarta.activation-api</artifactId>
3737
</dependency>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<version>4.13.2</version>
42+
<scope>test</scope>
43+
</dependency>
3844
</dependencies>
3945

4046
<build>

api/src/main/java/jakarta/xml/bind/DatatypeConverterImpl.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public static Boolean _parseBoolean(CharSequence literal) {
289289
ch = literal.charAt(i++);
290290
} while ((strTrue.charAt(strIndex++) == ch) && i < len && strIndex < 3);
291291

292-
if (strIndex == 3) {
292+
if (strIndex == 3 && strTrue.charAt(strIndex - 1) == ch) {
293293
value = true;
294294
} else {
295295
throw new IllegalArgumentException("String \"" + literal + "\" is not valid boolean value.");
@@ -303,7 +303,7 @@ public static Boolean _parseBoolean(CharSequence literal) {
303303
} while ((strFalse.charAt(strIndex++) == ch) && i < len && strIndex < 4);
304304

305305

306-
if (strIndex == 4) {
306+
if (strIndex == 4 && strFalse.charAt(strIndex - 1) == ch) {
307307
value = false;
308308
} else {
309309
throw new IllegalArgumentException("String \"" + literal + "\" is not valid boolean value.");
@@ -312,10 +312,8 @@ public static Boolean _parseBoolean(CharSequence literal) {
312312
break;
313313
}
314314

315-
if (i < len) {
316-
do {
317-
ch = literal.charAt(i++);
318-
} while (WhiteSpaceProcessor.isWhiteSpace(ch) && i < len);
315+
while (i < len && WhiteSpaceProcessor.isWhiteSpace(literal.charAt(i))) {
316+
i++;
319317
}
320318

321319
if (i == len) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2023, 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
package org.eclipse.jaxb.api;
12+
13+
import jakarta.xml.bind.DatatypeConverter;
14+
import org.junit.Assert;
15+
import org.junit.Test;
16+
17+
public class DatatypeConverterTest {
18+
19+
@Test
20+
public void testParseBoolean() {
21+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean(null));
22+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean(""));
23+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("11"));
24+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("1A"));
25+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("non"));
26+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("fals"));
27+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("falses"));
28+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("false s"));
29+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("falst"));
30+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("tru"));
31+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("trux"));
32+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("truu"));
33+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("truxx"));
34+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("truth"));
35+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("truelle"));
36+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("truec"));
37+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("true c"));
38+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBoolean("oui"));
39+
40+
41+
Assert.assertEquals(false, DatatypeConverter.parseBoolean("0"));
42+
Assert.assertEquals(false, DatatypeConverter.parseBoolean(" 0"));
43+
Assert.assertEquals(false, DatatypeConverter.parseBoolean(" 0 "));
44+
Assert.assertEquals(false, DatatypeConverter.parseBoolean("0 "));
45+
Assert.assertEquals(true, DatatypeConverter.parseBoolean("1"));
46+
Assert.assertEquals(true, DatatypeConverter.parseBoolean(" 1"));
47+
Assert.assertEquals(true, DatatypeConverter.parseBoolean(" 1 "));
48+
Assert.assertEquals(true, DatatypeConverter.parseBoolean("1 "));
49+
Assert.assertEquals(false, DatatypeConverter.parseBoolean("false"));
50+
Assert.assertEquals(false, DatatypeConverter.parseBoolean(" false"));
51+
Assert.assertEquals(false, DatatypeConverter.parseBoolean("false "));
52+
Assert.assertEquals(false, DatatypeConverter.parseBoolean(" false "));
53+
Assert.assertEquals(true, DatatypeConverter.parseBoolean("true"));
54+
Assert.assertEquals(true, DatatypeConverter.parseBoolean(" true"));
55+
Assert.assertEquals(true, DatatypeConverter.parseBoolean("true "));
56+
Assert.assertEquals(true, DatatypeConverter.parseBoolean(" true "));
57+
}
58+
}

0 commit comments

Comments
 (0)