Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencyFormatter.java
- IDEA additional info:
- Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
- <+>UTF-8
- ===================================================================
- --- ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencyFormatter.java (revision 22937)
- +++ ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencyFormatter.java (revision )
- @@ -7,8 +7,10 @@
- import org.kuali.rice.core.api.util.type.KualiInteger;
- import java.text.DecimalFormat;
- +import java.text.MessageFormat;
- import java.text.NumberFormat;
- import java.text.ParseException;
- +import java.util.Locale;
- import java.util.regex.Pattern;
- /**
- @@ -39,11 +41,42 @@
- */
- private static Logger LOG = Logger.getLogger(CurrencyFormatter.class);
- public static final String SHOW_SYMBOL = "showCurrencySymbol";
- - private static final Pattern CURRENCY_PATTERN = Pattern.compile("[-\\(\\)\\"+getSymbolForCurrencyPattern()+"\\.,0-9]*");
- + private Pattern currencyPattern;
- private static final Pattern TRAILING_DECIMAL_PATTERN = Pattern.compile("^(\\.[0-9]{0,2}){0,1}\\)?$");
- // end Kuali Foundation modification
- + private CurrencySymbolHelper currencySymbolHelper;
- /**
- + * default contructor - uses the default currency symbol helper
- + */
- + public CurrencyFormatter() {
- + initializeCurrencyPattern();
- + }
- +
- + /**
- + * initiliaze the currency pattern
- + * escape any regex operator in the symbol e.g. $
- + */
- + protected void initializeCurrencyPattern() {
- + final String regexWithoutSymbolEscape = "[-\\(\\){0}\\.,0-9]*";
- + String symbol = getCurrencySymbolHelper().getSymbolForCurrencyPattern();
- +
- + symbol = symbol.replace("$", "\\$");
- + symbol = symbol.replace("^", "\\^");
- +
- + setCurrencyPattern(Pattern.compile(MessageFormat.format(regexWithoutSymbolEscape, symbol)));
- + }
- +
- + /**
- + * constructor with a currency symbol helper or a mock to use
- + * @param currencySymbolHelper
- + */
- + public CurrencyFormatter(CurrencySymbolHelper currencySymbolHelper) {
- + setCurrencySymbolHelper(currencySymbolHelper);
- + initializeCurrencyPattern();
- + }
- +
- + /**
- * begin Kuali Foundation modification
- * Unformats its argument and returns a KualiDecimal instance initialized with the resulting string value
- *
- @@ -65,19 +98,19 @@
- // parseable values are $1.23 and ($1.23), not (1.23)
- if (target.startsWith("(")) {
- - if (!target.startsWith("(" + getSymbol())) {
- - target = "(" + getSymbol() + StringUtils.substringAfter(target, "(");
- + if (!target.startsWith("(" + getCurrencySymbolHelper().getSymbol())) {
- + target = "(" + getCurrencySymbolHelper().getSymbol() + StringUtils.substringAfter(target, "(");
- }
- }
- // Insert currency symbol if absent
- - if (!(target.startsWith("(") || target.startsWith(getSymbol()))) {
- + if (!(target.startsWith("(") || target.startsWith(getCurrencySymbolHelper().getSymbol()))) {
- target = interpolateSymbol(target);
- }
- // preemptively detect non-numeric-related symbols, since NumberFormat.parse seems to be silently deleting them
- // (i.e. 9aaaaaaaaaaaaaaa is silently converted into 9)
- - if (!CURRENCY_PATTERN.matcher(target).matches()) {
- + if (!getCurrencyPattern().matcher(target).matches()) {
- throw new FormatException("parsing", RiceKeyConstants.ERROR_CURRENCY, rawString);
- }
- @@ -87,7 +120,7 @@
- }
- // actually reformat the numeric value
- - NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
- + NumberFormat formatter = getCurrencySymbolHelper().getCurrencyInstanceUsingParseBigDecimal();
- try {
- Number parsedNumber = formatter.parse(target);
- value = new KualiDecimal(parsedNumber.toString());
- @@ -107,26 +140,20 @@
- protected String interpolateSymbol(String target) {
- if (target.startsWith("-")) {
- int dashPos = target.indexOf('-');
- - int symbolPos = target.indexOf(getSymbol());
- + int symbolPos = target.indexOf(getCurrencySymbolHelper().getSymbol());
- int index = (dashPos > symbolPos ? dashPos : symbolPos);
- - return "(" + ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getPositivePrefix() + target.substring(index + 1) + ")";
- + return "(" + ((DecimalFormat) getCurrencySymbolHelper().getCurrencyInstanceUsingParseBigDecimal()).getPositivePrefix() + target.substring(index + 1) + ")";
- }
- - return target.startsWith("(") ? "(" + getSymbol() + target.indexOf("(" + 1) : getSymbol() + target;
- + return target.startsWith("(") ? "(" + getCurrencySymbolHelper().getSymbol() + target.indexOf("(" + 1) : getCurrencySymbolHelper().getSymbol() + target;
- }
- protected String removeSymbol(String target) {
- - int index = target.indexOf(getSymbol());
- + int index = target.indexOf(getCurrencySymbolHelper().getSymbol());
- String prefix = (index > 0 ? target.substring(0, index) : "");
- return prefix + target.substring(index + 1);
- }
- - protected String getSymbol() {
- - return ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getDecimalFormatSymbols().getCurrencySymbol();
- - }
- - public static String getSymbolForCurrencyPattern() {
- - return ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getDecimalFormatSymbols().getCurrencySymbol();
- - }
- protected boolean showSymbol() {
- String showSymbol = (settings == null ? null : (String) settings.get(SHOW_SYMBOL));
- @@ -176,7 +203,7 @@
- }
- - NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
- + NumberFormat formatter = getCurrencySymbolHelper().getCurrencyInstanceUsingParseBigDecimal();
- String string = null;
- try {
- @@ -211,22 +238,6 @@
- // begin Kuali Foundation modification
- /**
- - * retrieves a currency formatter instance and sets ParseBigDecimal to true to fix [KULEDOCS-742]
- - *
- - * @return CurrencyInstance
- - */
- - static final NumberFormat getCurrencyInstanceUsingParseBigDecimal() {
- - NumberFormat formatter = NumberFormat.getCurrencyInstance();
- - if (formatter instanceof DecimalFormat) {
- - ((DecimalFormat) formatter).setParseBigDecimal(true);
- - }
- - return formatter;
- - }
- - // end Kuali Foundation modification
- -
- - // begin Kuali Foundation modification
- -
- - /**
- * Validates a currency string by passing it into the convertToObject method and determining if conversion succeeded.
- *
- * @param currencyString The string to attempt to format.
- @@ -243,4 +254,28 @@
- return (currencyObject != null);
- }
- // end Kuali Foundation modification
- +
- + public CurrencySymbolHelper getCurrencySymbolHelper() {
- + return currencySymbolHelper;
- + }
- +
- + public void setCurrencySymbolHelper(CurrencySymbolHelper currencySymbolHelper) {
- + this.currencySymbolHelper = currencySymbolHelper;
- + }
- +
- + public Pattern getCurrencyPattern() {
- + return currencyPattern;
- + }
- +
- + public void setCurrencyPattern(Pattern currencyPattern) {
- + this.currencyPattern = currencyPattern;
- + }
- +
- + /**
- + * redirects to helper class
- + * @see CurrencySymbolHelper#getSymbolForCurrencyPattern()
- + */
- + public static String getSymbolForCurrencyPattern() {
- + return new CurrencySymbolHelper().getSymbolForCurrencyPattern();
- + }
- }
- Index: ole-app/olefs/src/test/java/org/kuali/rice/core/web/format/CurrencyFormatterTest.java
- IDEA additional info:
- Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
- <+>UTF-8
- ===================================================================
- --- ole-app/olefs/src/test/java/org/kuali/rice/core/web/format/CurrencyFormatterTest.java (revision )
- +++ ole-app/olefs/src/test/java/org/kuali/rice/core/web/format/CurrencyFormatterTest.java (revision )
- @@ -0,0 +1,50 @@
- +/**
- + *
- + */
- +package org.kuali.rice.core.web.format;
- +
- +import org.junit.Before;
- +import org.junit.Test;
- +
- +import static org.junit.Assert.assertEquals;
- +import static org.junit.Assert.fail;
- +import static org.mockito.Mockito.mock;
- +import static org.mockito.Mockito.when;
- +
- +/**
- + * @author mugo
- + *
- + */
- +public class CurrencyFormatterTest {
- +
- + private CurrencySymbolHelper currencySymbolHelper;
- +
- + @Before
- + public void setup() {
- + currencySymbolHelper = mock(CurrencySymbolHelper.class);
- + }
- +
- + @Test
- + public void testCurrencyPattern_za() {
- + when(currencySymbolHelper.getSymbolForCurrencyPattern()).thenReturn("R");
- + try {
- + CurrencyFormatter formatter = new CurrencyFormatter(currencySymbolHelper);
- + assertEquals("pattern differs", "[-\\(\\)R\\.,0-9]*", formatter.getCurrencyPattern().toString());
- + } catch (Exception e) {
- + fail(e.getMessage());
- + e.printStackTrace();
- + }
- + }
- +
- + @Test
- + public void testInstantiation_en_us() {
- + when(currencySymbolHelper.getSymbolForCurrencyPattern()).thenReturn("$");
- + try {
- + CurrencyFormatter formatter = new CurrencyFormatter(currencySymbolHelper);
- + assertEquals("pattern differs", "[-\\(\\)\\$\\.,0-9]*", formatter.getCurrencyPattern().toString());
- + } catch (Exception e) {
- + fail(e.getMessage());
- + e.printStackTrace();
- + }
- + }
- +}
- Index: ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencySymbolHelper.java
- IDEA additional info:
- Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
- <+>UTF-8
- ===================================================================
- --- ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencySymbolHelper.java (revision )
- +++ ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencySymbolHelper.java (revision )
- @@ -0,0 +1,35 @@
- +package org.kuali.rice.core.web.format;
- +
- +import java.text.DecimalFormat;
- +import java.text.NumberFormat;
- +
- +/**
- + * Methods extracted from {@link org.kuali.rice.core.web.format.CurrencyFormatter} to allow for mocking
- + * Created by kunadawa@gmail.com on 6/17/15.
- + */
- +public class CurrencySymbolHelper {
- + protected String getSymbol() {
- + return ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getDecimalFormatSymbols().getCurrencySymbol();
- + }
- +
- + public String getSymbolForCurrencyPattern() {
- + return ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getDecimalFormatSymbols().getCurrencySymbol();
- + }
- +
- + // begin Kuali Foundation modification
- +
- + /**
- + * retrieves a currency formatter instance and sets ParseBigDecimal to true to fix [KULEDOCS-742]
- + *
- + * @return CurrencyInstance
- + */
- + public NumberFormat getCurrencyInstanceUsingParseBigDecimal() {
- + NumberFormat formatter = NumberFormat.getCurrencyInstance();
- + if (formatter instanceof DecimalFormat) {
- + ((DecimalFormat) formatter).setParseBigDecimal(true);
- + }
- + return formatter;
- + }
- + // end Kuali Foundation modification
- +
- +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement