Skip to content

Commit 0b1f1c3

Browse files
committed
Added additional tests.
1 parent fdfa099 commit 0b1f1c3

File tree

9 files changed

+389
-36
lines changed

9 files changed

+389
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
/money-platform/.settings
2222
/money-platform/target
2323
/target
24+
/doc

src/main/java/javax/money/MonetaryContext.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,6 @@ public static enum AmountFlavor {
8585
/** The generic amount bahavioral characteristics. */
8686
private final AmountFlavor flavor;
8787

88-
/**
89-
* This map contains arbitrary attributes, identified by class. This allows
90-
* to store additional context data in a platform independent way, e.g. when
91-
* using {@link java.math.BigDecimal} as a representation type on SE, the
92-
* {@link java.math.RoundingMode} used can be stored as an attribute. Adding
93-
* it as part of the API would break compatibility with SE.
94-
*/
95-
@SuppressWarnings("rawtypes")
96-
private final Map<Class, Object> attributes = new HashMap<>();
97-
9888
/**
9989
* Flag, if the scale is fixed. Fixed scaled numbers will always have a
10090
* scale of {@link #maxScale}.
@@ -112,18 +102,8 @@ public static enum AmountFlavor {
112102
* Constructs a new {@code MonetaryContext} with the specified precision and
113103
* rounding mode.
114104
*
115-
* @param amountType
116-
* The {@link MonetaryAmount} implementation class.
117-
* @param precision
118-
* The non-negative {@code int} precision setting.
119-
* @param maxScale
120-
* the maximal scale.
121-
* @param fixedScale
122-
* Flag for determining a fixed scale context.
123-
* @param flavor
124-
* the {@link AmountFlavor} set.
125-
* @param attributes
126-
* Any additional attributes.
105+
* @param builder
106+
* The {@link Builder} with data to be used.
127107
* @throws IllegalArgumentException
128108
* if the {@code setPrecision} parameter is less than zero.
129109
*/
@@ -207,6 +187,8 @@ public int hashCode() {
207187
result = prime * result + maxScale;
208188
result = prime * result
209189
+ ((amountType == null) ? 0 : amountType.hashCode());
190+
result = prime * result
191+
+ ((flavor == null) ? 0 : flavor.hashCode());
210192
result = prime * result + precision;
211193
return result;
212194
}
@@ -239,6 +221,11 @@ public boolean equals(Object obj) {
239221
return false;
240222
} else if (!amountType.equals(other.amountType))
241223
return false;
224+
if (flavor == null) {
225+
if (other.flavor != null)
226+
return false;
227+
} else if (!flavor.equals(other.flavor))
228+
return false;
242229
if (precision != other.precision)
243230
return false;
244231
return true;
@@ -351,6 +338,7 @@ public Builder(MonetaryContext context) {
351338
this.maxScale = context.getMaxScale();
352339
this.fixedScale = context.isFixedScale();
353340
this.precision = context.getPrecision();
341+
this.amountFlavor = context.getAmountFlavor();
354342
}
355343

356344
/**

src/main/java/javax/money/UnknownCurrencyException.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ public Locale getLocale() {
8181
*/
8282
@Override
8383
public String toString() {
84-
return "UnknownCurrencyException [currencyCode=" + currencyCode + "]";
84+
if(locale==null){
85+
return "UnknownCurrencyException [currencyCode=" + currencyCode + "]";
86+
}
87+
else{
88+
return "UnknownCurrencyException [locale=" + locale + "]";
89+
}
8590
}
8691

8792
}

src/main/java/javax/money/spi/Bootstrap.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
*/
99
package javax.money.spi;
1010

11-
import java.util.ArrayList;
12-
import java.util.Collection;
13-
import java.util.Collections;
14-
import java.util.Comparator;
15-
import java.util.List;
16-
import java.util.ServiceLoader;
11+
import java.util.*;
1712
import java.util.logging.Logger;
1813

