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.util.Collection; |
---|
45 | import java.util.Vector; |
---|
46 | |
---|
47 | import org.eclipse.core.resources.IFile; |
---|
48 | import org.eclipse.core.resources.IProject; |
---|
49 | import org.modelica.mdt.core.CompilerProxy; |
---|
50 | import org.modelica.mdt.core.IModelicaClass; |
---|
51 | import org.modelica.mdt.core.IModelicaClass.Type; |
---|
52 | import org.modelica.mdt.core.compiler.CompilerInstantiationException; |
---|
53 | import org.modelica.mdt.core.compiler.ConnectException; |
---|
54 | import org.modelica.mdt.core.compiler.IElementLocation; |
---|
55 | import org.modelica.mdt.core.compiler.InvocationError; |
---|
56 | import org.modelica.mdt.core.compiler.UnexpectedReplyException; |
---|
57 | import org.modelica.mdt.test.util.Area51Projects; |
---|
58 | import org.modelica.mdt.test.util.Utility; |
---|
59 | |
---|
60 | import junit.framework.TestCase; |
---|
61 | |
---|
62 | /** |
---|
63 | * test org.modelica.mdt.core.CompilerProxy class' code |
---|
64 | */ |
---|
65 | public class TestCompilerProxy extends TestCase |
---|
66 | { |
---|
67 | /* a source code file use in some tests */ |
---|
68 | private IFile nested_models_mo; |
---|
69 | |
---|
70 | protected void setUp() |
---|
71 | throws ConnectException, CompilerInstantiationException |
---|
72 | { |
---|
73 | Area51Projects.createProjects(); |
---|
74 | |
---|
75 | /* |
---|
76 | * fetch reference to nested_models.mo file |
---|
77 | * from Area51Projects modelica project |
---|
78 | */ |
---|
79 | IProject proj = Utility.getProject( |
---|
80 | Area51Projects.MODELICA_PROJECT_NAME).getProject(); |
---|
81 | |
---|
82 | nested_models_mo = proj.getFile("nested_models.mo"); |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * test CompilerProxy.getRestrictionType() |
---|
88 | * @throws CompilerInstantiationException |
---|
89 | */ |
---|
90 | public void testGetRestrictionType() |
---|
91 | throws ConnectException, UnexpectedReplyException, |
---|
92 | CompilerInstantiationException |
---|
93 | { |
---|
94 | /* we need to load modelica package */ |
---|
95 | Collection<IModelicaClass> stdPackages = |
---|
96 | CompilerProxy.getStandardLibrary(); |
---|
97 | |
---|
98 | /* make some checks on the returned names of the standard packages */ |
---|
99 | Vector<String> v = new Vector<String>(); |
---|
100 | for (IModelicaClass clazz : stdPackages) |
---|
101 | { |
---|
102 | v.add(clazz.getElementName()); |
---|
103 | } |
---|
104 | assertTrue(v.size() >= 1); |
---|
105 | assertTrue(v.contains("Modelica")); |
---|
106 | |
---|
107 | if (CompilerProxy.getRestrictionType("Modelica") != Type.PACKAGE) |
---|
108 | { |
---|
109 | fail("Modelica class' restriction type is wrong"); |
---|
110 | } |
---|
111 | if (CompilerProxy.getRestrictionType("Modelica.Blocks.Examples.BusUsage") |
---|
112 | != Type.MODEL) |
---|
113 | { |
---|
114 | fail("Modelica.Blocks.Examples.BusUsage class' " + |
---|
115 | "restriction type is wrong"); |
---|
116 | } |
---|
117 | if (CompilerProxy.getRestrictionType("Modelica.Math.log") |
---|
118 | != Type.FUNCTION) |
---|
119 | { |
---|
120 | fail("Modelica.Math.log class' restriction type is wrong"); |
---|
121 | } |
---|
122 | if (CompilerProxy.getRestrictionType("Modelica.Icons.Record") |
---|
123 | != Type.RECORD) |
---|
124 | { |
---|
125 | fail("Modelica.Icons.Record class' restriction type is wrong"); |
---|
126 | } |
---|
127 | if (CompilerProxy.getRestrictionType("Modelica.Electrical.Analog.Interfaces.Pin") |
---|
128 | != Type.CONNECTOR) |
---|
129 | { |
---|
130 | fail("Modelica.Blocks.Interfaces.BooleanPort class' " + |
---|
131 | "restriction type is wrong"); |
---|
132 | } |
---|
133 | if (CompilerProxy.getRestrictionType("Modelica.Blocks.Continuous.Der") |
---|
134 | != Type.BLOCK) |
---|
135 | { |
---|
136 | fail("Modelica.Blocks.Continuous.Der class' " + |
---|
137 | "restriction type is wrong"); |
---|
138 | } |
---|
139 | if (CompilerProxy.getRestrictionType("Modelica.SIunits.Lethargy") |
---|
140 | != Type.TYPE) |
---|
141 | { |
---|
142 | fail("Modelica.SIunits.Lethargy class' restriction type is wrong"); |
---|
143 | } |
---|
144 | |
---|
145 | CompilerProxy.loadSourceFile(nested_models_mo); |
---|
146 | if (CompilerProxy.getRestrictionType("hepp.hehehe") |
---|
147 | != Type.CLASS) |
---|
148 | { |
---|
149 | fail("hepp.hehehe class' restriction type is wrong"); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * test CompilerProxy.getElementLocation() |
---|
155 | * @throws CompilerInstantiationException |
---|
156 | */ |
---|
157 | public void testGetClassLocation() |
---|
158 | throws ConnectException, UnexpectedReplyException, InvocationError, |
---|
159 | CompilerInstantiationException |
---|
160 | { |
---|
161 | CompilerProxy.loadSourceFile(nested_models_mo); |
---|
162 | |
---|
163 | /* |
---|
164 | * we are basicaly only interested in getting the right line number |
---|
165 | */ |
---|
166 | IElementLocation loc = CompilerProxy.getClassLocation("nested_models"); |
---|
167 | assertTrue(loc.getPath().endsWith("nested_models.mo")); |
---|
168 | assertEquals(loc.getLine(), 1); |
---|
169 | |
---|
170 | loc = CompilerProxy.getClassLocation("nested_models.hepp"); |
---|
171 | assertEquals(loc.getLine(), 3); |
---|
172 | |
---|
173 | loc = CompilerProxy.getClassLocation("nested_models.foo"); |
---|
174 | assertEquals(loc.getLine(), 4); |
---|
175 | |
---|
176 | loc = CompilerProxy.getClassLocation("nested_models.foo.bar"); |
---|
177 | assertEquals(loc.getLine(), 5); |
---|
178 | |
---|
179 | loc = CompilerProxy.getClassLocation("muu"); |
---|
180 | assertEquals(loc.getLine(), 8); |
---|
181 | |
---|
182 | loc = CompilerProxy.getClassLocation("foo"); |
---|
183 | assertEquals(loc.getLine(), 14); |
---|
184 | |
---|
185 | loc = CompilerProxy.getClassLocation("hej"); |
---|
186 | assertEquals(loc.getLine(), 19); |
---|
187 | |
---|
188 | loc = CompilerProxy.getClassLocation("hej.ine_paketen"); |
---|
189 | assertEquals(loc.getLine(), 20); |
---|
190 | |
---|
191 | loc = CompilerProxy.getClassLocation("hej.hejhej"); |
---|
192 | assertEquals(loc.getLine(), 22); |
---|
193 | |
---|
194 | loc = CompilerProxy.getClassLocation("hej.hejhej.foo"); |
---|
195 | assertEquals(loc.getLine(), 23); |
---|
196 | |
---|
197 | loc = CompilerProxy.getClassLocation("hepp"); |
---|
198 | assertEquals(loc.getLine(), 30); |
---|
199 | |
---|
200 | loc = CompilerProxy.getClassLocation("hepp.hopp"); |
---|
201 | assertEquals(loc.getLine(), 31); |
---|
202 | |
---|
203 | loc = CompilerProxy.getClassLocation("hepp.hehehe"); |
---|
204 | assertEquals(loc.getLine(), 33); |
---|
205 | |
---|
206 | } |
---|
207 | } |
---|