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.runtime.CoreException; |
---|
46 | import org.modelica.mdt.core.IModelicaElement; |
---|
47 | import org.modelica.mdt.core.IModelicaFile; |
---|
48 | import org.modelica.mdt.core.IModelicaSourceFile; |
---|
49 | import org.modelica.mdt.core.IModelicaFolder; |
---|
50 | import org.modelica.mdt.core.IModelicaClass; |
---|
51 | import org.modelica.mdt.core.compiler.CompilerException; |
---|
52 | import org.modelica.mdt.test.util.Area51Projects; |
---|
53 | import org.modelica.mdt.test.util.Utility; |
---|
54 | |
---|
55 | import junit.framework.TestCase; |
---|
56 | |
---|
57 | /** |
---|
58 | * Traverses a heirarchy of packages and makes consistensy checks. |
---|
59 | * |
---|
60 | * @author Elmir Jagudin |
---|
61 | */ |
---|
62 | public class TestTraversingPackages extends TestCase |
---|
63 | { |
---|
64 | private IModelicaFolder project_root = null; |
---|
65 | |
---|
66 | @Override |
---|
67 | protected void setUp() throws Exception |
---|
68 | { |
---|
69 | /* |
---|
70 | * create the project and fetch the reference to the root folder |
---|
71 | */ |
---|
72 | Area51Projects.createProjects(); |
---|
73 | |
---|
74 | project_root = |
---|
75 | Utility.getProject(Area51Projects.MODELICA_PROJECT_NAME).getRootFolder(); |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | public void testTraverse() throws CoreException, CompilerException |
---|
80 | { |
---|
81 | IModelicaClass root_package = null; |
---|
82 | IModelicaClass childless_package = null; |
---|
83 | IModelicaSourceFile root_package_model = null; |
---|
84 | IModelicaSourceFile root_package_function = null; |
---|
85 | |
---|
86 | /* for a temporary reference oo package.mo */ |
---|
87 | IModelicaSourceFile package_mo = null; |
---|
88 | |
---|
89 | /* root_package/plain_file */ |
---|
90 | IModelicaFile root_package_plain_file = null; |
---|
91 | |
---|
92 | /* root_package/root_package_folder */ |
---|
93 | IModelicaFolder root_package_folder = null; |
---|
94 | |
---|
95 | IModelicaClass sub_package = null; |
---|
96 | IModelicaClass leaf_package = null; |
---|
97 | IModelicaSourceFile sub_package_model = null; |
---|
98 | |
---|
99 | /* |
---|
100 | * traverse children of project_root |
---|
101 | */ |
---|
102 | String name; |
---|
103 | for (Object elm : project_root.getChildren()) |
---|
104 | { |
---|
105 | if (elm instanceof IFile) |
---|
106 | { |
---|
107 | name = ((IFile)elm).getName(); |
---|
108 | } |
---|
109 | else |
---|
110 | { |
---|
111 | name = ((IModelicaElement)elm).getElementName(); |
---|
112 | } |
---|
113 | |
---|
114 | if (name.equals("root_package")) |
---|
115 | { |
---|
116 | root_package = (IModelicaClass)elm; |
---|
117 | } |
---|
118 | else if (name.equals("childless_package")) |
---|
119 | { |
---|
120 | childless_package = (IModelicaClass)elm; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | assertNotNull("root_package not found", root_package); |
---|
125 | assertEquals("Package base name", root_package.getPrefix(), ""); |
---|
126 | assertEquals("Package name", root_package.getElementName(), |
---|
127 | "root_package"); |
---|
128 | |
---|
129 | assertNotNull("childless_package not found", childless_package); |
---|
130 | assertEquals("Package base name", childless_package.getPrefix(), ""); |
---|
131 | assertEquals("Package name", childless_package.getElementName(), |
---|
132 | "childless_package"); |
---|
133 | |
---|
134 | /* |
---|
135 | * traverse children of root_package |
---|
136 | */ |
---|
137 | for (Object elm : root_package.getChildren()) |
---|
138 | { |
---|
139 | if (elm instanceof IFile) |
---|
140 | { |
---|
141 | name = ((IFile)elm).getName(); |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | name = ((IModelicaElement)elm).getElementName(); |
---|
146 | } |
---|
147 | |
---|
148 | if (name.equals("sub_package")) |
---|
149 | { |
---|
150 | sub_package = (IModelicaClass)elm; |
---|
151 | } |
---|
152 | else if (name.equals("root_package_model.mo")) |
---|
153 | { |
---|
154 | root_package_model = (IModelicaSourceFile)elm; |
---|
155 | } |
---|
156 | else if (name.equals("root_package_function.mo")) |
---|
157 | { |
---|
158 | root_package_function = (IModelicaSourceFile)elm; |
---|
159 | } |
---|
160 | else if (name.equals("package.mo")) |
---|
161 | { |
---|
162 | package_mo = (IModelicaSourceFile)elm; |
---|
163 | } |
---|
164 | else if (name.equals("plain_file")) |
---|
165 | { |
---|
166 | root_package_plain_file = (IModelicaFile)elm;; |
---|
167 | } |
---|
168 | else if (name.equals("root_package_folder")) |
---|
169 | { |
---|
170 | root_package_folder = (IModelicaFolder)elm;; |
---|
171 | } |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | assertNotNull("sub_package not found", sub_package); |
---|
176 | checkFullName(sub_package, "root_package.sub_package"); |
---|
177 | |
---|
178 | assertNotNull("root_package_model.mo not found", root_package_model); |
---|
179 | assertNotNull("root_package_function.mo not found", |
---|
180 | root_package_function); |
---|
181 | assertNotNull("root_package/package.mo not found", |
---|
182 | package_mo); |
---|
183 | assertNotNull("root_package/plain_file not found", |
---|
184 | root_package_plain_file); |
---|
185 | assertNotNull("root_package/root_package_folder not found", |
---|
186 | root_package_folder); |
---|
187 | |
---|
188 | |
---|
189 | /* |
---|
190 | * traverse children of sub_package |
---|
191 | */ |
---|
192 | |
---|
193 | /* traverse packages */ |
---|
194 | for (Object elm : sub_package.getChildren()) |
---|
195 | { |
---|
196 | if (elm instanceof IFile) |
---|
197 | { |
---|
198 | name = ((IFile)elm).getName(); |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | name = ((IModelicaElement)elm).getElementName(); |
---|
203 | } |
---|
204 | |
---|
205 | if (name.equals("leaf_package")) |
---|
206 | { |
---|
207 | leaf_package = (IModelicaClass)elm; |
---|
208 | } |
---|
209 | else if (name.equals("package.mo")) |
---|
210 | { |
---|
211 | package_mo = (IModelicaSourceFile)elm; |
---|
212 | } |
---|
213 | else if (name.equals("sub_package_model.mo")) |
---|
214 | { |
---|
215 | sub_package_model = (IModelicaSourceFile)elm; |
---|
216 | } |
---|
217 | |
---|
218 | } |
---|
219 | assertNotNull("leaf_package not found", leaf_package); |
---|
220 | checkFullName(leaf_package, "root_package.sub_package.leaf_package"); |
---|
221 | assertNotNull("root_package/sub_package/package.mo not found", |
---|
222 | package_mo); |
---|
223 | assertNotNull("root_package/sub_package/sub_package_model.mo not found", |
---|
224 | sub_package_model); |
---|
225 | |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Check that ze package's full name matches the fullName, otherwize |
---|
230 | * fails. |
---|
231 | * @param zePackage |
---|
232 | * @param fullName |
---|
233 | */ |
---|
234 | private void checkFullName(IModelicaClass zePackage, String fullName) |
---|
235 | { |
---|
236 | assertEquals("Package full name does not match", |
---|
237 | fullName, |
---|
238 | zePackage.getPrefix() + "." + |
---|
239 | zePackage.getElementName()); |
---|
240 | } |
---|
241 | } |
---|