001: package org.bouncycastle.asn1.test;
002:
003: import org.bouncycastle.asn1.DERObject;
004: import org.bouncycastle.asn1.x509.qualified.Iso4217CurrencyCode;
005: import org.bouncycastle.util.test.SimpleTest;
006:
007: public class Iso4217CurrencyCodeUnitTest extends SimpleTest {
008: private static final String ALPHABETIC_CURRENCY_CODE = "AUD";
009: private static final int NUMERIC_CURRENCY_CODE = 1;
010:
011: public String getName() {
012: return "Iso4217CurrencyCode";
013: }
014:
015: public void performTest() throws Exception {
016: //
017: // alphabetic
018: //
019: Iso4217CurrencyCode cc = new Iso4217CurrencyCode(
020: ALPHABETIC_CURRENCY_CODE);
021:
022: checkNumeric(cc, ALPHABETIC_CURRENCY_CODE);
023:
024: cc = Iso4217CurrencyCode.getInstance(cc);
025:
026: checkNumeric(cc, ALPHABETIC_CURRENCY_CODE);
027:
028: DERObject obj = cc.toASN1Object();
029:
030: cc = Iso4217CurrencyCode.getInstance(obj);
031:
032: checkNumeric(cc, ALPHABETIC_CURRENCY_CODE);
033:
034: //
035: // numeric
036: //
037: cc = new Iso4217CurrencyCode(NUMERIC_CURRENCY_CODE);
038:
039: checkNumeric(cc, NUMERIC_CURRENCY_CODE);
040:
041: cc = Iso4217CurrencyCode.getInstance(cc);
042:
043: checkNumeric(cc, NUMERIC_CURRENCY_CODE);
044:
045: obj = cc.toASN1Object();
046:
047: cc = Iso4217CurrencyCode.getInstance(obj);
048:
049: checkNumeric(cc, NUMERIC_CURRENCY_CODE);
050:
051: cc = Iso4217CurrencyCode.getInstance(null);
052:
053: if (cc != null) {
054: fail("null getInstance() failed.");
055: }
056:
057: try {
058: Iso4217CurrencyCode.getInstance(new Object());
059:
060: fail("getInstance() failed to detect bad object.");
061: } catch (IllegalArgumentException e) {
062: // expected
063: }
064:
065: try {
066: new Iso4217CurrencyCode("ABCD");
067:
068: fail("constructor failed to detect out of range currencycode.");
069: } catch (IllegalArgumentException e) {
070: // expected
071: }
072:
073: try {
074: new Iso4217CurrencyCode(0);
075:
076: fail("constructor failed to detect out of range small numeric code.");
077: } catch (IllegalArgumentException e) {
078: // expected
079: }
080:
081: try {
082: new Iso4217CurrencyCode(1000);
083:
084: fail("constructor failed to detect out of range large numeric code.");
085: } catch (IllegalArgumentException e) {
086: // expected
087: }
088: }
089:
090: private void checkNumeric(Iso4217CurrencyCode cc, String code) {
091: if (!cc.isAlphabetic()) {
092: fail("non-alphabetic code found when one expected.");
093: }
094:
095: if (!cc.getAlphabetic().equals(code)) {
096: fail("string codes don't match.");
097: }
098: }
099:
100: private void checkNumeric(Iso4217CurrencyCode cc, int code) {
101: if (cc.isAlphabetic()) {
102: fail("alphabetic code found when one not expected.");
103: }
104:
105: if (cc.getNumeric() != code) {
106: fail("numeric codes don't match.");
107: }
108: }
109:
110: public static void main(String[] args) {
111: runTest(new Iso4217CurrencyCodeUnitTest());
112: }
113: }
|