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.IModelicaElement; |
---|
50 | import org.modelica.mdt.core.IModelicaFile; |
---|
51 | import org.modelica.mdt.core.IModelicaSourceFile; |
---|
52 | import org.modelica.mdt.core.IModelicaFolder; |
---|
53 | import org.modelica.mdt.core.compiler.CompilerInstantiationException; |
---|
54 | import org.modelica.mdt.core.compiler.ConnectException; |
---|
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 various methods in org.modelica.mdt.internal.core.ModelicaFolder |
---|
64 | * |
---|
65 | * @author Elmir Jagudin |
---|
66 | */ |
---|
67 | public class TestModelicaFolder extends TestCase |
---|
68 | { |
---|
69 | /* |
---|
70 | * the test subject |
---|
71 | * the root folder of Area51 modelica project |
---|
72 | */ |
---|
73 | private IModelicaFolder rootFolder; |
---|
74 | |
---|
75 | /* collection of expected children */ |
---|
76 | private Vector<String> expectedChildren = new Vector<String>(10); |
---|
77 | private Vector<String> expectedRootFolderChildren = new Vector<String>(1); |
---|
78 | |
---|
79 | /* expected modelica file names in package_look_alike folder */ |
---|
80 | private Vector<String> expectedPackageLookAlikeChildren |
---|
81 | = new Vector<String>(1); |
---|
82 | |
---|
83 | |
---|
84 | @Override |
---|
85 | protected void setUp() throws Exception |
---|
86 | { |
---|
87 | Area51Projects.createProjects(); |
---|
88 | |
---|
89 | rootFolder = |
---|
90 | Utility.getProject(Area51Projects.MODELICA_PROJECT_NAME). |
---|
91 | getRootFolder(); |
---|
92 | |
---|
93 | /* |
---|
94 | * setup expected collections |
---|
95 | */ |
---|
96 | assertTrue(Collections.addAll(expectedChildren, |
---|
97 | ".project", "empty_file", "README.txt", "empty_folder", |
---|
98 | "root_folder", "package_look_alike", "root_model.mo", |
---|
99 | "nested_models.mo", "root_package", "childless_package")); |
---|
100 | |
---|
101 | assertTrue(Collections.addAll(expectedRootFolderChildren, |
---|
102 | "hej_hopp")); |
---|
103 | |
---|
104 | assertTrue(Collections.addAll(expectedPackageLookAlikeChildren, |
---|
105 | "package.mo")); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * test ModelicaFolder.hasChildren() |
---|
110 | */ |
---|
111 | public void testHasChildren() |
---|
112 | { |
---|
113 | try |
---|
114 | { |
---|
115 | assertTrue(rootFolder.hasChildren()); |
---|
116 | } |
---|
117 | catch (Exception e) |
---|
118 | { |
---|
119 | fail("exception thrown " + e.getMessage()); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | * test ModelicaFolder.getChildren() method |
---|
126 | * @throws CompilerInstantiationException |
---|
127 | * |
---|
128 | */ |
---|
129 | public void testGetChildren() |
---|
130 | throws ConnectException, UnexpectedReplyException, |
---|
131 | InvocationError, CoreException, CompilerInstantiationException |
---|
132 | { |
---|
133 | IModelicaFolder root_folder = null; |
---|
134 | IModelicaFolder empty_folder = null; |
---|
135 | IModelicaFolder package_look_alike = null; |
---|
136 | |
---|
137 | |
---|
138 | String name = ""; |
---|
139 | for (Object elm : rootFolder.getChildren()) |
---|
140 | { |
---|
141 | |
---|
142 | if (elm instanceof IFile) |
---|
143 | { |
---|
144 | name = ((IFile)elm).getName(); |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | name = ((IModelicaElement)elm).getElementName(); |
---|
149 | } |
---|
150 | expectedChildren.remove(name); |
---|
151 | |
---|
152 | if (name.equals("root_folder")) |
---|
153 | { |
---|
154 | root_folder = (IModelicaFolder) elm; |
---|
155 | } |
---|
156 | else if (name.equals("empty_folder")) |
---|
157 | { |
---|
158 | empty_folder = (IModelicaFolder) elm; |
---|
159 | } |
---|
160 | else if (name.equals("package_look_alike")) |
---|
161 | { |
---|
162 | package_look_alike = (IModelicaFolder) elm; |
---|
163 | } |
---|
164 | |
---|
165 | } |
---|
166 | assertTrue("could not find all expected children in the root folder", |
---|
167 | expectedChildren.isEmpty()); |
---|
168 | |
---|
169 | assertNotNull("root_folder element not found", root_folder); |
---|
170 | assertNotNull("empty_folder element not found", empty_folder); |
---|
171 | assertNotNull("package_look_alike element not found", |
---|
172 | package_look_alike); |
---|
173 | |
---|
174 | for (Object elm : root_folder.getChildren()) |
---|
175 | { |
---|
176 | if (elm instanceof IModelicaFile) |
---|
177 | { |
---|
178 | /* we only expect (looking for) files inside root_folder */ |
---|
179 | name = ((IModelicaFile)elm).getElementName(); |
---|
180 | expectedRootFolderChildren.remove(name); |
---|
181 | } |
---|
182 | } |
---|
183 | assertTrue("could no find all expected files in root_folder", |
---|
184 | expectedRootFolderChildren.isEmpty()); |
---|
185 | |
---|
186 | for (Object elm : package_look_alike.getChildren()) |
---|
187 | { |
---|
188 | if (elm instanceof IModelicaSourceFile) |
---|
189 | { |
---|
190 | /* |
---|
191 | * we only expect (looking for) modelica files |
---|
192 | * inside package_look_alike |
---|
193 | */ |
---|
194 | name = ((IModelicaSourceFile)elm).getElementName(); |
---|
195 | expectedPackageLookAlikeChildren.remove(name); |
---|
196 | } |
---|
197 | } |
---|
198 | assertTrue("could no find all expected files in package_look_alike", |
---|
199 | expectedPackageLookAlikeChildren.isEmpty()); |
---|
200 | |
---|
201 | /* |
---|
202 | * test the empty_folder IS empty |
---|
203 | */ |
---|
204 | assertFalse("empty_folder not empty", empty_folder.hasChildren()); |
---|
205 | assertTrue("empty_folder returns children", |
---|
206 | empty_folder.getChildren().isEmpty()); |
---|
207 | } |
---|
208 | } |
---|