source: trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/TestTraversingPackages.java @ 121

Last change on this file since 121 was 121, checked in by boris, 19 years ago
  • implemented the backend part for tree view browsing of file system based modelica packages
  • regressions test added for testing hierarchy of file system based packages
  • some API changes
  • general cleanups
File size: 7.4 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.resources.IFile;
48import org.eclipse.core.runtime.CoreException;
49import org.modelica.mdt.core.IModelicaFile;
50import org.modelica.mdt.core.IModelicaFolder;
51import org.modelica.mdt.core.IModelicaPackage;
52import org.modelica.mdt.internal.omcproxy.InitializationException;
53import org.modelica.mdt.test.util.Area51Project;
54import org.modelica.mdt.test.util.Utility;
55
56import junit.framework.TestCase;
57
58/**
59 * Traverses a heirarchy of packages and makes consistensy checks.
60 *
61 * @author Elmir Jagudin
62 */
63public class TestTraversingPackages extends TestCase
64{
65    private IModelicaFolder project_root = null;
66    private Vector<String> root_package_names = new Vector<String>(2);
67   
68    @Override
69    protected void setUp() throws Exception
70    {
71        /*
72         * create the project and fetch the reference to the root folder
73         */
74        Area51Project.createProject();
75       
76        project_root = 
77            Utility.getProject(Area51Project.PROJECT_NAME).getRootFolder();
78       
79        /*
80         * init vector with expected root packages
81         * modelica prespective
82         */             
83        assertTrue(Collections.addAll(root_package_names,
84                "root_package",
85                "childless_root_package"));
86   
87    }
88   
89    public void testTraverse() throws CoreException, InitializationException
90    {
91        IModelicaPackage root_package = null;
92        IModelicaPackage childless_package = null;
93        IModelicaFile root_package_model = null;
94        IModelicaFile root_package_function = null;
95        /* for a temporal reference too package.mo */
96        IModelicaFile package_mo = null;
97        /* root_package/plain_file */
98        IFile root_package_plain_file = null;
99        /* root_package/root_package_folder */
100        IModelicaFolder root_package_folder = null;
101       
102        IModelicaPackage sub_package = null;
103        IModelicaPackage leaf_package = null;
104        IModelicaFile sub_package_model = null;
105
106
107        /*
108         * traverse children of project_root
109         */
110       
111        /* traverese packages */
112        for (IModelicaPackage pkg : project_root.getPackages())
113        {
114           
115            String name = pkg.getElementName();
116            if (name.equals("root_package"))
117            {
118                root_package = pkg;
119            }
120            else if (name.equals("childless_package"))
121            {
122                childless_package = pkg;
123            }
124        }
125       
126        assertNotNull("root_package not found", root_package);
127        assertEquals("Package base name", root_package.getBaseName(), "");
128        assertEquals("Package name", root_package.getElementName(), 
129                "root_package");
130               
131        assertNotNull("childless_package not found", childless_package);
132        assertEquals("Package base name", childless_package.getBaseName(), "");
133        assertEquals("Package name", childless_package.getElementName(), 
134                "childless_package");
135       
136        /*
137         * traverse children of root_package
138         */
139       
140        /* traverse packages */     
141        for (IModelicaPackage pkg : root_package.getPackages())
142        {
143            String name = pkg.getElementName();
144            if (name.equals("sub_package"))
145            {
146                sub_package = pkg;
147            }
148           
149        }
150        assertNotNull("sub_package not found", sub_package);
151        checkFullName(sub_package, "root_package.sub_package");
152
153       
154        /* traverse modelica files */
155        for (IModelicaFile file : root_package.getModelicaFiles())
156        {   
157            String name = file.getElementName();
158           
159            if (name.equals("root_package_model.mo"))
160            {
161                root_package_model = file;
162            }
163            else if (name.equals("root_package_function.mo"))
164            {
165                root_package_function = file;
166            }
167            else if (name.equals("package.mo"))
168            {
169                package_mo = file;
170            }
171        }
172        assertNotNull("root_package_model.mo not found", root_package_model);
173        assertNotNull("root_package_function.mo not found", 
174                root_package_function);
175        assertNotNull("root_package/package.mo not found",
176                package_mo);
177
178        /* traverse regular files */
179        for (IFile file : root_package.getFiles())
180        {
181            String name = file.getName();
182
183            if (name.equals("plain_file"))
184            {
185                root_package_plain_file = file;
186            }
187        }
188        assertNotNull("root_package/plain_file not found",
189                root_package_plain_file);
190       
191        /* traverse folders */
192        for (IModelicaFolder folder : root_package.getFolders())
193        {
194            String name = folder.getElementName();
195           
196            if (name.equals("root_package_folder"))
197            {
198                root_package_folder = folder;
199            }
200        }
201        assertNotNull("root_package/root_package_folder not found",
202                root_package_folder);
203
204        /*
205         * traverse children of sub_package
206         */
207
208        /* traverse packages */     
209        for (IModelicaPackage pkg : sub_package.getPackages())
210        {
211            String name = pkg.getElementName();
212            if (name.equals("leaf_package"))
213            {
214                leaf_package = pkg;
215            }
216           
217        }
218        assertNotNull("leaf_package not found", leaf_package);
219        checkFullName(leaf_package, "root_package.sub_package.leaf_package");
220       
221        /* traverse modelica files */       
222        package_mo = null;
223
224        for (IModelicaFile file : sub_package.getModelicaFiles())
225        {
226            String name = file.getElementName();
227            if (name.equals("package.mo"))
228            {
229                package_mo = file;
230            }
231            else if (name.equals("sub_package_model.mo"))
232            {
233                sub_package_model = file;
234            }       
235        }
236        assertNotNull("root_package/sub_package/package.mo not found",
237                package_mo);
238        assertNotNull("root_package/sub_package/sub_package_model.mo not found",
239                sub_package_model);
240
241
242       
243    }
244
245    /**
246     * Check that ze package's full name matches the fullName, otherwize
247     * fails.
248     * @param zePackage
249     * @param fullName
250     */
251    private void checkFullName(IModelicaPackage zePackage, String fullName) 
252    {
253        assertEquals("Package full name does not match",
254                     fullName, 
255                     zePackage.getBaseName() + "." + 
256                             zePackage.getElementName());
257    }
258}
Note: See TracBrowser for help on using the repository browser.