KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.netbeans.mdr.test;
2
3 /*
4  * Test.java
5  *
6  * Created on June 17, 2002, 7:42 PM
7  */

8 import java.util.*;
9 import org.netbeans.api.mdr.*;
10 import test.*;
11 import javax.jmi.reflect.*;
12 import javax.jmi.model.*;
13 import junit.extensions.*;
14 import junit.framework.*;
15 /**
16  *
17  * @author tz97951
18  */

19 public class TransientTest extends MDRTestCase {
20     
21     private static final int SOFT = 10000;
22     private static final int HARD = 1000000;
23     
24     private static final int SERVICE_COUNT = 1000;
25     private static final int SERVICE_COMPONENT_COUNT = 10;
26     private static final int COMPONENT_COUNT = 10;
27     
28     private static final String JavaDoc XMI_FILE_NAME = "transient.xml";
29     private static final String JavaDoc MODEL_PKG_NAME = "TransientTestModel";
30     private static final String JavaDoc PKG_NAME = "TransientTest";
31     private static final String JavaDoc PKG_ELEMENT = "Test";
32     
33     private TestPackage pkg;
34     
35     /** Creates a new instance of Test */
36     public TransientTest (String JavaDoc testName) {
37         super (testName);
38     }
39     
40     /**
41      * @param args the command line arguments
42      */

43     public void test () {
44         init ();
45     System.out.println ("INIT DONE");
46     try {
47             testTxT (this.pkg);
48             testTxP (this.pkg);
49     }catch (Exception JavaDoc e) {
50         e.printStackTrace (System.out);
51     }
52     }
53     
54     private void init () {
55         try {
56             RefPackage modelPackage = loadMOFModel (XMI_FILE_NAME, MODEL_PKG_NAME);
57             RefObject pkgObj = findMofPackage ((ModelPackage)modelPackage, PKG_ELEMENT);
58             this.pkg = (TestPackage) this.repository.createExtent (PKG_NAME, pkgObj);
59         } catch (Exception JavaDoc e) {
60         e.printStackTrace (System.out);
61             fail (e.toString ());
62         }
63     }
64     
65     private void testTxP (TestPackage pkg) {
66         ServiceClass cls = pkg.getService ();
67         Service[] services = new Service [SERVICE_COMPONENT_COUNT];
68         for (int i = 0; i< services.length; i++)
69             services[i] = pkg.getService().createService ("TPS-"+i);
70         Component[] components = new Component [COMPONENT_COUNT];
71         for (int i = 0; i< components.length; i++)
72             components[i] = pkg.getComponent().createComponent ("Cmp-"+i,1);
73         
74         for (int i=0; i< components.length; i++) {
75             Collection c = components[i].getProvidedService ();
76             for (int j=0; j< services.length; j++)
77                 c.add (services[j]);
78         }
79
80         System.out.print ("Verifying component links ...");
81         System.out.flush ();
82         for (int i=0; i< components.length; i++)
83             verifyComponentServiceLinks (components[i], SERVICE_COMPONENT_COUNT, true);
84         System.out.println("Done");
85         
86         System.out.print ("Verifying service links ...");
87         System.out.flush ();
88         for (int i=0; i< services.length; i++)
89             verifyServiceComponentLinks (services[i], COMPONENT_COUNT, true);
90         System.out.println("Done");
91         
92         
93         
94         for (int i=0; i< services.length; i++) {
95             if (i%2 == 0)
96                 services[i] = null;
97         }
98         
99         free (HARD);
100         System.out.println ("Verifying component links after free ...");
101         for (int i=0; i< components.length; i++)
102             verifyComponentServiceLinks (components[i], SERVICE_COMPONENT_COUNT, false);
103         System.out.println("Done");
104         
105     }
106     
107     private void testTxT (TestPackage pkg) {
108         ServiceClass cls = pkg.getService();
109         boolean sct = ((org.netbeans.mdr.storagemodel.StorableClass)((org.netbeans.mdr.handlers.ClassProxyHandler)cls)._getDelegate()).isTransient();
110         System.out.println("Service Class Transien ? " + sct);
111         if (!sct)
112             fail ("Service class was not recognized as transient.");
113         Service rootService = cls.createService("RootService");
114         Service lastService = rootService;
115         for (int i=0; i< SERVICE_COUNT; i++) {
116             Service service = cls.createService("Service_"+i);
117             pkg.getDelegate().add(lastService, service);
118             lastService = service;
119         }
120         
121         free(HARD);
122         verifyServiceInstances(cls, SERVICE_COUNT+1, true);
123         rootService = null;
124         lastService = null;
125         free(HARD);
126         verifyServiceInstances(cls, SERVICE_COUNT, false);
127     }
128     
129     
130     private void free (int count) {
131         System.out.print ("FREEING...");
132         System.out.flush ();
133         System.gc ();
134         String JavaDoc[] mem = new String JavaDoc [count];
135         for (int i=0; i< count; i++)
136             mem[i] = new String JavaDoc (Integer.toString (i));
137         System.gc ();
138         mem = null;
139         System.gc ();
140         System.out.println("DONE.");
141     }
142     
143     private static void verifyServiceInstances (ServiceClass cls, int c, boolean eq) {
144         int count = 0;
145         for (Iterator it = cls.refAllOfType ().iterator(); it.hasNext ();count++) {
146             ((Service)it.next()).getName();
147         }
148         System.out.print ("Service Count = "+count+" should be ");
149         if (eq) {
150             System.out.print ( "equal to ");
151         }
152         else {
153             System.out.print ("equal or less than ");
154         }
155         System.out.println (c);
156         
157         if (eq && c != count)
158             fail ("Count assertion failed.");
159     }
160     
161     private static void verifyComponentServiceLinks (Component c, int fc, boolean eq) {
162         int count = 0;
163         for (Iterator it = c.getProvidedService ().iterator (); it.hasNext (); count++) {
164             if (it.next()==null)
165                 System.out.println("NULL ENTRY");
166         }
167         System.out.print ("Link Count = "+count+" should be ");
168         if (eq) {
169             System.out.print ( "equal to ");
170         }
171         else {
172             System.out.print ("equal or less than ");
173         }
174         System.out.println (fc);
175         if (eq && fc != count)
176             fail ("Count assertion failed.");
177     }
178     
179     private static void verifyServiceComponentLinks (Service s, int fc, boolean eq) {
180         int count = 0;
181         for (Iterator it = s.getProvider ().iterator (); it.hasNext (); count++) {
182             if (it.next()==null)
183                 System.out.println("NULL ENTRY");
184         }
185         System.out.print ("Link Count = "+count+" should be ");
186         if (eq) {
187             System.out.print ( "equal to ");
188         }
189         else {
190             System.out.print ("equal or less than ");
191         }
192         System.out.println (fc);
193         if (eq && fc != count)
194             fail ("Count assertion failed.");
195     }
196     
197     
198     
199     
200     public static Test suite() {
201         TestSuite suite = new TestSuite();
202         suite.addTestSuite(TransientTest.class);
203         TestSetup setup = new TestSetup(suite) {
204             public void setUp() {
205             }
206             
207             public void tearDown() {
208             }
209         };
210         return setup;
211     }
212     
213     public static void main(String JavaDoc[] args) {
214         junit.textui.TestRunner.run(suite());
215     }
216     
217 }
218
Popular Tags