1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.portletbridge.portlet;
17  
18  import java.net.URI;
19  import java.security.MessageDigest;
20  import java.util.HashSet;
21  import java.util.Random;
22  import java.util.Set;
23  
24  import junit.framework.TestCase;
25  
26  /***
27   * @author JMcCrindle
28   */
29  public class RandomTest extends TestCase {
30  
31      public void testClash() throws Exception {
32          Random random = new Random();
33          Set set = new HashSet();
34          for(int i = 0; i < 10000; i++) {
35              String hex = Integer.toHexString(random.nextInt());
36              if(set.contains(hex)) {
37                  System.out.println(i);
38              } else {
39                  set.add(hex);
40              }
41          }
42      }
43  
44      public void testSubstring() throws Exception {
45          String blah = "blah";
46          assertEquals("ah", blah.substring(2));
47      }
48      
49      public void testURI() throws Exception {
50          URI uri = new URI("http://slashdot.org");
51          assertEquals("", uri.getPath());
52          uri = new URI("http://slashdot.org/");
53          assertEquals("/", uri.getPath());
54      }
55      
56      public void testShift() {
57          int x = 256233124;
58          System.out.println(Integer.toBinaryString(x));
59          do {
60              System.out.println("x=" + Integer.toBinaryString(x));
61              System.out.println("6=" + Integer.toBinaryString(x & 63));
62              System.out.println("b=" + table[(x & 63)]);
63              x >>>= 6;
64          } while(x > 0);
65      }
66      
67      public void testDigest() throws Exception {
68          long start = System.currentTimeMillis();
69          for(int i = 0; i < 100; i++) {
70              MessageDigest messageDigest = MessageDigest.getInstance("MD5");
71              messageDigest.update((i + "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrg" +
72                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
73                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
74                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
75                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
76                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
77                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
78                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
79                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
80                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
81                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
82                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
83                      "testsadfasdfasd gadfhaergadsfgadrheahgafdgasdgaedrgasd" +
84                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
85                      "testsadfasdfasdg adfhaergadsfgadrheahgafdgasdgaedrgasd" +
86                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
87                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
88                      "testsadfasdfasdga dfhaergadsfgadrheahgafdgasdgaedrgasd" +
89                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
90                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
91                      "testsadfasdfasdgad fhaergadsfgadrheahgafdgasdgaedrgasd" +
92                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
93                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
94                      "testsadfasdfasdgadf haergadsfgadrheahgafdgasdgaedrgasd" +
95                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
96                      "testsadfasdfasdgadfh aergadsfgadrheahgafdgasdgaedrgasd" +
97                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
98                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
99                      "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
100                     "testsadfasdfasdgadfh aergadsfgadrheahgafdgasdgaedrgasd" +
101                     "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
102                     "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
103                     "testsadfasdfasdgadfha ergadsfgadrheahgafdgasdgaedrgasd" +
104                     "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
105                     "testsadfasdfasdgadfhae rgadsfgadrheahgafdgasdgaedrgasd" +
106                     "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
107                     "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
108                     "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
109                     "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
110                     "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
111                     "testsadfasdfasdgadfhaergadsfgadrheahgafdgasdgaedrgasd" +
112                     "fasefawe").getBytes());
113             byte[] digest = messageDigest.digest();
114             if(digest != null) {
115                 // get rid of annoying warning
116             }
117         }
118         System.out.println(((System.currentTimeMillis() - start) / 100.0) * 1000);
119     }
120     
121     public void testUniqueMessageDigest() throws Exception {
122         MessageDigest m1 = MessageDigest.getInstance("MD5");
123         MessageDigest m2 = MessageDigest.getInstance("MD5");
124         assertTrue(m1 != m2);
125     }
126     
127     public static void main(String[] args) throws Exception {
128         RandomTest test = new RandomTest();
129         test.testDigest();
130     }
131     
132     private static final char[] table = {
133             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 
134             'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
135             'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 
136             'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '*', '-' 
137     };
138 }