]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/AssertStateValue.java
Added missing org.simantics.modeling.tests plug-ins.
[simantics/platform.git] / bundles / org.simantics.modeling.tests / src / org / simantics / modeling / tests / commands / AssertStateValue.java
1 package org.simantics.modeling.tests.commands;
2
3 import junit.framework.Assert;
4 import junit.framework.AssertionFailedError;
5
6 import org.simantics.db.layer0.util.EvaluatingListener;
7 import org.simantics.db.layer0.util.EvaluatingListener.Criterion;
8 import org.simantics.db.layer0.util.EvaluatingListener.Evaluation;
9 import org.simantics.db.testing.common.Command;
10 import org.simantics.modeling.tests.cases.ModelingCommandSequenceTest;
11 import org.simantics.modeling.tests.traits.SingleResourceTrait;
12
13 public class AssertStateValue extends Command<ModelingCommandSequenceTest> {
14
15         
16         private transient final SingleResourceTrait run;
17         private transient final String rvi;
18         private transient final Number expectedValue;
19         private transient final double tolerance;
20         
21         public AssertStateValue(SingleResourceTrait run, String rvi, Number expectedValue, double tolerance) {
22                 this.run = run;
23                 this.rvi = rvi;
24                 this.expectedValue = expectedValue;
25                 this.tolerance = tolerance;
26         }
27         
28         public void run(ModelingCommandSequenceTest environment) throws Exception {
29                 
30                 //final double tolerance = 0.0001;
31                 
32                 Criterion<Number> criterion = new EvaluatingListener.Criterion<Number>() {
33                         @Override
34                         public Evaluation evaluate(Number result) {
35                                 if (result == null){
36                                         return Evaluation.IGNORE;
37                                 }
38                                 else{
39                                         double resultDouble = result.doubleValue();
40                                         double expectedDouble = expectedValue.doubleValue();
41                                         if (Math.abs(expectedDouble - resultDouble) <= tolerance){
42                                         return Evaluation.ACCEPT;
43                                         }
44                                 }
45                                 return Evaluation.IGNORE;
46                         }
47
48                 };
49                 
50                 Number result = Utils.readValue(run, rvi, criterion);
51                 if (result != null){
52                         double resultDouble = result.doubleValue();
53                         double expectedDouble = expectedValue.doubleValue();
54                         
55                         Assert.assertEquals(expectedDouble, resultDouble, tolerance);
56                 }
57                 else
58                         throw new AssertionFailedError();
59
60
61         }
62
63 }