KickJava   Java API By Example, From Geeks To Geeks.

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


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.storagemodel.*;
34 import org.netbeans.mdr.handlers.*;
35 import org.netbeans.mdr.NBMDRepositoryImpl;
36 import org.netbeans.lib.jmi.xmi.*;
37 import org.netbeans.lib.jmi.mapping.*;
38
39 import javax.jmi.reflect.*;
40 import javax.jmi.model.*;
41
42 import text.*;
43
44 /**
45  * Multivalued index test case.
46  */

47 public class AdditionalIndexTest extends MDRTestCase {
48         
49     private static final int KEY_COUNT = 3;
50     private static final int MAX = 400;
51     private static final String JavaDoc INDEX_NAME = "Indexik";
52     
53     public AdditionalIndexTest(String JavaDoc testName) {
54         super (testName);
55     }
56     
57     public static void main (String JavaDoc[] args) {
58         junit.textui.TestRunner.run (suite ());
59     }
60     
61     public static Test suite () {
62         TestSuite suite = new TestSuite ();
63         suite.addTestSuite (AdditionalIndexTest.class);
64         
65         TestSetup setup = new TestSetup (suite) {
66             public void setUp () {
67             }
68             public void tearDown () {
69             }
70         };
71         return setup;
72     }
73
74     public void test_1 () {
75         Iterator iter;
76         int x, y, z;
77         
78         ModelPackage modelPackage = loadMOFModel ("IndexedModel.xml", "Text");
79         TextPackage pkg = (TextPackage) createExtent (findMofPackage (modelPackage, "Text"), "test");
80         
81         Integer JavaDoc i1 = new Integer JavaDoc (10);
82         Integer JavaDoc i2 = new Integer JavaDoc (222);
83         Integer JavaDoc i3 = new Integer JavaDoc (3000);
84         
85         Sentence s1 = pkg.getSentence().createSentence();
86         Sentence s2 = pkg.getSentence().createSentence();
87         Object JavaDoc [][] keys = {{"a", "bb", "ccc"}, {i1, i2, i3}, {s1, s2, null}};
88         int [][][] counts = new int [KEY_COUNT][KEY_COUNT][KEY_COUNT];
89         
90         for (x = 0; x < KEY_COUNT; x++) {
91             for (y = 0; y < KEY_COUNT; y++) {
92                 for (z = 0; z < KEY_COUNT; z++) {
93                     counts [x][y][z] = 0;
94                 } // for
95
} // for
96
} // for
97

98         // generate instances
99
WordClass proxy = pkg.getWord ();
100         Word2Class proxy2 = pkg.getWord2 ();
101         Random random = new Random (0);
102         for (x = 0; x < MAX; x++) {
103             boolean wordInstance = random.nextBoolean();
104             int [] index = new int [KEY_COUNT];
105             for (y = 0; y < KEY_COUNT; y++) {
106                 index [y] = random.nextInt (KEY_COUNT);
107             } // for
108
counts[index[0]][index[1]][index[2]]++;
109             List params = new LinkedList ();
110             params.add (keys[0][index[0]]);
111             params.add (keys[1][index[1]]);
112             params.add (keys[1][index[1]]);
113             if (wordInstance) {
114                 ((Word) proxy.refCreateInstance (params)).setSentence ((Sentence) keys[2][index[2]]);
115             } else {
116                 params.add (keys[0][index[0]]);
117                 ((Word2) proxy2.refCreateInstance (params)).setSentence ((Sentence) keys[2][index[2]]);
118             }
119         } // for
120

121         MdrStorage storage = ((BaseObjectHandler) proxy)._getDelegate().getMdrStorage ();
122         
123         for (x = 0; x < KEY_COUNT; x++) {
124             for (y = 0; y < KEY_COUNT; y++) {
125                 for (z = 0; z < KEY_COUNT; z++) {
126                     Map map = new HashMap ();
127                     map.put ("color", keys[0][x]);
128                     map.put ("number", keys[1][y]);
129                     map.put ("sentence", keys[2][z]);
130                     int size = storage.queryAdditionalIndex(((BaseObjectHandler)pkg)._getDelegate().getMofId(), INDEX_NAME, map).size();
131                     if (size != counts[x][y][z]) {
132                         System.out.println("Wrong result, indexes: " + x + " " + y + " " + z);
133                         fail();
134                     }
135                 } // for
136
} // for
137
} // for
138

139         pkg.refDelete ();
140         modelPackage.refDelete ();
141     }
142     
143     public void test_2 () {
144         ModelPackage modelPackage = loadMOFModel ("IndexedModel.xml", "Text");
145         TextPackage pkg = (TextPackage) createExtent (findMofPackage (modelPackage, "Text"), "test");
146         
147         int x;
148         int num = 'z' - 'a' + 1;
149         Random random = new Random (0);
150         int [] count = new int [num];
151         Word [] word = new Word [MAX];
152         
153         for (x = 0; x < num; x++)
154             count[x] = 0;
155         
156         WordClass proxy = pkg.getWord ();
157         Word2Class proxy2 = pkg.getWord2 ();
158         MdrStorage storage = ((BaseObjectHandler) proxy)._getDelegate().getMdrStorage ();
159         
160         for (x = 0; x < MAX; x++) {
161             int n = random.nextInt (num);
162             count[n]++;
163             String JavaDoc s = "" + ((char) ('a' + n));
164             if (random.nextBoolean())
165                 word [x] = proxy.createWord (s, 1, 2);
166             else
167                 word [x] = proxy2.createWord2 (s, 1, 2, s);
168         }
169          
170         for (x = 0; x < MAX * 4; x++) {
171             int n = random.nextInt (num);
172             int index = random.nextInt (MAX);
173             int old = word[index].getColor().charAt (0) - 'a';
174             count[old]--;
175             word[index].setColor ("" + ((char) ('a' + n)));
176             count[n]++;
177         }
178         
179         for (x = 0; x < MAX / 3; x++) {
180             int index = random.nextInt (MAX);
181             if (word[index] != null) {
182                 int old = word[index].getColor().charAt (0) - 'a';
183                 count[old]--;
184                 word[index].refDelete();
185                 word[index] = null;
186             }
187         }
188         
189         for (x = 0; x < num; x++) {
190             String JavaDoc s = "" + ((char) ('a' + x));
191             int size = storage.getObjectsFromAdditionalIndex(((BaseObjectHandler)pkg)._getDelegate().getMofId(), "Indexik2", s).size();
192             if (size != count[x]) {
193                 System.out.println("counts do not match: expected " + count[x] + ", found " + size);
194                 fail ();
195             } // if
196
} // for
197

198         pkg.refDelete ();
199         modelPackage.refDelete ();
200     }
201     
202     public void test_3 () {
203         ModelPackage modelPackage = loadMOFModel ("IndexedModel.xml", "Text");
204         TextPackage pkg = (TextPackage) createExtent (findMofPackage (modelPackage, "Text"), "test");
205         
206         Sentence s1 = pkg.getSentence().createSentence();
207         Sentence s2 = pkg.getSentence().createSentence();
208         
209         // generate instances
210
WordClass proxy = pkg.getWord ();
211         Word2Class proxy2 = pkg.getWord2 ();
212         
213         proxy.createWord ("a", 1, 2).setSentence (s1);
214         proxy.createWord ("b", 1, 2);
215         Word w = proxy.createWord ("c", 1, 2);
216         w.setSentence (s2);
217         w.setSentence (s1);
218         w.setSentence (s2);
219         proxy.createWord ("a", 1, 2).setSentence (s1);
220         proxy.createWord ("b", 1, 2).refDelete ();
221         
222         MdrStorage storage = ((BaseObjectHandler) proxy)._getDelegate().getMdrStorage ();
223         int size;
224         
225         size = storage.getObjectsFromAdditionalIndex(((BaseObjectHandler)pkg)._getDelegate().getMofId(), "Indexik3", s1).size();
226         if (size != 2)
227             fail ("found: " + size);
228                 
229         size = storage.getObjectsFromAdditionalIndex(((BaseObjectHandler)pkg)._getDelegate().getMofId(), "Indexik3", s2).size();
230         if (size != 1)
231             fail ("found: " + size);
232                 
233         size = storage.getObjectsFromAdditionalIndex(((BaseObjectHandler)pkg)._getDelegate().getMofId(), "Indexik3", null).size();
234         if (size != 1)
235             fail ("found: " + size);
236         
237         pkg.refDelete ();
238         modelPackage.refDelete ();
239     }
240     
241     public void test_4 () {
242         ModelPackage modelPackage = loadMOFModel ("IndexedModel.xml", "Text");
243         TextPackage pkg = (TextPackage) createExtent (findMofPackage (modelPackage, "Text"), "test");
244         
245         WordClass proxy = pkg.getWord ();
246         Word3Class proxy3 = pkg.getWord3 ();
247         MdrStorage storage = ((BaseObjectHandler) proxy)._getDelegate().getMdrStorage ();
248         
249         Integer JavaDoc i1 = new Integer JavaDoc (10);
250         Integer JavaDoc i2 = new Integer JavaDoc (222);
251         Integer JavaDoc i3 = new Integer JavaDoc (3000);
252         
253         Sentence s1 = pkg.getSentence().createSentence();
254         Sentence s2 = pkg.getSentence().createSentence();
255         Sentence s3 = pkg.getSentence().createSentence();
256                                 
257         Word v1 = proxy.createWord ("a", 1, 2);
258         Word v2 = proxy.createWord ("a", 1, 3);
259         Word v3 = proxy.createWord ("a", 1, 3);
260         
261         List list_1 = new LinkedList ();
262         list_1.add ("a"); list_1.add ("b"); list_1.add ("c");
263         List list_2 = new LinkedList ();
264         list_2.add ("b"); list_2.add ("a"); list_2.add ("c");
265         List list_3 = new LinkedList ();
266         list_3.add ("b"); list_3.add ("c"); list_3.add ("a");
267         
268         Word3 w1, w2, w3, w4, w5;
269         w1 = proxy3.createWord3 ("a", 0, 0, "a", list_1, list_2);
270         w2 = proxy3.createWord3 ("a", 0, 0, "a", list_2, null);
271         w3 = proxy3.createWord3 ("a", 0, 0, "a", list_2, null);
272         w4 = proxy3.createWord3 ("b", 0, 0, "a", null, null);
273         w5 = proxy3.createWord3 ();
274         
275         w5.setColor ("blue");
276         Collection list = w5.getOrderedMulti ();
277         System.out.println(list.getClass().getName());
278         list.add ("a"); list.add ("b"); list.add ("c");
279         list = w5.getUnorderedMulti ();
280         list.add ("c"); list.add ("b"); list.add ("a");
281         
282         HashMap map = new HashMap ();
283         map.put ("orderedMulti", list_1);
284         map.put ("secondEnd", new LinkedList ());
285         map.put ("unorderedMulti", list_3);
286         
287         int size = storage.queryAdditionalIndex(((BaseObjectHandler)pkg)._getDelegate().getMofId(), "Index", map).size();
288         if (size != 2)
289             fail ("found: " + size);
290         
291         list = w2.getRef ();
292         list.add (v1); list.add (v2); list.add (v3);
293         list = w3.getRef ();
294         list.add (v2); list.add (v3); list.add (v1);
295         list = w4.getRef ();
296         list.add (v2); list.add (v3); list.add (v1);
297         w4.refDelete ();
298         ListIterator iter = w2.getOrderedMulti ().listIterator ();
299         iter.next (); iter.remove (); iter.next (); iter.add ("d");
300         iter = w3.getOrderedMulti ().listIterator ();
301         iter.next (); iter.remove (); iter.next (); iter.add ("d");
302         
303         list_1.clear ();
304         list_1.add ("a"); list_1.add ("d"); list_1.add ("c");
305         list_2.clear ();
306         list_2.add (v1); list_2.add (v2); list_2.add (v3);
307         
308         map.clear ();
309         map.put ("orderedMulti", list_1);
310         map.put ("secondEnd", list_2);
311         map.put ("unorderedMulti", new LinkedList ());
312         
313         size = storage.queryAdditionalIndex(((BaseObjectHandler)pkg)._getDelegate().getMofId(), "Index", map).size();
314         if (size != 2)
315             fail ("found: " + size);
316         
317         pkg.refDelete ();
318         modelPackage.refDelete ();
319     }
320     
321 }
322
Popular Tags