KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > approval > TestApproval


1 package se.anatom.ejbca.approval;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.ObjectOutputStream JavaDoc;
5 import java.security.cert.X509Certificate JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import junit.framework.TestCase;
12
13 import org.ejbca.core.model.approval.Approval;
14 import org.ejbca.core.model.approval.ApprovalDataUtil;
15 import org.ejbca.util.Base64;
16 import org.ejbca.util.CertTools;
17
18 /**
19  * Test to externalize an approval
20  * @author Philip Vendil
21  * $Id: TestApproval.java,v 1.1 2006/07/30 18:19:03 herrvendil Exp $
22  */

23
24 public class TestApproval extends TestCase {
25     
26     private static byte[] testcertenc = Base64.decode(("MIIDATCCAmqgAwIBAgIIczEoghAwc3EwDQYJKoZIhvcNAQEFBQAwLzEPMA0GA1UE"
27             + "AxMGVGVzdENBMQ8wDQYDVQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFMB4XDTAzMDky"
28             + "NDA2NDgwNFoXDTA1MDkyMzA2NTgwNFowMzEQMA4GA1UEAxMHcDEydGVzdDESMBAG"
29             + "A1UEChMJUHJpbWVUZXN0MQswCQYDVQQGEwJTRTCBnTANBgkqhkiG9w0BAQEFAAOB"
30             + "iwAwgYcCgYEAnPAtfpU63/0h6InBmesN8FYS47hMvq/sliSBOMU0VqzlNNXuhD8a"
31             + "3FypGfnPXvjJP5YX9ORu1xAfTNao2sSHLtrkNJQBv6jCRIMYbjjo84UFab2qhhaJ"
32             + "wqJgkQNKu2LHy5gFUztxD8JIuFPoayp1n9JL/gqFDv6k81UnDGmHeFcCARGjggEi"
33             + "MIIBHjAPBgNVHRMBAf8EBTADAQEAMA8GA1UdDwEB/wQFAwMHoAAwOwYDVR0lBDQw"
34             + "MgYIKwYBBQUHAwEGCCsGAQUFBwMCBggrBgEFBQcDBAYIKwYBBQUHAwUGCCsGAQUF"
35             + "BwMHMB0GA1UdDgQWBBTnT1aQ9I0Ud4OEfNJkSOgJSrsIoDAfBgNVHSMEGDAWgBRj"
36             + "e/R2qFQkjqV0pXdEpvReD1eSUTAiBgNVHREEGzAZoBcGCisGAQQBgjcUAgOgCQwH"
37             + "Zm9vQGZvbzASBgNVHSAECzAJMAcGBSkBAQEBMEUGA1UdHwQ+MDwwOqA4oDaGNGh0"
38             + "dHA6Ly8xMjcuMC4wLjE6ODA4MC9lamJjYS93ZWJkaXN0L2NlcnRkaXN0P2NtZD1j"
39             + "cmwwDQYJKoZIhvcNAQEFBQADgYEAU4CCcLoSUDGXJAOO9hGhvxQiwjGD2rVKCLR4"
40             + "emox1mlQ5rgO9sSel6jHkwceaq4A55+qXAjQVsuy76UJnc8ncYX8f98uSYKcjxo/"
41             + "ifn1eHMbL8dGLd5bc2GNBZkmhFIEoDvbfn9jo7phlS8iyvF2YhC4eso8Xb+T7+BZ"
42             + "QUOBOvc=").getBytes());
43
44     protected void setUp() throws Exception JavaDoc {
45         super.setUp();
46         CertTools.installBCProvider();
47     }
48
49     public void testWriteExternal() throws Exception JavaDoc {
50         X509Certificate JavaDoc testcert = CertTools.getCertfromByteArray(testcertenc);
51         ArrayList JavaDoc approvals = new ArrayList JavaDoc();
52         
53         Approval ap = new Approval("test");
54         Date JavaDoc apDate = ap.getApprovalDate();
55         ap.setApprovalCertificateAndUsername(true,testcert, "USERNAME");
56         approvals.add(ap);
57         
58         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
59         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
60         
61         int size = approvals.size();
62         oos.writeInt(size);
63         Iterator JavaDoc iter = approvals.iterator();
64         while(iter.hasNext()){
65             Approval next = (Approval) iter.next();
66             oos.writeObject(next);
67         }
68         oos.flush();
69         String JavaDoc result = new String JavaDoc(Base64.encode(baos.toByteArray(),false));
70
71         
72         Collection JavaDoc readapprovals = ApprovalDataUtil.getApprovals(result);
73         assertTrue(readapprovals.size() == 1);
74         
75         Approval rap = (Approval) readapprovals.iterator().next();
76         assertTrue(rap.getAdminCertIssuerDN().equals(CertTools.getIssuerDN(testcert)));
77         assertTrue(rap.getAdminCertSerialNumber().equals(testcert.getSerialNumber()));
78         assertTrue(rap.getUsername().equals("USERNAME"));
79         assertTrue(rap.isApproved());
80         assertTrue(rap.getComment().equals("test"));
81         assertTrue(rap.getApprovalDate().equals(apDate));
82     }
83
84 }
85
Popular Tags