Skip to content

Commit 194b172

Browse files
committed
Added 2 tests.
Added testable flags to thread-safety assertions.
1 parent 4fe104b commit 194b172

File tree

3 files changed

+113
-119
lines changed

3 files changed

+113
-119
lines changed

src/test/java/org/javamoney/tck/tests/CurrencyUnitTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,6 @@ public void testIsImmutable() {
143143
}
144144
}
145145

146-
@SpecAssertion(section = "4.2.1", id = "421-B5")
147-
@Test
148-
public void testIsThreadSafe() throws NoSuchMethodException,
149-
SecurityException, IllegalAccessException,
150-
IllegalArgumentException, InvocationTargetException {
151-
for (Currency cur : Currency.getAvailableCurrencies()) {
152-
CurrencyUnit unit = MonetaryCurrencies.getCurrency(cur
153-
.getCurrencyCode());
154-
fail("Not yet implemented: IsThreadSafe");
155-
}
156-
}
157146

158147
@SpecAssertion(section = "4.2.1", id = "421-B6")
159148
@Test
Lines changed: 109 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,128 @@
11
package org.javamoney.tck.tests;
22

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-
143
import org.javamoney.tck.ClassTester;
15-
import org.javamoney.tck.TCKTestSetup;
164
import org.jboss.test.audit.annotations.SpecAssertion;
175
import org.jboss.test.audit.annotations.SpecVersion;
186
import org.junit.Ignore;
197
import org.junit.Test;
208

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+
2117
@SpecVersion(spec = "JSR 354", version = "1.0.0")
22-
public class MonetaryAmountTest {
18+
public class MonetaryAmountTest{
2319

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+
}
3026

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+
}
4641

4742

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+
}
5368

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+
}
6687

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+
}
8098

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+
}
94109

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+
}
122127

123128
}

test-audit.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
<text>CurrencyUnit implementation must be immutable.
7979
</text>
8080
</assertion>
81-
<assertion id="421-B5">
81+
<assertion id="421-B5" testable="false">
8282
<text>CurrencyUnit implementation must be thread safe.
8383
</text>
8484
</assertion>
@@ -412,7 +412,7 @@
412412
<text>Implementations of MonetaryAmount should be immutable.
413413
</text>
414414
</assertion>
415-
<assertion id="422-G2">
415+
<assertion id="422-G2" testable="false">
416416
<text>Implementations of MonetaryAmount should be thread safe.
417417
</text>
418418
</assertion>
@@ -627,13 +627,13 @@
627627
<assertion id="4242-B1">
628628
<text>Monetary operators are recommended to be immutable</text>
629629
</assertion>
630-
<assertion id="4242-B2">
630+
<assertion id="4242-B2" testable="false">
631631
<text>Monetary operators are recommended to be thread-safe</text>
632632
</assertion>
633633
<assertion id="4242-B3">
634634
<text>Monetary queries are recommended to be immutable</text>
635635
</assertion>
636-
<assertion id="4242-B4">
636+
<assertion id="4242-B4" testable="false">
637637
<text>Monetary queries are recommended to be thread-safe</text>
638638
</assertion>
639639
</group>

0 commit comments

Comments
 (0)