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 | |
---|
42 | package org.modelica.mdt.test; |
---|
43 | |
---|
44 | import org.eclipse.core.resources.IFile; |
---|
45 | import org.eclipse.core.resources.IProject; |
---|
46 | import org.eclipse.core.runtime.CoreException; |
---|
47 | import org.eclipse.core.runtime.IAdaptable; |
---|
48 | import org.eclipse.core.runtime.Platform; |
---|
49 | import org.eclipse.jface.text.IRegion; |
---|
50 | import org.eclipse.ui.model.IWorkbenchAdapter; |
---|
51 | import org.modelica.mdt.core.IModelicaClass; |
---|
52 | import org.modelica.mdt.core.IModelicaComponent; |
---|
53 | import org.modelica.mdt.core.IModelicaElement; |
---|
54 | import org.modelica.mdt.core.IModelicaImport; |
---|
55 | import org.modelica.mdt.core.IModelicaSourceFile; |
---|
56 | import org.modelica.mdt.core.IModelicaProject; |
---|
57 | import org.modelica.mdt.core.IStandardLibrary; |
---|
58 | import org.modelica.mdt.core.ModelicaCore; |
---|
59 | import org.modelica.mdt.core.compiler.CompilerInstantiationException; |
---|
60 | import org.modelica.mdt.core.compiler.ConnectException; |
---|
61 | import org.modelica.mdt.core.compiler.InvocationError; |
---|
62 | import org.modelica.mdt.core.compiler.UnexpectedReplyException; |
---|
63 | import org.modelica.mdt.internal.core.InnerClass; |
---|
64 | import org.modelica.mdt.test.util.Area51Projects; |
---|
65 | import org.modelica.mdt.test.util.Utility; |
---|
66 | import org.osgi.framework.Bundle; |
---|
67 | import org.osgi.framework.BundleException; |
---|
68 | |
---|
69 | import junit.framework.TestCase; |
---|
70 | |
---|
71 | /** |
---|
72 | * test org.modelica.mdt.core.compiler.InnerClass class' code |
---|
73 | */ |
---|
74 | public class TestInnerClass extends TestCase |
---|
75 | { |
---|
76 | |
---|
77 | /* the file on which we make the changing locations test */ |
---|
78 | private static final String CHANGING_FILE = "changing_file.mo"; |
---|
79 | |
---|
80 | /* the test subject */ |
---|
81 | private InnerClass componentsBananza; |
---|
82 | private InnerClass importRichModel; |
---|
83 | |
---|
84 | private IModelicaProject proj; |
---|
85 | |
---|
86 | |
---|
87 | @Override |
---|
88 | protected void setUp() throws Exception |
---|
89 | { |
---|
90 | Area51Projects.createProjects(); |
---|
91 | |
---|
92 | /* navigate to the model 'component_bananza' */ |
---|
93 | proj = Utility.getProject(Area51Projects.MODELICA_PROJECT_NAME); |
---|
94 | |
---|
95 | IModelicaSourceFile file = |
---|
96 | Utility.findModelicaFileInFolder(proj.getRootFolder(), |
---|
97 | "component_model.mo"); |
---|
98 | |
---|
99 | componentsBananza = null; |
---|
100 | for (IModelicaElement el : file.getChildren()) |
---|
101 | { |
---|
102 | if (el instanceof InnerClass) |
---|
103 | { |
---|
104 | if (el.getElementName().equals("components_bananza")) |
---|
105 | { |
---|
106 | componentsBananza = (InnerClass)el; |
---|
107 | break; |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | /* navigate to the model 'import_rich_model' */ |
---|
114 | file = Utility.findModelicaFileInFolder(proj.getRootFolder(), |
---|
115 | "import_rich_model.mo"); |
---|
116 | |
---|
117 | importRichModel = null; |
---|
118 | for (IModelicaElement el : file.getChildren()) |
---|
119 | { |
---|
120 | if (el instanceof InnerClass) |
---|
121 | { |
---|
122 | if (el.getElementName().equals("import_rich_model")) |
---|
123 | { |
---|
124 | importRichModel = (InnerClass)el; |
---|
125 | break; |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | assertNotNull("could not find the model import_rich_model", |
---|
131 | importRichModel); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * do some sanity checks on InnerClass children |
---|
136 | */ |
---|
137 | public void testChildren() |
---|
138 | throws ConnectException, UnexpectedReplyException, |
---|
139 | InvocationError, CoreException, CompilerInstantiationException, |
---|
140 | BundleException |
---|
141 | { |
---|
142 | /* |
---|
143 | * we want to check if we can convert modelica elemnts to workbench |
---|
144 | * object and get correct label and some image |
---|
145 | * the adapter factory that handles modelica elements object |
---|
146 | * is installed by the org.modelica.mdt.ui plugin, |
---|
147 | * make sure it is loaded before running tests |
---|
148 | */ |
---|
149 | Bundle bundle = Platform.getBundle("org.modelica.mdt.ui"); |
---|
150 | bundle.start(); |
---|
151 | |
---|
152 | |
---|
153 | IModelicaComponent a_real = null; |
---|
154 | IModelicaComponent an_undocumented_real = null; |
---|
155 | IModelicaComponent a_protected_real = null; |
---|
156 | IModelicaComponent a_protected_integer = null; |
---|
157 | IModelicaClass a_package = null; |
---|
158 | IModelicaClass a_class = null; |
---|
159 | IModelicaClass a_model = null; |
---|
160 | IModelicaClass a_connector = null; |
---|
161 | IModelicaClass a_record = null; |
---|
162 | IModelicaClass a_block = null; |
---|
163 | IModelicaClass a_type = null; |
---|
164 | IModelicaClass a_function = null; |
---|
165 | |
---|
166 | /* |
---|
167 | * fetch children to local variables |
---|
168 | * so we can perfort check on 'em |
---|
169 | */ |
---|
170 | for (IModelicaElement elm : componentsBananza.getChildren()) |
---|
171 | { |
---|
172 | if (elm instanceof IModelicaComponent) |
---|
173 | { |
---|
174 | IModelicaComponent comp = (IModelicaComponent) elm; |
---|
175 | |
---|
176 | if (comp.getElementName().endsWith("a_real")) |
---|
177 | { |
---|
178 | a_real = comp; |
---|
179 | } |
---|
180 | else if (comp.getElementName().endsWith("an_undocumented_real")) |
---|
181 | { |
---|
182 | an_undocumented_real = comp; |
---|
183 | } |
---|
184 | else if (comp.getElementName().endsWith("a_protected_real")) |
---|
185 | { |
---|
186 | a_protected_real = comp; |
---|
187 | } |
---|
188 | else if (comp.getElementName().endsWith("a_protected_integer")) |
---|
189 | { |
---|
190 | a_protected_integer = comp; |
---|
191 | } |
---|
192 | } |
---|
193 | else if (elm instanceof IModelicaClass) |
---|
194 | { |
---|
195 | switch (((IModelicaClass)elm).getRestriction()) |
---|
196 | { |
---|
197 | case PACKAGE: |
---|
198 | a_package = (IModelicaClass)elm; |
---|
199 | break; |
---|
200 | case BLOCK: |
---|
201 | a_block = (IModelicaClass)elm; |
---|
202 | break; |
---|
203 | case CLASS: |
---|
204 | a_class = (IModelicaClass)elm; |
---|
205 | break; |
---|
206 | case CONNECTOR: |
---|
207 | a_connector = (IModelicaClass)elm; |
---|
208 | break; |
---|
209 | case FUNCTION: |
---|
210 | a_function = (IModelicaClass)elm; |
---|
211 | break; |
---|
212 | case MODEL: |
---|
213 | a_model = (IModelicaClass)elm; |
---|
214 | break; |
---|
215 | case RECORD: |
---|
216 | a_record = (IModelicaClass)elm; |
---|
217 | break; |
---|
218 | case TYPE: |
---|
219 | a_type = (IModelicaClass)elm; |
---|
220 | break; |
---|
221 | } |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | /* |
---|
226 | * sanity checks on components_bananza.a_type |
---|
227 | */ |
---|
228 | assertNotNull("components_bananza.a_type not found", a_type); |
---|
229 | assertEquals("wrong element name", "a_type", a_type.getElementName()); |
---|
230 | assertEquals("wrong element full name", |
---|
231 | "components_bananza.a_type", a_type.getFullName()); |
---|
232 | assertTrue("fishy file path", |
---|
233 | a_type.getFilePath().endsWith("component_model.mo")); |
---|
234 | IRegion reg = a_type.getLocation().getRegion(); |
---|
235 | assertEquals("wrong start offset", 342, reg.getOffset()); |
---|
236 | assertEquals("wrong length", 364-342+1, reg.getLength()); |
---|
237 | |
---|
238 | /* check if element is adaptable */ |
---|
239 | assertTrue("element object must be adaptable", |
---|
240 | (a_type instanceof IAdaptable)); |
---|
241 | |
---|
242 | /* check if we can convert element to workbench adapter */ |
---|
243 | IWorkbenchAdapter wbAdapter = |
---|
244 | (IWorkbenchAdapter)a_type.getAdapter(IWorkbenchAdapter.class); |
---|
245 | |
---|
246 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
247 | assertEquals("wrong label", "a_type", |
---|
248 | wbAdapter.getLabel(a_type)); |
---|
249 | assertNotNull("no image provided", |
---|
250 | wbAdapter.getImageDescriptor(a_type)); |
---|
251 | |
---|
252 | |
---|
253 | /* |
---|
254 | * sanity checks on components_bananza.a_package |
---|
255 | */ |
---|
256 | assertNotNull("components_bananza.a_package not found", a_package); |
---|
257 | assertEquals("wrong element name", "a_package", |
---|
258 | a_package.getElementName()); |
---|
259 | assertEquals("wrong element full name", "components_bananza.a_package", |
---|
260 | a_package.getFullName()); |
---|
261 | assertTrue("fishy file path", |
---|
262 | a_package.getFilePath().endsWith("component_model.mo")); |
---|
263 | reg = a_package.getLocation().getRegion(); |
---|
264 | assertEquals("wrong start offset", 95, reg.getOffset()); |
---|
265 | assertEquals("wrong length", 126-95+1, reg.getLength()); |
---|
266 | |
---|
267 | /* check if element is adaptable */ |
---|
268 | assertTrue("element object must be adaptable", |
---|
269 | (a_package instanceof IAdaptable)); |
---|
270 | |
---|
271 | /* check if we can convert element to workbench adapter */ |
---|
272 | wbAdapter = (IWorkbenchAdapter)a_package.getAdapter(IWorkbenchAdapter.class); |
---|
273 | |
---|
274 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
275 | wbAdapter.getLabel(a_package); |
---|
276 | assertEquals("wrong label", "a_package", |
---|
277 | wbAdapter.getLabel(a_package)); |
---|
278 | assertNotNull("no image provided", |
---|
279 | wbAdapter.getImageDescriptor(a_package)); |
---|
280 | |
---|
281 | /* |
---|
282 | * sanity checks on components_bananza.a_block |
---|
283 | */ |
---|
284 | assertNotNull("components_bananza.a_block not found", a_block); |
---|
285 | assertEquals("wrong element name", "a_block", |
---|
286 | a_block.getElementName()); |
---|
287 | assertEquals("wrong element full name", "components_bananza.a_block", |
---|
288 | a_block.getFullName()); |
---|
289 | assertTrue("fishy file path", |
---|
290 | a_block.getFilePath().endsWith("component_model.mo")); |
---|
291 | reg = a_block.getLocation().getRegion(); |
---|
292 | assertEquals("wrong start offset", 271, reg.getOffset()); |
---|
293 | assertEquals("wrong length", 296-271+1, reg.getLength()); |
---|
294 | |
---|
295 | /* check if element is adaptable */ |
---|
296 | assertTrue("element object must be adaptable", |
---|
297 | (a_block instanceof IAdaptable)); |
---|
298 | |
---|
299 | /* check if we can convert element to workbench adapter */ |
---|
300 | wbAdapter = (IWorkbenchAdapter)a_block.getAdapter(IWorkbenchAdapter.class); |
---|
301 | |
---|
302 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
303 | assertEquals("wrong label", "a_block", |
---|
304 | wbAdapter.getLabel(a_block)); |
---|
305 | assertNotNull("no image provided", |
---|
306 | wbAdapter.getImageDescriptor(a_block)); |
---|
307 | |
---|
308 | /* |
---|
309 | * sanity checks on components_bananza.a_class |
---|
310 | */ |
---|
311 | assertNotNull("components_bananza.a_class not found", a_class); |
---|
312 | assertEquals("wrong element name", "a_class", a_class.getElementName()); |
---|
313 | assertEquals("wrong element full name", "components_bananza.a_class", |
---|
314 | a_class.getFullName()); |
---|
315 | assertTrue("fishy file path", |
---|
316 | a_class.getFilePath().endsWith("component_model.mo")); |
---|
317 | reg = a_class.getLocation().getRegion(); |
---|
318 | assertEquals("wrong start offset", 132, reg.getOffset()); |
---|
319 | assertEquals("wrong length", 157-132+1, reg.getLength()); |
---|
320 | |
---|
321 | /* check if element is adaptable */ |
---|
322 | assertTrue("element object must be adaptable", |
---|
323 | (a_class instanceof IAdaptable)); |
---|
324 | |
---|
325 | /* check if we can convert element to workbench adapter */ |
---|
326 | wbAdapter = (IWorkbenchAdapter)a_class.getAdapter(IWorkbenchAdapter.class); |
---|
327 | |
---|
328 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
329 | assertEquals("wrong label", "a_class", |
---|
330 | wbAdapter.getLabel(a_class)); |
---|
331 | assertNotNull("no image provided", |
---|
332 | wbAdapter.getImageDescriptor(a_class)); |
---|
333 | |
---|
334 | /* |
---|
335 | * sanity checks on components_bananza.a_connector |
---|
336 | */ |
---|
337 | assertNotNull("components_bananza.a_connector not found", a_connector); |
---|
338 | assertEquals("wrong element name", "a_connector", |
---|
339 | a_connector.getElementName()); |
---|
340 | assertEquals("wrong element full name", "components_bananza.a_connector", |
---|
341 | a_connector.getFullName()); |
---|
342 | assertTrue("fishy file path", |
---|
343 | a_connector.getFilePath().endsWith("component_model.mo")); |
---|
344 | reg = a_connector.getLocation().getRegion(); |
---|
345 | assertEquals("wrong start offset", 194, reg.getOffset()); |
---|
346 | assertEquals("wrong length", 231-194+1, reg.getLength()); |
---|
347 | |
---|
348 | /* check if element is adaptable */ |
---|
349 | assertTrue("element object must be adaptable", |
---|
350 | (a_connector instanceof IAdaptable)); |
---|
351 | |
---|
352 | /* check if we can convert element to workbench adapter */ |
---|
353 | wbAdapter = (IWorkbenchAdapter)a_connector.getAdapter(IWorkbenchAdapter.class); |
---|
354 | |
---|
355 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
356 | assertEquals("wrong label", "a_connector", |
---|
357 | wbAdapter.getLabel(a_connector)); |
---|
358 | assertNotNull("no image provided", |
---|
359 | wbAdapter.getImageDescriptor(a_connector)); |
---|
360 | |
---|
361 | /* |
---|
362 | * sanity checks on components_bananza.a_function |
---|
363 | */ |
---|
364 | assertNotNull("components_bananza.a_function not found", a_function); |
---|
365 | assertEquals("wrong element name", "a_function", |
---|
366 | a_function.getElementName()); |
---|
367 | assertEquals("wrong element full name", "components_bananza.a_function", |
---|
368 | a_function.getFullName()); |
---|
369 | assertTrue("fishy file path", |
---|
370 | a_function.getFilePath().endsWith("component_model.mo")); |
---|
371 | reg = a_function.getLocation().getRegion(); |
---|
372 | assertEquals("wrong start offset", 302, reg.getOffset()); |
---|
373 | assertEquals("wrong length", 336-302+1, reg.getLength()); |
---|
374 | |
---|
375 | /* check if element is adaptable */ |
---|
376 | assertTrue("element object must be adaptable", |
---|
377 | (a_function instanceof IAdaptable)); |
---|
378 | |
---|
379 | /* check if we can convert element to workbench adapter */ |
---|
380 | wbAdapter = (IWorkbenchAdapter)a_function.getAdapter(IWorkbenchAdapter.class); |
---|
381 | |
---|
382 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
383 | assertEquals("wrong label", "a_function", |
---|
384 | wbAdapter.getLabel(a_function)); |
---|
385 | assertNotNull("no image provided", |
---|
386 | wbAdapter.getImageDescriptor(a_function)); |
---|
387 | |
---|
388 | /* |
---|
389 | * sanity checks on components_bananza.a_model |
---|
390 | */ |
---|
391 | assertNotNull("components_bananza.a_model not found", a_model); |
---|
392 | assertEquals("wrong element name", "a_model", a_model.getElementName()); |
---|
393 | assertEquals("wrong element full name", "components_bananza.a_model", |
---|
394 | a_model.getFullName()); |
---|
395 | assertTrue("fishy file path", |
---|
396 | a_model.getFilePath().endsWith("component_model.mo")); |
---|
397 | reg = a_model.getLocation().getRegion(); |
---|
398 | assertEquals("wrong start offset", 163, reg.getOffset()); |
---|
399 | assertEquals("wrong length", 188-163+1, reg.getLength()); |
---|
400 | |
---|
401 | /* check if element is adaptable */ |
---|
402 | assertTrue("element object must be adaptable", |
---|
403 | (a_model instanceof IAdaptable)); |
---|
404 | |
---|
405 | /* check if we can convert element to workbench adapter */ |
---|
406 | wbAdapter = (IWorkbenchAdapter)a_model.getAdapter(IWorkbenchAdapter.class); |
---|
407 | |
---|
408 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
409 | assertEquals("wrong label", "a_model", |
---|
410 | wbAdapter.getLabel(a_model)); |
---|
411 | assertNotNull("no image provided", |
---|
412 | wbAdapter.getImageDescriptor(a_model)); |
---|
413 | |
---|
414 | /* |
---|
415 | * sanity checks on components_bananza.a_record |
---|
416 | */ |
---|
417 | assertNotNull("components_bananza.a_record not found", a_record); |
---|
418 | assertEquals("wrong element name", "a_record", a_record.getElementName()); |
---|
419 | assertEquals("wrong element full name", "components_bananza.a_record", |
---|
420 | a_record.getFullName()); |
---|
421 | assertTrue("fishy file path", |
---|
422 | a_record.getFilePath().endsWith("component_model.mo")); |
---|
423 | reg = a_record.getLocation().getRegion(); |
---|
424 | assertEquals("wrong start offset", 237, reg.getOffset()); |
---|
425 | assertEquals("wrong length", 265-237+1, reg.getLength()); |
---|
426 | |
---|
427 | /* check if element is adaptable */ |
---|
428 | assertTrue("element object must be adaptable", |
---|
429 | (a_record instanceof IAdaptable)); |
---|
430 | |
---|
431 | /* check if we can convert element to workbench adapter */ |
---|
432 | wbAdapter = (IWorkbenchAdapter)a_record.getAdapter(IWorkbenchAdapter.class); |
---|
433 | |
---|
434 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
435 | assertEquals("wrong label", "a_record", |
---|
436 | wbAdapter.getLabel(a_record)); |
---|
437 | assertNotNull("no image provided", |
---|
438 | wbAdapter.getImageDescriptor(a_record)); |
---|
439 | |
---|
440 | /* |
---|
441 | * sanity checks on components_bananza.a_real |
---|
442 | */ |
---|
443 | assertNotNull("components_bananza.a_real not found", a_real); |
---|
444 | assertEquals("wrong element name", "a_real", a_real.getElementName()); |
---|
445 | assertEquals("wrong element full name", "components_bananza.a_real", |
---|
446 | a_real.getFullName()); |
---|
447 | assertEquals("wrong visibility", a_real.getVisibility(), |
---|
448 | IModelicaComponent.Visibility.PUBLIC); |
---|
449 | |
---|
450 | reg = a_real.getLocation().getRegion(); |
---|
451 | assertEquals("wrong start offset", 29, reg.getOffset()); |
---|
452 | assertEquals("wrong length", 58-29+1, reg.getLength()); |
---|
453 | |
---|
454 | /* check if element is adaptable */ |
---|
455 | assertTrue("element object must be adaptable", |
---|
456 | (a_real instanceof IAdaptable)); |
---|
457 | |
---|
458 | /* check if we can convert element to workbench adapter */ |
---|
459 | wbAdapter = (IWorkbenchAdapter)a_real.getAdapter(IWorkbenchAdapter.class); |
---|
460 | |
---|
461 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
462 | assertEquals("wrong label", "a_real", |
---|
463 | wbAdapter.getLabel(a_real)); |
---|
464 | assertNotNull("no image provided", |
---|
465 | wbAdapter.getImageDescriptor(a_real)); |
---|
466 | |
---|
467 | /* |
---|
468 | * sanity checks on components_bananza.a_undocumented_real |
---|
469 | */ |
---|
470 | assertNotNull("components_bananza.an_undocumented_real not found", |
---|
471 | an_undocumented_real); |
---|
472 | assertEquals("wrong element name", "an_undocumented_real", |
---|
473 | an_undocumented_real.getElementName()); |
---|
474 | assertEquals("wrong element full name", |
---|
475 | "components_bananza.an_undocumented_real", |
---|
476 | an_undocumented_real.getFullName()); |
---|
477 | assertEquals("wrong visibility", an_undocumented_real.getVisibility(), |
---|
478 | IModelicaComponent.Visibility.PUBLIC); |
---|
479 | reg = an_undocumented_real.getLocation().getRegion(); |
---|
480 | assertEquals("wrong start offset", 64, reg.getOffset()); |
---|
481 | assertEquals("wrong length", 89-64+1, reg.getLength()); |
---|
482 | |
---|
483 | /* check if element is adaptable */ |
---|
484 | assertTrue("element object must be adaptable", |
---|
485 | (an_undocumented_real instanceof IAdaptable)); |
---|
486 | |
---|
487 | /* check if we can convert element to workbench adapter */ |
---|
488 | wbAdapter = |
---|
489 | (IWorkbenchAdapter)an_undocumented_real.getAdapter(IWorkbenchAdapter.class); |
---|
490 | |
---|
491 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
492 | assertEquals("wrong label", "an_undocumented_real", |
---|
493 | wbAdapter.getLabel(an_undocumented_real)); |
---|
494 | assertNotNull("no image provided", |
---|
495 | wbAdapter.getImageDescriptor(an_undocumented_real)); |
---|
496 | |
---|
497 | /* |
---|
498 | * sanity checks on components_bananza.a_protected_integer |
---|
499 | */ |
---|
500 | assertNotNull("components_bananza.a_protected_integer not found", |
---|
501 | a_protected_integer); |
---|
502 | assertEquals("wrong element name", "a_protected_integer", |
---|
503 | a_protected_integer.getElementName()); |
---|
504 | assertEquals("wrong element full name", |
---|
505 | "components_bananza.a_protected_integer", |
---|
506 | a_protected_integer.getFullName()); |
---|
507 | assertEquals("wrong visibility", a_protected_integer.getVisibility(), |
---|
508 | IModelicaComponent.Visibility.PROTECTED); |
---|
509 | reg = a_protected_integer.getLocation().getRegion(); |
---|
510 | assertEquals("wrong start offset", 407, reg.getOffset()); |
---|
511 | assertEquals("wrong length", 451-407+1, reg.getLength()); |
---|
512 | |
---|
513 | /* check if element is adaptable */ |
---|
514 | assertTrue("element object must be adaptable", |
---|
515 | (a_protected_integer instanceof IAdaptable)); |
---|
516 | |
---|
517 | /* check if we can convert element to workbench adapter */ |
---|
518 | wbAdapter = |
---|
519 | (IWorkbenchAdapter)a_protected_integer.getAdapter(IWorkbenchAdapter.class); |
---|
520 | |
---|
521 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
522 | assertEquals("wrong label", "a_protected_integer", |
---|
523 | wbAdapter.getLabel(a_protected_integer)); |
---|
524 | assertNotNull("no image provided", |
---|
525 | wbAdapter.getImageDescriptor(a_protected_integer)); |
---|
526 | |
---|
527 | /* |
---|
528 | * sanity checks on components_bananza.a_protected_real |
---|
529 | */ |
---|
530 | assertNotNull("components_bananza.a_protected_real not found", |
---|
531 | a_protected_real); |
---|
532 | assertEquals("wrong element name", "a_protected_real", |
---|
533 | a_protected_real.getElementName()); |
---|
534 | assertEquals("wrong element full name", |
---|
535 | "components_bananza.a_protected_real", |
---|
536 | a_protected_real.getFullName()); |
---|
537 | assertEquals("wrong visibility", a_protected_real.getVisibility(), |
---|
538 | IModelicaComponent.Visibility.PROTECTED); |
---|
539 | reg = a_protected_real.getLocation().getRegion(); |
---|
540 | assertEquals("wrong start offset", 380, reg.getOffset()); |
---|
541 | assertEquals("wrong length", 401-380+1, reg.getLength()); |
---|
542 | |
---|
543 | /* check if element is adaptable */ |
---|
544 | assertTrue("element object must be adaptable", |
---|
545 | (a_protected_real instanceof IAdaptable)); |
---|
546 | |
---|
547 | /* check if we can convert element to workbench adapter */ |
---|
548 | wbAdapter = |
---|
549 | (IWorkbenchAdapter)a_protected_real.getAdapter(IWorkbenchAdapter.class); |
---|
550 | |
---|
551 | assertNotNull("could not fetch workbench adapter", wbAdapter); |
---|
552 | assertEquals("wrong label", "a_protected_real", |
---|
553 | wbAdapter.getLabel(a_protected_real)); |
---|
554 | assertNotNull("no image provided", |
---|
555 | wbAdapter.getImageDescriptor(a_protected_real)); |
---|
556 | } |
---|
557 | |
---|
558 | /** |
---|
559 | * do some sanity checks on InnerClass' imports |
---|
560 | */ |
---|
561 | public void testImports() |
---|
562 | throws ConnectException, UnexpectedReplyException, InvocationError, |
---|
563 | CompilerInstantiationException, CoreException |
---|
564 | { |
---|
565 | int importCounter = 0; |
---|
566 | IModelicaClass importedPackage; |
---|
567 | for (IModelicaImport imp : importRichModel.getImports()) |
---|
568 | { |
---|
569 | importCounter++; |
---|
570 | |
---|
571 | /* |
---|
572 | * we are expecting 8 import statments in following order: |
---|
573 | * 1. qualified (import Modelica) |
---|
574 | * 2. qualified (import Modelica.Math.sin) |
---|
575 | * 3. unqualified (import Modelica.*) |
---|
576 | * 4. renaming (import mm = Modelica.Math) |
---|
577 | * 5. local renaming (import foo = hepp) |
---|
578 | * 6. local qualified (import hepp.hopp) |
---|
579 | * 7. local unqualified (import hepp.*) |
---|
580 | * 8. local single definition |
---|
581 | * (import root_package.root_package_model) |
---|
582 | */ |
---|
583 | |
---|
584 | switch (importCounter) |
---|
585 | { |
---|
586 | case 1: // import Modelica |
---|
587 | assertEquals(IModelicaImport.Type.QUALIFIED, imp.getType()); |
---|
588 | importedPackage = imp.getImportedPackage(); |
---|
589 | assertEquals("wrong imported package returned", |
---|
590 | "Modelica", |
---|
591 | importedPackage.getFullName()); |
---|
592 | break; |
---|
593 | case 2: // import Modelica.Math.sin |
---|
594 | assertEquals(IModelicaImport.Type.QUALIFIED, imp.getType()); |
---|
595 | importedPackage = imp.getImportedPackage(); |
---|
596 | assertEquals("wrong imported package returned", |
---|
597 | "Modelica.Math.sin", |
---|
598 | importedPackage.getFullName()); |
---|
599 | break; |
---|
600 | case 3: // import Modelica.* |
---|
601 | assertEquals(IModelicaImport.Type.UNQUALIFIED, imp.getType()); |
---|
602 | importedPackage = imp.getImportedPackage(); |
---|
603 | assertEquals("wrong imported package returned", |
---|
604 | "Modelica", |
---|
605 | importedPackage.getFullName()); |
---|
606 | break; |
---|
607 | case 4: // import mm = Modelica.Math |
---|
608 | assertEquals(IModelicaImport.Type.RENAMING, imp.getType()); |
---|
609 | assertEquals("mm", imp.getAlias()); |
---|
610 | break; |
---|
611 | case 5: // import foo = hepp |
---|
612 | assertEquals(IModelicaImport.Type.RENAMING, imp.getType()); |
---|
613 | assertEquals("foo", imp.getAlias()); |
---|
614 | break; |
---|
615 | case 6: // import hepp.hopp |
---|
616 | assertEquals(IModelicaImport.Type.QUALIFIED, imp.getType()); |
---|
617 | break; |
---|
618 | case 7: // import hepp.* |
---|
619 | assertEquals(IModelicaImport.Type.UNQUALIFIED, imp.getType()); |
---|
620 | break; |
---|
621 | case 8: // import root_package.root_package_model |
---|
622 | // TODO this is broken, fix this |
---|
623 | // assertEquals(IModelicaImport.Type.QUALIFIED, imp.getType()); |
---|
624 | break; |
---|
625 | default: |
---|
626 | fail("unexpectedly many imports found"); |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | assertFalse("did not find all import statments", |
---|
631 | importCounter < 4); |
---|
632 | |
---|
633 | } |
---|
634 | |
---|
635 | public void testIsEncapsulated() |
---|
636 | throws ConnectException, CompilerInstantiationException, UnexpectedReplyException, |
---|
637 | InvocationError, CoreException |
---|
638 | { |
---|
639 | IModelicaClass clazz; |
---|
640 | IStandardLibrary stdLib = |
---|
641 | ModelicaCore.getModelicaRoot().getStandardLibrary(null); |
---|
642 | |
---|
643 | /* |
---|
644 | * check correctness of isEncapsulated() in classes from standard library |
---|
645 | */ |
---|
646 | clazz = stdLib.getPackage("Modelica.Blocks.Examples.BusUsage"); |
---|
647 | assertNotNull(clazz); |
---|
648 | assertTrue("wrong encapsulated status", clazz.isEncapsulated()); |
---|
649 | |
---|
650 | clazz = stdLib.getPackage("Modelica.Math.acos"); |
---|
651 | assertNotNull(clazz); |
---|
652 | assertFalse("wrong encapsulated status", clazz.isEncapsulated()); |
---|
653 | |
---|
654 | clazz = stdLib.getPackage("Modelica.Mechanics.Rotational.Examples.Friction"); |
---|
655 | assertNotNull(clazz); |
---|
656 | assertTrue("wrong encapsulated status", clazz.isEncapsulated()); |
---|
657 | |
---|
658 | clazz = stdLib.getPackage("Modelica.Mechanics.Rotational.Examples.Friction"); |
---|
659 | assertNotNull(clazz); |
---|
660 | assertTrue("wrong encapsulated status", clazz.isEncapsulated()); |
---|
661 | |
---|
662 | clazz = stdLib.getPackage("Modelica"); |
---|
663 | assertNotNull(clazz); |
---|
664 | assertFalse("wrong encapsulated status", clazz.isEncapsulated()); |
---|
665 | |
---|
666 | /* |
---|
667 | * check correctness of isEncapsulated() on classes from area51 project |
---|
668 | */ |
---|
669 | |
---|
670 | /* test inner packager */ |
---|
671 | clazz = proj.getClass("root_model"); |
---|
672 | assertNotNull(clazz); |
---|
673 | assertTrue("wrong encapsulated status", clazz.isEncapsulated()); |
---|
674 | |
---|
675 | clazz = proj.getClass("file_package1"); |
---|
676 | assertNotNull(clazz); |
---|
677 | assertFalse("wrong encapsulated status", clazz.isEncapsulated()); |
---|
678 | |
---|
679 | clazz = proj.getClass("file_package2"); |
---|
680 | assertNotNull(clazz); |
---|
681 | assertTrue("wrong encapsulated status", clazz.isEncapsulated()); |
---|
682 | |
---|
683 | clazz = proj.getClass("nested_models.foo.bar"); |
---|
684 | assertNotNull(clazz); |
---|
685 | assertFalse("wrong encapsulated status", clazz.isEncapsulated()); |
---|
686 | |
---|
687 | /* test folder packages */ |
---|
688 | clazz = proj.getClass("root_package"); |
---|
689 | assertNotNull(clazz); |
---|
690 | assertFalse("wrong encapsulated status", clazz.isEncapsulated()); |
---|
691 | |
---|
692 | clazz = proj.getClass("root_package.sub_package"); |
---|
693 | assertNotNull(clazz); |
---|
694 | assertFalse("wrong encapsulated status", clazz.isEncapsulated()); |
---|
695 | //TODO this is broken, fix it |
---|
696 | // clazz = proj.getClass("root_package.sub_package.leaf_package"); |
---|
697 | // assertNotNull(clazz); |
---|
698 | // assertTrue("wrong encapsulated status", clazz.isEncapsulated()); |
---|
699 | |
---|
700 | clazz = proj.getClass("childless_package"); |
---|
701 | assertNotNull(clazz); |
---|
702 | assertTrue("wrong encapsulated status", clazz.isEncapsulated()); |
---|
703 | } |
---|
704 | |
---|
705 | /** |
---|
706 | * test that the when a class definiton is changed in the disk |
---|
707 | * it's location is updated |
---|
708 | */ |
---|
709 | public void testLocationUpdates() throws CoreException, |
---|
710 | ConnectException, CompilerInstantiationException, |
---|
711 | UnexpectedReplyException, InvocationError |
---|
712 | { |
---|
713 | |
---|
714 | /* create initial class definitons */ |
---|
715 | createClassDef(); |
---|
716 | /* |
---|
717 | * check the initial locations |
---|
718 | */ |
---|
719 | IModelicaClass clazz; |
---|
720 | IRegion defReg; |
---|
721 | |
---|
722 | /* checks on EquationComponent */ |
---|
723 | clazz = proj.getClass("EquationComponent"); |
---|
724 | assertNotNull(clazz); |
---|
725 | |
---|
726 | defReg = clazz.getLocation().getRegion(); |
---|
727 | assertEquals("wrong start offset", 111, defReg.getOffset()); |
---|
728 | assertEquals("wrong length", 239-111+1, defReg.getLength()); |
---|
729 | |
---|
730 | /* checks on EquationComponent.R */ |
---|
731 | clazz = proj.getClass("EquationComponent.R"); |
---|
732 | assertNotNull(clazz); |
---|
733 | |
---|
734 | defReg = clazz.getLocation().getRegion(); |
---|
735 | assertEquals("wrong start offset", 137, defReg.getOffset()); |
---|
736 | assertEquals("wrong length", 167-137+1, defReg.getLength()); |
---|
737 | |
---|
738 | |
---|
739 | /* modify class defintions */ |
---|
740 | modifyClassDef(); |
---|
741 | |
---|
742 | /* |
---|
743 | * check that locations where updated |
---|
744 | */ |
---|
745 | /* checks on EquationComponent */ |
---|
746 | clazz = proj.getClass("EquationComponent"); |
---|
747 | assertNotNull(clazz); |
---|
748 | |
---|
749 | defReg = clazz.getLocation().getRegion(); |
---|
750 | assertEquals("wrong start offset", 25, defReg.getOffset()); |
---|
751 | assertEquals("wrong length", 179-25+1, defReg.getLength()); |
---|
752 | |
---|
753 | /* checks on EquationComponent.R */ |
---|
754 | clazz = proj.getClass("EquationComponent.R"); |
---|
755 | assertNotNull(clazz); |
---|
756 | |
---|
757 | defReg = clazz.getLocation().getRegion(); |
---|
758 | assertEquals("wrong start offset", 76, defReg.getOffset()); |
---|
759 | assertEquals("wrong length", 106-76+1, defReg.getLength()); |
---|
760 | |
---|
761 | } |
---|
762 | |
---|
763 | /** |
---|
764 | * utility function for the testLocationUpdates test |
---|
765 | * |
---|
766 | * overwrites the initial definitions in a source file with |
---|
767 | * some modifications |
---|
768 | */ |
---|
769 | private void modifyClassDef() throws CoreException |
---|
770 | { |
---|
771 | String contents = |
---|
772 | "// funny comment removed\n" + |
---|
773 | "class EquationComponent\n" + |
---|
774 | " /* records are nice */\n" + |
---|
775 | " record R\n" + |
---|
776 | " Real x,y;\n" + |
---|
777 | " end R;\n" + |
---|
778 | "\n" + |
---|
779 | " R a,b,c;\n" + |
---|
780 | "equation\n" + |
---|
781 | " a = if true then b else c;\n" + |
---|
782 | "end EquationComponent;"; |
---|
783 | |
---|
784 | IProject project = proj.getWrappedProject(); |
---|
785 | IFile file = project.getFile(CHANGING_FILE); |
---|
786 | |
---|
787 | file.setContents(Utility.getByteStream(contents), true, true, null); |
---|
788 | } |
---|
789 | |
---|
790 | /** |
---|
791 | * utility function for the testLocationUpdates test |
---|
792 | * |
---|
793 | * Creates the initial definitions in a source file |
---|
794 | * |
---|
795 | */ |
---|
796 | private void createClassDef() throws CoreException |
---|
797 | { |
---|
798 | String contents = |
---|
799 | "/*\n" + |
---|
800 | " * However, don't ever expect you can parse\n" + |
---|
801 | " * the error message from the compiler.\n" + |
---|
802 | " * -- Adrian Pop\n" + |
---|
803 | " */\n" + |
---|
804 | "class EquationComponent\n" + |
---|
805 | " record R\n" + |
---|
806 | " Real x,y;\n" + |
---|
807 | " end R;\n" + |
---|
808 | " R a,b,c;\n" + |
---|
809 | "equation\n" + |
---|
810 | " a = if true then b else c;\n" + |
---|
811 | "end EquationComponent;"; |
---|
812 | |
---|
813 | IProject project = proj.getWrappedProject(); |
---|
814 | IFile file = project.getFile(CHANGING_FILE); |
---|
815 | |
---|
816 | file.create(Utility.getByteStream(contents), true, null); |
---|
817 | } |
---|
818 | } |
---|