|
1 | 1 | package org.javamoney.tck.tests; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertEquals; |
4 | | -import static org.junit.Assert.assertNotNull; |
5 | | -import static org.junit.Assert.assertTrue; |
6 | | -import static org.junit.Assert.fail; |
7 | | - |
8 | | -import java.util.Currency; |
9 | | - |
10 | | -import javax.money.MonetaryAmount; |
11 | | -import javax.money.MonetaryAmounts; |
12 | | -import javax.money.MonetaryOperator; |
13 | | - |
14 | 3 | import org.javamoney.tck.ClassTester; |
15 | | -import org.javamoney.tck.TCKTestSetup; |
16 | 4 | import org.jboss.test.audit.annotations.SpecAssertion; |
17 | 5 | import org.jboss.test.audit.annotations.SpecVersion; |
18 | 6 | import org.junit.Ignore; |
19 | 7 | import org.junit.Test; |
20 | 8 |
|
| 9 | +import javax.money.MonetaryAmount; |
| 10 | +import javax.money.MonetaryAmounts; |
| 11 | +import javax.money.MonetaryOperator; |
| 12 | +import javax.money.MonetaryQuery; |
| 13 | +import java.util.Currency; |
| 14 | + |
| 15 | +import static org.junit.Assert.*; |
| 16 | + |
21 | 17 | @SpecVersion(spec = "JSR 354", version = "1.0.0") |
22 | | -public class MonetaryAmountTest { |
| 18 | +public class MonetaryAmountTest{ |
23 | 19 |
|
24 | | - @SpecAssertion(section = "4.2.2", id = "422-0") |
25 | | - @Test |
26 | | - public void testEnsureMonetaryAmount() { |
27 | | - assertNotNull(MonetaryAmounts.getAmountTypes()); |
28 | | - assertTrue(MonetaryAmounts.getAmountTypes().size() > 0); |
29 | | - } |
| 20 | + @SpecAssertion(section = "4.2.2", id = "422-0") |
| 21 | + @Test |
| 22 | + public void testEnsureMonetaryAmount(){ |
| 23 | + assertNotNull(MonetaryAmounts.getAmountTypes()); |
| 24 | + assertTrue(MonetaryAmounts.getAmountTypes().size() > 0); |
| 25 | + } |
30 | 26 |
|
31 | | - @SpecAssertion(section = "4.2.2", id = "422-A1") |
32 | | - @Test |
33 | | - public void testCurrencyCode() { |
34 | | - for (Class type : MonetaryAmounts.getAmountTypes()) { |
35 | | - for (Currency jdkCur : Currency.getAvailableCurrencies()) { |
36 | | - MonetaryAmount amount = MonetaryAmounts.getAmountFactory() |
37 | | - .setCurrency(jdkCur.getCurrencyCode()).setNumber(10.15) |
38 | | - .create(); |
39 | | - assertNotNull(amount); |
40 | | - assertNotNull(amount.getCurrency()); |
41 | | - assertEquals(jdkCur.getCurrencyCode(), amount.getCurrency() |
42 | | - .getCurrencyCode()); |
43 | | - } |
44 | | - } |
45 | | - } |
| 27 | + @SpecAssertion(section = "4.2.2", id = "422-A1") |
| 28 | + @Test |
| 29 | + public void testCurrencyCode(){ |
| 30 | + for(Class type : MonetaryAmounts.getAmountTypes()){ |
| 31 | + for(Currency jdkCur : Currency.getAvailableCurrencies()){ |
| 32 | + MonetaryAmount amount = |
| 33 | + MonetaryAmounts.getAmountFactory().setCurrency(jdkCur.getCurrencyCode()).setNumber(10.15) |
| 34 | + .create(); |
| 35 | + assertNotNull(amount); |
| 36 | + assertNotNull(amount.getCurrency()); |
| 37 | + assertEquals(jdkCur.getCurrencyCode(), amount.getCurrency().getCurrencyCode()); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
46 | 41 |
|
47 | 42 |
|
48 | | - @SpecAssertion(section = "4.2.2", id = "422-E1") |
49 | | - @Test |
50 | | - @Ignore |
51 | | - public void testWith() { |
52 | | - } |
| 43 | + @SpecAssertion(section = "4.2.2", id = "422-E1") |
| 44 | + @Test |
| 45 | + public void testWith(){ |
| 46 | + MonetaryOperator op = new MonetaryOperator(){ |
| 47 | + @Override |
| 48 | + public <T extends MonetaryAmount> T apply(T value){ |
| 49 | + return value; |
| 50 | + } |
| 51 | + }; |
| 52 | + for(Class type : MonetaryAmounts.getAmountTypes()){ |
| 53 | + MonetaryAmount amount = MonetaryAmounts.getAmountFactory(type).setCurrency("CHF").setNumber(10).create(); |
| 54 | + MonetaryAmount amount2 = amount.with(op); |
| 55 | + assertTrue(amount == amount2); |
| 56 | + final MonetaryAmount result = |
| 57 | + MonetaryAmounts.getAmountFactory(type).setCurrency("CHF").setNumber(4).create(); |
| 58 | + MonetaryOperator op2 = new MonetaryOperator(){ |
| 59 | + @Override |
| 60 | + public <T extends MonetaryAmount> T apply(T value){ |
| 61 | + return (T) result; |
| 62 | + } |
| 63 | + }; |
| 64 | + amount2 = amount.with(op); |
| 65 | + assertTrue(amount == amount2); |
| 66 | + } |
| 67 | + } |
53 | 68 |
|
54 | | - @SpecAssertion(section = "4.2.2", id = "422-E3") |
55 | | - @Test |
56 | | - @Ignore |
57 | | - public void testQuery() { |
58 | | - for (Class type : MonetaryAmounts.getAmountTypes()) { |
59 | | - MonetaryAmount amount = MonetaryAmounts.getAmountFactory() |
60 | | - .setCurrency("XXX").setNumber(0).create(); |
61 | | - // amount.query(); |
62 | | - fail("not implemented."); |
63 | | - // TODO |
64 | | - } |
65 | | - } |
| 69 | + @SpecAssertion(section = "4.2.2", id = "422-E3") |
| 70 | + @Test |
| 71 | + public void testQuery(){ |
| 72 | + MonetaryQuery<Integer> query = new MonetaryQuery<Integer>(){ |
| 73 | + @Override |
| 74 | + public Integer queryFrom(MonetaryAmount amount){ |
| 75 | + return amount.getNumber().intValue(); |
| 76 | + } |
| 77 | + }; |
| 78 | + for(Class type : MonetaryAmounts.getAmountTypes()){ |
| 79 | + MonetaryAmount amount = MonetaryAmounts.getAmountFactory(type).setCurrency("CHF").setNumber(10).create(); |
| 80 | + Integer value = amount.query(query); |
| 81 | + assertTrue(value == 10); |
| 82 | + amount = MonetaryAmounts.getAmountFactory(type).setCurrency("CHF").setNumber(4.5).create(); |
| 83 | + value = amount.query(query); |
| 84 | + assertTrue(value == 4); |
| 85 | + } |
| 86 | + } |
66 | 87 |
|
67 | | - @SpecAssertion(section = "4.2.2", id = "422-F2") |
68 | | - @Test |
69 | | - public void testImplementsEquals() { |
70 | | - for (Class type : MonetaryAmounts.getAmountTypes()) { |
71 | | - MonetaryAmount amount = MonetaryAmounts.getAmountFactory() |
72 | | - .setCurrency("XXX").setNumber(0).create(); |
73 | | - ClassTester.testHasPublicStaticMethodOpt(type, type, "equals", |
74 | | - MonetaryOperator.class); |
75 | | - MonetaryAmount amount2 = MonetaryAmounts.getAmountFactory() |
76 | | - .setCurrency("XXX").setNumber(0).create(); |
77 | | - assertEquals(amount, amount2); |
78 | | - } |
79 | | - } |
| 88 | + @SpecAssertion(section = "4.2.2", id = "422-F2") |
| 89 | + @Test |
| 90 | + public void testImplementsEquals(){ |
| 91 | + for(Class type : MonetaryAmounts.getAmountTypes()){ |
| 92 | + MonetaryAmount amount = MonetaryAmounts.getAmountFactory().setCurrency("XXX").setNumber(0).create(); |
| 93 | + ClassTester.testHasPublicStaticMethodOpt(type, type, "equals", MonetaryOperator.class); |
| 94 | + MonetaryAmount amount2 = MonetaryAmounts.getAmountFactory().setCurrency("XXX").setNumber(0).create(); |
| 95 | + assertEquals(amount, amount2); |
| 96 | + } |
| 97 | + } |
80 | 98 |
|
81 | | - @SpecAssertion(section = "4.2.2", id = "422-F1") |
82 | | - @Test |
83 | | - public void testImplementsHashCode() { |
84 | | - for (Class type : MonetaryAmounts.getAmountTypes()) { |
85 | | - MonetaryAmount amount = MonetaryAmounts.getAmountFactory() |
86 | | - .setCurrency("USD").setNumber(0).create(); |
87 | | - ClassTester.testHasPublicStaticMethodOpt(type, type, "hashCode", |
88 | | - MonetaryOperator.class); |
89 | | - MonetaryAmount amount2 = MonetaryAmounts.getAmountFactory() |
90 | | - .setCurrency("USD").setNumber(0).create(); |
91 | | - assertEquals(amount.hashCode(), amount2.hashCode()); |
92 | | - } |
93 | | - } |
| 99 | + @SpecAssertion(section = "4.2.2", id = "422-F1") |
| 100 | + @Test |
| 101 | + public void testImplementsHashCode(){ |
| 102 | + for(Class type : MonetaryAmounts.getAmountTypes()){ |
| 103 | + MonetaryAmount amount = MonetaryAmounts.getAmountFactory().setCurrency("USD").setNumber(0).create(); |
| 104 | + ClassTester.testHasPublicStaticMethodOpt(type, type, "hashCode", MonetaryOperator.class); |
| 105 | + MonetaryAmount amount2 = MonetaryAmounts.getAmountFactory().setCurrency("USD").setNumber(0).create(); |
| 106 | + assertEquals(amount.hashCode(), amount2.hashCode()); |
| 107 | + } |
| 108 | + } |
94 | 109 |
|
95 | | - @SpecAssertion(section = "4.2.2", id = "422-F3") |
96 | | - @Test |
97 | | - public void testImplementComparable() { |
98 | | - for (Class type : MonetaryAmounts.getAmountTypes()) { |
99 | | - ClassTester.testComparable(type); |
100 | | - MonetaryAmount amount = MonetaryAmounts.getAmountFactory() |
101 | | - .setCurrency("XXX").setNumber(0).create(); |
102 | | - ClassTester.testHasPublicStaticMethodOpt(type, type, "hashCode", |
103 | | - MonetaryOperator.class); |
104 | | - MonetaryAmount amount2 = MonetaryAmounts.getAmountFactory() |
105 | | - .setCurrency("XXX").setNumber(0).create(); |
106 | | - assertTrue("Comparable failed for: " + type.getName(), |
107 | | - ((Comparable) amount).compareTo(amount2) == 0); |
108 | | - MonetaryAmount amount3 = MonetaryAmounts.getAmountFactory() |
109 | | - .setCurrency("CHF").setNumber(1).create(); |
110 | | - assertTrue("Comparable failed for: " + type.getName(), |
111 | | - ((Comparable) amount).compareTo(amount3) > 0); |
112 | | - assertTrue("Comparable failed for: " + type.getName(), |
113 | | - ((Comparable) amount3).compareTo(amount) < 0); |
114 | | - MonetaryAmount amount4 = MonetaryAmounts.getAmountFactory() |
115 | | - .setCurrency("XXX").setNumber(1).create(); |
116 | | - assertTrue("Comparable failed for: " + type.getName(), |
117 | | - ((Comparable) amount3).compareTo(amount4) < 0); |
118 | | - assertTrue("Comparable failed for: " + type.getName(), |
119 | | - ((Comparable) amount4).compareTo(amount3) > 0); |
120 | | - } |
121 | | - } |
| 110 | + @SpecAssertion(section = "4.2.2", id = "422-F3") |
| 111 | + @Test |
| 112 | + public void testImplementComparable(){ |
| 113 | + for(Class type : MonetaryAmounts.getAmountTypes()){ |
| 114 | + ClassTester.testComparable(type); |
| 115 | + MonetaryAmount amount = MonetaryAmounts.getAmountFactory().setCurrency("XXX").setNumber(0).create(); |
| 116 | + ClassTester.testHasPublicStaticMethodOpt(type, type, "hashCode", MonetaryOperator.class); |
| 117 | + MonetaryAmount amount2 = MonetaryAmounts.getAmountFactory().setCurrency("XXX").setNumber(0).create(); |
| 118 | + assertTrue("Comparable failed for: " + type.getName(), ((Comparable) amount).compareTo(amount2) == 0); |
| 119 | + MonetaryAmount amount3 = MonetaryAmounts.getAmountFactory().setCurrency("CHF").setNumber(1).create(); |
| 120 | + assertTrue("Comparable failed for: " + type.getName(), ((Comparable) amount).compareTo(amount3) > 0); |
| 121 | + assertTrue("Comparable failed for: " + type.getName(), ((Comparable) amount3).compareTo(amount) < 0); |
| 122 | + MonetaryAmount amount4 = MonetaryAmounts.getAmountFactory().setCurrency("XXX").setNumber(1).create(); |
| 123 | + assertTrue("Comparable failed for: " + type.getName(), ((Comparable) amount3).compareTo(amount4) < 0); |
| 124 | + assertTrue("Comparable failed for: " + type.getName(), ((Comparable) amount4).compareTo(amount3) > 0); |
| 125 | + } |
| 126 | + } |
122 | 127 |
|
123 | 128 | } |
0 commit comments