KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
23
24 import junit.framework.*;
25 import junit.extensions.*;
26
27 import org.netbeans.api.mdr.*;
28 import org.openide.util.Lookup;
29
30 import org.netbeans.mdr.NBMDRepositoryImpl;
31 import org.netbeans.lib.jmi.xmi.*;
32 import org.netbeans.junit.*;
33
34 import javax.jmi.model.*;
35 import javax.jmi.model.ModelPackage;
36 import javax.jmi.reflect.*;
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38
39 import java.io.File JavaDoc;
40
41 public class MDRLoadModelTest extends NbTestCase implements MDRTestConstants {
42
43     private MDRepository repository = null;
44     
45     public MDRLoadModelTest(String JavaDoc testName) {
46         super(testName);
47     }
48     
49     public static void main(String JavaDoc[] args) {
50         junit.textui.TestRunner.run(suite());
51     }
52     
53     public static NbTest suite() {
54         
55         NbTestSuite suite = new NbTestSuite();
56         suite.addTestSuite(MDRLoadModelTest.class);
57         
58         NbTestSetup setup = new NbTestSetup(suite) {
59             public void setUp() {
60                 org.netbeans.mdr.handlers.BaseObjectHandler.setDefaultClassLoader(
61                 this.getClass().getClassLoader() );
62             }
63             public void tearDown() {
64                 System.out.println("Saving MDR data ...");
65                 NBMDRepositoryImpl.saveAll();
66             }
67         };
68         
69         return setup;
70         
71     }
72     
73     public void testLoadTestModel_1() {
74         
75         ModelPackage mp = null;
76         
77         try {
78             System.out.println("Loading Test Model ..." );
79             mp = (ModelPackage) repository.getExtent(TEST_MODEL);
80         } catch (Exception JavaDoc e) {
81             // We have to create the model
82
System.out.println("Name not resolved, loading ...");
83         }
84         if (mp == null) {
85             loadModel(TEST_MODEL, "data/testmodel_1.xml");
86         }
87         
88         assertTrue("Model " + TEST_MODEL + " was not loaded!",
89                isModelLoaded(TEST_MODEL));
90         
91     }
92     
93     public void testLoadTestModel_2() {
94         
95         ModelPackage mp = null;
96         
97         try {
98             System.out.println("Loading Inheritance Model ..." );
99             mp = (ModelPackage) repository.getExtent(TEST_MODEL2);
100         } catch (Exception JavaDoc e) {
101             // We have to create the model
102
System.out.println("Name not resolved, loading ...");
103         }
104         if (mp == null) {
105             loadModel(TEST_MODEL2, "data/testmodel_2.xml");
106         }
107         
108         assertTrue("Model " + TEST_MODEL2 + " was not loaded!",
109                isModelLoaded(TEST_MODEL2));
110         
111     }
112     
113     public void testLoadFilesystemsModel() {
114         
115         ModelPackage mp = null;
116         
117         try {
118             System.out.println("Loading Inheritance Model ..." );
119             mp = (ModelPackage) repository.getExtent(FILESYSTEMS_MODEL);
120         } catch (Exception JavaDoc e) {
121             // We have to create the model
122
System.out.println("Name not resolved, loading ...");
123         }
124         if (mp == null) {
125             loadModel(FILESYSTEMS_MODEL, "data/filesystems.xml");
126         }
127         
128         assertTrue("Model " + FILESYSTEMS_MODEL + " was not loaded!",
129                isModelLoaded(FILESYSTEMS_MODEL));
130         
131     }
132     
133     public void testLoadJavaModel() {
134         
135         ModelPackage mp = null;
136         
137         try {
138             System.out.println("Loading Java Model ..." );
139             mp = (ModelPackage)repository.getExtent(JAVA_MODEL);
140         } catch (Exception JavaDoc e) {
141             // We have to create the model
142
System.out.println("Name not resolved, loading ...");
143         }
144         if (mp == null) {
145             loadModel(JAVA_MODEL, "data/java.xml");
146         }
147         
148         assertTrue("Model " + JAVA_MODEL + " was not loaded!",
149                isModelLoaded(JAVA_MODEL));
150         
151     }
152
153     private void loadModel(String JavaDoc name, String JavaDoc xmi_location) {
154         
155         try {
156             ModelPackage model = (ModelPackage) repository.createExtent(name);
157             XMIReader xmiReaderTest = XMIReader.getDefault();
158             xmiReaderTest.read(MDRLoadModelTest.class.getResource(xmi_location).toExternalForm(), new RefPackage[] { model } );
159         } catch (Exception JavaDoc exc) {
160             fail(exc.toString());
161         }
162         
163     }
164     
165     private boolean isModelLoaded(String JavaDoc name) {
166         
167         ModelPackage vmp = null;
168         
169         try {
170             vmp = (ModelPackage) repository.getExtent(name);
171         } catch (Exception JavaDoc e) {
172             fail("Model not loaded, name not resolved.");
173         }
174         if (vmp != null && vmp instanceof ModelPackage) {
175             return true;
176         } else {
177             return false;
178         }
179         
180     }
181     
182     protected void setUp() {
183         
184         String JavaDoc workDir = System.getProperty("work.dir");
185         
186         // properties will be set only if running outside of IDE
187
if (System.getProperty("org.openide.version") == null) {
188             System.setProperty("org.netbeans.mdr.persistence.Dir", workDir + File.separator + "test_repository_1");
189             System.setProperty("org.netbeans.mdr.storagemodel.StorageFactoryClassName",
190             "org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory");
191             System.setProperty("org.openide.util.Lookup", "org.netbeans.mdr.test.MDRTestLookup");
192             System.setProperty("org.netbeans.mdr.SaveStorageOnExit", "true");
193         }
194         
195         repository = MDRManager.getDefault().getDefaultRepository();
196         if (repository == null) {
197             fail("Repository not found!");
198         } else {
199             // start up the repository
200
repository.getExtentNames();
201         }
202         
203     }
204     
205     protected void tearDown() {
206         
207     }
208     
209 }
210
Popular Tags