1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gworkflowdl.protocol.server;
7   
8   import net.kwfgrid.gworkflowdl.protocol.*;
9   import net.kwfgrid.gworkflowdl.protocol.structure.*;
10  import net.kwfgrid.gworkflowdl.protocol.xupdate.XUModificationHandler;
11  import net.kwfgrid.gworkflowdl.protocol.xupdate.XUMethodCallEncoder;
12  import net.kwfgrid.jxupdate.xupdate.XUNamespace;
13  import net.kwfgrid.gworkflowdl.protocol.xml.GWDLNamespace;
14  import net.kwfgrid.gworkflowdl.structure.*;
15  
16  import junit.framework.TestCase;
17  
18  import org.apache.log4j.Logger;
19  
20  import java.util.List;
21  
22  /***
23     Test for the complete server side of the protocol.
24   */
25  public class TestServer extends TestCase implements XUNamespace, GWDLNamespace {
26      private static final Logger logger = Logger.getLogger(TestServer.class);
27  
28      public void testServer() throws Exception {
29  	// configure Protocol
30  	IMethodCallStrategy defaults = new DefaultMethodCallStrategy();
31  	IMethodCallStrategy protocol = new DefaultServerDelegate();
32  	IModificationHandler handler = new XUModificationHandler();
33  	IMethodCallEncoder encoder = new XUMethodCallEncoder();
34  	Protocol.setDefaultMethodCallStrategy(defaults);
35  	Protocol.setProtocolMethodCallStrategy(protocol);
36  	Protocol.setModificationHandler(handler);
37  	Protocol.setMethodCallEncoder(encoder);
38  
39  	Creator creator = new ServerCreator(new DefaultCreator());	
40  	Factory.setCreator(creator);
41  
42  	ServerWorkflow wf = (ServerWorkflow)Factory.newWorkflow();
43  
44  	// check if i can call edgeSetPlace
45  	Edge e = Factory.newEdge();
46  	Transition t = Factory.newTransition();
47  	t.setID("t");
48  	Place p = Factory.newPlace();
49  	p.setID("p");
50  	wf.addTransition(t);
51  	t.addInEdge(e);
52  	e.setPlace(p);
53  	wf.removeTransition(0);
54  
55  	int initialversion = wf.getVersionNumber();
56  
57  	// to initialize version handling
58  	wf.getModificationsForUpdate(-1);
59  
60  	logger.info("GWorkflowDL -----------\n"+wf.getXML());
61  	
62  	wf.commitModification(initialversion, 
63  			      "<xu:modifications xmlns:xu=\""+NS+"\">"+
64  			      "<xu:append xu:select=\"/workflow\" xu:child=\"first()\">"+
65  			      "<xu:element xu:name=\"description\" xu:namespace=\""+GWDL_NS+"\">icke</xu:element>"+
66  			      "</xu:append>"+
67  			      "</xu:modifications>");
68  
69  	assertEquals("Wrong description.", "icke", wf.getDescription());
70  	assertEquals("Wrong version number.", initialversion+1, wf.getVersionNumber());
71  	assertFalse("XML should not be valid.", wf.isXMLValid());
72  
73  	logger.info("GWorkflowDL -----------\n"+wf.getXML());
74  
75  	assertTrue("XML should be valid.", wf.isXMLValid());
76  	String[][] m = wf.getModificationsForUpdate(-1);
77  	assertEquals("Wrong number of modifications for update.", 1, m.length);
78  	assertEquals("Wrong type of modifications for update.", Protocol.IDENTIFIER_FULLUPDATE, m[0][1]);
79  	assertEquals("Wrong version number in update.", ""+(initialversion+1), m[0][0]);
80  	Workflow wf2 = JdomString.string2workflow(m[0][2]);
81  	assertEquals("Wrong description in new workflow.", "icke", wf2.getDescription());
82  
83  	m = wf.getModificationsForUpdate(initialversion);
84  	assertEquals("Wrong number of modifications for update.", 1, m.length);
85  	assertEquals("Wrong type of modifications for update.", Protocol.IDENTIFIER_MODIFICATION, m[0][1]);
86  	assertEquals("Wrong version number in update.", ""+(initialversion+1), m[0][0]);
87  	
88  	m = wf.getModificationsForUpdate(initialversion+1);
89  	assertEquals("Wrong number of modifications for update.", 0, m.length);
90  
91  	wf.setDescription("ditte");
92  
93  	assertEquals("Wrong description.", "ditte", wf.getDescription());
94  	assertEquals("Wrong version number.", initialversion+2, wf.getVersionNumber());
95  	assertFalse("XML should not be valid.", wf.isXMLValid());
96  
97  	logger.info("GWorkflowDL -----------\n"+wf.getXML());
98  
99  	assertTrue("XML should be valid.", wf.isXMLValid());
100 	m = wf.getModificationsForUpdate(-1);
101 	assertEquals("Wrong number of modifications for update.", 1, m.length);
102 	assertEquals("Wrong type of modifications for update.", Protocol.IDENTIFIER_FULLUPDATE, m[0][1]);
103 	assertEquals("Wrong version number in update.", ""+(initialversion+2), m[0][0]);
104 	wf2 = JdomString.string2workflow(m[0][2]);
105 	assertEquals("Wrong description in new workflow.", "ditte", wf2.getDescription());
106 
107 	m = wf.getModificationsForUpdate(initialversion);
108 	assertEquals("Wrong number of modifications for update.", 2, m.length);
109 	assertEquals("Wrong type of modifications for update.", Protocol.IDENTIFIER_MODIFICATION, m[0][1]);
110 	assertEquals("Wrong type of modifications for update.", Protocol.IDENTIFIER_MODIFICATION, m[1][1]);
111 	assertEquals("Wrong version number in update.", ""+(initialversion+1), m[0][0]);
112 	assertEquals("Wrong version number in update.", ""+(initialversion+2), m[1][0]);
113     }
114 
115     private class Listener implements IStructureListenerExt {
116 	public void beginModifications(IRootObject root) {
117 	    logger.info("Begin of modifications sequence.");
118 	}
119 
120 	public void endModifications(IRootObject root) {
121 	    logger.info("End of modifications sequence.");
122 	}
123 
124 	public void exception(IRootObject root, Throwable x) {
125 	    logger.error("Exception", x);
126 	}
127 
128 	public void objectsAdded(IStructureObject parent, String ns, String n, List objects) {
129 	    if (ProtocolPlace.NAME.equals(n)) {
130 		final Place p = (Place)objects.get(0);
131 		parent.getRoot().getTaskQueue().add(new IStructureTask() {
132 			public void run() {
133 			    logger.info("Inner method call.");
134 			    p.getProperties().put("The prop", "the val");
135 			}
136 		    });
137 	    }
138 	}
139 
140 	public void objectsRemoved(IStructureObject parent, String ns, String n, List objects) {
141 	}
142 
143 	public void propertyChanged(IStructureObject parent, String ns, String n, Object newvalue) {
144 	}
145     }
146 
147     public void testServerExtWithInnerMethodCalls() throws Exception {
148 	// configure Protocol
149 	StructureService service = new StructureService();
150 	IMethodCallStrategy defaults = new DefaultMethodCallStrategy();
151 	IMethodCallStrategy protocol = new DefaultServerDelegateExt();
152 	IModificationHandler handler = new XUModificationHandler();
153 	IMethodCallEncoder encoder = new XUMethodCallEncoder();
154 	Protocol.setDefaultMethodCallStrategy(defaults);
155 	Protocol.setProtocolMethodCallStrategy(protocol);
156 	Protocol.setModificationHandler(handler);
157 	Protocol.setMethodCallEncoder(encoder);
158 
159 	Creator creator = new ServerCreator(new DefaultCreator());	
160 	Factory.setCreator(creator);
161 
162 	String id = service.instantiate();
163 
164 	ServerWorkflow wf = (ServerWorkflow)Factory.newWorkflow(id);
165 
166 	wf.addStructureListener(new Listener());
167 
168 	
169 	assertEquals("Wrong version number.", 0, wf.getVersionNumber());
170 
171 	wf.setDescription("Ditte");
172 	assertEquals("Wrong version number.", 1, wf.getVersionNumber());
173 	assertEquals("Wrong description.", "Ditte", wf.getDescription());
174 
175 	wf.setDescription("watis");
176 	assertEquals("Wrong version number.", 2, wf.getVersionNumber());
177 	assertEquals("Wrong description.", "watis", wf.getDescription());
178 	
179 	assertEquals("Wrong version number.", 2, wf.getVersionNumber());
180 	assertEquals("Wrong description.", "watis", wf.getDescription());
181 
182 	Place p = Factory.newPlace();
183 	p.setID("thePlace");
184 
185 	wf.addPlace(p);
186 
187 	assertEquals("Property in inner method call not set.", "the val", p.getProperties().get("The prop"));
188     }
189 }