samples/messagedigest/src-test/test/OldAPIToNewAPITest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:23 +0200
changeset 46 c75861f07646
parent 44 716af5f2ebd1
child 50 019f1e9f7741
permissions -rw-r--r--
Using real pieces of code the talk about bridges
jtulach@44
     1
/*
jtulach@44
     2
 * To change this template, choose Tools | Templates
jtulach@44
     3
 * and open the template in the editor.
jtulach@44
     4
 */
jtulach@44
     5
jtulach@44
     6
package test;
jtulach@44
     7
jtulach@44
     8
import org.apidesign.api.security.*;
jtulach@44
     9
import java.nio.ByteBuffer;
jtulach@44
    10
import java.security.MessageDigest;
jtulach@44
    11
import java.util.Arrays;
jtulach@44
    12
import java.util.Random;
jtulach@44
    13
import org.junit.After;
jtulach@44
    14
import org.junit.BeforeClass;
jtulach@44
    15
import org.junit.Test;
jtulach@44
    16
import static org.junit.Assert.*;
jtulach@44
    17
jtulach@44
    18
/** Compares that the MessageDigest and Digest yield the same results for
jtulach@44
    19
 * default provider.
jtulach@44
    20
 *
jtulach@44
    21
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@44
    22
 */
jtulach@44
    23
public class OldAPIToNewAPITest {
jtulach@44
    24
    private static byte[] arr;
jtulach@44
    25
    private static long time;
jtulach@44
    26
    private static byte[] resOld;
jtulach@44
    27
    private static byte[] resNew;
jtulach@44
    28
jtulach@44
    29
    public OldAPIToNewAPITest() {
jtulach@44
    30
    }
jtulach@44
    31
jtulach@44
    32
    @BeforeClass
jtulach@44
    33
    public static void setUp() {
jtulach@44
    34
        time = System.currentTimeMillis();
jtulach@44
    35
        Random r = new Random(time);
jtulach@44
    36
        arr = new byte[r.nextInt(1024)];
jtulach@44
    37
        r.nextBytes(arr);
jtulach@44
    38
    }
jtulach@44
    39
jtulach@44
    40
    @After
jtulach@44
    41
    public void tearDown() {
jtulach@44
    42
    }
jtulach@44
    43
jtulach@44
    44
    @Test
jtulach@44
    45
    public void generateHashUsingMessageDigest() throws Exception {
jtulach@46
    46
        // BEGIN: day.end.bridges.BridgeToOldRegister
jtulach@44
    47
        // The java.security.Providers cannot be registered in META-INF/services
jtulach@44
    48
        // that is why one needs to either configure various properties or
jtulach@44
    49
        // make some dummy call that will initialize our bridge class.
jtulach@44
    50
        // Then the bridge class registers itself as a MessageDigest provider
jtulach@44
    51
        // in its constructor.
jtulach@44
    52
        //
jtulach@44
    53
        // This is the call:
jtulach@44
    54
        Digest initialize = Digest.getInstance("MD5");
jtulach@46
    55
        // END: day.end.bridges.BridgeToOldRegister
jtulach@44
    56
        
jtulach@44
    57
        MessageDigest md = MessageDigest.getInstance("cnt");
jtulach@44
    58
        byte[] res = md.digest(arr);
jtulach@44
    59
        resOld = res;
jtulach@44
    60
    }
jtulach@44
    61
    
jtulach@44
    62
    
jtulach@44
    63
    @Test
jtulach@44
    64
    public void generateHashUsingNewDigest() throws Exception {
jtulach@44
    65
        Digest d = Digest.getInstance("cnt");
jtulach@44
    66
        ByteBuffer bb = ByteBuffer.wrap(arr);
jtulach@44
    67
        byte[] res = d.digest(bb);
jtulach@44
    68
        resNew = res;
jtulach@44
    69
    }
jtulach@44
    70
jtulach@44
    71
jtulach@44
    72
    
jtulach@44
    73
    @Test
jtulach@44
    74
    public void compareTheHashes() throws Exception {
jtulach@44
    75
        if (!Arrays.equals(resOld, resNew)) {
jtulach@44
    76
            fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));
jtulach@44
    77
        }
jtulach@44
    78
    }
jtulach@44
    79
}