01: package org.bouncycastle.asn1.test;
02:
03: import org.bouncycastle.asn1.ASN1InputStream;
04: import org.bouncycastle.asn1.ASN1Sequence;
05: import org.bouncycastle.asn1.x509.qualified.Iso4217CurrencyCode;
06: import org.bouncycastle.asn1.x509.qualified.MonetaryValue;
07: import org.bouncycastle.util.test.SimpleTest;
08:
09: public class MonetaryValueUnitTest extends SimpleTest {
10: private static final int TEST_AMOUNT = 100;
11: private static final int ZERO_EXPONENT = 0;
12:
13: private static final String CURRENCY_CODE = "AUD";
14:
15: public String getName() {
16: return "MonetaryValue";
17: }
18:
19: public void performTest() throws Exception {
20: MonetaryValue mv = new MonetaryValue(new Iso4217CurrencyCode(
21: CURRENCY_CODE), TEST_AMOUNT, ZERO_EXPONENT);
22:
23: checkValues(mv, TEST_AMOUNT, ZERO_EXPONENT);
24:
25: mv = MonetaryValue.getInstance(mv);
26:
27: checkValues(mv, TEST_AMOUNT, ZERO_EXPONENT);
28:
29: ASN1InputStream aIn = new ASN1InputStream(mv.toASN1Object()
30: .getEncoded());
31:
32: ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
33:
34: mv = MonetaryValue.getInstance(seq);
35:
36: checkValues(mv, TEST_AMOUNT, ZERO_EXPONENT);
37:
38: mv = MonetaryValue.getInstance(null);
39:
40: if (mv != null) {
41: fail("null getInstance() failed.");
42: }
43:
44: try {
45: MonetaryValue.getInstance(new Object());
46:
47: fail("getInstance() failed to detect bad object.");
48: } catch (IllegalArgumentException e) {
49: // expected
50: }
51: }
52:
53: private void checkValues(MonetaryValue mv, int amount, int exponent) {
54: if (mv.getAmount().intValue() != amount) {
55: fail("amounts don't match.");
56: }
57:
58: if (mv.getExponent().intValue() != exponent) {
59: fail("exponents don't match.");
60: }
61:
62: Iso4217CurrencyCode cc = mv.getCurrency();
63:
64: if (!cc.getAlphabetic().equals(CURRENCY_CODE)) {
65: fail("currency code wrong");
66: }
67: }
68:
69: public static void main(String[] args) {
70: runTest(new MonetaryValueUnitTest());
71: }
72: }
|