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.Collections; |
---|
45 | import java.util.Vector; |
---|
46 | |
---|
47 | import org.eclipse.core.resources.IFile; |
---|
48 | import org.eclipse.core.runtime.CoreException; |
---|
49 | import org.modelica.mdt.core.IModelicaFile; |
---|
50 | import org.modelica.mdt.core.IModelicaFolder; |
---|
51 | import org.modelica.mdt.core.IModelicaPackage; |
---|
52 | import org.modelica.mdt.internal.omcproxy.CompilerException; |
---|
53 | import org.modelica.mdt.test.util.Area51Projects; |
---|
54 | import org.modelica.mdt.test.util.Utility; |
---|
55 | |
---|
56 | |
---|
57 | import junit.framework.TestCase; |
---|
58 | |
---|
59 | /** |
---|
60 | * Test various methods in org.modelica.mdt.internal.core.ModelicaFolder |
---|
61 | * |
---|
62 | * @author Elmir Jagudin |
---|
63 | */ |
---|
64 | public class TestModelicaFolder extends TestCase |
---|
65 | { |
---|
66 | /* the test subject */ |
---|
67 | private IModelicaFolder root; |
---|
68 | |
---|
69 | /* collection of expected objects */ |
---|
70 | private Vector<String> expectedFileNames = new Vector<String>(3); |
---|
71 | private Vector<String> expectedFolderNames = new Vector<String>(3); |
---|
72 | private Vector<String> expectedModelicaFileNames = new Vector<String>(2); |
---|
73 | private Vector<String> expectedPackageNames = new Vector<String>(2); |
---|
74 | private Vector<String> expectedRootFolderFileNames = new Vector<String>(1); |
---|
75 | /* expected modelica file names in package_look_alike folder */ |
---|
76 | private Vector<String> expectedPackageLookAlikeNames |
---|
77 | = new Vector<String>(1); |
---|
78 | |
---|
79 | @Override |
---|
80 | protected void setUp() throws Exception |
---|
81 | { |
---|
82 | Area51Projects.createProjects(); |
---|
83 | |
---|
84 | root = |
---|
85 | Utility.getProject(Area51Projects.MODELICA_PROJECT_NAME). |
---|
86 | getRootFolder(); |
---|
87 | |
---|
88 | /* |
---|
89 | * setup expected collections |
---|
90 | */ |
---|
91 | assertTrue(Collections.addAll(expectedFileNames, |
---|
92 | ".project", "empty_file", "README.txt")); |
---|
93 | |
---|
94 | assertTrue(Collections.addAll(expectedFolderNames, |
---|
95 | "empty_folder", "root_folder", "package_look_alike")); |
---|
96 | |
---|
97 | assertTrue(Collections.addAll(expectedModelicaFileNames, |
---|
98 | "root_model.mo", "nested_models.mo")); |
---|
99 | |
---|
100 | assertTrue(Collections.addAll(expectedPackageNames, |
---|
101 | "root_package", "childless_package")); |
---|
102 | |
---|
103 | assertTrue(Collections.addAll(expectedRootFolderFileNames, |
---|
104 | "hej_hopp")); |
---|
105 | |
---|
106 | assertTrue(Collections.addAll(expectedPackageLookAlikeNames, |
---|
107 | "package.mo")); |
---|
108 | |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | /** |
---|
114 | * test ModelicaFolder.getFiles() |
---|
115 | */ |
---|
116 | public void testGetFiles() |
---|
117 | { |
---|
118 | try |
---|
119 | { |
---|
120 | for (IFile file : root.getFiles()) |
---|
121 | { |
---|
122 | expectedFileNames.remove(file.getName()); |
---|
123 | } |
---|
124 | assertTrue("could not find all expected files in the root folder", |
---|
125 | expectedFileNames.isEmpty()); |
---|
126 | |
---|
127 | } |
---|
128 | catch (CoreException e) |
---|
129 | { |
---|
130 | fail("exception thrown while fetching files"); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * test ModelicaFolder.getFolders() |
---|
136 | * and check how nested IModelicaFolder:s are doing |
---|
137 | */ |
---|
138 | public void testGetFolders() |
---|
139 | { |
---|
140 | IModelicaFolder empty_folder = null; |
---|
141 | IModelicaFolder root_folder = null; |
---|
142 | IModelicaFolder package_look_alike = null; |
---|
143 | String name; |
---|
144 | |
---|
145 | try |
---|
146 | { |
---|
147 | for (IModelicaFolder folder : root.getFolders()) |
---|
148 | { |
---|
149 | name = folder.getElementName(); |
---|
150 | |
---|
151 | expectedFolderNames.remove(name); |
---|
152 | if (name.equals("empty_folder")) |
---|
153 | { |
---|
154 | empty_folder = folder; |
---|
155 | } |
---|
156 | else if (name.equals("root_folder")) |
---|
157 | { |
---|
158 | root_folder = folder; |
---|
159 | } |
---|
160 | else if (name.equals("package_look_alike")) |
---|
161 | { |
---|
162 | package_look_alike = folder; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | assertTrue("could not find all expected folders in the root folder", |
---|
167 | expectedFolderNames.isEmpty()); |
---|
168 | assertNotNull(empty_folder); |
---|
169 | assertNotNull(root_folder); |
---|
170 | assertNotNull(package_look_alike); |
---|
171 | |
---|
172 | /* |
---|
173 | * empty folder should not have any children |
---|
174 | */ |
---|
175 | assertFalse(empty_folder.hasChildren()); |
---|
176 | assertTrue(empty_folder.getChildren().isEmpty()); |
---|
177 | assertTrue(empty_folder.getFiles().isEmpty()); |
---|
178 | assertTrue(empty_folder.getFolders().isEmpty()); |
---|
179 | assertTrue(empty_folder.getPackages().isEmpty()); |
---|
180 | assertTrue(empty_folder.getModelicaFiles().isEmpty()); |
---|
181 | |
---|
182 | /* check contents of root_folder */ |
---|
183 | for (IFile file : root_folder.getFiles()) |
---|
184 | { |
---|
185 | expectedRootFolderFileNames.remove(file.getName()); |
---|
186 | } |
---|
187 | assertTrue("could no find all expected files in root_folder", |
---|
188 | expectedRootFolderFileNames.isEmpty()); |
---|
189 | |
---|
190 | /* check contents of package_look_alike */ |
---|
191 | for (IModelicaFile file : package_look_alike.getModelicaFiles()) |
---|
192 | { |
---|
193 | expectedPackageLookAlikeNames.remove(file.getElementName()); |
---|
194 | } |
---|
195 | assertTrue("could no find all expected files in package_look_alike", |
---|
196 | expectedPackageLookAlikeNames.isEmpty()); |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | } |
---|
201 | catch (CoreException e) |
---|
202 | { |
---|
203 | fail("exception thrown while fetching files"); |
---|
204 | } |
---|
205 | catch (CompilerException e) |
---|
206 | { |
---|
207 | fail("exception thrown while fetching files"); |
---|
208 | } |
---|
209 | |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * test ModelicaFolder.getModelicaFiles() |
---|
214 | */ |
---|
215 | public void testGetModelicaFiles() |
---|
216 | { |
---|
217 | try |
---|
218 | { |
---|
219 | for (IModelicaFile file : root.getModelicaFiles()) |
---|
220 | { |
---|
221 | expectedModelicaFileNames.remove(file.getElementName()); |
---|
222 | } |
---|
223 | assertTrue("could not find all expected mo files in the root folder", |
---|
224 | expectedModelicaFileNames.isEmpty()); |
---|
225 | |
---|
226 | } |
---|
227 | catch (CoreException e) |
---|
228 | { |
---|
229 | fail("exception thrown while fetching files"); |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * test ModelicaFolder.getPackages() |
---|
235 | */ |
---|
236 | public void testPackages() |
---|
237 | { |
---|
238 | try |
---|
239 | { |
---|
240 | for (IModelicaPackage pkg : root.getPackages()) |
---|
241 | { |
---|
242 | expectedPackageNames.remove(pkg.getElementName()); |
---|
243 | } |
---|
244 | |
---|
245 | assertTrue("could not find all expected mo files in the root folder", |
---|
246 | expectedPackageNames.isEmpty()); |
---|
247 | |
---|
248 | } |
---|
249 | catch (CoreException e) |
---|
250 | { |
---|
251 | fail("exception thrown while fetching packages"); |
---|
252 | } |
---|
253 | catch (CompilerException e) |
---|
254 | { |
---|
255 | fail("exception thrown while fetching packages"); |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|