1 package org.simantics.sysdyn.tests.fmi;
\r
3 import static org.junit.Assert.fail;
\r
4 import junit.framework.Assert;
\r
6 import org.junit.Test;
\r
7 import org.simantics.modelica.fmi.FMUControlJNI;
\r
8 import org.simantics.modelica.fmi.FMUJNIException;
\r
11 * Tests calls to fmu control before model has been loaded or initialized.
\r
13 * @author Teemu Lempinen
\r
16 public class CallFMUBeforeInitializationTests {
\r
19 * Try fmu jni methods without loading an fmu.
\r
20 * All but unload should throw an exception.
\r
23 public void testBeforeLoad() {
\r
24 FMUControlJNI control = new FMUControlJNI();
\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
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
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
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
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
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
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
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
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
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
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
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
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
121 public void testBeforeInitialization() {
\r
122 FMUControlJNI control = new FMUControlJNI();
\r
124 String path = ".\\Models\\FMUTests\\FMUAllTypesOfVariablesTest\\FMUAllTypesOfVariablesTestModel.fmu";
\r
126 // Initialize a model
\r
129 control.loadFMUFile(path);
\r
130 } catch (FMUJNIException e) {
\r
131 fail("FMU loading failed");
\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
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
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
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
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
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
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
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
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
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
216 control.initializeSimulation();
\r
217 } catch (FMUJNIException e) {
\r
218 fail(e.getMessage());
\r
219 // control should NOT throw this exception
\r
223 control.unloadFMU();
\r
224 } catch (FMUJNIException e) {
\r
225 // control should NOT throw this exception
\r
226 fail(e.getMessage());
\r