KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Simple MDR test
3  */

4
5 package org.netbeans.mdr.test;
6
7 import java.io.*;
8 import java.util.*;
9 import java.beans.*;
10
11 import junit.framework.*;
12 import junit.extensions.*;
13
14 import org.netbeans.api.mdr.*;
15 import org.openide.util.Lookup;
16 import org.openide.filesystems.*;
17
18 import org.netbeans.mdr.util.*;
19 import org.netbeans.mdr.NBMDRepositoryImpl;
20 import org.netbeans.lib.jmi.xmi.*;
21 import org.netbeans.lib.jmi.mapping.*;
22 import org.netbeans.junit.*;
23 import org.netbeans.modules.mdr.FSSFImpl;
24
25 import javax.jmi.model.ModelPackage;
26 import javax.jmi.reflect.*;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28
29 public class MDRGenerationTest extends NbTestCase implements MDRTestConstants {
30     
31     private MDRepository repository = null;
32     private String JavaDoc work_dir = null;
33     private String JavaDoc file_sep = null;
34     
35     public MDRGenerationTest(String JavaDoc testName) {
36         super(testName);
37     }
38     
39     public static void main(String JavaDoc[] args) {
40         junit.textui.TestRunner.run(suite());
41     }
42     
43     public static NbTest suite() {
44         
45         NbTestSuite suite = new NbTestSuite();
46         suite.addTestSuite(MDRGenerationTest.class);
47         
48         NbTestSetup setup = new NbTestSetup(suite) {
49             public void setUp() {
50                 org.netbeans.mdr.handlers.BaseObjectHandler.setDefaultClassLoader(
51                 this.getClass().getClassLoader() );
52             }
53             public void tearDown() {
54                 System.out.println("Saving MDR data ...");
55                 NBMDRepositoryImpl.saveAll();
56             }
57         };
58         
59         return setup;
60         
61     }
62     
63     public void testGenIntfsForTestModel_1() {
64         loadModel(TEST_MODEL, "data/testmodel_1.xml");
65         generateInterfaces(TEST_MODEL);
66     }
67     
68     public void testGenIntfsForTestModel_2() {
69         loadModel(TEST_MODEL2, "data/testmodel_2.xml");
70         generateInterfaces(TEST_MODEL2);
71     }
72     
73     public void testGenIntfsForFilesystemsModel() {
74         loadModel(FILESYSTEMS_MODEL, "data/filesystems.xml");
75         generateInterfaces(FILESYSTEMS_MODEL);
76     }
77         
78     
79     public void testGenIntfsForJavaModel() {
80         loadModel(FILESYSTEMS_MODEL, "data/java.xml");
81         generateInterfaces(JAVA_MODEL);
82     }
83     
84     private void generateInterfaces(String JavaDoc pkg_name) {
85         
86         FileSystem targetFS = new LocalFileSystem();
87         File intf_dir = null;
88         
89         try {
90             intf_dir = new File(work_dir + file_sep + "interfaces");
91             intf_dir.mkdir();
92             ((LocalFileSystem) targetFS).setRootDirectory(intf_dir);
93         } catch (PropertyVetoException e) {
94             fail("Unable to create JavaDataObject!\n" + e);
95         } catch (IOException e) {
96             fail("Unable to create JavaDataObject!\n" + e);
97         }
98         
99         ModelPackage pkg = getPackage(pkg_name);
100         JMIMapper jmi_mapper = (JMIMapper) Lookup.getDefault().lookup(JMIMapper.class);
101         
102         assertNotNull(jmi_mapper);
103         System.out.println("MDRGenerationTest - generating interfaces for package: " + pkg);
104         try {
105             jmi_mapper.generate(new FSSFImpl(targetFS), pkg);
106         } catch (java.io.IOException JavaDoc ioe) {
107             fail(ioe.toString());
108         }
109         
110     }
111     
112     private ModelPackage getPackage(String JavaDoc name) {
113         ModelPackage vmp = null;
114         try {
115             vmp = (ModelPackage) repository.getExtent(name);
116         } catch (Exception JavaDoc e) {
117             fail("Model not loaded, name not resolved.");
118         }
119         return vmp;
120     }
121     
122     private void loadModel(String JavaDoc name, String JavaDoc xmi_location) {
123         ModelPackage mp = null;
124         mp = (ModelPackage) repository.getExtent(name);
125         if ( mp == null ) {
126             try {
127                 ModelPackage model = (ModelPackage) repository.createExtent(name);
128                 XMIReader xmiReaderTest = XMIReader.getDefault();
129                 xmiReaderTest.read(MDRLoadModelTest.class.getResource(xmi_location).toExternalForm(), new RefPackage[] { model } );
130             } catch (Exception JavaDoc exc) {
131                 fail(exc.toString());
132             }
133         } else {
134             // Model is probably loaded
135
}
136     }
137     
138     protected void setUp() {
139         
140         work_dir = System.getProperty("work.dir");
141         file_sep = System.getProperty("file.separator");
142         
143         // properties will be set only if running outside of IDE
144
if (System.getProperty("org.openide.version") == null) {
145             System.setProperty("org.netbeans.mdr.persistence.Dir", work_dir + file_sep + "test_repository_1");
146             System.setProperty("org.netbeans.mdr.storagemodel.StorageFactoryClassName",
147             "org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory");
148             System.setProperty("org.openide.util.Lookup", "org.netbeans.mdr.test.MDRTestLookup");
149             System.setProperty("org.netbeans.mdr.LoadStorage", "true");
150             System.setProperty("org.netbeans.mdr.SaveStorageOnExit", "true");
151         }
152         
153         repository = MDRManager.getDefault().getDefaultRepository();
154         if (repository == null) {
155             fail("Repository not found!");
156         } else {
157             // start up the repository
158
repository.getExtentNames();
159         }
160         
161     }
162     
163     protected void tearDown() {
164         
165     }
166     
167 }
168
Popular Tags