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

Last change on this file since 316 was 312, checked in by boris, 19 years ago
  • update the code to not use class* fields from the omc's getElementsInfo() output
File size: 18.2 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.IModelicaImport;
51import org.modelica.mdt.core.IModelicaSourceFile;
52import org.modelica.mdt.core.IModelicaProject;
53import org.modelica.mdt.core.compiler.CompilerInstantiationException;
54import org.modelica.mdt.core.compiler.ConnectException;
55import org.modelica.mdt.core.compiler.InvocationError;
56import org.modelica.mdt.core.compiler.UnexpectedReplyException;
57import org.modelica.mdt.internal.core.InnerClass;
58import org.modelica.mdt.test.util.Area51Projects;
59import org.modelica.mdt.test.util.Utility;
60
61import junit.framework.TestCase;
62
63/**
64 * test org.modelica.mdt.core.compiler.InnerClass class' code
65 */
66public class TestInnerClass extends TestCase
67{
68
69    /* the test subject */
70    private InnerClass componentsBananza;
71    private InnerClass importRichModel;
72   
73    /* teh Modelica package (from the standard library) */
74    private InnerClass modelica = null;
75   
76    @Override
77    protected void setUp() throws Exception
78    {
79        Area51Projects.createProjects();
80       
81        /* navigate to the model 'component_bananza' */
82        IModelicaProject proj = 
83            Utility.getProject(Area51Projects.MODELICA_PROJECT_NAME);
84       
85        IModelicaSourceFile file = 
86            Utility.findModelicaFileInFolder(proj.getRootFolder(), 
87                "component_model.mo");
88       
89        componentsBananza = null;
90        for (IModelicaElement el : file.getChildren())
91        {
92            if (el instanceof InnerClass)
93            {
94                if (el.getElementName().equals("components_bananza"))
95                {
96                    componentsBananza = (InnerClass)el;
97                    break;
98                }
99            }
100        }
101
102
103        /* navigate to the model 'import_rich_model' */
104        file = Utility.findModelicaFileInFolder(proj.getRootFolder(), 
105                "import_rich_model.mo");
106       
107        importRichModel = null;
108        for (IModelicaElement el : file.getChildren())
109        {
110            if (el instanceof InnerClass)
111            {
112                if (el.getElementName().equals("import_rich_model"))
113                {
114                    importRichModel = (InnerClass)el;
115                    break;
116                }
117            }
118        }
119
120        assertNotNull("could not find the model import_rich_model",
121                importRichModel);
122       
123        /* fetch teh Modelica package from the standard library */
124        for (IModelicaClass clazz : CompilerProxy.getStandardLibrary())
125        {
126            if (clazz.getElementName().equals("Modelica"))
127            {
128                modelica = (InnerClass)clazz;
129                break;
130            }
131        }
132        assertNotNull("could not found standard library package 'Modelica'",
133                modelica);
134       
135
136    }
137   
138    /**
139     * do some sanity checks on InnerClass children
140     */
141    public void testChildren()
142    throws ConnectException, UnexpectedReplyException,
143        InvocationError, CoreException, CompilerInstantiationException
144    { 
145        IModelicaComponent a_real = null;
146        IModelicaComponent an_undocumented_real = null;
147        IModelicaComponent a_protected_real = null;
148        IModelicaComponent a_protected_integer = null;
149        IModelicaClass a_package = null;
150        IModelicaClass a_class = null;
151        IModelicaClass a_model = null;
152        IModelicaClass a_connector = null;
153        IModelicaClass a_record = null;
154        IModelicaClass a_block = null;
155        IModelicaClass a_type = null;
156        IModelicaClass a_function = null;       
157       
158        /*
159         * fetch children to local variables
160         * so we can perfort check on 'em
161         */
162        for (IModelicaElement elm : componentsBananza.getChildren())
163        {
164            if (elm instanceof IModelicaComponent)
165            {
166                IModelicaComponent comp = (IModelicaComponent) elm;
167               
168                if (comp.getElementName().endsWith("a_real"))
169                {
170                    a_real = comp;
171                }
172                else if (comp.getElementName().endsWith("an_undocumented_real"))
173                {
174                    an_undocumented_real = comp;
175                }
176                else if (comp.getElementName().endsWith("a_protected_real"))
177                {
178                    a_protected_real = comp;
179                }
180                else if (comp.getElementName().endsWith("a_protected_integer"))
181                {
182                    a_protected_integer = comp;
183                }
184            }
185            else if (elm instanceof IModelicaClass)
186            {
187                switch (((IModelicaClass)elm).getRestrictionType())
188                {
189                case PACKAGE:
190                    a_package = (IModelicaClass)elm;
191                    break;
192                case BLOCK:
193                    a_block = (IModelicaClass)elm;
194                    break;
195                case CLASS:
196                    a_class = (IModelicaClass)elm;
197                    break;
198                case CONNECTOR:
199                    a_connector = (IModelicaClass)elm;
200                    break;
201                case FUNCTION:
202                    a_function = (IModelicaClass)elm;
203                    break;
204                case MODEL:
205                    a_model = (IModelicaClass)elm;
206                    break;
207                case RECORD:
208                    a_record = (IModelicaClass)elm;
209                    break;
210                case TYPE:
211                    a_type = (IModelicaClass)elm;
212                    break;
213                }
214            }
215        }
216       
217        /* sanity checks on components_bananza.a_type */
218        assertNotNull("components_bananza.a_type not found", a_type);
219        assertEquals("wrong element name", a_type.getElementName(), "a_type");
220        assertTrue("fishy file path", 
221                a_type.getFilePath().endsWith("component_model.mo"));
222        IRegion reg = a_type.getLocation();
223        assertEquals("wrong start offset", 338, reg.getOffset());
224        assertEquals("wrong length", 28, reg.getLength());
225
226        /* sanity checks on components_bananza.a_package */
227        assertNotNull("components_bananza.a_package not found", a_package);
228        assertEquals("wrong element name",
229                a_package.getElementName(), "a_package");
230        assertTrue("fishy file path", 
231                a_package.getFilePath().endsWith("component_model.mo"));
232        reg = a_package.getLocation();
233        assertEquals("wrong start offset", 91, reg.getOffset());
234        assertEquals("wrong length", 37, reg.getLength());
235
236        /* sanity checks on components_bananza.a_block */
237        assertNotNull("components_bananza.a_block not found", a_block);
238        assertEquals("wrong element name",
239                a_block.getElementName(), "a_block");
240        assertTrue("fishy file path", 
241                a_block.getFilePath().endsWith("component_model.mo"));
242        reg = a_block.getLocation();
243        assertEquals("wrong start offset", 267, reg.getOffset());
244        assertEquals("wrong length", 31, reg.getLength());
245
246        /* sanity checks on components_bananza.a_class */
247        assertNotNull("components_bananza.a_class not found", a_class);
248        assertEquals("wrong element name", a_class.getElementName(), "a_class");
249        assertTrue("fishy file path", 
250                a_class.getFilePath().endsWith("component_model.mo"));
251        reg = a_class.getLocation();
252        assertEquals("wrong start offset", 128, reg.getOffset());
253        assertEquals("wrong length", 31, reg.getLength());
254
255        /* sanity checks on components_bananza.a_connector */
256        assertNotNull("components_bananza.a_connector not found", a_connector);
257        assertEquals("wrong element name", a_connector.getElementName(), 
258                "a_connector");
259        assertTrue("fishy file path", 
260                a_connector.getFilePath().endsWith("component_model.mo"));
261        reg = a_connector.getLocation();
262        assertEquals("wrong start offset", 190, reg.getOffset());
263        assertEquals("wrong length", 43, reg.getLength());
264
265        /* sanity checks on components_bananza.a_function */
266        assertNotNull("components_bananza.a_function not found", a_function);
267        assertEquals("wrong element name", a_function.getElementName(),
268                "a_function");
269        assertTrue("fishy file path", 
270                a_function.getFilePath().endsWith("component_model.mo"));
271        reg = a_function.getLocation();
272        assertEquals("wrong start offset", 298, reg.getOffset());
273        assertEquals("wrong length", 40, reg.getLength());
274
275        /* sanity checks on components_bananza.a_model */
276        assertNotNull("components_bananza.a_model not found", a_model);
277        assertEquals("wrong element name", a_model.getElementName(), "a_model");
278        assertTrue("fishy file path", 
279                a_model.getFilePath().endsWith("component_model.mo"));
280        reg = a_model.getLocation();
281        assertEquals("wrong start offset", 159, reg.getOffset());
282        assertEquals("wrong length", 31, reg.getLength());
283
284        /* sanity checks on components_bananza.a_record */
285        assertNotNull("components_bananza.a_record not found", a_record);
286        assertEquals("wrong element name", a_record.getElementName(), "a_record");
287        assertTrue("fishy file path", 
288                a_record.getFilePath().endsWith("component_model.mo"));
289        reg = a_record.getLocation();
290        assertEquals("wrong start offset", 233, reg.getOffset());
291        assertEquals("wrong length", 34, reg.getLength());
292
293        /* sanity checks on components_bananza.a_real */
294        assertNotNull("components_bananza.a_real not found", a_real);
295        assertEquals("wrong element name", a_real.getElementName(), "a_real");
296        assertEquals("wrong visibility", a_real.getVisbility(),
297                IModelicaComponent.Visibility.PUBLIC);
298
299        reg = a_real.getLocation();
300        assertEquals("wrong start offset", 25, reg.getOffset());
301        assertEquals("wrong length", 35, reg.getLength());
302       
303        /* sanity checks on components_bananza.a_undocumented_real */
304        assertNotNull("components_bananza.an_undocumented_real not found", 
305                an_undocumented_real);
306        assertEquals("wrong element name", an_undocumented_real.getElementName(),
307                "an_undocumented_real");
308        assertEquals("wrong visibility", an_undocumented_real.getVisbility(),
309                IModelicaComponent.Visibility.PUBLIC);
310        reg = an_undocumented_real.getLocation();
311        assertEquals("wrong start offset", 60, reg.getOffset());
312        assertEquals("wrong length", 31, reg.getLength());
313       
314        /* sanity checks on components_bananza.a_protected_integer */
315        assertNotNull("components_bananza.a_protected_integer not found", 
316                a_protected_integer);
317        assertEquals("wrong element name", a_protected_integer.getElementName(),
318                "a_protected_integer");
319        assertEquals("wrong visibility", a_protected_integer.getVisbility(),
320                IModelicaComponent.Visibility.PROTECTED);
321        reg = a_protected_integer.getLocation();
322        assertEquals("wrong start offset", 400, reg.getOffset());
323        assertEquals("wrong length", 47, reg.getLength());
324
325
326        /* sanity checks on components_bananza.a_protected_real */
327        assertNotNull("components_bananza.a_protected_real not found", 
328                a_protected_real);
329        assertEquals("wrong element name", a_protected_real.getElementName(), 
330                "a_protected_real");
331        assertEquals("wrong visibility", a_protected_real.getVisbility(),
332                IModelicaComponent.Visibility.PROTECTED);
333        reg = a_protected_real.getLocation();
334        assertEquals("wrong start offset", 376, reg.getOffset());
335        assertEquals("wrong length", 24, reg.getLength());
336
337    }
338   
339    /**
340     * do some sanity checks on InnerClass' imports
341     */
342    public void testImports() 
343        throws ConnectException, UnexpectedReplyException, InvocationError, 
344            CompilerInstantiationException
345    {
346        int importCounter = 0;
347        for (IModelicaImport imp : importRichModel.getImports())
348        {
349            importCounter++;
350           
351            /*
352             * we are expecting 4 import statments in following order:
353             * 1. qualified         (import Modelica)
354             * 2. single definition (import Modelica.Math.sin)
355             * 3. unqualified       (import Modelica.*)
356             * 4. renaming          (import mm = Modelica.Math)
357             */
358
359            switch (importCounter)
360            {
361            case 1: // import Modelica
362                assertEquals(IModelicaImport.Type.QUALIFIED, imp.getType());
363                break;
364            case 2: // import Modelica.Math.sin
365                // this is basicaly not implemented and maybe will go away...argh
366//              assertEquals(IModelicaImport.Type.SINGLE_DEFINITION,
367//                      imp.getType());
368                break;
369            case 3: // import Modelica.*
370                assertEquals(IModelicaImport.Type.UNQUALIFIED, imp.getType());
371                break;
372            case 4: // import mm = Modelica.Math
373                assertEquals(IModelicaImport.Type.RENAMING, imp.getType());
374                break;
375            default:
376                fail("unexpectedly many imports found"); 
377            }
378        }
379       
380        assertFalse("did not find all import statments", 
381                importCounter < 4);
382
383    }
384   
385    /**
386     * Do some integrity tests on classes/packages from the standard library.
387     */
388    public void testStandardLibraryElements()
389        throws ConnectException, UnexpectedReplyException, 
390            InvocationError, CompilerInstantiationException, CoreException
391    {
392        /*
393         * do checks on Modelica package
394         */
395        assertNull("standard library should be defined outside of workspace",
396                modelica.getResource());
397        assertFalse("empty path to the source file", 
398                modelica.getFilePath().equals(""));
399        IRegion reg = modelica.getLocation();
400        assertTrue("negative element region can't be", reg.getOffset() >= 0);
401        assertTrue("elements length must be positive", reg.getLength() > 0);
402
403
404        /*
405         * do checks on Modelica.Blocks and Modelica.Constants packages
406         */
407
408        InnerClass blocks = null;
409        InnerClass constants = null;
410 
411        for (IModelicaElement el : modelica.getChildren())
412        {
413            String name = el.getElementName();
414           
415            if (name.equals("Blocks"))
416            {
417                blocks = (InnerClass)el;
418            }
419            else if (name.equals("Constants"))
420            {
421                constants = (InnerClass)el;
422            }
423        } 
424       
425        /* check Modelica.Blocks */
426        assertNotNull("could not find package Modelica.Blocks in standard" +
427                "library ", blocks);
428        assertNull("standard library should be defined outside of workspace",
429                blocks.getResource());
430        assertFalse("empty path to the source file", 
431                blocks.getFilePath().equals(""));
432        reg = blocks.getLocation();
433        assertTrue("negative element region can't be", reg.getOffset() >= 0);
434        assertTrue("elements length must be positive", reg.getLength() > 0);
435       
436        /* check Modelica.Blocks imports */
437        boolean foundSIimport = false;
438
439        for (IModelicaImport imp : blocks.getImports())
440        {
441            switch (imp.getType())
442            {
443            case RENAMING:
444                if (imp.getAlias().equals("SI"))
445                {
446                    foundSIimport = true;
447                }
448                break;
449                /* ignore all other types of imports */
450            }
451        }
452       
453        assertTrue("could not find the representation of" +
454                " 'import SI = Modelica.SIunits;' statment", foundSIimport); 
455       
456        /* check Modelica.Constants */
457        assertNotNull("could not find package Modelica.Constants in standard" +
458                "library ", constants);
459        assertNull("standard library should be defined outside of workspace",
460                constants.getResource());
461        assertFalse("empty path to the source file", 
462                constants.getFilePath().equals(""));
463        reg = constants.getLocation();
464        assertTrue("negative element region can't be", reg.getOffset() >= 0);
465        assertTrue("elements length must be positive", reg.getLength() > 0);
466
467
468        IModelicaComponent pi = null;
469        IModelicaComponent D2R = null;
470       
471        /*
472         * do checks on Modelica.Constants components
473         */
474        for (IModelicaElement el : constants.getChildren())
475        {
476            String name = el.getElementName();
477           
478            if (name.equals("pi"))
479            {
480                pi = (IModelicaComponent)el;
481            }
482            else if (name.equals("D2R"))
483            {
484                D2R = (IModelicaComponent)el;
485            }
486        }
487
488        /* check Modelica.Constants.pi */
489        assertNotNull("could not find package Modelica.Constants.pi in standard" 
490                + "library ", pi);
491        assertEquals("pi must have public visibility", 
492                IModelicaComponent.Visibility.PUBLIC,
493                pi.getVisbility());
494        assertNull("standard library should be defined outside of workspace",
495                pi.getResource());
496        assertFalse("empty path to the source file", 
497                pi.getFilePath().equals(""));
498        reg = pi.getLocation();
499        assertTrue("negative element region can't be", reg.getOffset() >= 0);
500        assertTrue("elements length must be positive", reg.getLength() > 0);
501
502        /* check Modelica.Constants.D2R */
503        assertNotNull("could not find package Modelica.Constants.pi in standard" 
504                + "library ", D2R);
505        assertEquals("pi must have public visibility", 
506                IModelicaComponent.Visibility.PUBLIC,
507                D2R.getVisbility());
508        assertNull("standard library should be defined outside of workspace",
509                D2R.getResource());
510        assertFalse("empty path to the source file", 
511                D2R.getFilePath().equals(""));
512        reg = pi.getLocation();
513        assertTrue("negative element region can't be", reg.getOffset() >= 0);
514        assertTrue("elements length must be positive", reg.getLength() > 0);
515       
516        /* check Modelica.Constants imports */
517        foundSIimport = false;
518        boolean foundNonSIimport = false;
519
520        for (IModelicaImport imp : constants.getImports())
521        {
522            switch (imp.getType())
523            {
524            case RENAMING:
525                if (imp.getAlias().equals("SI"))
526                {
527                    foundSIimport = true;
528                }
529                else if (imp.getAlias().equals("NonSI"))
530                {
531                    foundNonSIimport = true;
532                }
533                break;
534                /* ignore all other types of imports */
535            }
536        }
537       
538        assertTrue("could not find the representation of" +
539                " 'import SI = Modelica.SIunits;' statment", foundSIimport); 
540        assertTrue("could not find the representation of" +
541                " import NonSI = Modelica.SIunits.Conversions.NonSIunits;' " +
542                "statment", foundNonSIimport); 
543    }
544}
Note: See TracBrowser for help on using the repository browser.