KickJava   Java API By Example, From Geeks To Geeks.

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


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.xmi.*;
30 import org.netbeans.api.xmi.sax.*;
31 import org.openide.util.Lookup;
32
33 import org.netbeans.mdr.util.*;
34 import org.netbeans.mdr.NBMDRepositoryImpl;
35 import org.netbeans.lib.jmi.xmi.*;
36 import org.netbeans.lib.jmi.mapping.*;
37
38 import org.xml.sax.*;
39
40 import javax.jmi.reflect.*;
41 import javax.jmi.model.*;
42
43 import text.*;
44
45 /**
46  * Test case verifying functionality of XmiReader and XmiWriter.
47  */

48 public class AdvancedXMITest extends MDRTestCase {
49
50     public static final int N = 3;
51     public static String JavaDoc [] name = new String JavaDoc [N];
52     public static File [] file = new File [N];
53     
54     static {
55         name [0] = "temp.xml";
56         name [1] = "extFile_A.xml";
57         name [2] = "extFile_B.xml";
58         
59         for (int x = 0; x < N; x++) {
60             file [x] = getFile (name [x]);
61         }
62     }
63     
64     public AdvancedXMITest(String JavaDoc testName) {
65         super (testName);
66     }
67     
68     public static void main (String JavaDoc[] args) {
69         junit.textui.TestRunner.run (suite ());
70     }
71     
72     public static Test suite () {
73         TestSuite suite = new TestSuite ();
74         suite.addTestSuite (AdvancedXMITest.class);
75         
76         TestSetup setup = new TestSetup (suite) {
77             public void setUp () {
78             }
79             public void tearDown () {
80             }
81         };
82         return setup;
83     }
84
85     // **************************************************************************
86

87     public void testEncoding () {
88         RefPackage pkg = instantiateModel ("Text.xml", "Text", "EncodingPkg_1");
89         new RandomDataGenerator ().generate (pkg, 0, 6);
90         
91         Lookup lookup = Lookup.getDefault ();
92         
93         XMIWriter writer = XMIWriterFactory.getDefault ().createXMIWriter ();
94         XMIReader reader = XMIReaderFactory.getDefault ().createXMIReader ();
95         writer.getConfiguration().setEncoding("UTF-8");
96
97         final String JavaDoc name = "test_encod.xml";
98         try {
99             OutputStream os = new FileOutputStream (getFile(name));
100             repository.beginTrans (true);
101             writer.write (os, name, pkg, null);
102             os.flush ();
103             os.close ();
104         } catch (Exception JavaDoc e) {
105             e.printStackTrace();
106             fail (e.getMessage ());
107         } finally {
108             repository.endTrans ();
109         }
110         
111         RefPackage pkg_2 = instantiateModel ("Text.xml", "Text", "EncodingPkg_2");
112         try {
113             repository.beginTrans (true);
114             reader.read (getFile (name).toURL ().toExternalForm (), pkg_2);
115         } catch (Exception JavaDoc e) {
116             fail (e.getMessage ());
117         } finally {
118             repository.endTrans ();
119         }
120         
121         compareData (pkg, pkg_2, new HashSet ());
122         deleteModel (pkg);
123         
124     }
125     
126     // **************************************************************************
127

128     public void testConsumerProducer () {
129         RefPackage pkg = instantiateModel ("Java.xml", "Core", "ConsumerPkg_1");
130         new RandomDataGenerator ().generate (pkg, 122, 8);
131         RefPackage pkg_2 = instantiateModel ("Java.xml", "Core", "ConsumerPkg_2");
132         
133         Lookup lookup = Lookup.getDefault ();
134         
135         XMIProducer producer = XMIProducerFactory.getDefault ().createXMIProducer ();
136         XMIConsumer consumer = XMIConsumerFactory.getDefault ().createXMIConsumer ();
137         
138         producer.setSource (pkg);
139         producer.setContentHandler (consumer);
140         consumer.setExtent (pkg_2);
141         
142         try {
143             producer.parse ((String JavaDoc) null);
144         } catch (Exception JavaDoc e) {
145             fail (e.getMessage ());
146         }
147         
148         compareData (pkg, pkg_2, new HashSet ());
149         
150         deleteModel (pkg);
151         deleteModel (pkg_2);
152     }
153     
154     // **************************************************************************
155

156     public void testReferenceProvider () {
157         RefPackage pkg = instantiateModel ("Java.xml", "Core", "Provider_1");
158         new RandomDataGenerator ().generate (pkg, 0, 7);
159         
160         Lookup lookup = Lookup.getDefault ();
161         
162         XMIWriter writer = XMIWriterFactory.getDefault ().createXMIWriter ();
163         XMIReader reader = XMIReaderFactory.getDefault ().createXMIReader ();
164         writer.getConfiguration().setReferenceProvider(new Provider ());
165         
166         for (int x = 0; x < N; x++) {
167             try {
168                 OutputStream os = new FileOutputStream (file [x]);
169                 repository.beginTrans (true);
170                 writer.write (os, name [x], pkg, null);
171                 os.flush ();
172                 os.close ();
173             } catch (Exception JavaDoc e) {
174                 e.printStackTrace();
175                 fail (e.getMessage ());
176             } finally {
177                 repository.endTrans ();
178             }
179         }
180         
181         RefPackage pkg_2 = instantiateModel ("Java.xml", "Core", "Provider_2");
182         try {
183             repository.beginTrans (true);
184             reader.read (file[0].toURL ().toExternalForm (), pkg_2);
185         } catch (Exception JavaDoc e) {
186             fail (e.getMessage ());
187         } finally {
188             repository.endTrans ();
189         }
190         
191         compareData (pkg, pkg_2, new HashSet ());
192     
193         deleteModel (pkg);
194         deleteModel (pkg_2);
195     }
196     
197     // **************************************************************************
198

199     public void testReferenceResolver () {
200         RefPackage pkg = instantiateModel ("Text.xml", "Text", "Resolver_1");
201         new RandomDataGenerator ().generate (pkg, 0, 3);
202         
203         Lookup lookup = Lookup.getDefault ();
204         
205         XMIWriter writer = XMIWriterFactory.getDefault ().createXMIWriter ();
206         XMIReader reader = XMIReaderFactory.getDefault ().createXMIReader ();
207         writer.getConfiguration().setReferenceProvider(new TextProvider ());
208         reader.getConfiguration().setReferenceResolver(new TextResolver ());
209         
210         try {
211             OutputStream os = new FileOutputStream (getFile("textExp.xml"));
212             repository.beginTrans (true);
213             writer.write (os, "textExp.xml", pkg, null);
214             os.flush ();
215             os.close ();
216         } catch (Exception JavaDoc e) {
217             e.printStackTrace();
218             fail (e.getMessage ());
219         } finally {
220             repository.endTrans ();
221         }
222         
223         try {
224             OutputStream os = new FileOutputStream (getFile("words.xml"));
225             repository.beginTrans (true);
226             writer.write (os, "words.xml", pkg, null);
227             os.flush ();
228             os.close ();
229         } catch (Exception JavaDoc e) {
230             e.printStackTrace();
231             fail (e.getMessage ());
232         } finally {
233             repository.endTrans ();
234         }
235         
236         File file = getFile ("textExp.xml");
237         RefPackage pkg_2 = instantiateModel ("Text.xml", "Text", "Resolver_2");
238         try {
239             repository.beginTrans (true);
240             reader.read (file.toURL ().toExternalForm (), pkg_2);
241         } catch (Exception JavaDoc e) {
242             fail (e.getMessage ());
243         } finally {
244             repository.endTrans ();
245         }
246         
247         // compareData (pkg, pkg_2, new HashSet ());
248
deleteModel (pkg);
249         deleteModel (pkg_2);
250     }
251
252     // **************************************************************************
253

254     public void testHeaderProvider () {
255         RefPackage pkg = instantiateModel ("Java.xml", "Core", "HeaderProvider_1");
256         new RandomDataGenerator ().generate (pkg, 0, 3);
257         
258         Lookup lookup = Lookup.getDefault ();
259         
260         XMIWriter writer = XMIWriterFactory.getDefault ().createXMIWriter ();
261         XMIReader reader = XMIReaderFactory.getDefault ().createXMIReader ();
262         ((OutputConfig)writer.getConfiguration()).setHeaderProvider(new HeaderProvider ());
263         ((InputConfig)reader.getConfiguration()).setHeaderConsumer(new HeaderConsumer ());
264                 
265         try {
266             OutputStream os = new FileOutputStream (getFile("headerTest.xml"));
267             repository.beginTrans (true);
268             writer.write (os, null, pkg, null);
269             os.flush ();
270             os.close ();
271         } catch (Exception JavaDoc e) {
272             e.printStackTrace();
273             fail (e.getMessage ());
274         } finally {
275             repository.endTrans ();
276         }
277         
278         File file = getFile ("headerTest.xml");
279         RefPackage pkg_2 = instantiateModel ("Java.xml", "Core", "HeaderProvider_2");
280         try {
281             repository.beginTrans (true);
282             reader.read (file.toURL ().toExternalForm (), pkg_2);
283         } catch (Exception JavaDoc e) {
284             fail (e.getMessage ());
285         } finally {
286             repository.endTrans ();
287         }
288
289         compareData (pkg, pkg_2, new HashSet ());
290         deleteModel (pkg);
291         deleteModel (pkg_2);
292         
293     }
294     
295     // **************************************************************************
296

297     /**
298      * Creates extent for a metamodel specified by an XMI document.
299      *
300      * @param docName XMI document name
301      * @param mainPackageName name of a MofPackage (included in XMI document) to instantiate
302      *
303      * @return created extent
304      */

305     private RefPackage instantiateModel (String JavaDoc docName, String JavaDoc mainPackageName, String JavaDoc metaPkgName) {
306         ModelPackage pkg = loadMOFModel (docName, metaPkgName);
307         RefPackage result = createExtent (
308             findMofPackage (pkg, mainPackageName),
309             metaPkgName + "_"
310         );
311         return result;
312     }
313     
314     private void deleteModel (RefPackage pkg) {
315         RefPackage meta = pkg.refMetaObject ().refOutermostPackage ();
316         pkg.refDelete ();
317         meta.refDelete ();
318     }
319     
320     /**
321      * Compares instances counts in packages, fails if there is a proxy with different number
322      * of instantiated objects in each package.
323      */

324     private void compareData (RefPackage pkg_1, RefPackage pkg_2, Set checkedPackages) {
325         Set set = new HashSet ();
326         checkedPackages.add (pkg_1);
327         Iterator iter = pkg_1.refAllClasses ().iterator ();
328         while (iter.hasNext ()) {
329             RefClass proxy = (RefClass) iter.next ();
330             ModelElement meta = (ModelElement) proxy.refMetaObject ();
331             int count = proxy.refAllOfClass ().size ();
332             int count2 = pkg_2.refClass (meta).refAllOfClass ().size ();
333             if (count != count2)
334                 fail ("Number of instances does not match: " + meta.getName () + " " + count + " " + count2);
335         }
336         iter = pkg_1.refAllAssociations ().iterator ();
337         while (iter.hasNext ()) {
338             RefAssociation proxy = (RefAssociation) iter.next ();
339             ModelElement meta = (ModelElement) proxy.refMetaObject ();
340             int count = proxy.refAllLinks ().size ();
341             int count2 = pkg_2.refAssociation (meta).refAllLinks ().size ();
342             if (count != count2)
343                 fail ("Number of links does not match: " + meta.getName () + " " + count + " " + count2);
344         }
345         iter = pkg_1.refAllPackages ().iterator ();
346         while (iter.hasNext ()) {
347             RefPackage p1 = (RefPackage) iter.next ();
348             ModelElement meta = (ModelElement) p1.refMetaObject ();
349             RefPackage p2 = pkg_2.refPackage (meta);
350             compareData (p1, p2, checkedPackages);
351         }
352     }
353     
354     // Providers & Resolvers ....................................................
355

356     public static class Provider implements XMIReferenceProvider {
357         
358         public XMIReferenceProvider.XMIReference getReference (RefObject obj) {
359             return new XMIReferenceProvider.XMIReference (name [obj.hashCode () % N], obj.refMofId ());
360         }
361         
362     }
363     
364     public static class TextProvider implements XMIReferenceProvider {
365         
366         public XMIReferenceProvider.XMIReference getReference (RefObject obj) {
367             String JavaDoc systemId;
368             String JavaDoc xmiId = obj.refMofId ();
369             if (((MofClass)obj.refMetaObject ()).getName ().equals ("Word"))
370                 systemId = "words.xml";
371             else
372                 systemId = "textExp.xml";
373             return new XMIReferenceProvider.XMIReference (systemId, xmiId);
374         }
375         
376     }
377     
378     public static class TextResolver implements XMIReferenceResolver {
379         
380         public void register(String JavaDoc systemId, String JavaDoc xmiId, RefObject object) {
381         }
382         
383         public void resolve(Client client, RefPackage extent, String JavaDoc systemId, XMIInputConfig configuration, Collection hrefs) {
384             Iterator iter = hrefs.iterator ();
385             while (iter.hasNext ()) {
386                 String JavaDoc href = (String JavaDoc) iter.next ();
387                 WordClass proxy = ((TextPackage) extent).getWord ();
388                 client.resolvedReference (href, proxy.createWord ());
389             } // while
390
}
391     
392     }
393     
394     // Header provider & consumer ...............................................
395

396     public static class HeaderProvider implements XMIHeaderProvider {
397         
398         public void writeHeader (Writer writer) {
399             try {
400                 writer.write("<XMI.documentation>\n\r");
401                 writer.write(" <XMI.exporter>Netbeans XMI Writer</XMI.exporter>\n\r");
402                 writer.write(" <XMI.exporterVersion>1.0</XMI.exporterVersion>\n\r");
403                 writer.write("</XMI.documentation>\n\r");
404             } catch (IOException e) {
405                 e.printStackTrace ();
406                 fail (e.getMessage ());
407             }
408         }
409         
410     }
411     
412     public static class HeaderConsumer implements XMIHeaderConsumer {
413         
414         public void consumeHeader (InputStream stream) {
415             System.out.println("HEADER:");
416             try {
417                 int b = stream.read ();
418                 while (b != -1) {
419                     System.out.print("" + ((char)b));
420                     b = stream.read ();
421                 }
422             } catch (IOException e) {
423             }
424         }
425         
426     }
427     
428 }
429
Popular Tags