source: trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/TestModelicaProject.java @ 302

Last change on this file since 302 was 288, checked in by boris, 19 years ago
  • added a 'parent package' field to new package dialog and implemented infrastructure to be animate it among others:
  • IModelicaProject.getPackageRoots() method to fetch all top level package in a project
  • IModelicaProject.getPackage() method to fetch a package by its full name
  • IModelicaProject.findElement() method to fetch project's elements by path
  • modelica source files are now wraped by IModelicaSourceFile
  • plain files are now wraped by IModelicaFile inside a modelica project
  • an abstract wizard page superclass NewTypePage? added wich implemets source and parent package fields which are used in createing new classes/packages
  • some other stuff i forgot about, this kinda got out of hand, smaller commits in the future !
File size: 11.1 KB
Line 
1/*
2 * This file is part of Modelica Development Tooling.
3 *
4 * Copyright (c) 2005, Linköpings universitet, Department of
5 * Computer and Information Science, PELAB
6 *
7 * All rights reserved.
8 *
9 * (The new BSD license, see also
10 * http://www.opensource.org/licenses/bsd-license.php)
11 *
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
15 * met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 *   notice, this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above copyright
21 *   notice, this list of conditions and the following disclaimer in
22 *   the documentation and/or other materials provided with the
23 *   distribution.
24 *
25 * * Neither the name of Linköpings universitet nor the names of its
26 *   contributors may be used to endorse or promote products derived from
27 *   this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42package org.modelica.mdt.test;
43
44import java.util.Collections;
45import java.util.Vector;
46
47import org.eclipse.core.runtime.CoreException;
48import org.eclipse.core.runtime.Path;
49import org.modelica.mdt.core.IModelicaClass;
50import org.modelica.mdt.core.IModelicaElement;
51import org.modelica.mdt.core.IModelicaFile;
52import org.modelica.mdt.core.IModelicaFolder;
53import org.modelica.mdt.core.IModelicaProject;
54import org.modelica.mdt.core.IModelicaSourceFile;
55import org.modelica.mdt.core.compiler.CompilerInstantiationException;
56import org.modelica.mdt.core.compiler.ConnectException;
57import org.modelica.mdt.core.compiler.InvocationError;
58import org.modelica.mdt.core.compiler.UnexpectedReplyException;
59import org.modelica.mdt.test.util.Area51Projects;
60import org.modelica.mdt.test.util.Utility;
61
62import junit.framework.TestCase;
63
64/**
65 * Test various methods in org.modelica.mdt.internal.core.ModelicaProject
66 */
67public class TestModelicaProject extends TestCase
68{
69     /* the test subject, Area51 modelica project */
70    private IModelicaProject project;
71
72    /* the names of the expected root packages in the area51 modelica project */
73    private Vector<String> expectedRootPackages = new Vector<String>(6);
74
75    @Override
76    protected void setUp() throws Exception
77    {
78        Area51Projects.createProjects();
79       
80        project = 
81            Utility.getProject(Area51Projects.MODELICA_PROJECT_NAME);
82       
83        assertTrue(Collections.addAll(expectedRootPackages,
84                "childless_package", "root_package", "hepp", "folder_package",
85                "file_package1", "file_package2"));
86
87    }
88   
89    /**
90     * test (I)ModelciaProject.getPackage() method
91     */
92    public void testGetPackage() 
93        throws ConnectException, CompilerInstantiationException, 
94            UnexpectedReplyException, InvocationError, CoreException
95    {
96        /*
97         * check that find existing packages works
98         */
99        IModelicaClass pkg = project.getPackage("root_package");
100        assertEquals("root_package", pkg.getFullName());
101       
102        pkg = project.getPackage("childless_package");
103        assertEquals("childless_package", pkg.getFullName());
104       
105        pkg = project.getPackage("folder_package");
106        assertEquals("folder_package", pkg.getFullName());
107       
108        pkg = project.getPackage("root_package.sub_package");
109        assertEquals("root_package.sub_package", pkg.getFullName());
110
111        pkg = project.getPackage("root_package.sub_package.leaf_package");
112        assertEquals("root_package.sub_package.leaf_package",
113                pkg.getFullName());
114
115        pkg = project.getPackage("hepp");
116        assertEquals("hepp", pkg.getFullName());
117
118        pkg = project.getPackage("hepp.hopp");
119        assertEquals("hepp.hopp", pkg.getFullName());
120       
121        /*
122         * check that non-existent packages are not found
123         * there is a slight posability that below tests break
124         * if the packages it tries to not found are added to
125         * the Area51 modelica project. even if the changes is one in
126         * a million, still, beware !
127         */
128       
129        pkg = project.getPackage("non_existing_package");
130        assertNull("unexpectedly found a supposedly non-existent package", pkg);
131
132        pkg = project.getPackage("non_existing_package.hej");
133        assertNull("unexpectedly found a supposedly non-existent package", pkg);
134       
135        pkg = project.getPackage("non_existing_package.hej.hopp");
136        assertNull("unexpectedly found a supposedly non-existent package", pkg);
137
138        pkg = project.getPackage("childless_package.a_child");
139        assertNull("unexpectedly found a supposedly non-existent package", pkg);
140
141        pkg = project.getPackage("");
142        assertNull("unexpectedly found a supposedly non-existent package", pkg);
143       
144        pkg = project.getPackage("root_package.sub_package.leaf_package.meep");
145        assertNull("unexpectedly found a supposedly non-existent package", pkg);
146
147    }
148   
149   
150    /**
151     * test (I)ModelciaProject.getRootPackages() method
152     */
153    public void testGetRootPackages() 
154        throws ConnectException, CompilerInstantiationException,
155            UnexpectedReplyException, CoreException
156    {
157
158        for (IModelicaClass pkg : project.getRootPackages())
159        {
160            assertEquals("non root package found", "", pkg.getPrefix());
161            assertEquals("non package returned", IModelicaClass.Type.PACKAGE,
162                    pkg.getRestrictionType());
163            expectedRootPackages.remove(pkg.getElementName());
164        }
165       
166        assertTrue("could not find all expected root packages in the project",
167                expectedRootPackages.isEmpty());
168
169    }
170   
171    /**
172     * test (I)ModelciaProject.findElement() method
173     */
174    public void testFindElement()
175        throws ConnectException, UnexpectedReplyException,
176            CompilerInstantiationException, InvocationError, CoreException
177    {
178        IModelicaElement element =
179            project.findElement(new Path("empty_folder"));
180        assertNotNull(element);
181        assertTrue(element instanceof IModelicaFolder);
182       
183        element = project.findElement(new Path("package_look_alike"));
184        assertNotNull(element);
185        assertTrue(element instanceof IModelicaFolder);
186       
187        element = 
188            project.findElement(new Path("package_look_alike/package.mo"));
189        assertNotNull(element);
190        assertTrue(element instanceof IModelicaSourceFile);
191       
192        element = project.findElement(new Path("packages_folder"));
193        assertNotNull(element);
194        assertTrue(element instanceof IModelicaFolder);
195
196        element = 
197            project.findElement(new Path("packages_folder/folder_package"));
198        assertNotNull(element);
199        assertTrue(element instanceof IModelicaClass);
200        assertEquals(IModelicaClass.Type.PACKAGE, 
201                ((IModelicaClass)element).getRestrictionType());
202
203        element = 
204            project.findElement(new Path("packages_folder/folder_package/" +
205                    "package.mo"));
206        assertNotNull(element);
207        assertTrue(element instanceof IModelicaSourceFile);
208       
209        element = 
210            project.findElement(new Path("packages_folder/file_package.mo"));
211        assertNotNull(element);
212        assertTrue(element instanceof IModelicaSourceFile);
213
214        /* file_package1 should not be found by this path */
215        element = 
216            project.findElement(new Path("packages_folder/file_package.mo/" +
217                    "file_package1"));
218        assertNull(element);
219       
220        /* file_package2 should not be found by this path */
221        element = 
222            project.findElement(new Path("packages_folder/file_package.mo/" +
223                    "file_package2"));
224        assertNull(element);
225       
226
227        element = project.findElement(new Path("root_folder"));
228        assertNotNull(element);
229        assertTrue(element instanceof IModelicaFolder);
230       
231        element = project.findElement(new Path("root_folder/hej_hopp"));
232        assertNotNull(element);
233        assertTrue(element instanceof IModelicaFile);
234       
235        element = project.findElement(new Path("childless_package"));
236        assertNotNull(element);
237        assertTrue(element instanceof IModelicaClass);
238        assertEquals(IModelicaClass.Type.PACKAGE, 
239                ((IModelicaClass)element).getRestrictionType());
240       
241        element = project.findElement(new Path("childless_package/package.mo"));
242        assertNotNull(element);
243        assertTrue(element instanceof IModelicaSourceFile);
244
245        element = project.findElement(new Path("root_package/"+ 
246                "root_package_folder"));
247        assertNotNull(element);
248        assertTrue(element instanceof IModelicaFolder);
249       
250        element = project.findElement(new Path("root_package/sub_package"));
251        assertNotNull(element);
252        assertTrue(element instanceof IModelicaClass);
253        assertEquals(IModelicaClass.Type.PACKAGE, 
254                ((IModelicaClass)element).getRestrictionType());
255
256        element = project.findElement(new Path("root_package/sub_package/" +
257                "leaf_package"));
258        assertNotNull(element);
259        assertTrue(element instanceof IModelicaClass);
260        assertEquals(IModelicaClass.Type.PACKAGE, 
261                ((IModelicaClass)element).getRestrictionType());
262       
263        element = project.findElement(new Path("root_package/sub_package/" +
264                "leaf_package/package.mo"));
265        assertNotNull(element);
266        assertTrue(element instanceof IModelicaSourceFile);
267
268        element = project.findElement(new Path("root_package/package.mo"));
269        assertNotNull(element);
270        assertTrue(element instanceof IModelicaSourceFile);
271
272        element = project.findElement(new Path("root_package/" +
273                "root_package_function.mo"));
274        assertNotNull(element);
275        assertTrue(element instanceof IModelicaSourceFile);
276
277        element = project.findElement(new Path("component_model.mo"));
278        assertNotNull(element);
279        assertTrue(element instanceof IModelicaSourceFile);
280
281        element = project.findElement(new Path("nested_models.mo"));
282        assertNotNull(element);
283        assertTrue(element instanceof IModelicaSourceFile);
284
285        element = project.findElement(new Path("root_model.mo"));
286        assertNotNull(element);
287        assertTrue(element instanceof IModelicaSourceFile);
288
289        element = project.findElement(new Path("empty_file"));
290        assertNotNull(element);
291        assertTrue(element instanceof IModelicaFile);       
292       
293        element = project.findElement(new Path("README.txt"));
294        assertNotNull(element);
295        assertTrue(element instanceof IModelicaFile);
296       
297        /*
298         * check what happens when trying to find non-existing elements
299         *
300         * BEWARE, the test below can break if elements search for are added
301         * to the area51 modelica project
302         */
303       
304        element = project.findElement(new Path("non_existing_file"));
305        assertNull(element);
306
307        element = project.findElement(new Path("non_existing_folder/" +
308                "package.mo"));     
309        assertNull(element);
310       
311        element = project.findElement(new Path("non_existing_folder/" +
312            "non_existing_subfolder"));
313        assertNull(element);
314       
315        element = project.findElement(new Path("non_existing_folder/" +
316            "non_existing_subfolder/package.mo"));
317        assertNull(element);
318
319        element = project.findElement(new Path("a/lot/of/slashes/in/a/path/"));
320        assertNull(element);
321       
322        /* empty string path */
323        element = project.findElement(new Path(""));
324        assertNull(element);
325       
326        /* project name */
327        element = 
328            project.findElement(new Path(Area51Projects.MODELICA_PROJECT_NAME));
329        assertNull(element);
330    }
331}
Note: See TracBrowser for help on using the repository browser.