Tests rename to describe the intention, e.g. new -> old bridge and vice versa, and modified to include also a call that initializes the bridge
1.1 --- a/samples/messagedigest/src/api/Digest.java Sat Jun 14 09:52:22 2008 +0200
1.2 +++ b/samples/messagedigest/src/api/Digest.java Sat Jun 14 09:52:22 2008 +0200
1.3 @@ -12,7 +12,7 @@
1.4 */
1.5 public final class Digest {
1.6 private final DigestImplementation impl;
1.7 -
1.8 +
1.9 /** Factory method is better than constructor */
1.10 private Digest(DigestImplementation impl) {
1.11 this.impl = impl;
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/messagedigest/test/api/NewAPIToOldAPITest.java Sat Jun 14 09:52:22 2008 +0200
2.3 @@ -0,0 +1,64 @@
2.4 +/*
2.5 + * To change this template, choose Tools | Templates
2.6 + * and open the template in the editor.
2.7 + */
2.8 +
2.9 +package api;
2.10 +
2.11 +import java.nio.ByteBuffer;
2.12 +import java.security.MessageDigest;
2.13 +import java.util.Arrays;
2.14 +import java.util.Random;
2.15 +import org.junit.After;
2.16 +import org.junit.BeforeClass;
2.17 +import org.junit.Test;
2.18 +import static org.junit.Assert.*;
2.19 +
2.20 +/** Compares that the MessageDigest and Digest yield the same results for
2.21 + * default provider.
2.22 + *
2.23 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.24 + */
2.25 +public class NewAPIToOldAPITest {
2.26 + private static byte[] arr;
2.27 + private static long time;
2.28 + private static byte[] resOld;
2.29 + private static byte[] resNew;
2.30 +
2.31 + public NewAPIToOldAPITest() {
2.32 + }
2.33 +
2.34 + @BeforeClass
2.35 + public static void setUp() {
2.36 + time = System.currentTimeMillis();
2.37 + Random r = new Random(time);
2.38 + arr = new byte[r.nextInt(1024)];
2.39 + r.nextBytes(arr);
2.40 + }
2.41 +
2.42 + @After
2.43 + public void tearDown() {
2.44 + }
2.45 +
2.46 + @Test
2.47 + public void generateHashUsingMessageDigest() throws Exception {
2.48 + MessageDigest md = MessageDigest.getInstance("MD5");
2.49 + byte[] res = md.digest(arr);
2.50 + resOld = res;
2.51 + }
2.52 +
2.53 + @Test
2.54 + public void generateHashUsingNewDigest() throws Exception {
2.55 + Digest d = Digest.getInstance("MD5");
2.56 + ByteBuffer bb = ByteBuffer.wrap(arr);
2.57 + byte[] res = d.digest(bb);
2.58 + resNew = res;
2.59 + }
2.60 +
2.61 + @Test
2.62 + public void compareTheHashes() throws Exception {
2.63 + if (!Arrays.equals(resOld, resNew)) {
2.64 + fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));
2.65 + }
2.66 + }
2.67 +}
2.68 \ No newline at end of file
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/samples/messagedigest/test/api/OldAPIToNewAPITest.java Sat Jun 14 09:52:22 2008 +0200
3.3 @@ -0,0 +1,77 @@
3.4 +/*
3.5 + * To change this template, choose Tools | Templates
3.6 + * and open the template in the editor.
3.7 + */
3.8 +
3.9 +package api;
3.10 +
3.11 +import impl.independent.module.BridgeToOld;
3.12 +import java.nio.ByteBuffer;
3.13 +import java.security.MessageDigest;
3.14 +import java.util.Arrays;
3.15 +import java.util.Random;
3.16 +import org.junit.After;
3.17 +import org.junit.BeforeClass;
3.18 +import org.junit.Test;
3.19 +import static org.junit.Assert.*;
3.20 +
3.21 +/** Compares that the MessageDigest and Digest yield the same results for
3.22 + * default provider.
3.23 + *
3.24 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.25 + */
3.26 +public class OldAPIToNewAPITest {
3.27 + private static byte[] arr;
3.28 + private static long time;
3.29 + private static byte[] resOld;
3.30 + private static byte[] resNew;
3.31 +
3.32 + public OldAPIToNewAPITest() {
3.33 + }
3.34 +
3.35 + @BeforeClass
3.36 + public static void setUp() {
3.37 + time = System.currentTimeMillis();
3.38 + Random r = new Random(time);
3.39 + arr = new byte[r.nextInt(1024)];
3.40 + r.nextBytes(arr);
3.41 + }
3.42 +
3.43 + @After
3.44 + public void tearDown() {
3.45 + }
3.46 +
3.47 + @Test
3.48 + public void generateHashUsingMessageDigest() throws Exception {
3.49 + // The java.security.Providers cannot be registered in META-INF/services
3.50 + // that is why one needs to either configure various properties or
3.51 + // make some dummy call that will initialize our bridge class.
3.52 + // Then the bridge class registers itself as a MessageDigest provider
3.53 + // in its constructor.
3.54 + //
3.55 + // This is the call:
3.56 + Digest initialize = Digest.getInstance("MD5");
3.57 +
3.58 + MessageDigest md = MessageDigest.getInstance("cnt");
3.59 + byte[] res = md.digest(arr);
3.60 + resOld = res;
3.61 + }
3.62 +
3.63 +
3.64 + @Test
3.65 + public void generateHashUsingNewDigest() throws Exception {
3.66 + Digest d = Digest.getInstance("cnt");
3.67 + ByteBuffer bb = ByteBuffer.wrap(arr);
3.68 + byte[] res = d.digest(bb);
3.69 + resNew = res;
3.70 + }
3.71 +
3.72 +
3.73 +
3.74 + @Test
3.75 + public void compareTheHashes() throws Exception {
3.76 + if (!Arrays.equals(resOld, resNew)) {
3.77 + fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));
3.78 + }
3.79 + }
3.80 +}
3.81 \ No newline at end of file