Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.asn1.test;
  
  
 
 public class NameOrPseudonymUnitTest
     extends ASN1UnitTest
 {
     public String getName()
     {
         return "NameOrPseudonym";
     }
 
     public void performTest()
         throws Exception
     {
         String          pseudonym = "pseudonym";
         DirectoryString surname = new DirectoryString("surname");
         ASN1Sequence    givenName = new DERSequence(new DirectoryString("givenName"));
 
         NameOrPseudonym id = new NameOrPseudonym(pseudonym);
 
         checkConstruction(idpseudonymnullnull);
 
         id = new NameOrPseudonym(surnamegivenName);
 
         checkConstruction(idnullsurnamegivenName);
 
         id = NameOrPseudonym.getInstance(null);
 
         if (id != null)
         {
             fail("null getInstance() failed.");
         }
 
         try
         {
             NameOrPseudonym.getInstance(new Object());
 
             fail("getInstance() failed to detect bad object.");
         }
         catch (IllegalArgumentException e)
         {
             // expected
         }
     }
 
     private void checkConstruction(
         NameOrPseudonym id,
         String          pseudonym,
         DirectoryString surname,
         ASN1Sequence    givenName)
         throws IOException
     {
         checkValues(idpseudonymsurnamegivenName);
 
         id = NameOrPseudonym.getInstance(id);
 
         checkValues(idpseudonymsurnamegivenName);
 
         ASN1InputStream aIn = new ASN1InputStream(id.toASN1Object().getEncoded());
 
         if (surname != null)
         {
             ASN1Sequence seq = (ASN1Sequence)aIn.readObject();
 
             id = NameOrPseudonym.getInstance(seq);
         }
         else
         {
             ASN1String s = (ASN1String)aIn.readObject();
 
             id = NameOrPseudonym.getInstance(s);
         }
 
         checkValues(idpseudonymsurnamegivenName);
     }
 
     private void checkValues(
         NameOrPseudonym id,
         String          pseudonym,
         DirectoryString surname,
         ASN1Sequence    givenName)
     {
 
         if (surname != null)
         {
             checkMandatoryField("surname"surnameid.getSurname());
             checkMandatoryField("givenName"givenNamenew DERSequence(id.getGivenName()[0]));
         }
         else
         {
             checkOptionalField("pseudonym"new DirectoryString(pseudonym), id.getPseudonym());
        }
    }
    public static void main(
        String[]    args)
    {
        runTest(new NameOrPseudonymUnitTest());
    }
New to GrepCode? Check out our FAQ X