samples/messagedigest/src-test/test/NewAPIToOldAPITest.java
changeset 44 716af5f2ebd1
child 263 7e8e995065c5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/messagedigest/src-test/test/NewAPIToOldAPITest.java	Sat Jun 14 09:52:23 2008 +0200
     1.3 @@ -0,0 +1,65 @@
     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 java.nio.ByteBuffer;
    1.12 +import java.security.MessageDigest;
    1.13 +import java.util.Arrays;
    1.14 +import java.util.Random;
    1.15 +import org.apidesign.api.security.Digest;
    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 NewAPIToOldAPITest {
    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 NewAPIToOldAPITest() {
    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 +        MessageDigest md = MessageDigest.getInstance("MD5");
    1.50 +        byte[] res = md.digest(arr);
    1.51 +        resOld = res;
    1.52 +    }
    1.53 +
    1.54 +    @Test
    1.55 +    public void generateHashUsingNewDigest() throws Exception {
    1.56 +        Digest d = Digest.getInstance("MD5");
    1.57 +        ByteBuffer bb = ByteBuffer.wrap(arr);
    1.58 +        byte[] res = d.digest(bb);
    1.59 +        resNew = res;
    1.60 +    }
    1.61 +    
    1.62 +    @Test
    1.63 +    public void compareTheHashes() throws Exception {
    1.64 +        if (!Arrays.equals(resOld, resNew)) {
    1.65 +            fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));
    1.66 +        }
    1.67 +    }
    1.68 +}
    1.69 \ No newline at end of file