samples/messagedigest/src-test/test/OldAPIToNewAPITest.java
changeset 44 716af5f2ebd1
child 46 c75861f07646
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/messagedigest/src-test/test/OldAPIToNewAPITest.java	Sat Jun 14 09:52:23 2008 +0200
     1.3 @@ -0,0 +1,77 @@
     1.4 +/*
     1.5 + * To change this template, choose Tools | Templates
     1.6 + * and open the template in the editor.
     1.7 + */
     1.8 +
     1.9 +package test;
    1.10 +
    1.11 +import org.apidesign.api.security.*;
    1.12 +import java.nio.ByteBuffer;
    1.13 +import java.security.MessageDigest;
    1.14 +import java.util.Arrays;
    1.15 +import java.util.Random;
    1.16 +import org.junit.After;
    1.17 +import org.junit.BeforeClass;
    1.18 +import org.junit.Test;
    1.19 +import static org.junit.Assert.*;
    1.20 +
    1.21 +/** Compares that the MessageDigest and Digest yield the same results for
    1.22 + * default provider.
    1.23 + *
    1.24 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.25 + */
    1.26 +public class OldAPIToNewAPITest {
    1.27 +    private static byte[] arr;
    1.28 +    private static long time;
    1.29 +    private static byte[] resOld;
    1.30 +    private static byte[] resNew;
    1.31 +
    1.32 +    public OldAPIToNewAPITest() {
    1.33 +    }
    1.34 +
    1.35 +    @BeforeClass
    1.36 +    public static void setUp() {
    1.37 +        time = System.currentTimeMillis();
    1.38 +        Random r = new Random(time);
    1.39 +        arr = new byte[r.nextInt(1024)];
    1.40 +        r.nextBytes(arr);
    1.41 +    }
    1.42 +
    1.43 +    @After
    1.44 +    public void tearDown() {
    1.45 +    }
    1.46 +
    1.47 +    @Test
    1.48 +    public void generateHashUsingMessageDigest() throws Exception {
    1.49 +        // The java.security.Providers cannot be registered in META-INF/services
    1.50 +        // that is why one needs to either configure various properties or
    1.51 +        // make some dummy call that will initialize our bridge class.
    1.52 +        // Then the bridge class registers itself as a MessageDigest provider
    1.53 +        // in its constructor.
    1.54 +        //
    1.55 +        // This is the call:
    1.56 +        Digest initialize = Digest.getInstance("MD5");
    1.57 +        
    1.58 +        MessageDigest md = MessageDigest.getInstance("cnt");
    1.59 +        byte[] res = md.digest(arr);
    1.60 +        resOld = res;
    1.61 +    }
    1.62 +    
    1.63 +    
    1.64 +    @Test
    1.65 +    public void generateHashUsingNewDigest() throws Exception {
    1.66 +        Digest d = Digest.getInstance("cnt");
    1.67 +        ByteBuffer bb = ByteBuffer.wrap(arr);
    1.68 +        byte[] res = d.digest(bb);
    1.69 +        resNew = res;
    1.70 +    }
    1.71 +
    1.72 +
    1.73 +    
    1.74 +    @Test
    1.75 +    public void compareTheHashes() throws Exception {
    1.76 +        if (!Arrays.equals(resOld, resNew)) {
    1.77 +            fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));
    1.78 +        }
    1.79 +    }
    1.80 +}
    1.81 \ No newline at end of file