]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
86a095c847f5d9a3ce29320ecbe01d91b3ff9ea0
[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                         double d = control.getRealValue("dummy");\r
42                         fail("Got a Real value before loading fmu: " + d);\r
43                 } catch (FMUJNIException e) {\r
44                         // control should throw this exception: There are no variables\r
45                 }\r
46                 \r
47                 try {\r
48                         control.getSubscribedResults(new double[0]);\r
49                         fail("Got subscribed results before loading fmu");\r
50                 } catch (FMUJNIException e) {\r
51                         // control should throw this exception: There is no model or subscription\r
52                 }\r
53                 \r
54                 try {\r
55                         control.setBooleanValue("dummy", true);\r
56                         fail("Set a boolean value  before loading fmu");\r
57                 } catch (FMUJNIException e) {\r
58                         // control should throw this exception: There are no variables\r
59                 }\r
60                 \r
61                 try {\r
62                         control.setRealValue("dummy", 1.0);\r
63                         fail("Set a real 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.setIntegerValue("dummy", 1);\r
70                         fail("Set an integer 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.setStepLength(0.1);\r
77                         fail("Set step length before loading fmu");\r
78                 } catch (FMUJNIException e) {\r
79                         // control should throw this exception: There is no model for which to set the step length\r
80                 }\r
81                 \r
82                 try {\r
83                         control.simulateStep();\r
84                         fail("Simulated a step before loading fmu");\r
85                 } catch (FMUJNIException e) {\r
86                         // control should throw this exception: There is no model to simulate\r
87                 }\r
88                 \r
89                 try {\r
90                         control.subscribe(new String[] {"dummy"});\r
91                         fail("Subscribed variables before loading fmu");\r
92                 } catch (FMUJNIException e) {\r
93                         // control should throw this exception: There is no model to subscribe from\r
94                 }\r
95                 \r
96                 \r
97                 // succeeds\r
98                 try {\r
99                         control.unloadFMU();\r
100                 } catch (FMUJNIException e) {\r
101                         // control should NOT throw this exception: Unload only unloads if something has been loaded\r
102                         fail(e.getMessage());\r
103                 }\r
104                 \r
105         }\r
106         \r
107         /*\r
108          * Test:\r
109          * Load a valid fmu, but do not initialize it. Test that all\r
110          * methods work as they are supposed to. Some throw exceptions while\r
111          * others work.\r
112          */\r
113         @Test\r
114         public void testBeforeInitialization() {\r
115                 FMUControlJNI control = new FMUControlJNI();\r
116 \r
117                 String path = ".\\Models\\FMUTests\\FMUAllTypesOfVariablesTest\\FMUAllTypesOfVariablesTestModel.fmu";\r
118 \r
119                 // Initialize a model\r
120                 try {\r
121 \r
122                         control.loadFMUFile(path);\r
123                 } catch (FMUJNIException e) {\r
124                         fail("FMU loading failed");\r
125                 }\r
126                 \r
127                 // Fails\r
128                 \r
129                 try {\r
130                         double d = control.getRealValue("dummy");\r
131                         fail("Got a Real value before loading fmu: " + d);\r
132                 } catch (FMUJNIException e) {\r
133                         // control should throw this exception: There are no variables\r
134                 }\r
135                 \r
136                 try {\r
137                         control.setBooleanValue("dummy", true);\r
138                         fail("Set a boolean value  before loading fmu");\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.setRealValue("dummy", 1.0);\r
145                         fail("Set a real 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.setIntegerValue("dummy", 1);\r
152                         fail("Set an integer 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.simulateStep();\r
159                         fail("Simulated a step before loading fmu");\r
160                 } catch (FMUJNIException e) {\r
161                         // control should throw this exception: There is no model to simulate\r
162                 }\r
163                 \r
164 \r
165                 \r
166                 // Succeeds\r
167                 try {\r
168                         control.setStepLength(0.1);\r
169                 } catch (FMUJNIException e) {\r
170                         // control should NOT throw this exception: simulation step length can be set\r
171                         // beforehand for future simulations\r
172                         fail(e.getMessage());\r
173                 }\r
174                 \r
175                 try {\r
176                         control.getAllVariables();\r
177                 } catch (FMUJNIException e) {\r
178                         // control should NOT throw this exception: Variables are found in loadFMU\r
179                         fail(e.getMessage());\r
180                 }\r
181                 \r
182                 \r
183                 try {\r
184                         double[] array = new double[0];\r
185                         control.getSubscribedResults(array);\r
186                         Assert.assertTrue("Returned a wrong kind of array", array.length == 0);\r
187                 } catch (FMUJNIException e) {\r
188                         // control should NOT throw this exception: Should return the given double\r
189                         // array filled with subscribed values or 0.0 if there was some problem\r
190                         fail(e.getMessage());\r
191                 }\r
192                 \r
193                 try {\r
194                         control.subscribe(new String[] {"dummy"});\r
195                 } catch (FMUJNIException e) {\r
196                         // control should NOT throw this exception: users can subscribe before initialization\r
197                         fail(e.getMessage());\r
198                 }\r
199                 \r
200                 \r
201                 try {\r
202                         control.initializeSimulation();\r
203                 } catch (FMUJNIException e) {\r
204                         fail(e.getMessage());\r
205                         // control should NOT throw this exception\r
206                 }\r
207                 \r
208                 try {\r
209                         control.unloadFMU();\r
210                 } catch (FMUJNIException e) {\r
211                         // control should NOT throw this exception\r
212                         fail(e.getMessage());\r
213                 }\r
214         }\r
215 }\r