Skip to content

Commit 04fc9f4

Browse files
author
Gilles Sadowski
committed
MATH-1681: Throw exception when parsing fails.
1 parent 1e59551 commit 04fc9f4

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/ComplexFormat.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public NumberFormat getRealFormat() {
350350
public Complex parse(String source) throws MathParseException {
351351
ParsePosition parsePosition = new ParsePosition(0);
352352
Complex result = parse(source, parsePosition);
353-
if (parsePosition.getIndex() == 0) {
353+
if (result == null) {
354354
throw new MathParseException(source,
355355
parsePosition.getErrorIndex(),
356356
Complex.class);
@@ -363,7 +363,8 @@ public Complex parse(String source) throws MathParseException {
363363
*
364364
* @param source the string to parse
365365
* @param pos input/output parsing parameter.
366-
* @return the parsed {@link Complex} object.
366+
* @return the parsed {@link Complex} object, or {@code null} if
367+
* parsing failed.
367368
*/
368369
public Complex parse(String source, ParsePosition pos) {
369370
int initialIndex = pos.getIndex();

commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
3030
import org.apache.commons.math4.legacy.exception.NoDataException;
3131
import org.apache.commons.math4.legacy.exception.NullArgumentException;
32+
import org.apache.commons.math4.legacy.exception.MathParseException;
3233
import org.apache.commons.math4.core.jdkmath.JdkMath;
3334

3435
public abstract class ComplexFormatAbstractTest {
@@ -406,4 +407,12 @@ public void testForgottenImaginaryCharacter() {
406407
Assert.assertNull(new ComplexFormat().parse("1 + 1", pos));
407408
Assert.assertEquals(5, pos.getErrorIndex());
408409
}
410+
411+
// MATH-1681.
412+
@Test(expected=MathParseException.class)
413+
public void testParseMissingImaginaryCharacter() {
414+
ComplexFormat format = new ComplexFormat();
415+
String invalidComplex = "3 + 5";
416+
format.parse(invalidComplex);
417+
}
409418
}

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Caveat:
9696
to support the whole codebase (it was one of the main reasons for
9797
creating more focused components).
9898
">
99+
<action dev="erans" type="fix" issue="MATH-1681" due-to="Ruiqi Dong">
100+
"ComplexFormat": Throw exception when parsing fails.
101+
</action>
99102
<action dev="erans" type="fix" issue="MATH-1683" due-to="Ruiqi Dong">
100103
"ElkanKMeansPlusPlusClusterer": More explicit error message.
101104
</action>

0 commit comments

Comments
 (0)