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