Skip to content

Commit cb12d7b

Browse files
committed
Added Tests for MonetaryAmountFormat.
1 parent a7119ee commit cb12d7b

File tree

2 files changed

+356
-0
lines changed

2 files changed

+356
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.javamoney.tck.tests;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
6+
import java.util.Currency;
7+
import java.util.Locale;
8+
9+
import javax.money.MonetaryAmount;
10+
import javax.money.MonetaryAmounts;
11+
import javax.money.format.MonetaryAmountFormat;
12+
import javax.money.format.MonetaryFormats;
13+
14+
import org.jboss.test.audit.annotations.SpecAssertion;
15+
import org.jboss.test.audit.annotations.SpecVersion;
16+
import org.junit.Test;
17+
18+
@SpecVersion(spec = "JSR 354", version = "1.0.0")
19+
public class MonetaryAmountFormatTest {
20+
21+
@SpecAssertion(section = "4.4.1", id = "441-A1")
22+
@Test
23+
public void testNoDepOnAmountImplementation() {
24+
final Locale defaultLocale = Locale.getDefault();
25+
MonetaryAmountFormat amountFormat = MonetaryFormats
26+
.getAmountFormat(defaultLocale);
27+
final Number[] values = new Number[] { 100, 10000000000000L };// TODO
28+
// other
29+
// values
30+
// and
31+
// currencies
32+
final Currency currency = Currency.getAvailableCurrencies().iterator()
33+
.next();
34+
for (Number value : values) {
35+
MonetaryAmount amount = MonetaryAmounts.getAmountFactory()
36+
.setCurrency(currency.getCurrencyCode()).setNumber(value)
37+
.create();
38+
String formattedAmount = amountFormat.format(amount);
39+
MonetaryAmount amountMock = TestMonetaryAmountFactory.getAmount(
40+
value, currency);
41+
String formattedAmountMock = amountFormat.format(amountMock);
42+
assertNotNull(formattedAmountMock);
43+
assertEquals(formattedAmountMock, formattedAmount);
44+
}
45+
}
46+
}
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
package org.javamoney.tck.tests;
2+
3+
import java.math.BigDecimal;
4+
import java.math.BigInteger;
5+
import java.util.Currency;
6+
7+
import javax.money.CurrencyUnit;
8+
import javax.money.MonetaryAmount;
9+
import javax.money.MonetaryAmountFactory;
10+
import javax.money.MonetaryContext;
11+
import javax.money.MonetaryOperator;
12+
import javax.money.MonetaryQuery;
13+
import javax.money.NumberValue;
14+
15+
public class TestMonetaryAmountFactory {
16+
17+
private Number value;
18+
private Currency currency;
19+
20+
public static MonetaryAmount getAmount(final Number value,
21+
final Currency currency) {
22+
return new MonetaryAmount() {
23+
24+
@Override
25+
public MonetaryAmount with(MonetaryOperator operator) {
26+
return this;
27+
}
28+
29+
@Override
30+
public MonetaryAmount subtract(MonetaryAmount amount) {
31+
return this;
32+
}
33+
34+
@Override
35+
public MonetaryAmount stripTrailingZeros() {
36+
return this;
37+
}
38+
39+
@Override
40+
public int signum() {
41+
return 0;
42+
}
43+
44+
@Override
45+
public MonetaryAmount scaleByPowerOfTen(int power) {
46+
return this;
47+
}
48+
49+
@Override
50+
public MonetaryAmount remainder(Number divisor) {
51+
return this;
52+
}
53+
54+
@Override
55+
public MonetaryAmount remainder(double divisor) {
56+
return this;
57+
}
58+
59+
@Override
60+
public MonetaryAmount remainder(long divisor) {
61+
return this;
62+
}
63+
64+
@Override
65+
public <R> R query(MonetaryQuery<R> query) {
66+
return null;
67+
}
68+
69+
@Override
70+
public MonetaryAmount plus() {
71+
return this;
72+
}
73+
74+
@Override
75+
public MonetaryAmount negate() {
76+
return this;
77+
}
78+
79+
@Override
80+
public MonetaryAmount multiply(Number multiplicand) {
81+
return this;
82+
}
83+
84+
@Override
85+
public MonetaryAmount multiply(double multiplicand) {
86+
return this;
87+
}
88+
89+
@Override
90+
public MonetaryAmount multiply(long multiplicand) {
91+
return this;
92+
}
93+
94+
@Override
95+
public boolean isZero() {
96+
return false;
97+
}
98+
99+
@Override
100+
public boolean isPositiveOrZero() {
101+
return true;
102+
}
103+
104+
@Override
105+
public boolean isPositive() {
106+
return true;
107+
}
108+
109+
@Override
110+
public boolean isNegativeOrZero() {
111+
return false;
112+
}
113+
114+
@Override
115+
public boolean isNegative() {
116+
return false;
117+
}
118+
119+
@Override
120+
public boolean isLessThanOrEqualTo(MonetaryAmount amt) {
121+
return false;
122+
}
123+
124+
@Override
125+
public boolean isLessThan(MonetaryAmount amount) {
126+
return false;
127+
}
128+
129+
@Override
130+
public boolean isGreaterThanOrEqualTo(MonetaryAmount amount) {
131+
return false;
132+
}
133+
134+
@Override
135+
public boolean isGreaterThan(MonetaryAmount amount) {
136+
return false;
137+
}
138+
139+
@Override
140+
public boolean isEqualTo(MonetaryAmount amount) {
141+
return false;
142+
}
143+
144+
@Override
145+
public NumberValue getNumber() {
146+
return new NumberValue() {
147+
148+
private static final long serialVersionUID = 1L;
149+
150+
@Override
151+
public long longValue() {
152+
return value.longValue();
153+
}
154+
155+
@Override
156+
public int intValue() {
157+
return value.intValue();
158+
}
159+
160+
@Override
161+
public float floatValue() {
162+
return value.floatValue();
163+
}
164+
165+
@Override
166+
public double doubleValue() {
167+
return value.doubleValue();
168+
}
169+
170+
@Override
171+
public <T extends Number> T numberValueExact(
172+
Class<T> numberType) {
173+
return null;
174+
}
175+
176+
@Override
177+
public <T extends Number> T numberValue(Class<T> numberType) {
178+
if (numberType.equals(Integer.class)) {
179+
return (T) Integer.valueOf(value.intValue());
180+
}
181+
if (numberType.equals(BigInteger.class)) {
182+
return (T) BigInteger.valueOf(value.intValue());
183+
}
184+
if (numberType.equals(BigDecimal.class)) {
185+
return (T) BigDecimal.valueOf(value.doubleValue());
186+
}
187+
throw new UnsupportedOperationException(
188+
numberType.getCanonicalName());
189+
}
190+
191+
@Override
192+
public long longValueExact() {
193+
return value.longValue();
194+
}
195+
196+
@Override
197+
public int intValueExact() {
198+
return value.intValue();
199+
}
200+
201+
@Override
202+
public int getScale() {
203+
return 0;
204+
}
205+
206+
@Override
207+
public int getPrecision() {
208+
return 0;
209+
}
210+
211+
@Override
212+
public Class<?> getNumberType() {
213+
return null;
214+
}
215+
216+
@Override
217+
public double doubleValueExact() {
218+
return value.doubleValue();
219+
}
220+
};
221+
}
222+
223+
@Override
224+
public MonetaryContext getMonetaryContext() {
225+
return null;
226+
}
227+
228+
@Override
229+
public MonetaryAmountFactory<? extends MonetaryAmount> getFactory() {
230+
return null;
231+
}
232+
233+
@Override
234+
public CurrencyUnit getCurrency() {
235+
return new CurrencyUnit() {
236+
237+
@Override
238+
public String getCurrencyCode() {
239+
return currency.getCurrencyCode();
240+
}
241+
242+
@Override
243+
public int getNumericCode() {
244+
return currency.getNumericCode();
245+
}
246+
247+
@Override
248+
public int getDefaultFractionDigits() {
249+
return currency.getDefaultFractionDigits();
250+
}
251+
};
252+
}
253+
254+
@Override
255+
public MonetaryAmount divideToIntegralValue(Number divisor) {
256+
return this;
257+
}
258+
259+
@Override
260+
public MonetaryAmount divideToIntegralValue(double divisor) {
261+
return this;
262+
}
263+
264+
@Override
265+
public MonetaryAmount divideToIntegralValue(long divisor) {
266+
return this;
267+
}
268+
269+
@Override
270+
public MonetaryAmount[] divideAndRemainder(Number divisor) {
271+
return new MonetaryAmount[0];
272+
}
273+
274+
@Override
275+
public MonetaryAmount[] divideAndRemainder(double divisor) {
276+
return new MonetaryAmount[0];
277+
}
278+
279+
@Override
280+
public MonetaryAmount[] divideAndRemainder(long divisor) {
281+
return new MonetaryAmount[0];
282+
}
283+
284+
@Override
285+
public MonetaryAmount divide(Number divisor) {
286+
return this;
287+
}
288+
289+
@Override
290+
public MonetaryAmount divide(double divisor) {
291+
return this;
292+
}
293+
294+
@Override
295+
public MonetaryAmount divide(long divisor) {
296+
return this;
297+
}
298+
299+
@Override
300+
public MonetaryAmount add(MonetaryAmount amount) {
301+
return this;
302+
}
303+
304+
@Override
305+
public MonetaryAmount abs() {
306+
return this;
307+
}
308+
};
309+
}
310+
}

0 commit comments

Comments
 (0)