KickJava   Java API By Example, From Geeks To Geeks.

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


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 TransactionTest extends MDRTestCase {
44
45     private static final int MAX = 500;
46     private static int counter = 0;
47     private Random random = new Random (0);
48     
49     public TransactionTest(String JavaDoc testName) {
50         super (testName);
51     }
52     
53     public static void main (String JavaDoc[] args) {
54         junit.textui.TestRunner.run (suite ());
55     }
56     
57     public static Test suite () {
58         TestSuite suite = new TestSuite ();
59         suite.addTestSuite (TransactionTest.class);
60         
61         TestSetup setup = new TestSetup (suite) {
62             public void setUp () {
63 // org.netbeans.mdr.handlers.BaseObjectHandler.setDefaultClassLoader (this.getClass ().getClassLoader ());
64
}
65             public void tearDown () {
66             }
67         };
68         return setup;
69     }
70     
71     public void test () {
72         int x;
73         VisibilityKindEnum visKind = null;
74         Contains assoc = modelPackage.getContains ();
75         MofClass mainClass = generateClass ();
76         AttributeClass attribClass = modelPackage.getAttribute ();
77
78         int classes_incr, links_incr;
79         int classes_num = modelPackage.getMofClass ().refAllOfType ().size ();
80         int links_num = assoc.refQuery ("container", mainClass).size ();
81         
82         // start with one rollback ...
83
try {
84             repository.beginTrans (true);
85         } catch (Exception JavaDoc e) {
86             e.printStackTrace ();
87             fail (e.getMessage ());
88         } finally {
89             repository.endTrans (true);
90         }
91         
92         // test rollback of attribute values
93
final int VIS_NUM = 50;
94         Attribute [] attrib = new Attribute [VIS_NUM];
95         VisibilityKind [] vis = new VisibilityKind [VIS_NUM];
96         
97         for (x = 0; x < VIS_NUM; x++) {
98             attrib [x] = attribClass.createAttribute ();
99             int rnd = random.nextInt (3);
100             switch (rnd) {
101                 case 0: visKind = VisibilityKindEnum.PUBLIC_VIS; break;
102                 case 1: visKind = VisibilityKindEnum.PROTECTED_VIS; break;
103                 case 2: visKind = VisibilityKindEnum.PRIVATE_VIS; break;
104             }
105             attrib [x].setVisibility (visKind);
106             vis [x] = visKind;
107         }
108         try {
109             repository.beginTrans (true);
110             for (x = 0; x < VIS_NUM; x++) {
111                 int rnd = random.nextInt (3);
112                 switch (rnd) {
113                     case 0: visKind = VisibilityKindEnum.PUBLIC_VIS; break;
114                     case 1: visKind = VisibilityKindEnum.PROTECTED_VIS; break;
115                     case 2: visKind = VisibilityKindEnum.PRIVATE_VIS; break;
116                 }
117                 attrib [x].setVisibility (visKind);
118             }
119         } catch (Exception JavaDoc e) {
120             e.printStackTrace ();
121             fail (e.getMessage ());
122         } finally {
123             repository.endTrans (true);
124         }
125         // check if all values have been rolled back
126
for (x = 0; x < VIS_NUM; x++) {
127             if (attrib [x].getVisibility () != vis[x])
128                 fail ();
129         }
130         
131         // a sequence of random commits and rollbacks follows ...
132
for (int y = 0; y < 100; y++) {
133             classes_incr = links_incr = 0;
134             boolean rollback = (random.nextInt (3) == 2);
135             try {
136                 repository.beginTrans (true);
137                 int top = 10 + random.nextInt (MAX);
138                 for (x = 0; x < top; x++) {
139                     MofClass obj = generateClass ();
140                     classes_incr++;
141                     if (random.nextInt (3) == 1) {
142                         obj.refDelete ();
143                         classes_incr--;
144                     } else if (random.nextInt (6) == 1) {
145                         obj.setContainer (mainClass);
146                         links_incr++;
147                     }
148                 } // for
149
} catch (Exception JavaDoc e) {
150                 e.printStackTrace ();
151                 fail (e.getMessage ());
152             } finally {
153                 repository.endTrans (rollback);
154             }
155             
156             if (!rollback) {
157                 classes_num += classes_incr;
158                 links_num += links_incr;
159             }
160
161             if (classes_num != modelPackage.getMofClass ().refAllOfType ().size ()) {
162                 fail ("number of instances does not match: " + classes_num);
163             }
164             if (links_num != assoc.refQuery ("container", mainClass).size ()) {
165                 fail ("number of links does not match: " + links_num);
166             }
167             
168         } // for
169

170     }
171     
172     public void testMemoryStorage () {
173         System.setProperty("org.netbeans.mdr.persistence.memoryimpl.persistent", "false");
174         System.setProperty("org.netbeans.mdr.storagemodel.StorageFactoryClassName",
175             "org.netbeans.mdr.persistence.memoryimpl.StorageFactoryImpl");
176         test ();
177     }
178         
179     private MofClass generateClass () {
180         MofClassClass proxy = modelPackage.getMofClass ();
181         return proxy.createMofClass ("name" + counter++, "", true, true, true, VisibilityKindEnum.PUBLIC_VIS, true);
182     }
183     
184     private void printListElements (List list) {
185         Iterator iter = list.iterator ();
186         System.out.println("list size: " + list.size () + " " + list.getClass ().getName ());
187         while (iter.hasNext ()) {
188             ModelElement element = (ModelElement) iter.next ();
189             System.out.println((element != null ? element.getName () : "null") + " ");
190         }
191         System.out.println("");
192     }
193     
194 }
Popular Tags