1.1 --- a/samples/messagedigest/build.xml Sat Jun 14 09:52:22 2008 +0200
1.2 +++ b/samples/messagedigest/build.xml Sat Jun 14 09:52:23 2008 +0200
1.3 @@ -1,69 +1,80 @@
1.4 <?xml version="1.0" encoding="UTF-8"?>
1.5 -<!-- You may freely edit this file. See commented blocks below for -->
1.6 -<!-- some examples of how to customize the build. -->
1.7 -<!-- (If you delete it and reopen the project it will be recreated.) -->
1.8 -<project name="messagedigest" default="default" basedir=".">
1.9 - <description>Builds, tests, and runs the project messagedigest.</description>
1.10 - <import file="nbproject/build-impl.xml"/>
1.11 - <!--
1.12 -
1.13 - There exist several targets which are by default empty and which can be
1.14 - used for execution of your tasks. These targets are usually executed
1.15 - before and after some main targets. They are:
1.16 -
1.17 - -pre-init: called before initialization of project properties
1.18 - -post-init: called after initialization of project properties
1.19 - -pre-compile: called before javac compilation
1.20 - -post-compile: called after javac compilation
1.21 - -pre-compile-single: called before javac compilation of single file
1.22 - -post-compile-single: called after javac compilation of single file
1.23 - -pre-compile-test: called before javac compilation of JUnit tests
1.24 - -post-compile-test: called after javac compilation of JUnit tests
1.25 - -pre-compile-test-single: called before javac compilation of single JUnit test
1.26 - -post-compile-test-single: called after javac compilation of single JUunit test
1.27 - -pre-jar: called before JAR building
1.28 - -post-jar: called after JAR building
1.29 - -post-clean: called after cleaning build products
1.30 -
1.31 - (Targets beginning with '-' are not intended to be called on their own.)
1.32 -
1.33 - Example of inserting an obfuscator after compilation could look like this:
1.34 -
1.35 - <target name="-post-compile">
1.36 - <obfuscate>
1.37 - <fileset dir="${build.classes.dir}"/>
1.38 - </obfuscate>
1.39 - </target>
1.40 -
1.41 - For list of available properties check the imported
1.42 - nbproject/build-impl.xml file.
1.43 -
1.44 -
1.45 - Another way to customize the build is by overriding existing main targets.
1.46 - The targets of interest are:
1.47 -
1.48 - -init-macrodef-javac: defines macro for javac compilation
1.49 - -init-macrodef-junit: defines macro for junit execution
1.50 - -init-macrodef-debug: defines macro for class debugging
1.51 - -init-macrodef-java: defines macro for class execution
1.52 - -do-jar-with-manifest: JAR building (if you are using a manifest)
1.53 - -do-jar-without-manifest: JAR building (if you are not using a manifest)
1.54 - run: execution of project
1.55 - -javadoc-build: Javadoc generation
1.56 - test-report: JUnit report generation
1.57 -
1.58 - An example of overriding the target for project execution could look like this:
1.59 -
1.60 - <target name="run" depends="messagedigest-impl.jar">
1.61 - <exec dir="bin" executable="launcher.exe">
1.62 - <arg file="${dist.jar}"/>
1.63 - </exec>
1.64 - </target>
1.65 -
1.66 - Notice that the overridden target depends on the jar target and not only on
1.67 - the compile target as the regular run target does. Again, for a list of available
1.68 - properties which you can use, check the target you are overriding in the
1.69 - nbproject/build-impl.xml file.
1.70 -
1.71 - -->
1.72 +<project name="Build Script" default="test" basedir=".">
1.73 + <target name="clean">
1.74 + <delete dir="build"/>
1.75 + </target>
1.76 +
1.77 + <target name="build" depends="-libraries">
1.78 + <antcall target="-build-one">
1.79 + <param name="version" value="new-api"/>
1.80 + </antcall>
1.81 + <antcall target="-build-one">
1.82 + <param name="version" value="new-spi"/>
1.83 + <param name="cp" value="build/new-api/classes"/>
1.84 + </antcall>
1.85 + <antcall target="-build-one">
1.86 + <param name="version" value="bridge"/>
1.87 + <param name="cp" value="build/new-api/classes:build/new-spi/classes"/>
1.88 + </antcall>
1.89 +
1.90 + <antcall target="-build-one">
1.91 + <param name="version" value="test"/>
1.92 + <param name="cp" value="build/new-api/classes:build/new-spi/classes:${junit.jar}"/>
1.93 + </antcall>
1.94 + </target>
1.95 +
1.96 + <target name="test" depends="build">
1.97 + <echo level="info" message="Running the tests with bridge enabled. This should succeeds."/>
1.98 + <antcall target="-run-one">
1.99 + <param name="test.cp" value="build/new-api/classes:build/new-spi/classes:build/bridge/classes"/>
1.100 + </antcall>
1.101 + <echo level="info" message="Running the tests without bridge module. This should fail."/>
1.102 + <antcall target="-run-one">
1.103 + <param name="test.cp" value="build/new-api/classes:build/new-spi/classes"/>
1.104 + </antcall>
1.105 + </target>
1.106 +
1.107 + <!-- support methods -->
1.108 +
1.109 + <target name="-libraries">
1.110 + <ant dir="../libs/"/>
1.111 +
1.112 + <property name="junit.jar" location="../libs/dist/junit-4.4.jar"/>
1.113 + </target>
1.114 +
1.115 + <target name="-run-one">
1.116 + <mkdir dir="build/testresults"/>
1.117 + <junit dir="build/test/classes" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" showoutput="true">
1.118 + <batchtest todir="build/testresults">
1.119 + <fileset dir="build/test/classes">
1.120 + <filename name="**/*Test.class"/>
1.121 + </fileset>
1.122 + </batchtest>
1.123 + <classpath>
1.124 + <path path="${test.cp}"/>
1.125 + <path location="build/test/classes"/>
1.126 + <path location="${junit.jar}"/>
1.127 + </classpath>
1.128 + <formatter type="brief" usefile="false"/>
1.129 + <formatter type="xml"/>
1.130 + </junit>
1.131 + </target>
1.132 +
1.133 + <target name="-build-one">
1.134 + <fail message="You need to specify version number" unless="version"/>
1.135 +
1.136 + <mkdir dir="build/${version}/classes"/>
1.137 + <property name="cp" value=""/>
1.138 + <javac
1.139 + srcdir="src-${version}"
1.140 + destdir="build/${version}/classes"
1.141 + source="1.5" target="1.5"
1.142 + classpath="${cp}"
1.143 + />
1.144 + <copy todir="build/${version}/classes">
1.145 + <fileset dir="src-${version}">
1.146 + <exclude name="**/*.java"/>
1.147 + </fileset>
1.148 + </copy>
1.149 + </target>
1.150 </project>
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/messagedigest/src-bridge/META-INF/services/org.apidesign.impl.security.friendapi.DigestProvider Sat Jun 14 09:52:23 2008 +0200
2.3 @@ -0,0 +1,3 @@
2.4 +# this shall be in a separate module that provides the "bridge"
2.5 +org.apidesign.impl.security.extension.BridgeToOldAlgorithmsProvider
2.6 +
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToOld.java Sat Jun 14 09:52:23 2008 +0200
3.3 @@ -0,0 +1,82 @@
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 org.apidesign.impl.security.extension;
3.10 +
3.11 +import org.apidesign.api.security.Digest;
3.12 +import java.nio.ByteBuffer;
3.13 +import java.security.MessageDigest;
3.14 +import java.security.NoSuchAlgorithmException;
3.15 +import java.security.Provider;
3.16 +import java.security.Security;
3.17 +import java.util.Collections;
3.18 +import java.util.List;
3.19 +import java.util.Map;
3.20 +
3.21 +/**
3.22 + *
3.23 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.24 + */
3.25 +public class BridgeToOld extends Provider {
3.26 +
3.27 + public BridgeToOld() {
3.28 + super("spi.Digestor", 1.0, "");
3.29 + Security.addProvider(this);
3.30 + }
3.31 +
3.32 + @Override
3.33 + public synchronized Service getService(String type, String algorithm) {
3.34 + if ("MessageDigest".equals(type)) {
3.35 + Digest dig = Digest.getInstance(algorithm);
3.36 + if (dig != null) {
3.37 + return new ServiceImpl(dig, this, type, algorithm, "", Collections.<String>emptyList(), Collections.<String,String>emptyMap());
3.38 + }
3.39 + }
3.40 + return null;
3.41 + }
3.42 +
3.43 + private static class ServiceImpl<Data> extends Service {
3.44 + Digest dig;
3.45 +
3.46 + public ServiceImpl(Digest dig, Provider provider, String type, String algorithm, String className, List<String> aliases, Map<String, String> attributes) {
3.47 + super(provider, type, algorithm, className, aliases, attributes);
3.48 + this.dig = dig;
3.49 + }
3.50 +
3.51 + @Override
3.52 + public Object newInstance(Object constructorParameter) throws NoSuchAlgorithmException {
3.53 + return new MessageDigest(getAlgorithm()) {
3.54 + private byte[] res;
3.55 +
3.56 + @Override
3.57 + protected void engineUpdate(byte input) {
3.58 + ByteBuffer bb = ByteBuffer.wrap(new byte[] { input });
3.59 + res = dig.digest(bb);
3.60 + }
3.61 +
3.62 + @Override
3.63 + protected void engineUpdate(byte[] input, int offset, int len) {
3.64 + ByteBuffer bb = ByteBuffer.wrap(input);
3.65 + bb.position(offset);
3.66 + bb.limit(offset + len);
3.67 + res = dig.digest(bb);
3.68 + }
3.69 +
3.70 + @Override
3.71 + protected byte[] engineDigest() {
3.72 + return res;
3.73 + }
3.74 +
3.75 + @Override
3.76 + protected void engineReset() {
3.77 + dig = Digest.getInstance(getAlgorithm());
3.78 + }
3.79 + };
3.80 + }
3.81 +
3.82 +
3.83 + }
3.84 +
3.85 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToOldAlgorithmsProvider.java Sat Jun 14 09:52:23 2008 +0200
4.3 @@ -0,0 +1,46 @@
4.4 +/*
4.5 + * To change this template, choose Tools | Templates
4.6 + * and open the template in the editor.
4.7 + */
4.8 +
4.9 +package org.apidesign.impl.security.extension;
4.10 +
4.11 +import org.apidesign.impl.security.friendapi.DigestImplementation;
4.12 +import org.apidesign.impl.security.friendapi.DigestProvider;
4.13 +import java.nio.ByteBuffer;
4.14 +import java.security.MessageDigest;
4.15 +import java.security.NoSuchAlgorithmException;
4.16 +import java.util.logging.Level;
4.17 +import java.util.logging.Logger;
4.18 +
4.19 +/**
4.20 + *
4.21 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.22 + */
4.23 +public class BridgeToOldAlgorithmsProvider implements DigestProvider {
4.24 +
4.25 + public DigestImplementation create(String algorithm) {
4.26 + try {
4.27 + final MessageDigest md = MessageDigest.getInstance(algorithm);
4.28 + return new DigestImplementation(algorithm) {
4.29 +
4.30 + @Override
4.31 + public void update(ByteBuffer bb) {
4.32 + md.update(bb);
4.33 + }
4.34 +
4.35 + @Override
4.36 + public byte[] digest() {
4.37 + return md.digest();
4.38 + }
4.39 + };
4.40 + } catch (NoSuchAlgorithmException ex) {
4.41 + Logger.getLogger(BridgeToOldAlgorithmsProvider.class.getName()).log(Level.INFO, null, ex);
4.42 + return null;
4.43 + }
4.44 + }
4.45 +
4.46 + static {
4.47 + new BridgeToOld();
4.48 + }
4.49 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java Sat Jun 14 09:52:23 2008 +0200
5.3 @@ -0,0 +1,44 @@
5.4 +
5.5 +package org.apidesign.api.security;
5.6 +
5.7 +import java.nio.ByteBuffer;
5.8 +import org.apidesign.impl.security.friendapi.DigestImplementation;
5.9 +import org.apidesign.impl.security.friendapi.DigestProvider;
5.10 +import java.util.ServiceLoader;
5.11 +
5.12 +/** MessageDigest extends MessageDigestSpi, that means the javadoc
5.13 + *
5.14 + * @author Jaroslav Tulach
5.15 + */
5.16 +// BEGIN: day.end.bridges.Digest
5.17 +public final class Digest {
5.18 + private final DigestImplementation impl;
5.19 +
5.20 + /** Factory method is better than constructor */
5.21 + private Digest(DigestImplementation impl) {
5.22 + this.impl = impl;
5.23 + }
5.24 +
5.25 + /** Factory method to create digest for an algorithm.
5.26 + */
5.27 + public static Digest getInstance(String algorithm) {
5.28 + for (DigestProvider dp : ServiceLoader.load(DigestProvider.class)) {
5.29 + DigestImplementation impl = dp.create(algorithm);
5.30 + if (impl != null) {
5.31 + return new Digest(impl);
5.32 + }
5.33 + }
5.34 + throw new IllegalArgumentException(algorithm);
5.35 + }
5.36 +
5.37 + //
5.38 + // these methods are kept the same as in original MessageDigest,
5.39 + // but for simplicity choose just some from the original API
5.40 + //
5.41 +
5.42 + public byte[] digest(ByteBuffer bb) {
5.43 + impl.update(bb);
5.44 + return impl.digest();
5.45 + }
5.46 +}
5.47 +// END: day.end.bridges.Digest
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/impl/security/friendapi/DigestImplementation.java Sat Jun 14 09:52:23 2008 +0200
6.3 @@ -0,0 +1,23 @@
6.4 +/*
6.5 + * To change this template, choose Tools | Templates
6.6 + * and open the template in the editor.
6.7 + */
6.8 +
6.9 +package org.apidesign.impl.security.friendapi;
6.10 +
6.11 +import java.nio.ByteBuffer;
6.12 +
6.13 +/**
6.14 + *
6.15 + * @author Jaroslav Tulach
6.16 + */
6.17 +public abstract class DigestImplementation {
6.18 + final String name;
6.19 +
6.20 + protected DigestImplementation(String algorithm) {
6.21 + this.name = algorithm;
6.22 + }
6.23 +
6.24 + public abstract void update(ByteBuffer bb);
6.25 + public abstract byte[] digest();
6.26 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/impl/security/friendapi/DigestProvider.java Sat Jun 14 09:52:23 2008 +0200
7.3 @@ -0,0 +1,14 @@
7.4 +/*
7.5 + * To change this template, choose Tools | Templates
7.6 + * and open the template in the editor.
7.7 + */
7.8 +
7.9 +package org.apidesign.impl.security.friendapi;
7.10 +
7.11 +/**
7.12 + *
7.13 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
7.14 + */
7.15 +public interface DigestProvider {
7.16 + public abstract DigestImplementation create(String algorithm);
7.17 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/samples/messagedigest/src-new-spi/META-INF/services/org.apidesign.impl.security.friendapi.DigestProvider Sat Jun 14 09:52:23 2008 +0200
8.3 @@ -0,0 +1,2 @@
8.4 +# this is a provider for the SPI, shall be in the same module as the api & spi
8.5 +org.apidesign.impl.security.spi.DigestorProvider
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/samples/messagedigest/src-new-spi/org/apidesign/impl/security/spi/DigestorAccessor.java Sat Jun 14 09:52:23 2008 +0200
9.3 @@ -0,0 +1,35 @@
9.4 +/*
9.5 + * To change this template, choose Tools | Templates
9.6 + * and open the template in the editor.
9.7 + */
9.8 +
9.9 +package org.apidesign.impl.security.spi;
9.10 +
9.11 +import java.nio.ByteBuffer;
9.12 +import org.apidesign.spi.security.Digestor;
9.13 +
9.14 +/**
9.15 + *
9.16 + * @author jarda
9.17 + */
9.18 +public abstract class DigestorAccessor {
9.19 + private static DigestorAccessor INSTANCE;
9.20 +
9.21 + protected DigestorAccessor() {
9.22 + assert INSTANCE == null;
9.23 + INSTANCE = this;
9.24 + }
9.25 +
9.26 + public static DigestorAccessor getDefault() {
9.27 + try {
9.28 + Class.forName(Digestor.class.getName(), true, DigestorAccessor.class.getClassLoader());
9.29 + return INSTANCE;
9.30 + } catch (ClassNotFoundException ex) {
9.31 + throw new IllegalStateException(ex);
9.32 + }
9.33 + }
9.34 +
9.35 + protected abstract <Data> byte[] digest(Digestor<Data> dig, Data data);
9.36 + protected abstract <Data> Data create(Digestor<Data> dig, String algorithm);
9.37 + protected abstract <Data> void update(Digestor<Data> dig, Data data, ByteBuffer input);
9.38 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/samples/messagedigest/src-new-spi/org/apidesign/impl/security/spi/DigestorProvider.java Sat Jun 14 09:52:23 2008 +0200
10.3 @@ -0,0 +1,61 @@
10.4 +/*
10.5 + * To change this template, choose Tools | Templates
10.6 + * and open the template in the editor.
10.7 + */
10.8 +
10.9 +package org.apidesign.impl.security.spi;
10.10 +
10.11 +import org.apidesign.impl.security.friendapi.DigestImplementation;
10.12 +import org.apidesign.impl.security.friendapi.DigestProvider;
10.13 +import java.nio.ByteBuffer;
10.14 +import java.util.ServiceLoader;
10.15 +import org.apidesign.spi.security.Digestor;
10.16 +
10.17 +/**
10.18 + *
10.19 + * @author Jaroslav Tulach
10.20 + */
10.21 +public class DigestorProvider implements DigestProvider {
10.22 +
10.23 + public DigestImplementation create(String algorithm) {
10.24 + for (Digestor<?> d : ServiceLoader.load(Digestor.class)) {
10.25 + Handler<?> h = create(d, algorithm);
10.26 + if (h != null) {
10.27 + return h;
10.28 + }
10.29 + }
10.30 + return null;
10.31 + }
10.32 +
10.33 + private static <Data> Handler<Data> create(Digestor<Data> dig, String algorithm) {
10.34 + Data d = DigestorAccessor.getDefault().create(dig, algorithm);
10.35 + if (d == null) {
10.36 + return null;
10.37 + } else {
10.38 + return new Handler<Data>(algorithm, dig, d);
10.39 + }
10.40 + }
10.41 +
10.42 + private static final class Handler<Data> extends DigestImplementation {
10.43 + private final Digestor dig;
10.44 + private final Data data;
10.45 +
10.46 + public Handler(String algorithm, Digestor dig, Data data) {
10.47 + super(algorithm);
10.48 + this.dig = dig;
10.49 + this.data = data;
10.50 + }
10.51 +
10.52 +
10.53 + @Override
10.54 + public void update(ByteBuffer bb) {
10.55 + DigestorAccessor.getDefault().update(dig, data, bb);
10.56 + }
10.57 +
10.58 + @Override
10.59 + public byte[] digest() {
10.60 + return DigestorAccessor.getDefault().digest(dig, data);
10.61 + }
10.62 +
10.63 + }
10.64 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/samples/messagedigest/src-new-spi/org/apidesign/spi/security/Digestor.java Sat Jun 14 09:52:23 2008 +0200
11.3 @@ -0,0 +1,40 @@
11.4 +/*
11.5 + * To change this template, choose Tools | Templates
11.6 + * and open the template in the editor.
11.7 + */
11.8 +
11.9 +package org.apidesign.spi.security;
11.10 +
11.11 +import org.apidesign.impl.security.spi.DigestorAccessor;
11.12 +import java.nio.ByteBuffer;
11.13 +
11.14 +/**
11.15 + *
11.16 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
11.17 + */
11.18 +// BEGIN: day.end.bridges.Digestor
11.19 +public abstract class Digestor<Data> {
11.20 + protected abstract byte[] digest(Data data);
11.21 + protected abstract Data create(String algorithm);
11.22 + protected abstract void update(Data data, ByteBuffer input);
11.23 +
11.24 +// END: day.end.bridges.Digestor
11.25 + static {
11.26 + new DigestorAccessor() {
11.27 + @Override
11.28 + protected <Data> byte[] digest(Digestor<Data> dig, Data data) {
11.29 + return dig.digest(data);
11.30 + }
11.31 +
11.32 + @Override
11.33 + protected <Data> Data create(Digestor<Data> dig, String algorithm) {
11.34 + return dig.create(algorithm);
11.35 + }
11.36 +
11.37 + @Override
11.38 + protected <Data> void update(Digestor<Data> dig, Data data, ByteBuffer input) {
11.39 + dig.update(data, input);
11.40 + }
11.41 + };
11.42 + }
11.43 +}
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/samples/messagedigest/src-test/META-INF/services/org.apidesign.spi.security.Digestor Sat Jun 14 09:52:23 2008 +0200
12.3 @@ -0,0 +1,3 @@
12.4 +# the provider registered from tests
12.5 +test.CountingDigestor
12.6 +
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/samples/messagedigest/src-test/test/CountingDigestor.java Sat Jun 14 09:52:23 2008 +0200
13.3 @@ -0,0 +1,35 @@
13.4 +/*
13.5 + * To change this template, choose Tools | Templates
13.6 + * and open the template in the editor.
13.7 + */
13.8 +
13.9 +package test;
13.10 +
13.11 +import java.nio.ByteBuffer;
13.12 +import org.apidesign.spi.security.Digestor;
13.13 +
13.14 +/**
13.15 + *
13.16 + * @author jarda
13.17 + */
13.18 +public final class CountingDigestor extends Digestor<int[]> {
13.19 +
13.20 + @Override
13.21 + protected byte[] digest(int[] data) {
13.22 + int i = data[0];
13.23 + byte[] arr = { (byte) (i & 255), (byte) ((i >> 8) & 255), (byte) ((i >> 16) & 255), (byte) ((i >> 24) & 255) };
13.24 + return arr;
13.25 + }
13.26 +
13.27 + @Override
13.28 + protected int[] create(String algorithm) {
13.29 + return "cnt".equals(algorithm) ? new int[1] : null; // NOI18N
13.30 + }
13.31 +
13.32 + @Override
13.33 + protected void update(int[] data, ByteBuffer input) {
13.34 + data[0] += input.remaining();
13.35 + input.position(input.position() + input.remaining());
13.36 + }
13.37 +
13.38 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
14.2 +++ b/samples/messagedigest/src-test/test/NewAPIToOldAPITest.java Sat Jun 14 09:52:23 2008 +0200
14.3 @@ -0,0 +1,65 @@
14.4 +/*
14.5 + * To change this template, choose Tools | Templates
14.6 + * and open the template in the editor.
14.7 + */
14.8 +
14.9 +package test;
14.10 +
14.11 +import java.nio.ByteBuffer;
14.12 +import java.security.MessageDigest;
14.13 +import java.util.Arrays;
14.14 +import java.util.Random;
14.15 +import org.apidesign.api.security.Digest;
14.16 +import org.junit.After;
14.17 +import org.junit.BeforeClass;
14.18 +import org.junit.Test;
14.19 +import static org.junit.Assert.*;
14.20 +
14.21 +/** Compares that the MessageDigest and Digest yield the same results for
14.22 + * default provider.
14.23 + *
14.24 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
14.25 + */
14.26 +public class NewAPIToOldAPITest {
14.27 + private static byte[] arr;
14.28 + private static long time;
14.29 + private static byte[] resOld;
14.30 + private static byte[] resNew;
14.31 +
14.32 + public NewAPIToOldAPITest() {
14.33 + }
14.34 +
14.35 + @BeforeClass
14.36 + public static void setUp() {
14.37 + time = System.currentTimeMillis();
14.38 + Random r = new Random(time);
14.39 + arr = new byte[r.nextInt(1024)];
14.40 + r.nextBytes(arr);
14.41 + }
14.42 +
14.43 + @After
14.44 + public void tearDown() {
14.45 + }
14.46 +
14.47 + @Test
14.48 + public void generateHashUsingMessageDigest() throws Exception {
14.49 + MessageDigest md = MessageDigest.getInstance("MD5");
14.50 + byte[] res = md.digest(arr);
14.51 + resOld = res;
14.52 + }
14.53 +
14.54 + @Test
14.55 + public void generateHashUsingNewDigest() throws Exception {
14.56 + Digest d = Digest.getInstance("MD5");
14.57 + ByteBuffer bb = ByteBuffer.wrap(arr);
14.58 + byte[] res = d.digest(bb);
14.59 + resNew = res;
14.60 + }
14.61 +
14.62 + @Test
14.63 + public void compareTheHashes() throws Exception {
14.64 + if (!Arrays.equals(resOld, resNew)) {
14.65 + fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));
14.66 + }
14.67 + }
14.68 +}
14.69 \ No newline at end of file
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
15.2 +++ b/samples/messagedigest/src-test/test/OldAPIToNewAPITest.java Sat Jun 14 09:52:23 2008 +0200
15.3 @@ -0,0 +1,77 @@
15.4 +/*
15.5 + * To change this template, choose Tools | Templates
15.6 + * and open the template in the editor.
15.7 + */
15.8 +
15.9 +package test;
15.10 +
15.11 +import org.apidesign.api.security.*;
15.12 +import java.nio.ByteBuffer;
15.13 +import java.security.MessageDigest;
15.14 +import java.util.Arrays;
15.15 +import java.util.Random;
15.16 +import org.junit.After;
15.17 +import org.junit.BeforeClass;
15.18 +import org.junit.Test;
15.19 +import static org.junit.Assert.*;
15.20 +
15.21 +/** Compares that the MessageDigest and Digest yield the same results for
15.22 + * default provider.
15.23 + *
15.24 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
15.25 + */
15.26 +public class OldAPIToNewAPITest {
15.27 + private static byte[] arr;
15.28 + private static long time;
15.29 + private static byte[] resOld;
15.30 + private static byte[] resNew;
15.31 +
15.32 + public OldAPIToNewAPITest() {
15.33 + }
15.34 +
15.35 + @BeforeClass
15.36 + public static void setUp() {
15.37 + time = System.currentTimeMillis();
15.38 + Random r = new Random(time);
15.39 + arr = new byte[r.nextInt(1024)];
15.40 + r.nextBytes(arr);
15.41 + }
15.42 +
15.43 + @After
15.44 + public void tearDown() {
15.45 + }
15.46 +
15.47 + @Test
15.48 + public void generateHashUsingMessageDigest() throws Exception {
15.49 + // The java.security.Providers cannot be registered in META-INF/services
15.50 + // that is why one needs to either configure various properties or
15.51 + // make some dummy call that will initialize our bridge class.
15.52 + // Then the bridge class registers itself as a MessageDigest provider
15.53 + // in its constructor.
15.54 + //
15.55 + // This is the call:
15.56 + Digest initialize = Digest.getInstance("MD5");
15.57 +
15.58 + MessageDigest md = MessageDigest.getInstance("cnt");
15.59 + byte[] res = md.digest(arr);
15.60 + resOld = res;
15.61 + }
15.62 +
15.63 +
15.64 + @Test
15.65 + public void generateHashUsingNewDigest() throws Exception {
15.66 + Digest d = Digest.getInstance("cnt");
15.67 + ByteBuffer bb = ByteBuffer.wrap(arr);
15.68 + byte[] res = d.digest(bb);
15.69 + resNew = res;
15.70 + }
15.71 +
15.72 +
15.73 +
15.74 + @Test
15.75 + public void compareTheHashes() throws Exception {
15.76 + if (!Arrays.equals(resOld, resNew)) {
15.77 + fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));
15.78 + }
15.79 + }
15.80 +}
15.81 \ No newline at end of file