KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jmi.reflect.*;
38 import javax.jmi.model.*;
39
40 /**
41  * Multivalued index test case.
42  */

43 public class MultivaluedIndexTest extends MDRTestCase {
44
45     // number of classes that are generated to test (Btree)ListIterator functionality
46
private static final int MAX = 2000;
47     
48     private long counter = 0;
49     private Random random = new Random (0);
50         
51     public MultivaluedIndexTest(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 (MultivaluedIndexTest.class);
62         
63         TestSetup setup = new TestSetup (suite) {
64             public void setUp () {
65 // org.netbeans.mdr.handlers.BaseObjectHandler.setDefaultClassLoader (this.getClass ().getClassLoader ());
66
}
67             public void tearDown () {
68             }
69         };
70         return setup;
71     }
72
73     public void test () {
74         MofClass classC = null, classN = null, classP = null;
75         MofException excF = null, excR = null;
76         Iterator iter = modelPackage.getMofClass ().refAllOfType ().iterator ();
77         while (iter.hasNext ()) {
78             MofClass mofClass = (MofClass) iter.next ();
79             String JavaDoc name = mofClass.getName ();
80             if (name.equals ("Namespace"))
81                 classN = mofClass;
82             if (name.equals ("Class"))
83                 classC = mofClass;
84             if (name.equals ("Package"))
85                 classP = mofClass;
86         }
87         iter = modelPackage.getMofException ().refAllOfType ().iterator ();
88         while (iter.hasNext ()) {
89             MofException mofExc = (MofException) iter.next ();
90             String JavaDoc name = mofExc.getName ();
91             if (name.equals ("NameNotFound"))
92                 excF = mofExc;
93             if (name.equals ("NameNotResolved"))
94                 excR = mofExc;
95         }
96         
97         iter = classC.getContents ().iterator ();
98         excF.setContainer (classP);
99         iter.next ();
100         excF.setContainer (classN);
101         while (iter.hasNext ())
102             iter.next ();
103         
104         try {
105             iter = classC.getContents ().iterator ();
106             excF.setContainer (classC);
107             excF.setContainer (classN);
108             iter.next ();
109             // CME should be thrown by iter.next () ...
110
fail ("ConcurrentModificationException has not been thrown.");
111         } catch (ConcurrentModificationException e) {
112         }
113         
114         iter = classC.getContents ().iterator ();
115         while (iter.hasNext ())
116             iter.next ();
117         
118         Contains assoc = modelPackage.getContains ();
119         List list = (List) assoc.refQuery ("container", classC);
120         list = null;
121         System.gc ();
122         list = (List) assoc.refQuery ("container", classC);
123         
124         ListIterator listIter = list.listIterator ();
125         Object JavaDoc obj = listIter.next ();
126         listIter.remove ();
127         Object JavaDoc obj2 = listIter.next ();
128         listIter.add (obj);
129         Object JavaDoc obj3 = listIter.next ();
130     }
131     
132     public void test2 () {
133         try {
134             repository.beginTrans (true);
135             int x;
136             MofClass main = generateClass ();
137             for (x = 0; x < MAX; x++)
138                 generateClass ().setContainer (main);
139             Contains assoc = modelPackage.getContains ();
140             List list = (List) assoc.refQuery ("container", main);
141             ListIterator iter = list.listIterator ();
142             
143             repository.endTrans ();
144             repository.beginTrans (true);
145             
146             // test adding to and removing from the start of the list
147
for (x = 0; x < MAX/10; x++)
148                 iter.add (generateClass ());
149             for (x = 0; x < MAX/10; x++) {
150                 iter.next ();
151                 iter.remove ();
152             }
153                 
154             repository.endTrans ();
155             repository.beginTrans (true);
156             
157             iter = list.listIterator ();
158             Random random = new Random ();
159             int size = MAX;
160             for (x = 0; x < MAX/2; x++)
161                 iter.next ();
162             for (x = 0; x < 4*MAX; x++) {
163                 int opCode = random.nextInt (5);
164                 switch (opCode) {
165                     case 0:
166                         if (iter.hasNext ())
167                             iter.next ();
168                     break;
169                     case 1:
170                         if (iter.hasPrevious ())
171                             iter.previous ();
172                     break;
173                     case 2:
174                         if (iter.hasNext ()) {
175                             iter.next ();
176                             iter.remove ();
177                             size--;
178                         } else if (iter.hasPrevious ()) {
179                             iter.previous ();
180                             iter.remove ();
181                             size--;
182                         }
183                     break;
184                     case 3:
185                         if (iter.hasNext ()) {
186                             iter.next ();
187                             iter.set (generateClass ());
188                         } else if (iter.hasPrevious ()) {
189                             iter.previous ();
190                             iter.set (generateClass ());
191                         }
192                     break;
193                     case 4:
194                         iter.add (generateClass ());
195                         size++;
196                     break;
197                 } // switch
198
} // for
199
if (list.size () != size)
200                 fail ("Counted list size does not match a value returned by size () method.");
201         } catch (Exception JavaDoc e) {
202             e.printStackTrace ();
203             fail (e.getMessage ());
204         } finally {
205             repository.endTrans ();
206         }
207     }
208     
209     public void test3 () {
210         try {
211             repository.beginTrans (true);
212             MofClass class_A = generateClass ();
213             MofClass class_B = generateClass ();
214             class_B.setContainer (class_A);
215             for (int x = 0; x < 3; x++)
216                 generateClass ().setContainer (class_B);
217             Contains assoc = modelPackage.getContains ();
218             List list = (List) assoc.refQuery ("container", class_A);
219             printListElements ((List) assoc.refQuery ("container", class_B));
220             printListElements ((List) assoc.refQuery ("containedElement", class_B));
221             Iterator iter = list.iterator ();
222             iter.next ();
223             iter.remove ();
224             printListElements (list);
225             printListElements ((List) assoc.refQuery ("container", class_B));
226             printListElements ((List) assoc.refQuery ("containedElement", class_B));
227             
228             MofClass class_C = generateClass ();
229             for (int x = 0; x < 100; x++)
230                 generateClass().setContainer(class_C);
231             int index = -1;
232             ListIterator liter = class_C.getContents().listIterator();
233             for (int x = 0; x < 90; x++) {
234                 int val = random.nextInt (5);
235                 switch (val) {
236                     case 0:
237                     case 1:
238                         liter.add (generateClass());
239                         index ++;
240                         break;
241                     case 2:
242                     case 3:
243                         liter.next ();
244                         index ++;
245                         break;
246                     case 4:
247                         if (index > 0) {
248                             liter.previous ();
249                             index--;
250                         }
251                 } // switch
252
} // for
253
if (liter.nextIndex() != (index + 1))
254                 fail ();
255             
256         } catch (Exception JavaDoc e) {
257             e.printStackTrace ();
258             fail (e.getMessage ());
259         } finally {
260             repository.endTrans ();
261         }
262     }
263     
264     public void test4 () {
265         try {
266             repository.beginTrans (true);
267             MofClass class_A = generateClass ();
268             MofClass class_B = generateClass ();
269             MofClass class_C = generateClass ();
270             class_B.setContainer (class_A);
271             for (int x = 0; x < 3; x++)
272                 generateClass ().setContainer (class_B);
273             class_C.setContainer (class_B);
274             
275             Contains assoc = modelPackage.getContains ();
276             
277             printListElements ((List) assoc.refQuery ("container", class_B));
278             printListElements ((List) assoc.refQuery ("containedElement", class_B));
279             
280             class_B.refDelete ();
281             
282             /*
283             Contains assoc = modelPackage.getContains ();
284             List list = (List) assoc.refQuery ("container", class_B);
285             printListElements (list);
286             Iterator iter = list.iterator ();
287             while (iter.hasNext ()) {
288                 System.out.println("size: " + list.size ());
289                 iter.next ();
290                 iter.remove ();
291             }
292             System.out.println("size: " + list.size ());
293             printListElements ((List) assoc.refQuery ("container", class_B));
294             System.out.println("-------");
295             */

296         } catch (Exception JavaDoc e) {
297             System.out.println(e);
298             e.printStackTrace ();
299             fail (e.getMessage ());
300         } finally {
301             repository.endTrans ();
302         }
303     }
304             
305     public void test5 () {
306         ModelPackage pkg = (ModelPackage) createExtent(findMofPackage(modelPackage, "Model"), "Test");
307         MofClassClass proxy = pkg.getMofClass ();
308         // create random tree structure (in containment hierarchy) of instances of MofClass
309
MofClass root = generateClass (proxy);
310         int num = proxy.refAllOfClass ().size ();
311         generateTree (root, 4, proxy);
312         deleteTree (root);
313         if (proxy.refAllOfClass ().size () != 1)
314             fail ();
315         generateTree2 (root, 4, proxy);
316         deleteTree (root);
317         if (proxy.refAllOfClass ().size () != 1)
318             fail ();
319     }
320     
321     private void generateTree (MofClass elem, int depth, MofClassClass proxy) {
322         int sons = 1 + random.nextInt (7);
323         for (int x = 0; x < sons; x++) {
324             MofClass mofClass = generateClass (proxy);
325             mofClass.setContainer (elem);
326             if (depth > 0)
327                 generateTree (mofClass, depth - 1, proxy);
328         }
329     }
330     
331     private void generateTree2 (MofClass elem, int depth, MofClassClass proxy) {
332         int sons = 4 + random.nextInt (5);
333         ListIterator iter = elem.getContents ().listIterator ();
334         for (int x = 0; x < sons; x++) {
335             MofClass mofClass = generateClass (proxy);
336             iter.add (mofClass);
337             if (depth > 0)
338                 generateTree (mofClass, depth - 1, proxy);
339             if (random.nextBoolean()) {
340                 deleteTree (mofClass);
341                 iter.previous ();
342                 iter.remove ();
343                 mofClass.refDelete ();
344             }
345         }
346     }
347     
348     private void deleteTree (MofClass node) {
349         Collection subNodes = node.getContents();
350         int size = subNodes.size();
351         for (Iterator it = subNodes.iterator(); it.hasNext();) {
352             MofClass subNode = (MofClass)it.next();
353             it.remove();
354             deleteTree (subNode);
355             subNode.refDelete();
356         }
357     }
358     
359     private MofClass generateClass (MofClassClass proxy) {
360         return proxy.createMofClass ("name" + counter++, "", true, true, true, VisibilityKindEnum.PUBLIC_VIS, true);
361     }
362     
363     private MofClass generateClass () {
364         MofClassClass proxy = modelPackage.getMofClass ();
365         return proxy.createMofClass ("name" + counter++, "", true, true, true, VisibilityKindEnum.PUBLIC_VIS, true);
366     }
367         
368     private void printListElements (List list) {
369         /*
370         Iterator iter = list.iterator ();
371         System.out.println("list size: " + list.size () + " " + list.getClass ().getName ());
372         while (iter.hasNext ()) {
373             ModelElement element = (ModelElement) iter.next ();
374             System.out.println((element != null ? element.getName () : "null") + " ");
375         }
376         System.out.println("");
377          */

378     }
379     
380 }
381
Popular Tags