Advertisement
mugs

Kuali OLE 1.6.0 locale fix

Jun 17th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 11.44 KB | None | 0 0
  1. Index: ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencyFormatter.java
  2. IDEA additional info:
  3. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  4. <+>UTF-8
  5. ===================================================================
  6. --- ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencyFormatter.java   (revision 22937)
  7. +++ ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencyFormatter.java   (revision )
  8. @@ -7,8 +7,10 @@
  9.  import org.kuali.rice.core.api.util.type.KualiInteger;
  10.  
  11.  import java.text.DecimalFormat;
  12. +import java.text.MessageFormat;
  13.  import java.text.NumberFormat;
  14.  import java.text.ParseException;
  15. +import java.util.Locale;
  16.  import java.util.regex.Pattern;
  17.  
  18.  /**
  19. @@ -39,11 +41,42 @@
  20.      */
  21.      private static Logger LOG = Logger.getLogger(CurrencyFormatter.class);
  22.      public static final String SHOW_SYMBOL = "showCurrencySymbol";
  23. -    private static final Pattern CURRENCY_PATTERN = Pattern.compile("[-\\(\\)\\"+getSymbolForCurrencyPattern()+"\\.,0-9]*");
  24. +    private Pattern currencyPattern;
  25.      private static final Pattern TRAILING_DECIMAL_PATTERN = Pattern.compile("^(\\.[0-9]{0,2}){0,1}\\)?$");
  26.      // end Kuali Foundation modification
  27. +    private CurrencySymbolHelper currencySymbolHelper;
  28.  
  29.      /**
  30. +     * default contructor - uses the default currency symbol helper
  31. +     */
  32. +    public CurrencyFormatter() {
  33. +        initializeCurrencyPattern();
  34. +    }
  35. +
  36. +    /**
  37. +     * initiliaze the currency pattern
  38. +     * escape any regex operator in the symbol e.g. $
  39. +     */
  40. +    protected void initializeCurrencyPattern() {
  41. +        final String regexWithoutSymbolEscape = "[-\\(\\){0}\\.,0-9]*";
  42. +        String symbol = getCurrencySymbolHelper().getSymbolForCurrencyPattern();
  43. +
  44. +        symbol = symbol.replace("$", "\\$");
  45. +        symbol = symbol.replace("^", "\\^");
  46. +
  47. +        setCurrencyPattern(Pattern.compile(MessageFormat.format(regexWithoutSymbolEscape, symbol)));
  48. +    }
  49. +
  50. +    /**
  51. +     * constructor with a currency symbol helper or a mock to use
  52. +     * @param currencySymbolHelper
  53. +     */
  54. +    public CurrencyFormatter(CurrencySymbolHelper currencySymbolHelper) {
  55. +        setCurrencySymbolHelper(currencySymbolHelper);
  56. +        initializeCurrencyPattern();
  57. +    }
  58. +
  59. +    /**
  60.       * begin Kuali Foundation modification
  61.       * Unformats its argument and returns a KualiDecimal instance initialized with the resulting string value
  62.       *
  63. @@ -65,19 +98,19 @@
  64.  
  65.              // parseable values are $1.23 and ($1.23), not (1.23)
  66.              if (target.startsWith("(")) {
  67. -                if (!target.startsWith("(" + getSymbol())) {
  68. -                    target = "(" + getSymbol() + StringUtils.substringAfter(target, "(");
  69. +                if (!target.startsWith("(" + getCurrencySymbolHelper().getSymbol())) {
  70. +                    target = "(" + getCurrencySymbolHelper().getSymbol() + StringUtils.substringAfter(target, "(");
  71.                  }
  72.              }
  73.  
  74.              // Insert currency symbol if absent
  75. -            if (!(target.startsWith("(") || target.startsWith(getSymbol()))) {
  76. +            if (!(target.startsWith("(") || target.startsWith(getCurrencySymbolHelper().getSymbol()))) {
  77.                  target = interpolateSymbol(target);
  78.              }
  79.  
  80.              // preemptively detect non-numeric-related symbols, since NumberFormat.parse seems to be silently deleting them
  81.              // (i.e. 9aaaaaaaaaaaaaaa is silently converted into 9)
  82. -            if (!CURRENCY_PATTERN.matcher(target).matches()) {
  83. +            if (!getCurrencyPattern().matcher(target).matches()) {
  84.                  throw new FormatException("parsing", RiceKeyConstants.ERROR_CURRENCY, rawString);
  85.              }
  86.  
  87. @@ -87,7 +120,7 @@
  88.              }
  89.  
  90.              // actually reformat the numeric value
  91. -            NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
  92. +            NumberFormat formatter = getCurrencySymbolHelper().getCurrencyInstanceUsingParseBigDecimal();
  93.              try {
  94.                  Number parsedNumber = formatter.parse(target);
  95.                  value = new KualiDecimal(parsedNumber.toString());
  96. @@ -107,26 +140,20 @@
  97.      protected String interpolateSymbol(String target) {
  98.          if (target.startsWith("-")) {
  99.              int dashPos = target.indexOf('-');
  100. -            int symbolPos = target.indexOf(getSymbol());
  101. +            int symbolPos = target.indexOf(getCurrencySymbolHelper().getSymbol());
  102.              int index = (dashPos > symbolPos ? dashPos : symbolPos);
  103. -            return "(" + ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getPositivePrefix() + target.substring(index + 1) + ")";
  104. +            return "(" + ((DecimalFormat) getCurrencySymbolHelper().getCurrencyInstanceUsingParseBigDecimal()).getPositivePrefix() + target.substring(index + 1) + ")";
  105.          }
  106. -        return target.startsWith("(") ? "(" + getSymbol() + target.indexOf("(" + 1) : getSymbol() + target;
  107. +        return target.startsWith("(") ? "(" + getCurrencySymbolHelper().getSymbol() + target.indexOf("(" + 1) : getCurrencySymbolHelper().getSymbol() + target;
  108.      }
  109.  
  110.      protected String removeSymbol(String target) {
  111. -        int index = target.indexOf(getSymbol());
  112. +        int index = target.indexOf(getCurrencySymbolHelper().getSymbol());
  113.          String prefix = (index > 0 ? target.substring(0, index) : "");
  114.          return prefix + target.substring(index + 1);
  115.      }
  116.  
  117. -    protected String getSymbol() {
  118. -        return ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getDecimalFormatSymbols().getCurrencySymbol();
  119. -    }
  120.  
  121. -    public static String getSymbolForCurrencyPattern() {
  122. -        return ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getDecimalFormatSymbols().getCurrencySymbol();
  123. -    }
  124.  
  125.      protected boolean showSymbol() {
  126.          String showSymbol = (settings == null ? null : (String) settings.get(SHOW_SYMBOL));
  127. @@ -176,7 +203,7 @@
  128.          }
  129.  
  130.  
  131. -        NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
  132. +        NumberFormat formatter = getCurrencySymbolHelper().getCurrencyInstanceUsingParseBigDecimal();
  133.          String string = null;
  134.  
  135.          try {
  136. @@ -211,22 +238,6 @@
  137.      // begin Kuali Foundation modification
  138.  
  139.      /**
  140. -     * retrieves a currency formatter instance and sets ParseBigDecimal to true to fix [KULEDOCS-742]
  141. -     *
  142. -     * @return CurrencyInstance
  143. -     */
  144. -    static final NumberFormat getCurrencyInstanceUsingParseBigDecimal() {
  145. -        NumberFormat formatter = NumberFormat.getCurrencyInstance();
  146. -        if (formatter instanceof DecimalFormat) {
  147. -            ((DecimalFormat) formatter).setParseBigDecimal(true);
  148. -        }
  149. -        return formatter;
  150. -    }
  151. -    // end Kuali Foundation modification
  152. -
  153. -    // begin Kuali Foundation modification
  154. -
  155. -    /**
  156.       * Validates a currency string by passing it into the convertToObject method and determining if conversion succeeded.
  157.       *
  158.       * @param currencyString The string to attempt to format.
  159. @@ -243,4 +254,28 @@
  160.          return (currencyObject != null);
  161.      }
  162.      // end Kuali Foundation modification
  163. +
  164. +    public CurrencySymbolHelper getCurrencySymbolHelper() {
  165. +        return currencySymbolHelper;
  166. +    }
  167. +
  168. +    public void setCurrencySymbolHelper(CurrencySymbolHelper currencySymbolHelper) {
  169. +        this.currencySymbolHelper = currencySymbolHelper;
  170. +    }
  171. +
  172. +    public Pattern getCurrencyPattern() {
  173. +        return currencyPattern;
  174. +    }
  175. +
  176. +    public void setCurrencyPattern(Pattern currencyPattern) {
  177. +        this.currencyPattern = currencyPattern;
  178. +    }
  179. +
  180. +    /**
  181. +     * redirects to helper class
  182. +     * @see CurrencySymbolHelper#getSymbolForCurrencyPattern()
  183. +     */
  184. +    public static String getSymbolForCurrencyPattern() {
  185. +        return new CurrencySymbolHelper().getSymbolForCurrencyPattern();
  186. +    }
  187.  }
  188. Index: ole-app/olefs/src/test/java/org/kuali/rice/core/web/format/CurrencyFormatterTest.java
  189. IDEA additional info:
  190. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  191. <+>UTF-8
  192. ===================================================================
  193. --- ole-app/olefs/src/test/java/org/kuali/rice/core/web/format/CurrencyFormatterTest.java   (revision )
  194. +++ ole-app/olefs/src/test/java/org/kuali/rice/core/web/format/CurrencyFormatterTest.java   (revision )
  195. @@ -0,0 +1,50 @@
  196. +/**
  197. + *
  198. + */
  199. +package org.kuali.rice.core.web.format;
  200. +
  201. +import org.junit.Before;
  202. +import org.junit.Test;
  203. +
  204. +import static org.junit.Assert.assertEquals;
  205. +import static org.junit.Assert.fail;
  206. +import static org.mockito.Mockito.mock;
  207. +import static org.mockito.Mockito.when;
  208. +
  209. +/**
  210. + * @author mugo
  211. + *
  212. + */
  213. +public class CurrencyFormatterTest {
  214. +
  215. +    private CurrencySymbolHelper currencySymbolHelper;
  216. +
  217. +    @Before
  218. +    public void setup() {
  219. +        currencySymbolHelper = mock(CurrencySymbolHelper.class);
  220. +    }
  221. +
  222. +   @Test
  223. +   public void testCurrencyPattern_za() {
  224. +        when(currencySymbolHelper.getSymbolForCurrencyPattern()).thenReturn("R");
  225. +        try {
  226. +            CurrencyFormatter formatter = new CurrencyFormatter(currencySymbolHelper);
  227. +            assertEquals("pattern differs", "[-\\(\\)R\\.,0-9]*", formatter.getCurrencyPattern().toString());
  228. +       } catch (Exception e) {
  229. +           fail(e.getMessage());
  230. +           e.printStackTrace();
  231. +       }
  232. +   }
  233. +
  234. +   @Test
  235. +   public void testInstantiation_en_us() {
  236. +        when(currencySymbolHelper.getSymbolForCurrencyPattern()).thenReturn("$");
  237. +        try {
  238. +            CurrencyFormatter formatter = new CurrencyFormatter(currencySymbolHelper);
  239. +            assertEquals("pattern differs", "[-\\(\\)\\$\\.,0-9]*", formatter.getCurrencyPattern().toString());
  240. +        } catch (Exception e) {
  241. +            fail(e.getMessage());
  242. +            e.printStackTrace();
  243. +        }
  244. +   }
  245. +}
  246. Index: ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencySymbolHelper.java
  247. IDEA additional info:
  248. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  249. <+>UTF-8
  250. ===================================================================
  251. --- ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencySymbolHelper.java    (revision )
  252. +++ ole-app/olefs/src/main/java/org/kuali/rice/core/web/format/CurrencySymbolHelper.java    (revision )
  253. @@ -0,0 +1,35 @@
  254. +package org.kuali.rice.core.web.format;
  255. +
  256. +import java.text.DecimalFormat;
  257. +import java.text.NumberFormat;
  258. +
  259. +/**
  260. + * Methods extracted from {@link org.kuali.rice.core.web.format.CurrencyFormatter} to allow for mocking
  261. + * Created by kunadawa@gmail.com on 6/17/15.
  262. + */
  263. +public class CurrencySymbolHelper {
  264. +    protected String getSymbol() {
  265. +        return ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getDecimalFormatSymbols().getCurrencySymbol();
  266. +    }
  267. +
  268. +    public String getSymbolForCurrencyPattern() {
  269. +        return ((DecimalFormat) getCurrencyInstanceUsingParseBigDecimal()).getDecimalFormatSymbols().getCurrencySymbol();
  270. +    }
  271. +
  272. +    // begin Kuali Foundation modification
  273. +
  274. +    /**
  275. +     * retrieves a currency formatter instance and sets ParseBigDecimal to true to fix [KULEDOCS-742]
  276. +     *
  277. +     * @return CurrencyInstance
  278. +     */
  279. +    public NumberFormat getCurrencyInstanceUsingParseBigDecimal() {
  280. +        NumberFormat formatter = NumberFormat.getCurrencyInstance();
  281. +        if (formatter instanceof DecimalFormat) {
  282. +            ((DecimalFormat) formatter).setParseBigDecimal(true);
  283. +        }
  284. +        return formatter;
  285. +    }
  286. +    // end Kuali Foundation modification
  287. +
  288. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement