11/*
2- * Copyright 2002-2015 the original author or authors.
2+ * Copyright 2002-2016 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .util ;
1818
19+ import java .io .ByteArrayInputStream ;
20+ import java .io .IOException ;
1921import java .io .UnsupportedEncodingException ;
2022
2123import org .junit .Before ;
2527
2628/**
2729 * @author Arjen Poutsma
30+ * @author Juergen Hoeller
2831 */
2932public class DigestUtilsTests {
3033
@@ -38,24 +41,39 @@ public void createBytes() throws UnsupportedEncodingException {
3841
3942
4043 @ Test
41- public void md5 () {
42- byte [] result = DigestUtils .md5Digest (bytes );
44+ public void md5 () throws IOException {
4345 byte [] expected = new byte []
4446 {-0x4f , 0xa , -0x73 , -0x4f , 0x64 , -0x20 , 0x75 , 0x41 , 0x5 , -0x49 , -0x57 , -0x65 , -0x19 , 0x2e , 0x3f , -0x1b };
47+
48+ byte [] result = DigestUtils .md5Digest (bytes );
49+ assertArrayEquals ("Invalid hash" , expected , result );
50+
51+ result = DigestUtils .md5Digest (new ByteArrayInputStream (bytes ));
4552 assertArrayEquals ("Invalid hash" , expected , result );
4653 }
4754
4855 @ Test
49- public void md5Hex () throws UnsupportedEncodingException {
56+ public void md5Hex () throws IOException {
57+ String expected = "b10a8db164e0754105b7a99be72e3fe5" ;
58+
5059 String hash = DigestUtils .md5DigestAsHex (bytes );
51- assertEquals ("Invalid hash" , "b10a8db164e0754105b7a99be72e3fe5" , hash );
60+ assertEquals ("Invalid hash" , expected , hash );
61+
62+ hash = DigestUtils .md5DigestAsHex (new ByteArrayInputStream (bytes ));
63+ assertEquals ("Invalid hash" , expected , hash );
5264 }
5365
5466 @ Test
55- public void md5StringBuilder () throws UnsupportedEncodingException {
67+ public void md5StringBuilder () throws IOException {
68+ String expected = "b10a8db164e0754105b7a99be72e3fe5" ;
69+
5670 StringBuilder builder = new StringBuilder ();
5771 DigestUtils .appendMd5DigestAsHex (bytes , builder );
58- assertEquals ("Invalid hash" , "b10a8db164e0754105b7a99be72e3fe5" , builder .toString ());
72+ assertEquals ("Invalid hash" , expected , builder .toString ());
73+
74+ builder = new StringBuilder ();
75+ DigestUtils .appendMd5DigestAsHex (new ByteArrayInputStream (bytes ), builder );
76+ assertEquals ("Invalid hash" , expected , builder .toString ());
5977 }
6078
6179}
0 commit comments