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.IProject; |
---|
49 | import org.eclipse.core.resources.ResourcesPlugin; |
---|
50 | import org.eclipse.core.runtime.CoreException; |
---|
51 | import org.eclipse.core.resources.IWorkspaceRoot; |
---|
52 | |
---|
53 | import org.modelica.mdt.core.IModelicaElementChange; |
---|
54 | import org.modelica.mdt.core.IModelicaElementChangeListener; |
---|
55 | import org.modelica.mdt.core.IModelicaProject; |
---|
56 | import org.modelica.mdt.core.IModelicaRoot; |
---|
57 | import org.modelica.mdt.core.ModelicaCore; |
---|
58 | import org.modelica.mdt.internal.core.CorePlugin; |
---|
59 | import org.modelica.mdt.test.util.Area51Projects; |
---|
60 | import org.modelica.mdt.test.util.Utility; |
---|
61 | |
---|
62 | import junit.framework.TestCase; |
---|
63 | |
---|
64 | /** |
---|
65 | * @author Elmir Jagudin |
---|
66 | */ |
---|
67 | public class TestModelicaRoot extends TestCase |
---|
68 | { |
---|
69 | /* this flags are set from the modelica element change listener */ |
---|
70 | private boolean simpleProjectAdded = false; |
---|
71 | private boolean modelicaProjectAdded = false; |
---|
72 | private boolean simpleProjectRemoved = false; |
---|
73 | private boolean modelicaProjectRemoved = false; |
---|
74 | |
---|
75 | public class ElementListener implements IModelicaElementChangeListener |
---|
76 | { |
---|
77 | |
---|
78 | public void elementsChanged(Collection<IModelicaElementChange> changes) |
---|
79 | { |
---|
80 | for (IModelicaElementChange change : changes) |
---|
81 | { |
---|
82 | Object res = change.getElement(); |
---|
83 | if (!(res instanceof IModelicaProject)) |
---|
84 | { |
---|
85 | /* we are only interested in changes on projects */ |
---|
86 | break; |
---|
87 | } |
---|
88 | IModelicaProject proj = (IModelicaProject)res; |
---|
89 | |
---|
90 | switch (change.getChangeType()) |
---|
91 | { |
---|
92 | case ADDED: |
---|
93 | if (proj.getElementName().equals(PROJECT_NAME_SIM_EXTRA)) |
---|
94 | { |
---|
95 | simpleProjectAdded = true; |
---|
96 | } |
---|
97 | else if (proj.getElementName().equals(PROJECT_NAME_MOD_EXTRA)) |
---|
98 | { |
---|
99 | modelicaProjectAdded = true; |
---|
100 | } |
---|
101 | |
---|
102 | break; |
---|
103 | case REMOVED: |
---|
104 | if (proj.getElementName().equals(PROJECT_NAME_SIM_EXTRA)) |
---|
105 | { |
---|
106 | simpleProjectRemoved = true; |
---|
107 | } |
---|
108 | else if (proj.getElementName().equals(PROJECT_NAME_MOD_EXTRA)) |
---|
109 | { |
---|
110 | modelicaProjectRemoved = true; |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | /* names of modelica projects */ |
---|
118 | private Vector<String> modelicaProjects = new Vector<String>(2); |
---|
119 | |
---|
120 | /* names of simple (no modelica) projects */ |
---|
121 | private Vector<String> simpleProjects = new Vector<String>(2); |
---|
122 | |
---|
123 | private static final String PROJECT_NAME_1 = |
---|
124 | TestModelicaRoot.class.getName() + "1"; |
---|
125 | private static final String PROJECT_NAME_2 = |
---|
126 | TestModelicaRoot.class.getName() + "2"; |
---|
127 | private static final String PROJECT_NAME_3 = |
---|
128 | TestModelicaRoot.class.getName() + "3"; |
---|
129 | |
---|
130 | /* the SIMple project which is added and removed */ |
---|
131 | private static final String PROJECT_NAME_SIM_EXTRA = |
---|
132 | TestModelicaRoot.class.getName() + "_SIM"; |
---|
133 | |
---|
134 | /* the MODelica project which is added and removed */ |
---|
135 | private static final String PROJECT_NAME_MOD_EXTRA = |
---|
136 | TestModelicaRoot.class.getName() + "_MOD"; |
---|
137 | |
---|
138 | /* name of a non existent project */ |
---|
139 | private static final String PROJECT_NAME_NON_EXISTENT = |
---|
140 | TestModelicaRoot.class.getName() + "_NON_EXISTENT"; |
---|
141 | |
---|
142 | |
---|
143 | private IWorkspaceRoot workspaceRoot = |
---|
144 | ResourcesPlugin.getWorkspace().getRoot(); |
---|
145 | private IModelicaRoot modelicaRoot = |
---|
146 | ModelicaCore.getModelicaRoot(); |
---|
147 | |
---|
148 | @Override |
---|
149 | protected void setUp() throws CoreException |
---|
150 | { |
---|
151 | Area51Projects.createProjects(); |
---|
152 | |
---|
153 | /* |
---|
154 | * create a modelica project |
---|
155 | */ |
---|
156 | IProject project = workspaceRoot.getProject(PROJECT_NAME_1); |
---|
157 | if (!project.exists()) |
---|
158 | { |
---|
159 | IModelicaProject moProj = |
---|
160 | ModelicaCore.getModelicaRoot().createProject(PROJECT_NAME_1); |
---|
161 | assertNotNull("failed to create project", moProj); |
---|
162 | } |
---|
163 | |
---|
164 | /* |
---|
165 | * create a regular projects |
---|
166 | */ |
---|
167 | project = workspaceRoot.getProject(PROJECT_NAME_2); |
---|
168 | if (!project.exists()) |
---|
169 | { |
---|
170 | project.create(null); |
---|
171 | project.open(null); |
---|
172 | } |
---|
173 | |
---|
174 | /* |
---|
175 | * create a regular projects which remains closed |
---|
176 | */ |
---|
177 | project = workspaceRoot.getProject(PROJECT_NAME_3); |
---|
178 | if (!project.exists()) |
---|
179 | { |
---|
180 | project.create(null); |
---|
181 | } |
---|
182 | |
---|
183 | assertTrue(Collections.addAll(modelicaProjects, |
---|
184 | PROJECT_NAME_1)); |
---|
185 | |
---|
186 | assertTrue(Collections.addAll(simpleProjects, |
---|
187 | PROJECT_NAME_2, PROJECT_NAME_3)); |
---|
188 | |
---|
189 | |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * Test if IModelicaRoot.getProjects() (implemented by ModelicaRoot) |
---|
194 | * works as prescribed. |
---|
195 | */ |
---|
196 | public void testGetProjects() throws CoreException |
---|
197 | { |
---|
198 | |
---|
199 | IModelicaProject mproj; |
---|
200 | for (Object p : modelicaRoot.getProjects()) |
---|
201 | { |
---|
202 | mproj = (IModelicaProject)p; |
---|
203 | |
---|
204 | if (modelicaProjects.contains(mproj.getElementName())) |
---|
205 | { |
---|
206 | if (mproj.getProject().isOpen()) |
---|
207 | { |
---|
208 | assertTrue("project without modelica nature wrapped", |
---|
209 | ((IModelicaProject)p).getProject(). |
---|
210 | hasNature(CorePlugin.MODELICA_NATURE)); |
---|
211 | } |
---|
212 | } |
---|
213 | else if (simpleProjects.contains(mproj.getElementName())) |
---|
214 | { |
---|
215 | if (mproj.getProject().isOpen()) |
---|
216 | { |
---|
217 | assertFalse("project with modelica nature not wrapped", |
---|
218 | mproj.getProject().hasNature(CorePlugin.MODELICA_NATURE)); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * Test if IModelicaRoot.getProject() (implemented by ModelicaRoot) |
---|
226 | * works as prescribed. |
---|
227 | */ |
---|
228 | public void testGetProject() |
---|
229 | { |
---|
230 | IModelicaProject proj; |
---|
231 | |
---|
232 | proj = modelicaRoot.getProject(PROJECT_NAME_1); |
---|
233 | assertEquals(PROJECT_NAME_1, proj.getElementName()); |
---|
234 | |
---|
235 | proj = modelicaRoot.getProject(PROJECT_NAME_3); |
---|
236 | assertEquals(PROJECT_NAME_3, proj.getElementName()); |
---|
237 | |
---|
238 | proj = modelicaRoot.getProject(PROJECT_NAME_2); |
---|
239 | assertEquals(PROJECT_NAME_2, proj.getElementName()); |
---|
240 | |
---|
241 | proj = modelicaRoot.getProject(PROJECT_NAME_NON_EXISTENT); |
---|
242 | assertNull("hmm, an non-existent project found?", proj); |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | /** |
---|
247 | * Test adding and removing projects to the workspace and check |
---|
248 | * if ModelicaRoot picks up the changes. |
---|
249 | */ |
---|
250 | public void testChangesToWorkspace() throws CoreException |
---|
251 | { |
---|
252 | ModelicaCore.getModelicaRoot(). |
---|
253 | addModelicaElementChangeListener(new ElementListener()); |
---|
254 | |
---|
255 | /* |
---|
256 | * check if ModelicaRoot picks up additions of projects |
---|
257 | */ |
---|
258 | IProject simpleProject = |
---|
259 | workspaceRoot.getProject(PROJECT_NAME_SIM_EXTRA); |
---|
260 | simpleProject.create(null); |
---|
261 | simpleProject.open(null); |
---|
262 | |
---|
263 | IProject modelicaProject = |
---|
264 | ModelicaCore.getModelicaRoot(). |
---|
265 | createProject(PROJECT_NAME_MOD_EXTRA).getProject(); |
---|
266 | modelicaProject.open(null); |
---|
267 | |
---|
268 | |
---|
269 | /* wait for the changes to kick throug */ |
---|
270 | while (!simpleProjectAdded && !modelicaProjectAdded) |
---|
271 | { |
---|
272 | Utility.sleep(this, 105); |
---|
273 | } |
---|
274 | |
---|
275 | boolean simpleProjFound = false; |
---|
276 | boolean modelicaProjFound = false; |
---|
277 | |
---|
278 | for (Object proj : modelicaRoot.getProjects()) |
---|
279 | { |
---|
280 | if (proj instanceof IModelicaProject) |
---|
281 | { |
---|
282 | String name = ((IModelicaProject)proj).getElementName(); |
---|
283 | |
---|
284 | if (name.equals(PROJECT_NAME_MOD_EXTRA)) |
---|
285 | { |
---|
286 | modelicaProjFound = true; |
---|
287 | } |
---|
288 | else if (name.equals(PROJECT_NAME_SIM_EXTRA)) |
---|
289 | { |
---|
290 | simpleProjFound = true; |
---|
291 | } |
---|
292 | } |
---|
293 | } |
---|
294 | assertTrue("ModelicaRoot did not pick up addition of " + |
---|
295 | PROJECT_NAME_SIM_EXTRA, simpleProjFound); |
---|
296 | |
---|
297 | assertTrue("ModelicaRoot did not pick up addition of " + |
---|
298 | PROJECT_NAME_MOD_EXTRA, modelicaProjFound); |
---|
299 | |
---|
300 | /* check if ModelicaRoot picks up removal of a project */ |
---|
301 | simpleProject.delete(true, true, null); |
---|
302 | modelicaProject.delete(true, true, null); |
---|
303 | |
---|
304 | /* wait for the changes to kick throug */ |
---|
305 | while (!simpleProjectRemoved && !modelicaProjectRemoved) |
---|
306 | { |
---|
307 | Utility.sleep(this, 97); |
---|
308 | } |
---|
309 | |
---|
310 | for (Object proj : modelicaRoot.getProjects()) |
---|
311 | { |
---|
312 | if (proj instanceof IModelicaProject) |
---|
313 | { |
---|
314 | String name = ((IModelicaProject)proj).getElementName(); |
---|
315 | |
---|
316 | if (name.equals(PROJECT_NAME_MOD_EXTRA)) |
---|
317 | { |
---|
318 | fail("ModelicaRoot did not pick up removal of " + |
---|
319 | PROJECT_NAME_MOD_EXTRA); |
---|
320 | } |
---|
321 | else if (name.equals(PROJECT_NAME_SIM_EXTRA)) |
---|
322 | { |
---|
323 | fail("ModelicaRoot did not pick up removal of " + |
---|
324 | PROJECT_NAME_SIM_EXTRA); |
---|
325 | } |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|
329 | } |
---|