1
2
3
4
5
6
7
8 package net.kwfgrid.gwes;
9
10 import junit.framework.Test;
11 import junit.framework.TestSuite;
12 import junit.framework.Assert;
13 import net.kwfgrid.gwes.exception.LoggingException;
14 import org.apache.log4j.Logger;
15 import org.jaxen.JaxenException;
16 import org.jaxen.SimpleNamespaceContext;
17 import org.jaxen.NamespaceContext;
18 import org.jdom.Element;
19 import org.jdom.Document;
20 import net.kwfgrid.gworkflowdl.structure.JdomString;
21
22 import java.io.IOException;
23
24
25
26
27
28
29
30
31
32
33 public final class ConditionCheckerTest extends LocalGWESAbstractTestCase {
34
35 static Logger logger = Logger.getLogger(ConditionCheckerTest.class);
36
37
38
39
40
41
42 public ConditionCheckerTest(String testName) throws LoggingException {
43 super(testName);
44 }
45
46
47
48
49 public static Test suite() {
50 return new TestSuite(ConditionCheckerTest.class);
51 }
52
53
54
55
56
57
58
59 public void testBasicConditions() throws JaxenException {
60 boolean ret;
61 String condition;
62 ret = evaluateCondition(null, null, condition="5 = 5");
63 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
64
65 ret = evaluateCondition(null, null, condition="5 < 5");
66 Assert.assertEquals("Return value for '"+condition+"'",false,ret);
67 }
68
69 public void testTemporalConditions() throws IOException, JaxenException {
70 SimpleNamespaceContext nsContext = new TransitionOccurrence.DefaultNamespaceContext();
71 boolean ret;
72 String condition;
73
74 ret = evaluateCondition(nsContext, null, condition="gwdl:current-dateTimeMs()");
75 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
76
77 ret = evaluateCondition(nsContext, null, condition="gwdl:ms2second(gwdl:current-dateTimeMs()) < 61");
78 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
79
80 ret = evaluateCondition(nsContext, null, condition="gwdl:ms2day(gwdl:dateTime2ms('2007-07-20T09:42:45Z')) = 20");
81 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
82
83
84 ret = evaluateCondition(nsContext, null, condition="gwdl:ms2hour(gwdl:dateTime2ms('2007-07-20T09:42:45Z')) = 11");
85 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
86
87 ret = evaluateCondition(nsContext, null, condition="gwdl:ms2minute(gwdl:dateTime2ms('2007-07-20T09:42:45Z')) = 42");
88 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
89
90
91 ret = evaluateCondition(nsContext, null, condition="gwdl:ms2month(gwdl:dateTime2ms('2007-07-20T09:42:45Z')) = 6");
92 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
93
94 ret = evaluateCondition(nsContext, null, condition="gwdl:ms2second(gwdl:dateTime2ms('2007-07-20T09:42:45Z')) = 45");
95 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
96
97 ret = evaluateCondition(nsContext, null, condition="gwdl:ms2year(gwdl:dateTime2ms('2007-07-20T09:42:45Z')) = 2007");
98 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
99 }
100
101 public void testDataConditions() throws IOException, JaxenException {
102 SimpleNamespaceContext nsContext = new TransitionOccurrence.DefaultNamespaceContext();
103 Element rootElement = new Element("data",JdomString.wfSpace);
104 Document context = new Document(rootElement);
105 Element v1 = new Element("value1");
106 v1.setText("1");
107 rootElement.addContent(v1);
108 Element v2 = new Element("value2");
109 v2.setText("2");
110 rootElement.addContent(v2);
111 logger.info("context:\n"+JdomString.document2string(context));
112
113 boolean ret;
114 String condition;
115
116 ret = evaluateCondition(nsContext, context, condition="/gwdl:data/value1 = 1");
117 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
118
119 ret = evaluateCondition(nsContext, context, condition="/gwdl:data/value1 = 2");
120 Assert.assertEquals("Return value for '"+condition+"'",false,ret);
121
122 ret = evaluateCondition(nsContext, context, condition="/gwdl:data/value1 < /gwdl:data/value2");
123 Assert.assertEquals("Return value for '"+condition+"'",true,ret);
124
125 }
126
127
128
129
130
131
132
133
134 private boolean evaluateCondition(NamespaceContext nsContext, Document context, String condition) throws JaxenException {
135 ConditionChecker checker = new ConditionChecker();
136 if (context != null) checker.setContext(context);
137 boolean result = checker.check(condition, nsContext);
138 logger.info("Evaluation of expression '" + condition + "' returns " + result);
139 return result;
140 }
141
142 }