KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FederationTest.java
3  *
4  * Created on May 24, 2002, 8:05 PM
5  */

6
7 package org.netbeans.mdr.test;
8
9 import java.io.*;
10 import java.util.*;
11 import java.net.*;
12 import java.text.MessageFormat JavaDoc;
13
14 import junit.extensions.*;
15 import junit.framework.*;
16
17 import org.netbeans.api.mdr.*;
18 import org.openide.util.Lookup;
19
20 import org.netbeans.mdr.util.*;
21 import org.netbeans.mdr.NBMDRepositoryImpl;
22 import org.netbeans.lib.jmi.xmi.*;
23 import org.netbeans.lib.jmi.mapping.*;
24
25 import javax.jmi.reflect.*;
26 import javax.jmi.model.*;
27
28 /**
29  *
30  * @author Tomas Zezula
31  */

32 public class FederationTest extends MDRTestCase {
33     
34     private static final String JavaDoc MODEL_XMI = "component.xml";
35     private static final String JavaDoc MODEL_PKG_NAME = "PartitionTest";
36     private static final String JavaDoc PKG_ELEMENT = "Test";
37     private static final String JavaDoc PKG_NAME = "PartitionExtent-{0}";
38     private static final String JavaDoc PARTITION_NAME = "part{0}";
39     private static final String JavaDoc INSTANCE_NAME = "{0}-ext{1}-id{2}";
40     private static final String JavaDoc STORAGE_FACTORY = "org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory";
41     private static final String JavaDoc SERVICE_NAME = "service";
42     private static final String JavaDoc COMPONENT_NAME = "component";
43     private static final int PART_COUNT = 2;
44     private static final int INSTANCE_COUNT = 100;
45     
46     /** Creates a new instance of FederationTest */
47     public FederationTest(String JavaDoc testName) {
48         super(testName);
49     }
50     
51     public void test() {
52         // Init
53
this.init(MODEL_XMI, MODEL_PKG_NAME);
54         if (!(this.repository instanceof NBMDRepositoryImpl)) {
55             fail("Not a NBMDRepositoryImpl instance");
56         }
57         // 1st stage of test: Mount all partitions
58
String JavaDoc[] storageIds = new String JavaDoc [PART_COUNT];
59         String JavaDoc workDir = System.getProperty("work.dir");
60         for (int i=0; i < PART_COUNT; i++) {
61             Properties params = new Properties();
62             params.put(org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory.STORAGE_FILE_NAME, MessageFormat.format(workDir+File.separator+PARTITION_NAME, new Object JavaDoc[] {new Integer JavaDoc(i)}));
63             storageIds[i] = mount(STORAGE_FACTORY, params);
64         }
65         // 2nd stage of test: Create extents in mounted partitions
66
test.TestPackage[] pkgs = new test.TestPackage [PART_COUNT];
67         for (int i=0; i< storageIds.length; i++) {
68             ModelPackage pkgExtend = (ModelPackage) this.repository.getExtent(MODEL_PKG_NAME);
69             RefObject pkgObj = findMofPackage(pkgExtend, PKG_ELEMENT);
70             try {
71                 pkgs[i] = (test.TestPackage) ((NBMDRepositoryImpl)this.repository).createExtent(MessageFormat.format(PKG_NAME, new Object JavaDoc[] { new Integer JavaDoc(i)}), pkgObj, null, storageIds[i]);
72             }catch (org.netbeans.api.mdr.CreationFailedException cfe) {
73                 fail("Creation of extent failed.");
74             }
75         }
76         
77         // 3rd stage of test: Content manipulation
78
// Create & query test
79
MessageFormat JavaDoc format = new MessageFormat JavaDoc(INSTANCE_NAME);
80         for (int i=0; i< pkgs.length; i++) {
81             for (int j=0; j< INSTANCE_COUNT; j++) {
82                 int instanceType = j % 2;
83                 String JavaDoc name ;
84                 switch (instanceType) {
85                     case 0:
86                         name = COMPONENT_NAME;
87                         break;
88                     case 1:
89                         name = SERVICE_NAME;
90                         break;
91                     default:
92                         name = null;
93                 }
94                 createInstance(pkgs[i],format.format(new Object JavaDoc[] { name, new Integer JavaDoc(i), new Integer JavaDoc(j)}),instanceType);
95             }
96             
97             Collection c = pkgs[i].getComponent().refAllOfClass();
98             if (c.size() != (INSTANCE_COUNT / 2)) {
99                 fail(MessageFormat.format("Extent no: {0} : invalid component instance count",
100                 new Object JavaDoc[] { new Integer JavaDoc(i)}));
101             }
102             for (Iterator it = c.iterator(); it.hasNext(); ) {
103                 test.Component ci = (test.Component) it.next();
104                 try {
105                     Object JavaDoc[] elements = format.parse(ci.getName());
106                     if (!elements[0].equals(COMPONENT_NAME))
107                         fail("Invalid name");
108                 } catch (java.text.ParseException JavaDoc pe) {
109                     fail("Invalid name");
110                 }
111                 
112             }
113             
114             c = pkgs[i].getService().refAllOfClass();
115             if (((INSTANCE_COUNT % 2) == 0 && c.size()!=(INSTANCE_COUNT/2))
116             || ((INSTANCE_COUNT % 2) == 1 && c.size()!=(INSTANCE_COUNT/2 + 1))) {
117                 fail(MessageFormat.format("Extent no: {0} : invalid service instance count",
118                 new Object JavaDoc[] { new Integer JavaDoc(i)}));
119             }
120             for (Iterator it = c.iterator(); it.hasNext();) {
121                 test.Service si = (test.Service) it.next();
122                 try {
123                     Object JavaDoc[] elements = format.parse(si.getName());
124                     if (!elements[0].equals(SERVICE_NAME))
125                         fail("Invalid name");
126                 }catch (java.text.ParseException JavaDoc pe) {
127                     fail("Invalid name");
128                 }
129             }
130         }
131         
132         // Delete & query test
133
if (INSTANCE_COUNT >= 4) {
134             for (int i=0; i< pkgs.length; i++) {
135                 Collection services = pkgs[i].getService().refAllOfClass();
136                 Collection components = pkgs[i].getComponent().refAllOfClass();
137                 int oldSCount = services.size();
138                 int oldCCount = components.size();
139                 Iterator it = services.iterator();
140                 test.Service si = (test.Service) it.next();
141                 it = components.iterator();
142                 test.Component ci = (test.Component) it.next();
143                 si.refDelete();
144                 ci.refDelete();
145                 services = pkgs[i].getService().refAllOfClass();
146                 components = pkgs[i].getComponent().refAllOfClass();
147                 if (services.size() != (oldSCount-1)) {
148                     fail(MessageFormat.format("Extent no: {0} : invalid service post delete count",
149                     new Object JavaDoc[] { new Integer JavaDoc(i)} ));
150                 }
151                 for (Iterator sit = services.iterator(); sit.hasNext();) {
152                     sit.next();
153                 }
154                 if (components.size() != (oldCCount-1)) {
155                     fail(MessageFormat.format("Extent no: {0} : invalid component post delete count",
156                     new Object JavaDoc[] { new Integer JavaDoc(i)} ));
157                 }
158                 for (Iterator cit = components.iterator(); cit.hasNext();) {
159                     cit.next();
160                 }
161             }
162         }
163         else {
164             System.out.println("Skipping delete test");
165         }
166         
167         // Attribute test
168
if (INSTANCE_COUNT >= 6) {
169             for (int i=0; i< pkgs.length; i++) {
170                 test.Component comp = null;
171                 test.Service serv = null;
172                 
173                 Collection c = pkgs[i].getComponent().refAllOfClass();
174                 for (Iterator it = c.iterator(); it.hasNext();) {
175                     test.Component ci = (test.Component) it.next();
176                     if (comp == null)
177                         comp = ci;
178                     ci.getName();
179                     if (ci.getVersion() != 1) {
180                         fail("Invalid value of argument");
181                     }
182                 }
183                 
184                 c = pkgs[i].getService().refAllOfClass();
185                 for (Iterator it = c.iterator(); it.hasNext();) {
186                     test.Service si = (test.Service) it.next();
187                     if (serv == null)
188                         serv = si;
189                     si.getName();
190                 }
191                 
192                 comp.setName("X");
193                 comp.setVersion(0);
194                 serv.setName("Y");
195                 
196                 comp = (test.Component) pkgs[i].getComponent().refAllOfClass().iterator().next();
197                 if (! comp.getName().equals("X"))
198                     fail("Invalid attribute value");
199                 if (comp.getVersion() != 0)
200                     fail("Invalid attribute value");
201                 
202                 serv = (test.Service) pkgs[i].getService().refAllOfClass().iterator().next();
203                 if (!serv.getName().equals("Y"))
204                     fail("Invalid attribute value");
205             }
206         }
207         else {
208             System.out.println("Skipping attribute test");
209         }
210         
211         //4th: Association test
212
if (PART_COUNT >= 2) {
213             test.ServiceClass sc = pkgs[0].getService();
214             test.ComponentClass cc = pkgs[1].getComponent();
215             test.Service serv = sc.createService("AssociationTestService");
216             test.Component comp = cc.createComponent("AssociationTestComponen",1);
217             comp.getProvidedService().add(serv);
218             Collection c = comp.getProvidedService();
219             if (c.size() != 1)
220                 fail("Cross-extent association failed (size): C->S");
221             test.Service stmp = (test.Service) c.iterator().next();
222             if (!stmp.refMofId().equals(serv.refMofId()))
223                 fail("Cross-extent association failed (wrong end): C->S");
224             System.out.println("C->S:"+comp.getName()+"->"+stmp.getName());
225             c = serv.getProvider();
226             if (c.size() != 1)
227                 fail("Cross-extent association failed (size): S->C");
228             test.Component ctemp = (test.Component) c.iterator().next();
229             if (!ctemp.refMofId().equals(comp.refMofId()))
230                 fail("Cross-extent association failed (wrong end): S->C");
231             System.out.println("S->C:"+serv.getName()+"->"+ctemp.getName());
232             test.Provides pa = pkgs[0].getProvides();
233             testAssociation (pa, comp, serv);
234             pa = pkgs[1].getProvides();
235             testAssociation (pa, comp, serv);
236             pa.remove (serv, comp);
237             if (serv.getProvider().size() != 0)
238                 fail ("Cross-extent association failed (size): S->C");
239             if (comp.getProvidedService().size() != 0)
240                 fail ("Cross-extent association failed (size): C->S");
241             pa.add (serv, comp);
242             testAssociation (pa, comp, serv);
243             pa = pkgs[0].getProvides();
244             testAssociation (pa, comp, serv);
245         }
246         else {
247             System.out.println("Skipping cross extent association test");
248         }
249         
250         int extentCount = repository.getExtentNames().length;
251         // 5th stage of test: Unmount partitions
252
for (int i=0; i < PART_COUNT; i++)
253             unmount(storageIds[i]);
254         if (extentCount != (repository.getExtentNames().length + PART_COUNT))
255             fail("Invalid number of extents after unmount");
256         
257         //6th stage of test: Remount partitions
258
extentCount = repository.getExtentNames().length;
259         for (int i=0; i< PART_COUNT; i++) {
260             Properties params = new Properties();
261             params.put(org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory.STORAGE_FILE_NAME, MessageFormat.format(workDir+File.separator+PARTITION_NAME, new Object JavaDoc[] {new Integer JavaDoc(i)}));
262             storageIds[i] = mount(STORAGE_FACTORY, params);
263         }
264         if (extentCount != (repository.getExtentNames().length - PART_COUNT))
265             fail("Invalid number of extents after remount");
266         
267         for (int i=0; i < PART_COUNT; i++) {
268             test.TestPackage pkg = (test.TestPackage) repository.getExtent(MessageFormat.format(PKG_NAME, new Object JavaDoc[] { new Integer JavaDoc(i)}));
269             Collection c = pkg.getService().refAllOfClass();
270             for (Iterator it = c.iterator(); it.hasNext();) {
271                 test.Service si = (test.Service) it.next();
272                 si.getName();
273             }
274             c = pkg.getComponent().refAllOfClass();
275             for (Iterator it = c.iterator(); it.hasNext();) {
276                 test.Component ci = (test.Component) it.next();
277                 ci.getName();
278                 ci.getVersion();
279             }
280         }
281     }
282     
283     private void init(String JavaDoc xmiName, String JavaDoc pkg) {
284         this.loadMOFModel(xmiName, pkg);
285     }
286     
287     
288     private void testAssociation(test.Provides pa, test.Component comp, test.Service serv) {
289         if (! pa.exists(serv,comp))
290             fail("Cross-extent association failed: exists");
291         
292         if (!((RefObject)pa.getProvidedService(comp).iterator().next()).refMofId().equals(serv.refMofId()))
293             fail("Cross-extent association failed: getProvidedService");
294         
295         if (!((RefObject)pa.getProvider(serv).iterator().next()).refMofId().equals(comp.refMofId()))
296             fail("Cross-extent association failed: getProvider");
297     }
298     
299     
300     private RefObject createInstance(test.TestPackage pkg, String JavaDoc name, int instanceType) {
301         switch (instanceType) {
302             case 0:
303                 return pkg.getComponent().createComponent(name,1);
304             case 1:
305                 return pkg.getService().createService(name);
306             default:
307                 return null;
308         }
309         
310     }
311     
312     private String JavaDoc mount(String JavaDoc factoryClass, Properties params) {
313         try {
314             return ((NBMDRepositoryImpl)this.repository).mountStorage(factoryClass, params);
315         }catch (org.netbeans.mdr.util.MountFailedException mfe) {
316             fail ("Mount failed: " + mfe.getRootCase());
317         return null;
318         }
319     }
320     
321     private void unmount(String JavaDoc storageId) {
322         ((NBMDRepositoryImpl)this.repository).unmountStorage(storageId);
323     }
324     
325     public static Test suite() {
326         TestSuite suite = new TestSuite();
327         suite.addTestSuite(FederationTest.class);
328         TestSetup setup = new TestSetup(suite) {
329             public void setUp() {
330             }
331             
332             public void tearDown() {
333             }
334         };
335         return setup;
336     }
337     
338     public static void main(String JavaDoc[] args) {
339         junit.textui.TestRunner.run(suite());
340     }
341 }
342
Popular Tags