source: trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/TestInnerClass.java @ 310

Last change on this file since 310 was 310, checked in by boris, 19 years ago
  • create an abstract way to access omc class getElementsInfo() thus close the hole into omc plugin, hurray !
  • added some tests that triggerd some bugs and fixed the bugs
File size: 15.5 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 org.eclipse.core.runtime.CoreException;
45import org.eclipse.jface.text.IRegion;
46import org.modelica.mdt.core.CompilerProxy;
47import org.modelica.mdt.core.IModelicaClass;
48import org.modelica.mdt.core.IModelicaComponent;
49import org.modelica.mdt.core.IModelicaElement;
50import org.modelica.mdt.core.IModelicaSourceFile;
51import org.modelica.mdt.core.IModelicaProject;
52import org.modelica.mdt.core.compiler.CompilerInstantiationException;
53import org.modelica.mdt.core.compiler.ConnectException;
54import org.modelica.mdt.core.compiler.InvocationError;
55import org.modelica.mdt.core.compiler.UnexpectedReplyException;
56import org.modelica.mdt.internal.core.InnerClass;
57import org.modelica.mdt.test.util.Area51Projects;
58import org.modelica.mdt.test.util.Utility;
59
60import junit.framework.TestCase;
61
62/**
63 * @author Elmir Jagudin
64 */
65
66public class TestInnerClass extends TestCase
67{
68
69    /* the test subject */
70    private InnerClass componentsBananza;
71   
72    /* teh Modelica package (from the standard library) */
73    private InnerClass modelica = null;
74   
75    @Override
76    protected void setUp() throws Exception
77    {
78        Area51Projects.createProjects();
79       
80        /* navigate to the model 'component_bananza' */
81        IModelicaProject proj = 
82            Utility.getProject(Area51Projects.MODELICA_PROJECT_NAME);
83       
84        IModelicaSourceFile file = 
85            Utility.findModelicaFileInFolder(proj.getRootFolder(), 
86                "component_model.mo");
87       
88        componentsBananza = null;
89        for (Object obj : file.getChildren())
90        {
91            if (obj instanceof InnerClass)
92            {
93                if (((InnerClass)obj).getElementName().equals("components_bananza"))
94                {
95                    componentsBananza = (InnerClass)obj;
96                    break;
97                }
98            }
99        }
100       
101        assertNotNull("could not find the model component_bonanza",
102                componentsBananza);
103       
104        /* fetch teh Modelica package from the standard library */
105        for (IModelicaClass clazz : CompilerProxy.getStandardLibrary())
106        {
107            if (clazz.getElementName().equals("Modelica"))
108            {
109                modelica = (InnerClass)clazz;
110                break;
111            }
112        }
113        assertNotNull("could not found standard library package 'Modelica'",
114                modelica);
115       
116
117    }
118   
119    /**
120     * do some sanity checks on InnerClass children
121     *
122     * @throws CoreException
123     * @throws InvocationError
124     * @throws UnexpectedReplyException
125     * @throws ConnectException
126     * @throws CompilerInstantiationException
127     */
128    public void testChildren()
129    throws ConnectException, UnexpectedReplyException,
130        InvocationError, CoreException, CompilerInstantiationException
131    { 
132        IModelicaComponent a_real = null;
133        IModelicaComponent an_undocumented_real = null;
134        IModelicaComponent a_protected_real = null;
135        IModelicaComponent a_protected_integer = null;
136        IModelicaClass a_package = null;
137        IModelicaClass a_class = null;
138        IModelicaClass a_model = null;
139        IModelicaClass a_connector = null;
140        IModelicaClass a_record = null;
141        IModelicaClass a_block = null;
142        IModelicaClass a_type = null;
143        IModelicaClass a_function = null;       
144       
145       
146        /*
147         * fetch children to local variables
148         * so we can perfort check on 'em
149         */
150        for (IModelicaElement elm : componentsBananza.getChildren())
151        {
152            if (elm instanceof IModelicaComponent)
153            {
154                IModelicaComponent comp = (IModelicaComponent) elm;
155               
156                if (comp.getElementName().endsWith("a_real"))
157                {
158                    a_real = comp;
159                }
160                else if (comp.getElementName().endsWith("an_undocumented_real"))
161                {
162                    an_undocumented_real = comp;
163                }
164                else if (comp.getElementName().endsWith("a_protected_real"))
165                {
166                    a_protected_real = comp;
167                }
168                else if (comp.getElementName().endsWith("a_protected_integer"))
169                {
170                    a_protected_integer = comp;
171                }
172            }
173            else if (elm instanceof IModelicaClass)
174            {
175                switch (((IModelicaClass)elm).getRestrictionType())
176                {
177                case PACKAGE:
178                    a_package = (IModelicaClass)elm;
179                    break;
180                case BLOCK:
181                    a_block = (IModelicaClass)elm;
182                    break;
183                case CLASS:
184                    a_class = (IModelicaClass)elm;
185                    break;
186                case CONNECTOR:
187                    a_connector = (IModelicaClass)elm;
188                    break;
189                case FUNCTION:
190                    a_function = (IModelicaClass)elm;
191                    break;
192                case MODEL:
193                    a_model = (IModelicaClass)elm;
194                    break;
195                case RECORD:
196                    a_record = (IModelicaClass)elm;
197                    break;
198                case TYPE:
199                    a_type = (IModelicaClass)elm;
200                    break;
201                }
202            }
203        }
204       
205        /* sanity checks on components_bananza.a_type */
206        assertNotNull("components_bananza.a_type not found", a_type);
207        assertEquals("wrong element name", a_type.getElementName(), "a_type");
208        System.out.println(a_type.getFilePath());
209        System.out.println(a_type.getFilePath());
210        assertTrue("fishy file path", 
211                a_type.getFilePath().endsWith("component_model.mo"));
212        IRegion reg = a_type.getLocation();
213        assertEquals("wrong start offset", 338, reg.getOffset());
214        assertEquals("wrong length", 28, reg.getLength());
215
216        /* sanity checks on components_bananza.a_package */
217        assertNotNull("components_bananza.a_package not found", a_package);
218        assertEquals("wrong element name",
219                a_package.getElementName(), "a_package");
220        assertTrue("fishy file path", 
221                a_package.getFilePath().endsWith("component_model.mo"));
222        reg = a_package.getLocation();
223        assertEquals("wrong start offset", 91, reg.getOffset());
224        assertEquals("wrong length", 37, reg.getLength());
225
226        /* sanity checks on components_bananza.a_block */
227        assertNotNull("components_bananza.a_block not found", a_block);
228        assertEquals("wrong element name",
229                a_block.getElementName(), "a_block");
230        assertTrue("fishy file path", 
231                a_block.getFilePath().endsWith("component_model.mo"));
232        reg = a_block.getLocation();
233        assertEquals("wrong start offset", 267, reg.getOffset());
234        assertEquals("wrong length", 31, reg.getLength());
235
236        /* sanity checks on components_bananza.a_class */
237        assertNotNull("components_bananza.a_class not found", a_class);
238        assertEquals("wrong element name", a_class.getElementName(), "a_class");
239        assertTrue("fishy file path", 
240                a_class.getFilePath().endsWith("component_model.mo"));
241        reg = a_class.getLocation();
242        assertEquals("wrong start offset", 128, reg.getOffset());
243        assertEquals("wrong length", 31, reg.getLength());
244
245        /* sanity checks on components_bananza.a_connector */
246        assertNotNull("components_bananza.a_connector not found", a_connector);
247        assertEquals("wrong element name", a_connector.getElementName(), 
248                "a_connector");
249        assertTrue("fishy file path", 
250                a_connector.getFilePath().endsWith("component_model.mo"));
251        reg = a_connector.getLocation();
252        assertEquals("wrong start offset", 190, reg.getOffset());
253        assertEquals("wrong length", 43, reg.getLength());
254
255        /* sanity checks on components_bananza.a_function */
256        assertNotNull("components_bananza.a_function not found", a_function);
257        assertEquals("wrong element name", a_function.getElementName(),
258                "a_function");
259        assertTrue("fishy file path", 
260                a_function.getFilePath().endsWith("component_model.mo"));
261        reg = a_function.getLocation();
262        assertEquals("wrong start offset", 298, reg.getOffset());
263        assertEquals("wrong length", 40, reg.getLength());
264
265        /* sanity checks on components_bananza.a_model */
266        assertNotNull("components_bananza.a_model not found", a_model);
267        assertEquals("wrong element name", a_model.getElementName(), "a_model");
268        assertTrue("fishy file path", 
269                a_model.getFilePath().endsWith("component_model.mo"));
270        reg = a_model.getLocation();
271        assertEquals("wrong start offset", 159, reg.getOffset());
272        assertEquals("wrong length", 31, reg.getLength());
273
274        /* sanity checks on components_bananza.a_record */
275        assertNotNull("components_bananza.a_record not found", a_record);
276        assertEquals("wrong element name", a_record.getElementName(), "a_record");
277        assertTrue("fishy file path", 
278                a_record.getFilePath().endsWith("component_model.mo"));
279        reg = a_record.getLocation();
280        assertEquals("wrong start offset", 233, reg.getOffset());
281        assertEquals("wrong length", 34, reg.getLength());
282
283        /* sanity checks on components_bananza.a_real */
284        assertNotNull("components_bananza.a_real not found", a_real);
285        assertEquals("wrong element name", a_real.getElementName(), "a_real");
286        assertEquals("wrong visibility", a_real.getVisbility(),
287                IModelicaComponent.Visibility.PUBLIC);
288
289        reg = a_real.getLocation();
290        assertEquals("wrong start offset", 25, reg.getOffset());
291        assertEquals("wrong length", 35, reg.getLength());
292       
293        /* sanity checks on components_bananza.a_undocumented_real */
294        assertNotNull("components_bananza.an_undocumented_real not found", 
295                an_undocumented_real);
296        assertEquals("wrong element name", an_undocumented_real.getElementName(),
297                "an_undocumented_real");
298        assertEquals("wrong visibility", an_undocumented_real.getVisbility(),
299                IModelicaComponent.Visibility.PUBLIC);
300        reg = an_undocumented_real.getLocation();
301        assertEquals("wrong start offset", 60, reg.getOffset());
302        assertEquals("wrong length", 31, reg.getLength());
303       
304        /* sanity checks on components_bananza.a_protected_integer */
305        assertNotNull("components_bananza.a_protected_integer not found", 
306                a_protected_integer);
307        assertEquals("wrong element name", a_protected_integer.getElementName(),
308                "a_protected_integer");
309        assertEquals("wrong visibility", a_protected_integer.getVisbility(),
310                IModelicaComponent.Visibility.PROTECTED);
311        reg = a_protected_integer.getLocation();
312        assertEquals("wrong start offset", 400, reg.getOffset());
313        assertEquals("wrong length", 47, reg.getLength());
314
315
316        /* sanity checks on components_bananza.a_protected_real */
317        assertNotNull("components_bananza.a_protected_real not found", 
318                a_protected_real);
319        assertEquals("wrong element name", a_protected_real.getElementName(), 
320                "a_protected_real");
321        assertEquals("wrong visibility", a_protected_real.getVisbility(),
322                IModelicaComponent.Visibility.PROTECTED);
323        reg = a_protected_real.getLocation();
324        assertEquals("wrong start offset", 376, reg.getOffset());
325        assertEquals("wrong length", 24, reg.getLength());
326
327    }
328   
329    /**
330     * Do some integrity tests on classes/packages from the standard library.
331     */
332    public void testStandardLibraryElements()
333        throws ConnectException, UnexpectedReplyException, 
334            InvocationError, CompilerInstantiationException, CoreException
335    {
336        /*
337         * do checks on Modelica package
338         */
339        assertNull("standard library should be defined outside of workspace",
340                modelica.getResource());
341        assertFalse("empty path to the source file", 
342                modelica.getFilePath().equals(""));
343        IRegion reg = modelica.getLocation();
344        assertTrue("negative element region can't be", reg.getOffset() >= 0);
345        assertTrue("elements length must be positive", reg.getLength() > 0);
346
347
348        /*
349         * do checks on Modelica.Blocks and Modelica.Constants packages
350         */
351
352        InnerClass blocks = null;
353        InnerClass constants = null;
354 
355        for (IModelicaElement el : modelica.getChildren())
356        {
357            String name = el.getElementName();
358           
359            if (name.equals("Blocks"))
360            {
361                blocks = (InnerClass)el;
362            }
363            else if (name.equals("Constants"))
364            {
365                constants = (InnerClass)el;
366            }
367        } 
368       
369        /* check Modelica.Blocks */
370        assertNotNull("could not find package Modelica.Blocks in standard" +
371                "library ", blocks);
372        assertNull("standard library should be defined outside of workspace",
373                blocks.getResource());
374        assertFalse("empty path to the source file", 
375                blocks.getFilePath().equals(""));
376        reg = blocks.getLocation();
377        assertTrue("negative element region can't be", reg.getOffset() >= 0);
378        assertTrue("elements length must be positive", reg.getLength() > 0);
379
380       
381        /* check Modelica.Constants */
382        assertNotNull("could not find package Modelica.Constants in standard" +
383                "library ", constants);
384        assertNull("standard library should be defined outside of workspace",
385                constants.getResource());
386        assertFalse("empty path to the source file", 
387                constants.getFilePath().equals(""));
388        reg = constants.getLocation();
389        assertTrue("negative element region can't be", reg.getOffset() >= 0);
390        assertTrue("elements length must be positive", reg.getLength() > 0);
391
392
393        IModelicaComponent pi = null;
394        IModelicaComponent D2R = null;
395       
396        /*
397         * do checks on Modelica.Constants components
398         */
399        for (IModelicaElement el : constants.getChildren())
400        {
401            String name = el.getElementName();
402           
403            if (name.equals("pi"))
404            {
405                pi = (IModelicaComponent)el;
406            }
407            else if (name.equals("D2R"))
408            {
409                D2R = (IModelicaComponent)el;
410            }
411        }
412
413        /* check Modelica.Constants.pi */
414        assertNotNull("could not find package Modelica.Constants.pi in standard" 
415                + "library ", pi);
416        assertEquals("pi must have public visibility", 
417                IModelicaComponent.Visibility.PUBLIC,
418                pi.getVisbility());
419        assertNull("standard library should be defined outside of workspace",
420                pi.getResource());
421        assertFalse("empty path to the source file", 
422                pi.getFilePath().equals(""));
423        reg = pi.getLocation();
424        assertTrue("negative element region can't be", reg.getOffset() >= 0);
425        assertTrue("elements length must be positive", reg.getLength() > 0);
426
427        /* check Modelica.Constants.D2R */
428        assertNotNull("could not find package Modelica.Constants.pi in standard" 
429                + "library ", D2R);
430        assertEquals("pi must have public visibility", 
431                IModelicaComponent.Visibility.PUBLIC,
432                D2R.getVisbility());
433        assertNull("standard library should be defined outside of workspace",
434                D2R.getResource());
435        assertFalse("empty path to the source file", 
436                D2R.getFilePath().equals(""));
437        reg = pi.getLocation();
438        assertTrue("negative element region can't be", reg.getOffset() >= 0);
439        assertTrue("elements length must be positive", reg.getLength() > 0);
440       
441    }
442}
Note: See TracBrowser for help on using the repository browser.