From 45ff8f9aff1ce08546b091505a706b1baf1bcb7b Mon Sep 17 00:00:00 2001 From: Julien Lepiller Date: Sat, 15 Sep 2018 00:09:39 +0200 Subject: [PATCH] Remove dependency on jsr354 (javax.currency). jsr354 is a non-free package: https://github.com/JavaMoney/jsr354-api/blob/master/EVALUATION-LICENCE.txt --- .../DefaultFormattingConversionService.java | 10 - .../number/money/MoneyFormattingTests.java | 272 ------------------ 2 files changed, 282 deletions(-) delete mode 100644 spring-context/src/test/java/org/springframework/format/number/money/MoneyFormattingTests.java diff --git a/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java b/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java index 2c7c60a..5e38006 100644 --- a/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java +++ b/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java @@ -21,9 +21,6 @@ import org.springframework.format.FormatterRegistry; import org.springframework.format.datetime.DateFormatterRegistrar; import org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar; import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar; -import org.springframework.format.number.money.CurrencyUnitFormatter; -import org.springframework.format.number.money.Jsr354NumberFormatAnnotationFormatterFactory; -import org.springframework.format.number.money.MonetaryAmountFormatter; import org.springframework.format.number.NumberFormatAnnotationFormatterFactory; import org.springframework.util.ClassUtils; import org.springframework.util.StringValueResolver; @@ -104,13 +101,6 @@ public class DefaultFormattingConversionService extends FormattingConversionServ // Default handling of number values formatterRegistry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); - // Default handling of monetary values - if (jsr354Present) { - formatterRegistry.addFormatter(new CurrencyUnitFormatter()); - formatterRegistry.addFormatter(new MonetaryAmountFormatter()); - formatterRegistry.addFormatterForFieldAnnotation(new Jsr354NumberFormatAnnotationFormatterFactory()); - } - // Default handling of date-time values if (jsr310Present) { // just handling JSR-310 specific date and time types diff --git a/spring-context/src/test/java/org/springframework/format/number/money/MoneyFormattingTests.java b/spring-context/src/test/java/org/springframework/format/number/money/MoneyFormattingTests.java deleted file mode 100644 index 28df446..0000000 --- a/spring-context/src/test/java/org/springframework/format/number/money/MoneyFormattingTests.java +++ /dev/null @@ -1,272 +0,0 @@ -/* - * Copyright 2002-2015 the original author or authors. - * - * 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 org.springframework.format.number.money; - -import java.util.Locale; -import javax.money.CurrencyUnit; -import javax.money.MonetaryAmount; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import org.springframework.beans.MutablePropertyValues; -import org.springframework.context.i18n.LocaleContextHolder; -import org.springframework.format.annotation.NumberFormat; -import org.springframework.format.support.DefaultFormattingConversionService; -import org.springframework.format.support.FormattingConversionService; -import org.springframework.validation.DataBinder; - -import static org.junit.Assert.*; - -/** - * @author Juergen Hoeller - * @since 4.2 - */ -public class MoneyFormattingTests { - - private final FormattingConversionService conversionService = new DefaultFormattingConversionService(); - - - @Before - public void setUp() { - LocaleContextHolder.setLocale(Locale.US); - } - - @After - public void tearDown() { - LocaleContextHolder.setLocale(null); - } - - - @Test - public void testAmountAndUnit() { - MoneyHolder bean = new MoneyHolder(); - DataBinder binder = new DataBinder(bean); - binder.setConversionService(conversionService); - - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("amount", "USD 10.50"); - propertyValues.add("unit", "USD"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount")); - assertEquals("USD", binder.getBindingResult().getFieldValue("unit")); - assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); - assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); - - LocaleContextHolder.setLocale(Locale.CANADA); - binder.bind(propertyValues); - LocaleContextHolder.setLocale(Locale.US); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount")); - assertEquals("USD", binder.getBindingResult().getFieldValue("unit")); - assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); - assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); - } - - @Test - public void testAmountWithNumberFormat1() { - FormattedMoneyHolder1 bean = new FormattedMoneyHolder1(); - DataBinder binder = new DataBinder(bean); - binder.setConversionService(conversionService); - - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("amount", "$10.50"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("$10.50", binder.getBindingResult().getFieldValue("amount")); - assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); - assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); - - LocaleContextHolder.setLocale(Locale.CANADA); - binder.bind(propertyValues); - LocaleContextHolder.setLocale(Locale.US); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("$10.50", binder.getBindingResult().getFieldValue("amount")); - assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); - assertEquals("CAD", bean.getAmount().getCurrency().getCurrencyCode()); - } - - @Test - public void testAmountWithNumberFormat2() { - FormattedMoneyHolder2 bean = new FormattedMoneyHolder2(); - DataBinder binder = new DataBinder(bean); - binder.setConversionService(conversionService); - - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("amount", "10.50"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("10.5", binder.getBindingResult().getFieldValue("amount")); - assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); - assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); - } - - @Test - public void testAmountWithNumberFormat3() { - FormattedMoneyHolder3 bean = new FormattedMoneyHolder3(); - DataBinder binder = new DataBinder(bean); - binder.setConversionService(conversionService); - - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("amount", "10%"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("10%", binder.getBindingResult().getFieldValue("amount")); - assertTrue(bean.getAmount().getNumber().doubleValue() == 0.1d); - assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); - } - - @Test - public void testAmountWithNumberFormat4() { - FormattedMoneyHolder4 bean = new FormattedMoneyHolder4(); - DataBinder binder = new DataBinder(bean); - binder.setConversionService(conversionService); - - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("amount", "010.500"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("010.500", binder.getBindingResult().getFieldValue("amount")); - assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); - assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); - } - - @Test - public void testAmountWithNumberFormat5() { - FormattedMoneyHolder5 bean = new FormattedMoneyHolder5(); - DataBinder binder = new DataBinder(bean); - binder.setConversionService(conversionService); - - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("amount", "USD 10.50"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount")); - assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); - assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); - - LocaleContextHolder.setLocale(Locale.CANADA); - binder.bind(propertyValues); - LocaleContextHolder.setLocale(Locale.US); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount")); - assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); - assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); - } - - - public static class MoneyHolder { - - private MonetaryAmount amount; - - private CurrencyUnit unit; - - public MonetaryAmount getAmount() { - return amount; - } - - public void setAmount(MonetaryAmount amount) { - this.amount = amount; - } - - public CurrencyUnit getUnit() { - return unit; - } - - public void setUnit(CurrencyUnit unit) { - this.unit = unit; - } - } - - - public static class FormattedMoneyHolder1 { - - @NumberFormat - private MonetaryAmount amount; - - public MonetaryAmount getAmount() { - return amount; - } - - public void setAmount(MonetaryAmount amount) { - this.amount = amount; - } - } - - - public static class FormattedMoneyHolder2 { - - @NumberFormat(style = NumberFormat.Style.NUMBER) - private MonetaryAmount amount; - - public MonetaryAmount getAmount() { - return amount; - } - - public void setAmount(MonetaryAmount amount) { - this.amount = amount; - } - } - - - public static class FormattedMoneyHolder3 { - - @NumberFormat(style = NumberFormat.Style.PERCENT) - private MonetaryAmount amount; - - public MonetaryAmount getAmount() { - return amount; - } - - public void setAmount(MonetaryAmount amount) { - this.amount = amount; - } - } - - - public static class FormattedMoneyHolder4 { - - @NumberFormat(pattern = "#000.000#") - private MonetaryAmount amount; - - public MonetaryAmount getAmount() { - return amount; - } - - public void setAmount(MonetaryAmount amount) { - this.amount = amount; - } - } - - - public static class FormattedMoneyHolder5 { - - @NumberFormat(pattern = "\u00A4\u00A4 #000.000#") - private MonetaryAmount amount; - - public MonetaryAmount getAmount() { - return amount; - } - - public void setAmount(MonetaryAmount amount) { - this.amount = amount; - } - } - -} -- 2.18.0