aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/core/src/test/java/org/spongycastle/asn1/test/MonetaryValueUnitTest.java
blob: 541dc49786ccf439c83d694a37c8edf8d467f46c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package org.spongycastle.asn1.test;

import org.spongycastle.asn1.ASN1InputStream;
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.x509.qualified.Iso4217CurrencyCode;
import org.spongycastle.asn1.x509.qualified.MonetaryValue;
import org.spongycastle.util.test.SimpleTest;

public class MonetaryValueUnitTest 
    extends SimpleTest
{
    private static final int TEST_AMOUNT = 100;
    private static final int ZERO_EXPONENT = 0;
    
    private static final String CURRENCY_CODE = "AUD";

    public String getName()
    {
        return "MonetaryValue";
    }

    public void performTest() 
        throws Exception
    {
        MonetaryValue mv = new MonetaryValue(new Iso4217CurrencyCode(CURRENCY_CODE), TEST_AMOUNT, ZERO_EXPONENT);

        checkValues(mv, TEST_AMOUNT, ZERO_EXPONENT);
        
        mv = MonetaryValue.getInstance(mv);
        
        checkValues(mv, TEST_AMOUNT, ZERO_EXPONENT);
        
        ASN1InputStream aIn = new ASN1InputStream(mv.toASN1Object().getEncoded());

        ASN1Sequence seq = (ASN1Sequence)aIn.readObject();
        
        mv = MonetaryValue.getInstance(seq);
        
        checkValues(mv, TEST_AMOUNT, ZERO_EXPONENT);
        
        mv = MonetaryValue.getInstance(null);
        
        if (mv != null)
        {
            fail("null getInstance() failed.");
        }
        
        try
        {
            MonetaryValue.getInstance(new Object());
            
            fail("getInstance() failed to detect bad object.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
    }

    private void checkValues(
        MonetaryValue mv,
        int           amount,
        int           exponent)
    {
        if (mv.getAmount().intValue() != amount)
        {
            fail("amounts don't match.");
        }
        
        if (mv.getExponent().intValue() != exponent)
        {
            fail("exponents don't match.");
        }
        
        Iso4217CurrencyCode cc = mv.getCurrency();
        
        if (!cc.getAlphabetic().equals(CURRENCY_CODE))
        {
            fail("currency code wrong");
        }
    }
    
    public static void main(
        String[]    args)
    {
        runTest(new MonetaryValueUnitTest());
    }
}