Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected DatatypeConverterImpl() {
}

public static BigInteger _parseInteger(CharSequence s) {
return new BigInteger(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString());
return new BigInteger(WhiteSpaceProcessor.trim(s).toString());
}

public static String _printInteger(BigInteger val) {
Expand Down Expand Up @@ -96,7 +96,7 @@ public static int _parseInt(CharSequence s) {
}

public static long _parseLong(CharSequence s) {
return Long.parseLong(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString());
return Long.parseLong(WhiteSpaceProcessor.trim(s).toString());
}

public static short _parseShort(CharSequence s) {
Expand Down Expand Up @@ -680,25 +680,6 @@ public static int _printBase64Binary(byte[] input, int offset, int len, byte[] o
return ptr;
}

private static CharSequence removeOptionalPlus(CharSequence s) {
int len = s.length();

if (len <= 1 || s.charAt(0) != '+') {
return s;
}

s = s.subSequence(1, len);
char ch = s.charAt(0);
if ('0' <= ch && ch <= '9') {
return s;
}
if ('.' == ch) {
return s;
}

throw new NumberFormatException();
}

private static boolean isDigitOrPeriodOrSign(char ch) {
if ('0' <= ch && ch <= '9') {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,79 @@

package org.glassfish.jaxb.runtime;

import org.junit.Assert;
import org.junit.Test;

import java.math.BigInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

public class DatatypeConverterImplTest {

@Test
@SuppressWarnings({"deprecation"})
public void testParseBoolean() {
Assert.assertNull(DatatypeConverterImpl._parseBoolean(null));
Assert.assertNull(DatatypeConverterImpl._parseBoolean(""));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("11"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("1A"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("non"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("fals"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("falses"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("false s"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("falst"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("tru"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("trux"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("truu"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("truxx"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("truth"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("truelle"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("truec"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("true c"));
Assert.assertNull(DatatypeConverterImpl._parseBoolean("oui"));

Assert.assertFalse(DatatypeConverterImpl._parseBoolean("0"));
Assert.assertFalse(DatatypeConverterImpl._parseBoolean(" 0"));
Assert.assertFalse(DatatypeConverterImpl._parseBoolean(" 0 "));
Assert.assertFalse(DatatypeConverterImpl._parseBoolean("0 "));
Assert.assertTrue(DatatypeConverterImpl._parseBoolean("1"));
Assert.assertTrue(DatatypeConverterImpl._parseBoolean(" 1"));
Assert.assertTrue(DatatypeConverterImpl._parseBoolean(" 1 "));
Assert.assertTrue(DatatypeConverterImpl._parseBoolean("1 "));
Assert.assertFalse(DatatypeConverterImpl._parseBoolean("false"));
Assert.assertFalse(DatatypeConverterImpl._parseBoolean(" false"));
Assert.assertFalse(DatatypeConverterImpl._parseBoolean("false "));
Assert.assertFalse(DatatypeConverterImpl._parseBoolean(" false "));
Assert.assertTrue(DatatypeConverterImpl._parseBoolean("true"));
Assert.assertTrue(DatatypeConverterImpl._parseBoolean(" true"));
Assert.assertTrue(DatatypeConverterImpl._parseBoolean("true "));
Assert.assertTrue(DatatypeConverterImpl._parseBoolean(" true "));
assertNull(DatatypeConverterImpl._parseBoolean(null));
assertNull(DatatypeConverterImpl._parseBoolean(""));
assertNull(DatatypeConverterImpl._parseBoolean("11"));
assertNull(DatatypeConverterImpl._parseBoolean("1A"));
assertNull(DatatypeConverterImpl._parseBoolean("non"));
assertNull(DatatypeConverterImpl._parseBoolean("fals"));
assertNull(DatatypeConverterImpl._parseBoolean("falses"));
assertNull(DatatypeConverterImpl._parseBoolean("false s"));
assertNull(DatatypeConverterImpl._parseBoolean("falst"));
assertNull(DatatypeConverterImpl._parseBoolean("tru"));
assertNull(DatatypeConverterImpl._parseBoolean("trux"));
assertNull(DatatypeConverterImpl._parseBoolean("truu"));
assertNull(DatatypeConverterImpl._parseBoolean("truxx"));
assertNull(DatatypeConverterImpl._parseBoolean("truth"));
assertNull(DatatypeConverterImpl._parseBoolean("truelle"));
assertNull(DatatypeConverterImpl._parseBoolean("truec"));
assertNull(DatatypeConverterImpl._parseBoolean("true c"));
assertNull(DatatypeConverterImpl._parseBoolean("oui"));

assertFalse(DatatypeConverterImpl._parseBoolean("0"));
assertFalse(DatatypeConverterImpl._parseBoolean(" 0"));
assertFalse(DatatypeConverterImpl._parseBoolean(" 0 "));
assertFalse(DatatypeConverterImpl._parseBoolean("0 "));
assertTrue(DatatypeConverterImpl._parseBoolean("1"));
assertTrue(DatatypeConverterImpl._parseBoolean(" 1"));
assertTrue(DatatypeConverterImpl._parseBoolean(" 1 "));
assertTrue(DatatypeConverterImpl._parseBoolean("1 "));
assertFalse(DatatypeConverterImpl._parseBoolean("false"));
assertFalse(DatatypeConverterImpl._parseBoolean(" false"));
assertFalse(DatatypeConverterImpl._parseBoolean("false "));
assertFalse(DatatypeConverterImpl._parseBoolean(" false "));
assertTrue(DatatypeConverterImpl._parseBoolean("true"));
assertTrue(DatatypeConverterImpl._parseBoolean(" true"));
assertTrue(DatatypeConverterImpl._parseBoolean("true "));
assertTrue(DatatypeConverterImpl._parseBoolean(" true "));
}

@Test
@SuppressWarnings({"deprecation"})
public void testParseInteger() {
// happily parses with and without leading sign
assertEquals(BigInteger.valueOf(12345), DatatypeConverterImpl._parseInteger("+12345"));
assertEquals(BigInteger.valueOf(12345), DatatypeConverterImpl._parseInteger("12345"));
assertEquals(BigInteger.valueOf(-12345), DatatypeConverterImpl._parseInteger("-12345"));

// rejects decimal points
assertThrows(NumberFormatException.class, () -> DatatypeConverterImpl._parseInteger(".12345"));
}

@Test
@SuppressWarnings({"deprecation"})
public void testParseLong() {
// happily parses with and without leading sign
assertEquals(12345, DatatypeConverterImpl._parseLong("+12345"));
assertEquals(12345, DatatypeConverterImpl._parseLong("12345"));
assertEquals(-12345, DatatypeConverterImpl._parseLong("-12345"));

// rejects decimal points
assertThrows(NumberFormatException.class, () -> DatatypeConverterImpl._parseLong(".12345"));
}
}