Skip to content
Closed
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 @@ -619,7 +619,7 @@ private static int parseIntXsdNumber(CharSequence ch, int min_value, int max_val

for (int i = 0; i < length - start; i++) {
c = ch.charAt(i + start);
int v = Character.digit(c, 10);
int v = (c >= '0' && c <= '9') ? c - '0' : -1;

if (v < 0) {
throw new NumberFormatException("For input string: \"" + ch.toString() + "\"");
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/misc/checkin/XsTypeConverterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package misc.checkin;

import org.apache.xmlbeans.impl.util.XsTypeConverter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class XsTypeConverterTest {

// fullwidth "123", arabic-indic "123" and devanagari "123" are unicode
// decimal digits but outside the xsd lexical space.
private static final String FULLWIDTH_123 = "123";
private static final String ARABIC_123 = "١٢٣";
private static final String DEVANAGARI_123 = "१२३";

@Test
void lexIntAcceptsAscii() {
assertEquals(123, XsTypeConverter.lexInt("123"));
assertEquals(-123, XsTypeConverter.lexInt("-123"));
assertEquals(123, XsTypeConverter.lexInt("+123"));
}

@Test
void lexIntRejectsNonAsciiDigits() {
assertThrows(NumberFormatException.class, () -> XsTypeConverter.lexInt(FULLWIDTH_123));
assertThrows(NumberFormatException.class, () -> XsTypeConverter.lexInt(ARABIC_123));
assertThrows(NumberFormatException.class, () -> XsTypeConverter.lexInt(DEVANAGARI_123));
}

@Test
void lexShortRejectsNonAsciiDigits() {
assertEquals(123, XsTypeConverter.lexShort("123"));
assertThrows(NumberFormatException.class, () -> XsTypeConverter.lexShort(FULLWIDTH_123));
assertThrows(NumberFormatException.class, () -> XsTypeConverter.lexShort(ARABIC_123));
}

@Test
void lexByteRejectsNonAsciiDigits() {
assertEquals(123, XsTypeConverter.lexByte("123"));
assertThrows(NumberFormatException.class, () -> XsTypeConverter.lexByte(FULLWIDTH_123));
assertThrows(NumberFormatException.class, () -> XsTypeConverter.lexByte(ARABIC_123));
}
}