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 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
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
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
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
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
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
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
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
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
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
114 public void testBeforeInitialization() {
\r
115 FMUControlJNI control = new FMUControlJNI();
\r
117 String path = ".\\Models\\FMUTests\\FMUAllTypesOfVariablesTest\\FMUAllTypesOfVariablesTestModel.fmu";
\r
119 // Initialize a model
\r
122 control.loadFMUFile(path);
\r
123 } catch (FMUJNIException e) {
\r
124 fail("FMU loading failed");
\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
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
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
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
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
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
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
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
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
202 control.initializeSimulation();
\r
203 } catch (FMUJNIException e) {
\r
204 fail(e.getMessage());
\r
205 // control should NOT throw this exception
\r
209 control.unloadFMU();
\r
210 } catch (FMUJNIException e) {
\r
211 // control should NOT throw this exception
\r
212 fail(e.getMessage());
\r