]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
c3974d9ee8b578007da135a7674977d3cb58532b
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.tests.fmi;\r
2 \r
3 import static org.junit.Assert.fail;\r
4 import junit.framework.Assert;\r
5 \r
6 import org.junit.Test;\r
7 import org.simantics.modelica.fmi.FMUControlJNI;\r
8 import org.simantics.modelica.fmi.FMUJNIException;\r
9 \r
10 /**\r
11  * Tests calls to fmu control before model has been loaded or initialized.\r
12  * \r
13  * @author Teemu Lempinen\r
14  *\r
15  */\r
16 public class CallFMUBeforeInitializationTests {\r
17         \r
18         /**\r
19          * Try fmu jni methods without loading an fmu.\r
20          * All but unload should throw an exception.\r
21          */\r
22         @Test\r
23         public void testBeforeLoad() {\r
24                 FMUControlJNI control = new FMUControlJNI();\r
25 \r
26                 try {\r
27                         control.initializeSimulation();\r
28                         fail("Initialized a simulation before loading fmu");\r
29                 } catch (FMUJNIException e) {\r
30                         // control should throw this exception: Tried to initialize simulation before loading it\r
31                 }\r
32                 \r
33                 try {\r
34                         control.getAllVariables();\r
35                         fail("getAllVariables returned something before loading fmu");\r
36                 } catch (FMUJNIException e) {\r
37                         // control should throw this exception: There is no model to get the variables from\r
38                 }\r
39                 \r
40                 try {\r
41                         control.filterVariables(".*");\r
42                         fail("filterVariables returned something before loading fmu");\r
43                 } catch (FMUJNIException e) {\r
44                         // control should throw this exception: There is no model to get the variables from\r
45                 }\r
46                 \r
47                 try {\r
48                         double d = control.getRealValue("dummy");\r
49                         fail("Got a Real value before loading fmu: " + d);\r
50                 } catch (FMUJNIException e) {\r
51                         // control should throw this exception: There are no variables\r
52                 }\r
53                 \r
54                 try {\r
55                         control.getSubscribedResults(new double[0]);\r
56                         fail("Got subscribed results before loading fmu");\r
57                 } catch (FMUJNIException e) {\r
58                         // control should throw this exception: There is no model or subscription\r
59                 }\r
60                 \r
61                 try {\r
62                         control.setBooleanValue("dummy", true);\r
63                         fail("Set a boolean value  before loading fmu");\r
64                 } catch (FMUJNIException e) {\r
65                         // control should throw this exception: There are no variables\r
66                 }\r
67                 \r
68                 try {\r
69                         control.setRealValue("dummy", 1.0);\r
70                         fail("Set a real value  before loading fmu");\r
71                 } catch (FMUJNIException e) {\r
72                         // control should throw this exception: There are no variables\r
73                 }\r
74                 \r
75                 try {\r
76                         control.setIntegerValue("dummy", 1);\r
77                         fail("Set an integer value  before loading fmu");\r
78                 } catch (FMUJNIException e) {\r
79                         // control should throw this exception: There are no variables\r
80                 }\r
81                 \r
82                 try {\r
83                         control.setStepLength(0.1);\r
84                         fail("Set step length before loading fmu");\r
85                 } catch (FMUJNIException e) {\r
86                         // control should throw this exception: There is no model for which to set the step length\r
87                 }\r
88                 \r
89                 try {\r
90                         control.simulateStep();\r
91                         fail("Simulated a step before loading fmu");\r
92                 } catch (FMUJNIException e) {\r
93                         // control should throw this exception: There is no model to simulate\r
94                 }\r
95                 \r
96                 try {\r
97                         control.subscribe(new String[] {"dummy"});\r
98                         fail("Subscribed variables before loading fmu");\r
99                 } catch (FMUJNIException e) {\r
100                         // control should throw this exception: There is no model to subscribe from\r
101                 }\r
102                 \r
103                 \r
104                 // succeeds\r
105                 try {\r
106                         control.unloadFMU();\r
107                 } catch (FMUJNIException e) {\r
108                         // control should NOT throw this exception: Unload only unloads if something has been loaded\r
109                         fail(e.getMessage());\r
110                 }\r
111                 \r
112         }\r
113         \r
114         /*\r
115          * Test:\r
116          * Load a valid fmu, but do not initialize it. Test that all\r
117          * methods work as they are supposed to. Some throw exceptions while\r
118          * others work.\r
119          */\r
120         @Test\r
121         public void testBeforeInitialization() {\r
122                 FMUControlJNI control = new FMUControlJNI();\r
123 \r
124                 String path = ".\\Models\\FMUTests\\FMUAllTypesOfVariablesTest\\FMUAllTypesOfVariablesTestModel.fmu";\r
125 \r
126                 // Initialize a model\r
127                 try {\r
128 \r
129                         control.loadFMUFile(path);\r
130                 } catch (FMUJNIException e) {\r
131                         fail("FMU loading failed");\r
132                 }\r
133                 \r
134                 // Fails\r
135                 \r
136                 try {\r
137                         double d = control.getRealValue("dummy");\r
138                         fail("Got a Real value before loading fmu: " + d);\r
139                 } catch (FMUJNIException e) {\r
140                         // control should throw this exception: There are no variables\r
141                 }\r
142                 \r
143                 try {\r
144                         control.setBooleanValue("dummy", true);\r
145                         fail("Set a boolean value  before loading fmu");\r
146                 } catch (FMUJNIException e) {\r
147                         // control should throw this exception: There are no variables\r
148                 }\r
149                 \r
150                 try {\r
151                         control.setRealValue("dummy", 1.0);\r
152                         fail("Set a real value  before loading fmu");\r
153                 } catch (FMUJNIException e) {\r
154                         // control should throw this exception: There are no variables\r
155                 }\r
156                 \r
157                 try {\r
158                         control.setIntegerValue("dummy", 1);\r
159                         fail("Set an integer value  before loading fmu");\r
160                 } catch (FMUJNIException e) {\r
161                         // control should throw this exception: There are no variables\r
162                 }\r
163                 \r
164                 try {\r
165                         control.simulateStep();\r
166                         fail("Simulated a step before loading fmu");\r
167                 } catch (FMUJNIException e) {\r
168                         // control should throw this exception: There is no model to simulate\r
169                 }\r
170                 \r
171 \r
172                 \r
173                 // Succeeds\r
174                 try {\r
175                         control.setStepLength(0.1);\r
176                 } catch (FMUJNIException e) {\r
177                         // control should NOT throw this exception: simulation step length can be set\r
178                         // beforehand for future simulations\r
179                         fail(e.getMessage());\r
180                 }\r
181                 \r
182                 try {\r
183                         control.getAllVariables();\r
184                 } catch (FMUJNIException e) {\r
185                         // control should NOT throw this exception: Variables are found in loadFMU\r
186                         fail(e.getMessage());\r
187                 }\r
188                 \r
189                 try {\r
190                         control.filterVariables(".*");\r
191                 } catch (FMUJNIException e) {\r
192                         // control should NOT throw this exception: Variables are found in loadFMU\r
193                         fail(e.getMessage());\r
194                 }\r
195                 \r
196                 \r
197                 try {\r
198                         double[] array = new double[0];\r
199                         control.getSubscribedResults(array);\r
200                         Assert.assertTrue("Returned a wrong kind of array", array.length == 0);\r
201                 } catch (FMUJNIException e) {\r
202                         // control should NOT throw this exception: Should return the given double\r
203                         // array filled with subscribed values or 0.0 if there was some problem\r
204                         fail(e.getMessage());\r
205                 }\r
206                 \r
207                 try {\r
208                         control.subscribe(new String[] {"dummy"});\r
209                 } catch (FMUJNIException e) {\r
210                         // control should NOT throw this exception: users can subscribe before initialization\r
211                         fail(e.getMessage());\r
212                 }\r
213                 \r
214                 \r
215                 try {\r
216                         control.initializeSimulation();\r
217                 } catch (FMUJNIException e) {\r
218                         fail(e.getMessage());\r
219                         // control should NOT throw this exception\r
220                 }\r
221                 \r
222                 try {\r
223                         control.unloadFMU();\r
224                 } catch (FMUJNIException e) {\r
225                         // control should NOT throw this exception\r
226                         fail(e.getMessage());\r
227                 }\r
228         }\r
229 }\r