KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > test > QueryByPrefixTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdr.test;
21
22 import java.io.*;
23 import java.util.*;
24 import java.net.*;
25
26 import junit.extensions.*;
27 import junit.framework.*;
28
29 import org.netbeans.api.mdr.*;
30 import org.openide.util.Lookup;
31
32 import org.netbeans.mdr.util.*;
33 import org.netbeans.mdr.NBMDRepositoryImpl;
34 import org.netbeans.lib.jmi.xmi.*;
35 import org.netbeans.lib.jmi.mapping.*;
36
37 import org.netbeans.mdr.persistence.*;
38 import org.netbeans.mdr.storagemodel.*;
39 import org.netbeans.mdr.persistence.btreeimpl.btreestorage.*;
40 import org.netbeans.mdr.persistence.memoryimpl.*;
41
42 import org.xml.sax.*;
43
44 import javax.jmi.reflect.*;
45 import javax.jmi.model.*;
46
47 public class QueryByPrefixTest extends MDRTestCase {
48         
49     public static Random random = new Random ();
50     
51     public QueryByPrefixTest(String JavaDoc testName) {
52         super (testName);
53     }
54     
55     public static void main (String JavaDoc[] args) {
56         junit.textui.TestRunner.run (suite ());
57     }
58     
59     public static Test suite () {
60         TestSuite suite = new TestSuite ();
61         suite.addTestSuite (QueryByPrefixTest.class);
62         
63         TestSetup setup = new TestSetup (suite) {
64             public void setUp () {
65             }
66             public void tearDown () {
67             }
68         };
69         return setup;
70     }
71
72     protected void setUp () {
73     }
74     
75     // **************************************************************************
76

77     public void testBtreeStorage () {
78         try {
79             StorageFactory factory = new BtreeFactory ();
80             Storage storage = factory.createStorage (new HashMap ());
81             storage.create (true, new Resolver ());
82
83             doSingleTest (storage);
84             doMultiTest (storage);
85
86             storage.close ();
87         } catch (Exception JavaDoc e) {
88             e.printStackTrace ();
89             fail (e.getMessage ());
90         }
91     }
92     
93     public void testMemoryStorage () {
94         try {
95             StorageFactory factory = new StorageFactoryImpl ();
96             Storage storage = factory.createStorage (new HashMap ());
97             storage.create (true, new Resolver ());
98
99             doSingleTest (storage);
100             doMultiTest (storage);
101
102             storage.close ();
103         } catch (Exception JavaDoc e) {
104             e.printStackTrace ();
105             fail (e.getMessage ());
106         }
107     }
108     
109     public void doSingleTest (Storage storage) {
110         try {
111             SinglevaluedIndex primaryIndex = storage.getPrimaryIndex ();
112             
113             Storage.EntryType entryType = Storage.EntryType.STRING;
114             SinglevaluedIndex index = storage.createSinglevaluedIndex (
115                 "singleIndex", entryType, entryType);
116                         
117             final int MAX = 10000;
118             final int PREFIXES_NUM = 50;
119             final int MAX_2 = 50;
120             
121             final String JavaDoc LAST_ELEMENT = "ZZZZZ";
122             
123             String JavaDoc [] prefixes = new String JavaDoc [PREFIXES_NUM];
124             int [] counts = new int [PREFIXES_NUM];
125             
126             for (int x = 0; x < PREFIXES_NUM; x++) {
127                 counts [x] = 0;
128                 prefixes [x] = randomString ("" + (char)('a' + x), 5, 8);
129             }
130             for (int x = 0; x < MAX; x++) {
131                 String JavaDoc s = randomString ("");
132                 if (index.getIfExists (s) == null) {
133                     index.add (s, s);
134                     for (int y = 0; y < PREFIXES_NUM; y++) {
135                         if (s.startsWith (prefixes [y]))
136                             counts [y]++;
137                     } // for
138
}
139             } // for
140
for (int x = 0; x < PREFIXES_NUM; x++) {
141                 int num = random.nextInt (MAX_2);
142                 for (int y = 0; y < num; y++) {
143                     String JavaDoc s = randomString (prefixes [x]);
144                     if (index.getIfExists (s) == null) {
145                         index.add (s,s);
146                         counts [x] ++;
147                     }
148                 }
149                 if (random.nextBoolean () && (index.getIfExists (prefixes[x]) == null)) {
150                     index.add (prefixes[x], prefixes[x]);
151                     counts [x] ++;
152                 }
153             }
154             
155             
156             for (int x = 0; x < PREFIXES_NUM; x++) {
157                 // query keys starting with the specified prefix ...
158
Collection list = index.queryByKeyPrefix (prefixes [x], primaryIndex);
159                 if (counts [x] != list.size ()) {
160                     fail("#" + x + ", " + prefixes [x] + ": found "
161                         + list.size () + ", expected " + counts [x]);
162                 }
163                 for (Iterator iter = list.iterator (); iter.hasNext ();) {
164                     Map.Entry entry = (Map.Entry) iter.next ();
165                     if (!entry.getValue ().equals (entry.getKey ())) {
166                         fail ();
167                     }
168                 } // for
169
}
170             
171             // should return an empty collection
172
Collection list = index.queryByKeyPrefix ("000", primaryIndex);
173             if (list.size () != 0) {
174                 fail(list.size () + " != 0");
175             }
176             
177             index.add (LAST_ELEMENT, LAST_ELEMENT);
178             list = index.queryByKeyPrefix (LAST_ELEMENT, primaryIndex);
179             if (list.size () != 1) {
180                 fail(list.size () + " != 1");
181             }
182             
183             storage.commitChanges ();
184         } catch (Exception JavaDoc e) {
185             e.printStackTrace ();
186             fail ();
187         }
188     }
189
190     public void doMultiTest (Storage storage) {
191         try {
192             SinglevaluedIndex primaryIndex = storage.getPrimaryIndex ();
193             
194             Storage.EntryType entryType = Storage.EntryType.STRING;
195             MultivaluedIndex index = storage.createMultivaluedIndex (
196                 "multiIndex", entryType, entryType, false);
197                         
198             final int MAX = 10000;
199             final int PREFIXES_NUM = 50;
200             final int MAX_2 = 50;
201             
202             final String JavaDoc LAST_ELEMENT = "ZZZZZ";
203             
204             String JavaDoc [] prefixes = new String JavaDoc [PREFIXES_NUM];
205             int [] counts = new int [PREFIXES_NUM];
206             
207             for (int x = 0; x < PREFIXES_NUM; x++) {
208                 counts [x] = 0;
209                 prefixes [x] = randomString ("" + (char)('a' + x), 5, 8);
210             }
211             for (int x = 0; x < MAX; x++) {
212                 String JavaDoc s = randomString ("");
213                 if ((index.getItems (s) == null) || (index.getItems (s).isEmpty ())) {
214                     index.add (s, s);
215                     for (int y = 0; y < PREFIXES_NUM; y++) {
216                         if (s.startsWith (prefixes [y]))
217                             counts [y]++;
218                     } // for
219
}
220             } // for
221
for (int x = 0; x < PREFIXES_NUM; x++) {
222                 int num = random.nextInt (MAX_2);
223                 for (int y = 0; y < num; y++) {
224                     String JavaDoc s = randomString (prefixes [x]);
225                     if ((index.getItems (s) == null) || (index.getItems (s).isEmpty ())) {
226                         index.add (s,s);
227                         counts [x] ++;
228                     }
229                 }
230                 if (random.nextBoolean () && ((index.getItems (prefixes[x]) == null)
231                     || (index.getItems (prefixes[x]).isEmpty ()))) {
232                     index.add (prefixes[x], prefixes[x]);
233                     counts [x] ++;
234                 }
235             }
236             
237             
238             for (int x = 0; x < PREFIXES_NUM; x++) {
239                 // query keys starting with the specified prefix ...
240
Collection list = index.queryByKeyPrefix (prefixes [x], primaryIndex);
241                 if (counts [x] != list.size ()) {
242                     fail("#" + x + ", " + prefixes [x] + ": found "
243                         + list.size () + ", expected " + counts [x]);
244                 }
245                 for (Iterator iter = list.iterator (); iter.hasNext ();) {
246                     Map.Entry entry = (Map.Entry) iter.next ();
247                     Collection values = (Collection) entry.getValue ();
248                     if (values.size () != 1) {
249                         fail (values.size () + " != 1");
250                     }
251                     Object JavaDoc val = values.iterator ().next ();
252                     if (!val.equals (entry.getKey ())) {
253                         fail ();
254                     }
255                 } // for
256
} // for
257

258             // should return an empty collection
259
Collection list = index.queryByKeyPrefix ("000", primaryIndex);
260             if (list.size () != 0) {
261                 fail(list.size () + " != 0");
262             }
263             
264             index.add (LAST_ELEMENT, LAST_ELEMENT);
265             list = index.queryByKeyPrefix (LAST_ELEMENT, primaryIndex);
266             if (list.size () != 1) {
267                 fail(list.size () + " != 1");
268             }
269             
270             storage.commitChanges ();
271         } catch (Exception JavaDoc e) {
272             e.printStackTrace ();
273             fail ();
274         }
275     }
276     
277     public String JavaDoc randomString (String JavaDoc prefix) {
278         final int minLength = 10;
279         final int maxLength = 20;
280         return randomString (prefix, minLength, maxLength);
281     }
282     
283     public String JavaDoc randomString (String JavaDoc prefix, int minLength, int maxLength) {
284         String JavaDoc res = "";
285         int length = Math.max (minLength, random.nextInt (maxLength + 1));
286         for (int x = prefix.length (); x <= length; x++) {
287             res = res + (char) (random.nextInt ('z' - 'a' + 1) + 'a');
288         }
289         return prefix + res;
290     }
291     
292     // ..........................................................................
293

294     private class Resolver implements ObjectResolver {
295         
296         public Object JavaDoc resolve(String JavaDoc storageID, Object JavaDoc key) {
297             System.out.println("resolve object called");
298             return null;
299         }
300         
301     }
302     
303 }
304
305
Popular Tags