1914
import javax.money.MonetaryException;
@@ -61,8 +56,9 @@ private static ServiceProvider loadDefaultServiceProvider() {
6156
* the new {@link ServiceProvider}
6257
*/
6358
public static void init(ServiceProvider serviceProvider) {
64-
synchronized (LOCK) {
65-
if (serviceProvider != null) {
59+
Objects.requireNonNull(serviceProvider);
60+
synchronized (LOCK) {
61+
if (Bootstrap.serviceProviderDelegate == null) {
6662
Bootstrap.serviceProviderDelegate = serviceProvider;
6763
Logger.getLogger(Bootstrap.class.getName()).info(
6864
"Money Bootstrap: new ServiceProvider set: "

src/main/java/javax/money/spi/JavaMoneyConfig.java renamed to src/main/java/javax/money/spi/MonetaryConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919
import java.util.logging.Logger;
2020

2121
/**
22-
* Loader for the Java Money configuration.
22+
* Loader for the Java Money JSR configuration.
2323
*
2424
* @author Anatole Tresch
2525
*/
26-
public final class JavaMoneyConfig {
26+
public final class MonetaryConfig{
2727

2828
private static final Logger LOG = Logger
29-
.getLogger(JavaMoneyConfig.class.getName());
29+
.getLogger(MonetaryConfig.class.getName());
3030

31-
private static final JavaMoneyConfig INSTANCE = new JavaMoneyConfig();
31+
private static final MonetaryConfig INSTANCE = new MonetaryConfig();
3232

3333
private Map<String, String> config = new HashMap<>();
3434
private Map<String, Integer> priorities = new HashMap<>();
3535

36-
private JavaMoneyConfig() {
36+
private MonetaryConfig() {
3737
try {
3838
Enumeration<URL> urls = getClass().getClassLoader().getResources(
3939
"javamoney.properties");
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
3+
* CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
4+
* PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
5+
* DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
6+
* AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
7+
* BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency
8+
* API ("Specification") Copyright (c) 2012-2013, Credit Suisse All rights
9+
* reserved.
10+
*/
11+
package javax.money;
12+
13+
import org.junit.Test;
14+
15+
import java.util.ArrayList;
16+
import java.util.HashSet;
17+
import java.util.List;
18+
import java.util.Set;
19+
20+
import static org.junit.Assert.*;
21+
22+
/**
23+
* Created by Anatole on 05.03.14.
24+
*/
25+
public class MonetaryContextTest{
26+
@Test
27+
public void testGetPrecision() throws Exception{
28+
MonetaryContext ctx = new MonetaryContext.Builder().setPrecision(299).create();
29+
assertTrue(ctx.getPrecision() == 299);
30+
}
31+
32+
@Test
33+
public void testIsFixedScale() throws Exception{
34+
MonetaryContext ctx = new MonetaryContext.Builder().setFixedScale(true).create();
35+
assertTrue(ctx.isFixedScale());
36+
}
37+
38+
@Test
39+
public void testGetMaxScale() throws Exception{
40+
MonetaryContext ctx = new MonetaryContext.Builder().setMaxScale(122).create();
41+
assertTrue(ctx.getMaxScale() == 122);
42+
}
43+
44+
@Test
45+
public void testGetAmountType() throws Exception{
46+
MonetaryContext ctx = new MonetaryContext.Builder().setMaxScale(122).create();
47+
assertEquals(ctx.getAmountType(), MonetaryAmount.class);
48+
abstract class TestAmount implements MonetaryAmount{}
49+
ctx = new MonetaryContext.Builder().setAmountType(TestAmount.class).create();
50+
assertEquals(ctx.getAmountType(), TestAmount.class);
51+
}
52+
53+
@Test
54+
public void testGetAmountFlavor() throws Exception{
55+
MonetaryContext ctx =
56+
new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE).create();
57+
assertEquals(MonetaryContext.AmountFlavor.PERFORMANCE, ctx.getAmountFlavor());
58+
ctx = new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PRECISION).create();
59+
assertEquals(MonetaryContext.AmountFlavor.PRECISION, ctx.getAmountFlavor());
60+
ctx = new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.UNDEFINED).create();
61+
assertEquals(MonetaryContext.AmountFlavor.UNDEFINED, ctx.getAmountFlavor());
62+
ctx = new MonetaryContext.Builder().create();
63+
assertEquals(MonetaryContext.AmountFlavor.UNDEFINED, ctx.getAmountFlavor());
64+
}
65+
66+
@Test
67+
public void testHashCode() throws Exception{
68+
List<MonetaryContext> contexts = new ArrayList<>();
69+
contexts.add(new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE).create());
70+
contexts.add(new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PRECISION).create());
71+
contexts.add(new MonetaryContext.Builder().setMaxScale(122).create());
72+
contexts.add(new MonetaryContext.Builder().setPrecision(299).create());
73+
contexts.add(new MonetaryContext.Builder().setFixedScale(true).create());
74+
Set<Integer> hashCodes = new HashSet<>();
75+
for(MonetaryContext ctx : contexts){
76+
hashCodes.add(ctx.hashCode());
77+
}
78+
// Check we have 5 distinct hash codes...
79+
assertTrue(hashCodes.size() == 5);
80+
}
81+
82+
@Test
83+
public void testEquals() throws Exception{
84+
List<MonetaryContext> contexts = new ArrayList<>();
85+
contexts.add(new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE).create());
86+
contexts.add(new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PRECISION).create());
87+
contexts.add(new MonetaryContext.Builder().setMaxScale(122).create());
88+
contexts.add(new MonetaryContext.Builder().setPrecision(299).create());
89+
contexts.add(new MonetaryContext.Builder().setFixedScale(true).create());
90+
Set<MonetaryContext> checkContexts = new HashSet<>();
91+
for(MonetaryContext ctx : contexts){
92+
checkContexts.add(ctx);
93+
checkContexts.add(ctx);
94+
}
95+
// Check we have 5 distinct hash codes...
96+
assertTrue(checkContexts.size() == 5);
97+
}
98+
99+
@Test
100+
public void testFrom() throws Exception{
101+
MonetaryContext rootCtx =
102+
new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE).create();
103+
MonetaryContext ctx = MonetaryContext.from(rootCtx, rootCtx.getAmountType());
104+
assertEquals(ctx, rootCtx);
105+
abstract class TestAmount implements MonetaryAmount{}
106+
ctx = MonetaryContext.from(rootCtx, TestAmount.class);
107+
assertFalse(ctx.equals(rootCtx));
108+
}
109+
110+
@Test
111+
public void testToString() throws Exception{
112+
abstract class TestAmount implements MonetaryAmount{}
113+
MonetaryContext ctx = new MonetaryContext.Builder().setFlavor(MonetaryContext.AmountFlavor.PERFORMANCE)
114+
.setAmountType(TestAmount.class).setFixedScale(true).setMaxScale(111).setPrecision(200)
115+
.set("myValue", "myKey").set("TEST").create();
116+
assertNotNull(ctx.toString());
117+
System.out.println(ctx.toString());
118+
assertTrue(ctx.toString().contains("PERFORMANCE"));
119+
assertTrue(ctx.toString().contains("flavor"));
120+
assertTrue(ctx.toString().contains("111"));
121+
assertTrue(ctx.toString().contains("200"));
122+
assertTrue(ctx.toString().contains("TEST"));
123+
assertTrue(ctx.toString().contains("myKey"));
124+
assertTrue(ctx.toString().contains("myValue"));
125+
assertTrue(ctx.toString().contains("String"));
126+
assertTrue(ctx.toString().contains("maxScale"));
127+
assertTrue(ctx.toString().contains("precision"));
128+
assertTrue(ctx.toString().contains("fixedScale"));
129+
assertTrue(ctx.toString().contains("TestAmount"));
130+
assertTrue(ctx.toString().contains("amountType"));
131+
assertTrue(ctx.toString().contains("MonetaryContext"));
132+
}
133+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
3+
* CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
4+
* PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
5+
* DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
6+
* AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
7+
* BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency
8+
* API ("Specification") Copyright (c) 2012-2013, Credit Suisse All rights
9+
* reserved.
10+
*/
11+
package javax.money;
12+
13+
import org.junit.Test;
14+
15+
import static org.junit.Assert.assertNotNull;
16+
import static org.junit.Assert.assertNull;
17+
import static org.junit.Assert.assertTrue;
18+
19+
/**
20+
* Created by Anatole on 05.03.14.
21+
*/
22+
public class MonetaryExceptionTest{
23+
@Test
24+
public void testGetMessage() throws Exception{
25+
MonetaryException e = new MonetaryException("message1");
26+
assertNotNull(e.getMessage());
27+
assertTrue(e.getMessage().contains("message1"));
28+
assertNull(e.getCause());
29+
}
30+
31+
@Test
32+
public void testGetCause() throws Exception{
33+
MonetaryException e = new MonetaryException("message1", new Exception("Test"));
34+
assertNotNull(e.getMessage());
35+
assertTrue(e.getMessage().contains("message1"));
36+
assertNotNull(e.getCause());
37+
assertTrue(e.getCause().getMessage().equals("Test"));
38+
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
3+
* CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
4+
* PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
5+
* DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
6+
* AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
7+
* BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency
8+
* API ("Specification") Copyright (c) 2012-2013, Credit Suisse All rights
9+
* reserved.
10+
*/
11+
package javax.money;
12+
13+
import org.junit.Test;
14+
15+
import java.util.Locale;
16+
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertNull;
19+
import static org.junit.Assert.assertTrue;
20+
21+
/**
22+
* Created by Anatole on 05.03.14.
23+
*/
24+
public class UnknownCurrencyExceptionTest{
25+
@Test
26+
public void testGetCurrencyCode() throws Exception{
27+
UnknownCurrencyException e = new UnknownCurrencyException("GGG");
28+
assertEquals("GGG", e.getCurrencyCode());
29+
assertNull(e.getLocale());
30+
assertTrue(e.toString().contains("GGG"));
31+
assertTrue(e.toString().contains("UnknownCurrencyException"));
32+
}
33+
34+
@Test
35+
public void testGetLocale() throws Exception{
36+
UnknownCurrencyException e = new UnknownCurrencyException(Locale.CANADA_FRENCH);
37+
assertEquals(Locale.CANADA_FRENCH, e.getLocale());
38+
assertNull(e.getCurrencyCode());
39+
System.out.println(e);
40+
assertTrue(e.toString().contains(Locale.CANADA_FRENCH.toString()));
41+
assertTrue(e.toString().contains("UnknownCurrencyException"));
42+
}
43+
44+
}

0 commit comments

Comments
 (0)