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