Skip to content

Commit 6256120

Browse files
committed
Refactor common code and complete test coverage (100%, see clover report). Javadoc.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130220 13f79535-47bb-0310-9956-ffa450edef68
1 parent fb41ecd commit 6256120

2 files changed

Lines changed: 97 additions & 109 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
Operations to simplifiy common <code>MessageDigest</code> tasks.
4+
</body>
5+
</html>

src/test/org/apache/commons/codec/digest/DigestUtilsTest.java

Lines changed: 92 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,142 +2,125 @@
22
* ====================================================================
33
*
44
* The Apache Software License, Version 1.1
5-
*
6-
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7-
* reserved.
8-
*
5+
*
6+
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights reserved.
7+
*
98
* Redistribution and use in source and binary forms, with or without
10-
* modification, are permitted provided that the following conditions
11-
* are met:
12-
*
13-
* 1. Redistributions of source code must retain the above copyright
14-
* notice, this list of conditions and the following disclaimer.
15-
*
16-
* 2. Redistributions in binary form must reproduce the above copyright
17-
* notice, this list of conditions and the following disclaimer in
18-
* the documentation and/or other materials provided with the
19-
* distribution.
20-
*
21-
* 3. The end-user documentation included with the redistribution,
22-
* if any, must include the following acknowledgement:
23-
* "This product includes software developed by the
24-
* Apache Software Foundation (http://www.apache.org/)."
25-
* Alternately, this acknowledgement may appear in the software itself,
26-
* if and wherever such third-party acknowledgements normally appear.
27-
*
28-
* 4. The names "Apache", "The Jakarta Project", "Commons", and "Apache Software
29-
* Foundation" must not be used to endorse or promote products derived
30-
* from this software without prior written permission. For written
31-
* permission, please contact apache@apache.org.
32-
*
33-
* 5. Products derived from this software may not be called "Apache",
34-
* "Apache" nor may "Apache" appear in their name without prior
35-
* written permission of the Apache Software Foundation.
36-
*
37-
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38-
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39-
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40-
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41-
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42-
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43-
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44-
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45-
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47-
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48-
* SUCH DAMAGE.
9+
* modification, are permitted provided that the following conditions are met:
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
* 3. The end-user documentation included with the redistribution, if any,
16+
* must include the following acknowledgement: "This product includes software
17+
* developed by the Apache Software Foundation (http://www.apache.org/)."
18+
* Alternately, this acknowledgement may appear in the software itself, if and
19+
* wherever such third-party acknowledgements normally appear.
20+
* 4. The names "Apache", "The Jakarta Project", "Commons", and "Apache
21+
* Software Foundation" must not be used to endorse or promote products derived
22+
* from this software without prior written permission. For written permission,
23+
* please contact apache@apache.org.
24+
* 5. Products derived from this software may not be called "Apache", "Apache"
25+
* nor may "Apache" appear in their name without prior written permission of
26+
* the Apache Software Foundation.
27+
*
28+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
29+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
30+
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31+
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
32+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4938
* ====================================================================
50-
*
51-
* This software consists of voluntary contributions made by many
52-
* individuals on behalf of the Apache Software Foundation. For more
53-
* information on the Apache Software Foundation, please see
54-
* <http://www.apache.org/>.
55-
*
56-
*/
39+
*
40+
* This software consists of voluntary contributions made by many individuals
41+
* on behalf of the Apache Software Foundation. For more information on the
42+
* Apache Software Foundation, please see <http://www.apache.org/> .
43+
*
44+
*/
5745

5846
package org.apache.commons.codec.digest;
5947

6048
import junit.framework.TestCase;
6149

6250
/**
6351
* Tests Digest methods.
64-
*
52+
*
6553
* @author Dave Dribin
6654
* @author David Graham
55+
* @author Gary Gregory
6756
*/
6857
public class DigestUtilsTest extends TestCase {
6958

70-
public void testMd5Hex() {
71-
// Examples from RFC 1321
72-
assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex(""));
59+
public void testInternalNoSuchAlgorithmException() {
60+
try {
61+
DigestUtils.getDigest("Bogus Bogus");
62+
fail("A RuntimeException should have been thrown.");
63+
} catch (RuntimeException e) {
64+
// Expected exception.
65+
}
66+
}
7367

74-
assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a"));
68+
public void testMd5Hex() {
69+
// Examples from RFC 1321
70+
assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex(""));
7571

76-
assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc"));
72+
assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a"));
7773

78-
assertEquals(
79-
"f96b697d7cb7938d525a2f31aaf161d0",
80-
DigestUtils.md5Hex("message digest"));
74+
assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc"));
8175

82-
assertEquals(
83-
"c3fcd3d76192e4007dfb496cca67e13b",
84-
DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz"));
76+
assertEquals("f96b697d7cb7938d525a2f31aaf161d0", DigestUtils.md5Hex("message digest"));
8577

86-
assertEquals(
87-
"d174ab98d277d9f5a5611c2c9f419d9f",
88-
DigestUtils.md5Hex(
89-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
90-
+ "abcdefghijklmnopqrstuvwxyz"
91-
+ "0123456789"));
78+
assertEquals("c3fcd3d76192e4007dfb496cca67e13b", DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz"));
9279

93-
assertEquals(
94-
"57edf4a22be3c955ac49da2e2107b67a",
95-
DigestUtils.md5Hex(
96-
"1234567890123456789012345678901234567890"
97-
+ "1234567890123456789012345678901234567890"));
98-
}
80+
assertEquals(
81+
"d174ab98d277d9f5a5611c2c9f419d9f",
82+
DigestUtils.md5Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"));
9983

100-
/**
84+
assertEquals(
85+
"57edf4a22be3c955ac49da2e2107b67a",
86+
DigestUtils.md5Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890"));
87+
}
88+
89+
/**
10190
* An MD5 hash converted to hex should always be 32 characters.
10291
*/
103-
public void testMD5HexLength() {
104-
String hashMe = "this is some string that is longer than 32 characters";
105-
String hash = DigestUtils.md5Hex(hashMe.getBytes());
106-
assertEquals(32, hash.length());
92+
public void testMD5HexLength() {
93+
String hashMe = "this is some string that is longer than 32 characters";
94+
String hash = DigestUtils.md5Hex(hashMe.getBytes());
95+
assertEquals(32, hash.length());
10796

108-
hashMe = "length < 32";
109-
hash = DigestUtils.md5Hex(hashMe.getBytes());
110-
assertEquals(32, hash.length());
111-
}
97+
hashMe = "length < 32";
98+
hash = DigestUtils.md5Hex(hashMe.getBytes());
99+
assertEquals(32, hash.length());
100+
}
112101

113-
/**
102+
/**
114103
* An MD5 hash should always be a 16 element byte[].
115104
*/
116-
public void testMD5Length() {
117-
String hashMe = "this is some string that is longer than 16 characters";
118-
byte[] hash = DigestUtils.md5(hashMe.getBytes());
119-
assertEquals(16, hash.length);
120-
121-
hashMe = "length < 16";
122-
hash = DigestUtils.md5(hashMe.getBytes());
123-
assertEquals(16, hash.length);
124-
}
125-
126-
public void testShaHex() {
127-
// Examples from FIPS 180-1
128-
assertEquals(
129-
"a9993e364706816aba3e25717850c26c9cd0d89d",
130-
DigestUtils.shaHex("abc"));
105+
public void testMD5Length() {
106+
String hashMe = "this is some string that is longer than 16 characters";
107+
byte[] hash = DigestUtils.md5(hashMe.getBytes());
108+
assertEquals(16, hash.length);
109+
110+
hashMe = "length < 16";
111+
hash = DigestUtils.md5(hashMe.getBytes());
112+
assertEquals(16, hash.length);
113+
}
114+
115+
public void testShaHex() {
116+
// Examples from FIPS 180-1
117+
assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc"));
118+
119+
assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc".getBytes()));
131120

132121
assertEquals(
133-
"a9993e364706816aba3e25717850c26c9cd0d89d",
134-
DigestUtils.shaHex("abc".getBytes()));
135-
136-
assertEquals(
137-
"84983e441c3bd26ebaae4aa1f95129e5e54670f1",
138-
DigestUtils.shaHex(
139-
"abcdbcdecdefdefgefghfghighij"
140-
+ "hijkijkljklmklmnlmnomnopnopq"));
141-
}
142-
122+
"84983e441c3bd26ebaae4aa1f95129e5e54670f1",
123+
DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
124+
}
125+
143126
}

0 commit comments

Comments
 (0)