KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > TestCmsUUID


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/util/TestCmsUUID.java,v $
3  * Date : $Date: 2006/03/27 14:52:42 $
4  * Version: $Revision: 1.11 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util;
33
34 import java.util.Random JavaDoc;
35
36 import junit.framework.TestCase;
37
38 import org.doomdark.uuid.UUID;
39
40 /**
41  * Test case for the UUID generator.<p>
42  *
43  * @author Alexander Kandzior
44  *
45  * @version $Revision: 1.11 $
46  *
47  * @since 6.0.0
48  */

49 public class TestCmsUUID extends TestCase {
50
51     /**
52      * Default JUnit constructor.<p>
53      *
54      * @param arg0 JUnit parameters
55      */

56     public TestCmsUUID(String JavaDoc arg0) {
57
58         super(arg0);
59     }
60
61     /**
62      * Tests UUID generation.<p>
63      *
64      * @throws Exception if the test fails
65      */

66     public void testUUID() throws Exception JavaDoc {
67
68         CmsUUID.init(CmsUUID.getDummyEthernetAddress());
69         CmsUUID id1 = new CmsUUID();
70         CmsUUID id2 = new CmsUUID();
71         CmsUUID id3 = new CmsUUID();
72
73         assertNotSame(id1, id2);
74         assertFalse(id1.equals(id2));
75         assertNotSame(id1, id3);
76         assertFalse(id1.equals(id3));
77         assertNotSame(id3, id2);
78         assertFalse(id3.equals(id2));
79
80         CmsUUID id4 = CmsUUID.getNullUUID();
81         assertTrue(id4.isNullUUID());
82         assertTrue(id4.equals(CmsUUID.getNullUUID()));
83     }
84     
85     /**
86      * Tests the {@link CmsUUID#isValidUUID(String)} method.<p>
87      *
88      * @throws Exception if the test fails
89      */

90     public void testUUIDisValid() throws Exception JavaDoc {
91         
92         assertTrue(CmsUUID.isValidUUID((new CmsUUID()).toString()));
93         assertTrue(CmsUUID.isValidUUID(CmsUUID.getNullUUID().toString()));
94         assertFalse(CmsUUID.isValidUUID(CmsUUID.getNullUUID().toString() + "0"));
95         assertFalse(CmsUUID.isValidUUID("0" + CmsUUID.getNullUUID().toString()));
96         assertFalse(CmsUUID.isValidUUID(null));
97         assertFalse(CmsUUID.isValidUUID(""));
98         assertFalse(CmsUUID.isValidUUID("kaputt"));
99     }
100
101     /**
102      * Tests UUID equals() method.<p>
103      *
104      * @throws Exception if the test fails
105      */

106     public void testUUIDEquals() throws Exception JavaDoc {
107
108         CmsUUID.init(CmsUUID.getDummyEthernetAddress());
109         CmsUUID id1 = new CmsUUID("c300ba5c-01e8-3727-b305-5dcc9ccae1ee");
110         CmsUUID id2 = new CmsUUID("c300ba5c-01e8-3727-b305-5dcc9ccae1ee");
111
112         assertNotSame(id1, id2);
113         assertEquals(id1, id2);
114         CmsUUID id3 = new CmsUUID();
115         assertFalse(id1.equals(id3));
116         assertFalse(id2.equals(id3));
117
118         UUID uid1 = new UUID("c300ba5c-01e8-3727-b305-5dcc9ccae1ee");
119         UUID uid2 = new UUID("c300ba5c-01e8-3727-b305-5dcc9ccae1ee");
120         assertNotSame(uid1, uid2);
121         assertEquals(uid1, uid2);
122
123         // check behaviour of equals method in JUG UUID class
124
UUID uid = new UUID("c300ba5c-01e8-3727-b305-5dcc9ccae1ee");
125         byte[] b1 = uid.asByteArray();
126         byte[] b2 = uid.toByteArray();
127         assertNotSame(b1, b2);
128         byte[] b3 = uid.asByteArray();
129         assertNotSame(b1, b3);
130         byte[] b4 = uid.toByteArray();
131         assertNotSame(b2, b4);
132
133         CmsUUID idNull = new CmsUUID("00000000-0000-0000-0000-000000000000");
134         assertTrue(idNull.isNullUUID());
135         CmsUUID id4 = CmsUUID.getNullUUID();
136         assertEquals(idNull, id4);
137
138         int testSize = 100000;
139         CmsUUID[] ids = new CmsUUID[testSize];
140         for (int i = 0; i < testSize; i++) {
141             ids[i] = new CmsUUID();
142         }
143
144         int hits1 = 0;
145         Random JavaDoc r = new Random JavaDoc();
146         long start = System.currentTimeMillis();
147         for (int i = 0; i < testSize; i++) {
148             int pos1 = r.nextInt(testSize);
149             int pos2 = r.nextInt(testSize);
150             if (ids[pos1].equals(ids[pos2])) {
151                 hits1++;
152             }
153         }
154         long time1 = System.currentTimeMillis() - start;
155         System.out.println("Time for UUID equals() implementation: " + time1);
156
157         //
158
// int hits2 = 0;
159
// start = System.currentTimeMillis();
160
// for (int i=0; i<testSize; i++) {
161
// int pos1 = r.nextInt(testSize);
162
// int pos2 = r.nextInt(testSize);
163
// if (ids[pos1].equals2(ids[pos2])) {
164
// hits2++;
165
// }
166
// }
167
// long time2 = System.currentTimeMillis() - start;
168
// System.out.println("Time 2 for equals(): " + time2);
169
}
170 }
Popular Tags