KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > test > OIDTest


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: OIDTest.java,v 1.3 2002/11/08 05:06:26 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store.test;
12
13 import com.triactive.jdo.store.OID;
14 import java.util.Random JavaDoc;
15 import junit.framework.Test;
16 import junit.framework.TestCase;
17
18
19 /**
20  * Tests the functionality of the {@link OID} class.
21  *
22  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
23  * @version $Revision: 1.3 $
24  */

25
26 public class OIDTest extends TestCase
27 {
28     /**
29      * Used by the JUnit framework to construct tests. Normally, programmers
30      * would never explicitly use this constructor.
31      *
32      * @param name Name of the <tt>TestCase</tt>.
33      */

34
35     public OIDTest(String JavaDoc name)
36     {
37         super(name);
38     }
39
40
41     /**
42      * Test the functionality of the {@link OID} class.
43      */

44
45     public void testOID()
46     {
47         int[] x = new int[] { 0, 1, 2, OID.MAX_CLASSID - 2, OID.MAX_CLASSID - 1, OID.MAX_CLASSID };
48         int[] y = new int[] { 0, 1, 2, OID.MAX_OBJIDHI - 2, OID.MAX_OBJIDHI - 1, OID.MAX_OBJIDHI };
49         int[] z = new int[] { 0, 1, 2, OID.MAX_OBJIDLO - 2, OID.MAX_OBJIDLO - 1, OID.MAX_OBJIDLO };
50
51         Random JavaDoc rnd = new Random JavaDoc(0);
52
53         for (int i = 0; i < x.length; ++i)
54         {
55             for (int j = 0; j < y.length; ++j)
56             {
57                 for (int k = 0; k < z.length; ++k)
58                 {
59                     tryOneOID(x[i], y[j], z[k]);
60
61                     tryOneOID(rnd.nextInt(OID.MAX_CLASSID),
62                               rnd.nextInt(OID.MAX_OBJIDHI),
63                               rnd.nextInt(OID.MAX_OBJIDLO));
64                 }
65             }
66         }
67     }
68
69
70     private void tryOneOID(int x, int y, int z)
71     {
72         OID id1 = new OID(x, y, z);
73
74         assertEquals(x, id1.getClassID());
75
76         String JavaDoc s = id1.toString();
77
78         assertEquals(OID.MAX_LENGTH, s.length());
79         assertEquals('-', s.charAt(3));
80         assertEquals('-', s.charAt(10));
81
82         String JavaDoc allowedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-";
83
84         for (int i = 0; i < OID.MAX_LENGTH; ++i)
85             assertTrue(allowedChars.indexOf(s.charAt(i)) >= 0);
86
87         int x1 = Integer.parseInt(s.substring(0, 3), 32);
88         int y1 = Integer.parseInt(s.substring(4, 10), 32);
89         int z1 = Integer.parseInt(s.substring(11), 32);
90
91         assertEquals(x, x1);
92         assertEquals(y, y1);
93         assertEquals(z, z1);
94
95         OID id2 = new OID(s.toLowerCase());
96         OID id3 = new OID(s.toUpperCase());
97         OID id4 = new OID(id1.longValue());
98
99         assertEquals(id1, id2);
100         assertEquals(id2, id3);
101         assertEquals(id3, id4);
102         assertEquals(id4, id1);
103     }
104 }
105
Popular Tags