source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/TestMD5Hash.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.9 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.io;
20
21import org.apache.hadoop.io.TestWritable;
22import junit.framework.TestCase;
23import java.security.MessageDigest;
24import java.util.Random;
25
26/** Unit tests for MD5Hash. */
27public class TestMD5Hash extends TestCase {
28  public TestMD5Hash(String name) { super(name); }
29
30  private static final Random RANDOM = new Random();
31
32  public static MD5Hash getTestHash() throws Exception {
33    MessageDigest digest = MessageDigest.getInstance("MD5");
34    byte[] buffer = new byte[1024];
35    RANDOM.nextBytes(buffer);
36    digest.update(buffer);
37    return new MD5Hash(digest.digest());
38  }
39
40  protected static byte[] D00 = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
41  protected static byte[] DFF = new byte[] {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; 
42 
43  public void testMD5Hash() throws Exception {
44    MD5Hash md5Hash = getTestHash();
45
46    final MD5Hash md5Hash00
47      = new MD5Hash(D00);
48
49    final MD5Hash md5HashFF
50      = new MD5Hash(DFF);
51   
52    MD5Hash orderedHash = new MD5Hash(new byte[]{1,2,3,4,5,6,7,8,9,10,11,12,
53                                                 13,14,15,16});
54    MD5Hash backwardHash = new MD5Hash(new byte[]{-1,-2,-3,-4,-5,-6,-7,-8,
55                                                  -9,-10,-11,-12, -13, -14,
56                                                  -15,-16});
57    MD5Hash closeHash1 = new MD5Hash(new byte[]{-1,0,0,0,0,0,0,0,
58                                                0,0,0,0,0,0,0,0});
59    MD5Hash closeHash2 = new MD5Hash(new byte[]{-1,1,0,0,0,0,0,0,
60                                                0,0,0,0,0,0,0,0});
61
62    // test i/o
63    TestWritable.testWritable(md5Hash);
64    TestWritable.testWritable(md5Hash00);
65    TestWritable.testWritable(md5HashFF);
66
67    // test equals()
68    assertEquals(md5Hash, md5Hash);
69    assertEquals(md5Hash00, md5Hash00);
70    assertEquals(md5HashFF, md5HashFF);
71
72    // test compareTo()
73    assertTrue(md5Hash.compareTo(md5Hash) == 0);
74    assertTrue(md5Hash00.compareTo(md5Hash) < 0);
75    assertTrue(md5HashFF.compareTo(md5Hash) > 0);
76
77    // test toString and string ctor
78    assertEquals(md5Hash, new MD5Hash(md5Hash.toString()));
79    assertEquals(md5Hash00, new MD5Hash(md5Hash00.toString()));
80    assertEquals(md5HashFF, new MD5Hash(md5HashFF.toString()));
81
82    assertEquals(0x01020304, orderedHash.quarterDigest());
83    assertEquals(0xfffefdfc, backwardHash.quarterDigest());
84   
85    assertEquals(0x0102030405060708L, orderedHash.halfDigest());
86    assertEquals(0xfffefdfcfbfaf9f8L, backwardHash.halfDigest());
87    assertTrue("hash collision", 
88               closeHash1.hashCode() != closeHash2.hashCode());
89     
90    Thread t1 = new Thread() {     
91      public void run() {
92        for (int i = 0; i < 100; i++) {
93          MD5Hash hash = new MD5Hash(DFF);
94          assertEquals(hash, md5HashFF);
95        }       
96      }
97    };
98   
99    Thread t2 = new Thread() {
100      public void run() {
101        for (int i = 0; i < 100; i++) {
102          MD5Hash hash = new MD5Hash(D00);
103          assertEquals(hash, md5Hash00);
104        }
105      }     
106    };
107   
108    t1.start();
109    t2.start();
110    t1.join();
111    t2.join();
112   
113  }
114       
115}
Note: See TracBrowser for help on using the repository browser.