summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/asn1/test/MonetaryLimitUnitTest.java
blob: 8456661ab5a7ee4a3a40f67a33e664e31915ee6d (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
package org.bouncycastle.asn1.test;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.isismtt.x509.MonetaryLimit;

import java.io.IOException;

public class MonetaryLimitUnitTest
    extends ASN1UnitTest
{
    public String getName()
    {
        return "MonetaryLimit";
    }

    public void performTest()
        throws Exception
    {
        String currency = "AUD";
        int    amount = 1;
        int    exponent = 2;

        MonetaryLimit limit = new MonetaryLimit(currency, amount, exponent);

        checkConstruction(limit, currency, amount, exponent);

        limit = MonetaryLimit.getInstance(null);

        if (limit != null)
        {
            fail("null getInstance() failed.");
        }

        try
        {
            MonetaryLimit.getInstance(new Object());

            fail("getInstance() failed to detect bad object.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
    }

    private void checkConstruction(
        MonetaryLimit limit,
        String currency,
        int    amount,
        int    exponent)
        throws IOException
    {
        checkValues(limit, currency, amount, exponent);

        limit = MonetaryLimit.getInstance(limit);

        checkValues(limit, currency, amount, exponent);

        ASN1InputStream aIn = new ASN1InputStream(limit.toASN1Object().getEncoded());

        ASN1Sequence seq = (ASN1Sequence)aIn.readObject();

        limit = MonetaryLimit.getInstance(seq);

        checkValues(limit, currency, amount, exponent);
    }

    private void checkValues(
        MonetaryLimit limit,
        String currency,
        int    amount,
        int    exponent)
    {
        checkMandatoryField("currency", currency, limit.getCurrency());
        checkMandatoryField("amount", amount, limit.getAmount().intValue());
        checkMandatoryField("exponent", exponent, limit.getExponent().intValue());
    }

    public static void main(
        String[]    args)
    {
        runTest(new MonetaryLimitUnitTest());
    }
}