KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > data > exe > DurationExpressionTest


1 package org.jbpm.bpel.data.exe;
2
3 import javax.xml.namespace.QName JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import org.w3c.dom.Element JavaDoc;
8
9 import org.jbpm.context.def.ContextDefinition;
10 import org.jbpm.graph.def.ProcessDefinition;
11 import org.jbpm.graph.exe.ProcessInstance;
12 import org.jbpm.graph.exe.Token;
13
14 import org.jbpm.bpel.data.def.ElementTypeInfo;
15 import org.jbpm.bpel.data.def.SchemaTypeInfo;
16 import org.jbpm.bpel.data.def.Snippet;
17 import org.jbpm.bpel.data.def.VariableDefinition;
18 import org.jbpm.bpel.xml.BpelConstants;
19 import org.jbpm.bpel.xml.BpelException;
20 import org.jbpm.bpel.xml.util.DatatypeUtil;
21 import org.jbpm.bpel.xml.util.Duration;
22 import org.jbpm.bpel.xml.util.NodeUtil;
23
24 /**
25  * @author Alejandro Guízar
26  * @version $Revision: 1.6 $ $Date: 2005/06/23 02:17:26 $
27  */

28 public class DurationExpressionTest extends TestCase {
29   
30   private Token token;
31   
32   private static final String JavaDoc ELEM_TEXT =
33     "<a>" +
34     " <b interval=\"P12Y75M60D\"/>" +
35     " <c>P8Y12M20DT15H45M30.05S</c>" +
36     "</a>";
37   
38   public DurationExpressionTest(String JavaDoc name) {
39     super(name);
40   }
41   
42   protected void setUp() throws Exception JavaDoc {
43     // process and context
44
ProcessDefinition pd = new ProcessDefinition();
45     pd.addDefinition(new ContextDefinition());
46     ProcessInstance pi = new ProcessInstance(pd);
47     token = pi.getRootToken();
48     // element variable
49
Element JavaDoc elem1 = NodeUtil.parseElement(ELEM_TEXT);
50     VariableDefinition var1 = new VariableDefinition();
51     var1.setName("var1");
52     var1.setTypeInfo(new ElementTypeInfo(new QName JavaDoc(elem1.getNamespaceURI(), elem1.getLocalName())));
53     var1.createInstance(token).setValue(elem1);
54   }
55   
56   public void testSimpleExtraction() {
57     // simple variable
58
VariableDefinition simple = new VariableDefinition();
59     simple.setName("simple");
60     simple.setTypeInfo(new SchemaTypeInfo(new QName JavaDoc(BpelConstants.NS_XML_SCHEMA, "duration")));
61     simple.createInstance(token).setValue(Duration.parseDuration("PT28H35M140.7S"));
62     // exprected value
63
Duration duration = new Duration(0, 0, 0, 28, 35, 140, 700);
64     // assertion
65
assertEquals(duration, eval("$simple"));
66   }
67   
68   public void testAttributeExtraction() {
69     assertEquals(new Duration(12, 75, 60, 0, 0, 0, 0), eval("$var1/b/@interval"));
70   }
71
72   public void testContentExtraction() {
73     assertEquals(new Duration(8, 12, 20, 15, 45, 30, 50), eval("$var1/c"));
74   }
75   
76   private Duration eval(String JavaDoc exprText) {
77     Duration value = null;
78     try {
79       Snippet expr = new Snippet();
80       expr.setText(exprText);
81       expr.setUse(Snippet.Use.EXPRESSION);
82       expr.parseScript();
83       value = DatatypeUtil.toDuration(expr.getScript().evaluate(token));
84     }
85     catch (BpelException e) {
86       fail(e.getMessage());
87     }
88     return value;
89   }
90 }
91
Popular Tags