Permalink
Browse files

Added additional tests.

  • Loading branch information...
atsticks committed Mar 4, 2014
1 parent fdfa099 commit 0b1f1c3c767f55eb2590fab8ae4424a6618143df
View
@@ -21,3 +21,4 @@
/money-platform/.settings
/money-platform/target
/target
+/doc
@@ -85,16 +85,6 @@
/** The generic amount bahavioral characteristics. */
private final AmountFlavor flavor;
- /**
- * This map contains arbitrary attributes, identified by class. This allows
- * to store additional context data in a platform independent way, e.g. when
- * using {@link java.math.BigDecimal} as a representation type on SE, the
- * {@link java.math.RoundingMode} used can be stored as an attribute. Adding
- * it as part of the API would break compatibility with SE.
- */
- @SuppressWarnings("rawtypes")
- private final Map<Class, Object> attributes = new HashMap<>();
-
/**
* Flag, if the scale is fixed. Fixed scaled numbers will always have a
* scale of {@link #maxScale}.
@@ -112,18 +102,8 @@
* Constructs a new {@code MonetaryContext} with the specified precision and
* rounding mode.
*
- * @param amountType
- * The {@link MonetaryAmount} implementation class.
- * @param precision
- * The non-negative {@code int} precision setting.
- * @param maxScale
- * the maximal scale.
- * @param fixedScale
- * Flag for determining a fixed scale context.
- * @param flavor
- * the {@link AmountFlavor} set.
- * @param attributes
- * Any additional attributes.
+ * @param builder
+ * The {@link Builder} with data to be used.
* @throws IllegalArgumentException
* if the {@code setPrecision} parameter is less than zero.
*/
@@ -207,6 +187,8 @@ public int hashCode() {
result = prime * result + maxScale;
result = prime * result
+ ((amountType == null) ? 0 : amountType.hashCode());
+ result = prime * result
+ + ((flavor == null) ? 0 : flavor.hashCode());
result = prime * result + precision;
return result;
}
@@ -239,6 +221,11 @@ public boolean equals(Object obj) {
return false;
} else if (!amountType.equals(other.amountType))
return false;
+ if (flavor == null) {
+ if (other.flavor != null)
+ return false;
+ } else if (!flavor.equals(other.flavor))
+ return false;
if (precision != other.precision)
return false;
return true;
@@ -351,6 +338,7 @@ public Builder(MonetaryContext context) {
this.maxScale = context.getMaxScale();
this.fixedScale = context.isFixedScale();
this.precision = context.getPrecision();
+ this.amountFlavor = context.getAmountFlavor();
}
/**
@@ -81,7 +81,12 @@ public Locale getLocale() {
*/
@Override
public String toString() {
- return "UnknownCurrencyException [currencyCode=" + currencyCode + "]";
+ if(locale==null){
+ return "UnknownCurrencyException [currencyCode=" + currencyCode + "]";
+ }
+ else{
+ return "UnknownCurrencyException [locale=" + locale + "]";
+ }
}
}
@@ -8,12 +8,7 @@
*/
package javax.money.spi;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
-import java.util.ServiceLoader;
+import java.util.*;
import java.util.logging.Logger;
import javax.money.MonetaryException;
@@ -61,8 +56,9 @@ private static ServiceProvider loadDefaultServiceProvider() {
* the new {@link ServiceProvider}
*/
public static void init(ServiceProvider serviceProvider) {
- synchronized (LOCK) {
- if (serviceProvider != null) {
+ Objects.requireNonNull(serviceProvider);
+ synchronized (LOCK) {
+ if (Bootstrap.serviceProviderDelegate == null) {
Bootstrap.serviceProviderDelegate = serviceProvider;
Logger.getLogger(Bootstrap.class.getName()).info(
"Money Bootstrap: new ServiceProvider set: "
@@ -19,21 +19,21 @@
import java.util.logging.Logger;
/**
- * Loader for the Java Money configuration.
+ * Loader for the Java Money JSR configuration.
*
* @author Anatole Tresch
*/
-public final class JavaMoneyConfig {
+public final class MonetaryConfig{
private static final Logger LOG = Logger
- .getLogger(JavaMoneyConfig.class.getName());
+ .getLogger(MonetaryConfig.class.getName());
- private static final JavaMoneyConfig INSTANCE = new JavaMoneyConfig();
+ private static final MonetaryConfig INSTANCE = new MonetaryConfig();
private Map<String, String> config = new HashMap<>();
private Map<String, Integer> priorities = new HashMap<>();
- private JavaMoneyConfig() {
+ private MonetaryConfig() {
try {
Enumeration<URL> urls = getClass().getClassLoader().getResources(
"javamoney.properties");
@@ -0,0 +1,133 @@
+/*
+ * CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
+ * CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
+ * PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
+ * DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
+ * AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
+ * BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency
+ * API ("Specification") Copyright (c) 2012-2013, Credit Suisse All rights
+ * reserved.
+ */
+package javax.money;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.junit.Assert.*;
+
+/**
+ * Created by Anatole on 05.03.14.
+ */
+public class MonetaryContextTest{
+ @Test
+ public void testGetPrecision() throws Exception{
+ MonetaryContext ctx = new MonetaryContext.Builder().setPrecision(299).create();
+ assertTrue(ctx.getPrecision() == 299);
+ }
+
+ @Test
+ public void testIsFixedScale() throws Exception{
+ MonetaryContext ctx = new MonetaryContext.Builder().setFixedScale(true).create();
+ assertTrue(ctx.isFixedScale());
+ }
+
+ @Test
+ public void testGetMaxScale() throws Exception{
+ MonetaryContext ctx = new MonetaryContext.Builder().setMaxScale(122).create();
+ assertTrue(ctx.getMaxScale() == 122);
+ }
+
+ @Test
+ public void testGetAmountType() throws Exception{
+ MonetaryContext ctx = new MonetaryContext.Builder().setMaxScale(122).create();
+ assertEquals(ctx.getAmountType(), MonetaryAmount.class);
+ abstract class TestAmount implements MonetaryAmount{}
+ ctx = new MonetaryContext.Builder().setAmountType(TestAmount.class).create();
+ assertEquals(ctx.getAmountType(), TestAmount.class);
+ }
+
+ @Test
+ public void testGetAmountFlavor() throws Exception{
+ MonetaryContext ctx =
+ new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE).create();
+ assertEquals(MonetaryContext.AmountFlavor.PERFORMANCE, ctx.getAmountFlavor());
+ ctx = new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PRECISION).create();
+ assertEquals(MonetaryContext.AmountFlavor.PRECISION, ctx.getAmountFlavor());
+ ctx = new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.UNDEFINED).create();
+ assertEquals(MonetaryContext.AmountFlavor.UNDEFINED, ctx.getAmountFlavor());
+ ctx = new MonetaryContext.Builder().create();
+ assertEquals(MonetaryContext.AmountFlavor.UNDEFINED, ctx.getAmountFlavor());
+ }
+
+ @Test
+ public void testHashCode() throws Exception{
+ List<MonetaryContext> contexts = new ArrayList<>();
+ contexts.add(new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE).create());
+ contexts.add(new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PRECISION).create());
+ contexts.add(new MonetaryContext.Builder().setMaxScale(122).create());
+ contexts.add(new MonetaryContext.Builder().setPrecision(299).create());
+ contexts.add(new MonetaryContext.Builder().setFixedScale(true).create());
+ Set<Integer> hashCodes = new HashSet<>();
+ for(MonetaryContext ctx : contexts){
+ hashCodes.add(ctx.hashCode());
+ }
+ // Check we have 5 distinct hash codes...
+ assertTrue(hashCodes.size() == 5);
+ }
+
+ @Test
+ public void testEquals() throws Exception{
+ List<MonetaryContext> contexts = new ArrayList<>();
+ contexts.add(new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE).create());
+ contexts.add(new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PRECISION).create());
+ contexts.add(new MonetaryContext.Builder().setMaxScale(122).create());
+ contexts.add(new MonetaryContext.Builder().setPrecision(299).create());
+ contexts.add(new MonetaryContext.Builder().setFixedScale(true).create());
+ Set<MonetaryContext> checkContexts = new HashSet<>();
+ for(MonetaryContext ctx : contexts){
+ checkContexts.add(ctx);
+ checkContexts.add(ctx);
+ }
+ // Check we have 5 distinct hash codes...
+ assertTrue(checkContexts.size() == 5);
+ }
+
+ @Test
+ public void testFrom() throws Exception{
+ MonetaryContext rootCtx =
+ new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE).create();
+ MonetaryContext ctx = MonetaryContext.from(rootCtx, rootCtx.getAmountType());
+ assertEquals(ctx, rootCtx);
+ abstract class TestAmount implements MonetaryAmount{}
+ ctx = MonetaryContext.from(rootCtx, TestAmount.class);
+ assertFalse(ctx.equals(rootCtx));
+ }
+
+ @Test
+ public void testToString() throws Exception{
+ abstract class TestAmount implements MonetaryAmount{}
+ MonetaryContext ctx = new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE)
+ .setAmountType(TestAmount.class).setFixedScale(true).setMaxScale(111).setPrecision(200)
+ .set("myValue", "myKey").set("TEST").create();
+ assertNotNull(ctx.toString());
+ System.out.println(ctx.toString());
+ assertTrue(ctx.toString().contains("PERFORMANCE"));
+ assertTrue(ctx.toString().contains("flavor"));
+ assertTrue(ctx.toString().contains("111"));
+ assertTrue(ctx.toString().contains("200"));
+ assertTrue(ctx.toString().contains("TEST"));
+ assertTrue(ctx.toString().contains("myKey"));
+ assertTrue(ctx.toString().contains("myValue"));
+ assertTrue(ctx.toString().contains("String"));
+ assertTrue(ctx.toString().contains("maxScale"));
+ assertTrue(ctx.toString().contains("precision"));
+ assertTrue(ctx.toString().contains("fixedScale"));
+ assertTrue(ctx.toString().contains("TestAmount"));
+ assertTrue(ctx.toString().contains("amountType"));
+ assertTrue(ctx.toString().contains("MonetaryContext"));
+ }
+}
@@ -0,0 +1,40 @@
+/*
+ * CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
+ * CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
+ * PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
+ * DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
+ * AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
+ * BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency
+ * API ("Specification") Copyright (c) 2012-2013, Credit Suisse All rights
+ * reserved.
+ */
+package javax.money;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Created by Anatole on 05.03.14.
+ */
+public class MonetaryExceptionTest{
+ @Test
+ public void testGetMessage() throws Exception{
+ MonetaryException e = new MonetaryException("message1");
+ assertNotNull(e.getMessage());
+ assertTrue(e.getMessage().contains("message1"));
+ assertNull(e.getCause());
+ }
+
+ @Test
+ public void testGetCause() throws Exception{
+ MonetaryException e = new MonetaryException("message1", new Exception("Test"));
+ assertNotNull(e.getMessage());
+ assertTrue(e.getMessage().contains("message1"));
+ assertNotNull(e.getCause());
+ assertTrue(e.getCause().getMessage().equals("Test"));
+
+ }
+}
@@ -0,0 +1,44 @@
+/*
+ * CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
+ * CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
+ * PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
+ * DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
+ * AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
+ * BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency
+ * API ("Specification") Copyright (c) 2012-2013, Credit Suisse All rights
+ * reserved.
+ */
+package javax.money;
+
+import org.junit.Test;
+
+import java.util.Locale;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Created by Anatole on 05.03.14.
+ */
+public class UnknownCurrencyExceptionTest{
+ @Test
+ public void testGetCurrencyCode() throws Exception{
+ UnknownCurrencyException e = new UnknownCurrencyException("GGG");
+ assertEquals("GGG", e.getCurrencyCode());
+ assertNull(e.getLocale());
+ assertTrue(e.toString().contains("GGG"));
+ assertTrue(e.toString().contains("UnknownCurrencyException"));
+ }
+
+ @Test
+ public void testGetLocale() throws Exception{
+ UnknownCurrencyException e = new UnknownCurrencyException(Locale.CANADA_FRENCH);
+ assertEquals(Locale.CANADA_FRENCH, e.getLocale());
+ assertNull(e.getCurrencyCode());
+ System.out.println(e);
+ assertTrue(e.toString().contains(Locale.CANADA_FRENCH.toString()));
+ assertTrue(e.toString().contains("UnknownCurrencyException"));
+ }
+
+}
Oops, something went wrong.

0 comments on commit 0b1f1c3

Please sign in to comment.