Skip to content
Snippets Groups Projects
Commit 86b4e033 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#731 - AmountParser: check if whole string is used otherwise throw an error

parent 45083c44
No related branches found
No related tags found
No related merge requests found
package de.deadlocker8.budgetmaster.transactions.csvimport;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.MessageFormat;
import java.text.ParseException;
import org.springframework.security.core.parameters.P;
import java.text.*;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -35,7 +34,7 @@ public class AmountParser
final String amount = matcher.group(2);
final DecimalFormat decimalFormat = new DecimalFormat();
final DecimalFormat decimalFormat = new DecimalFormat("#,###.#");
final DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(decimalSeparator);
symbols.setGroupingSeparator(groupingSeparator);
......@@ -43,9 +42,15 @@ public class AmountParser
decimalFormat.setDecimalFormatSymbols(symbols);
final String parseableString = MessageFormat.format("{0}{1}", sign, amount);
final ParsePosition parsePosition = new ParsePosition(0);
try
{
final double parseDouble = decimalFormat.parse(parseableString).doubleValue();
final double parseDouble = decimalFormat.parse(parseableString, parsePosition).doubleValue();
if(parsePosition.getIndex() != parseableString.length())
{
throw new ParseException("String not fully parsed", parsePosition.getIndex());
}
return Optional.of((int) (parseDouble * 100));
}
catch(ParseException e)
......
......@@ -246,6 +246,21 @@ class AmountParserTest
.get().isEqualTo(-123403);
}
@Test
void test_thousandsDelimiter_specialDelimiter_invalid()
{
assertThat(AmountParser.parse("-234.03 €", 'e', 't'))
.isEmpty();
}
@Test
void test_thousandsDelimiter_specialDelimiter_valid()
{
assertThat(AmountParser.parse("-1t234e03 €", 'e', 't'))
.isPresent()
.get().isEqualTo(-123403);
}
@Test
void test_thousandsDelimiter_bigNumber()
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment