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