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.client;
7   
8   import net.kwfgrid.gworkflowdl.protocol.*;
9   import net.kwfgrid.gworkflowdl.protocol.structure.*;
10  import net.kwfgrid.gworkflowdl.protocol.server.*;
11  import net.kwfgrid.gworkflowdl.protocol.xupdate.XUModificationHandler;
12  import net.kwfgrid.gworkflowdl.protocol.xupdate.XUMethodCallEncoder;
13  import net.kwfgrid.jxupdate.xupdate.XUNamespace;
14  import net.kwfgrid.gworkflowdl.protocol.xml.GWDLNamespace;
15  import net.kwfgrid.gworkflowdl.structure.*;
16  
17  import junit.framework.TestCase;
18  
19  import org.apache.log4j.Logger;
20  
21  import java.util.List;
22  
23  /***
24     Test for the complete client side of the protocol.
25   */
26  public class TestClient extends TestCase implements XUNamespace, GWDLNamespace {
27      private static final Logger logger = Logger.getLogger(TestClient.class);
28  
29      public void testClient() throws Exception {
30  	// configure Protocol
31  	StructureService service = new StructureService();
32  	IMethodCallStrategy defaults = new DefaultMethodCallStrategy();
33  	IMethodCallStrategy protocol = new DefaultClientDelegate(service, System.getProperty("user.name"));
34  	IModificationHandler handler = new XUModificationHandler();
35  	IMethodCallEncoder encoder = new XUMethodCallEncoder();
36  	Protocol.setDefaultMethodCallStrategy(defaults);
37  	Protocol.setProtocolMethodCallStrategy(protocol);
38  	Protocol.setModificationHandler(handler);
39  	Protocol.setMethodCallEncoder(encoder);
40  
41  	Creator creator = new ClientCreator(new DefaultCreator());	
42  	Factory.setCreator(creator);
43  
44  	String id = service.instantiate();
45  
46  	ClientWorkflow wf = (ClientWorkflow)Factory.newWorkflow(id);
47  
48  	wf.update();
49  	
50  	assertEquals("Wrong version number.", 0, wf.getVersionNumber());
51  
52  	wf.setDescription("Ditte");
53  	assertEquals("Wrong version number.", 1, wf.getVersionNumber());
54  	assertEquals("Wrong description.", "Ditte", wf.getDescription());
55  
56  	wf.setDescription("watis");
57  	assertEquals("Wrong version number.", 2, wf.getVersionNumber());
58  	assertEquals("Wrong description.", "watis", wf.getDescription());
59  	
60  	wf.update();
61  	assertEquals("Wrong version number.", 2, wf.getVersionNumber());
62  	assertEquals("Wrong description.", "watis", wf.getDescription());
63  
64  	ClientWorkflow wf2 = (ClientWorkflow)Factory.newWorkflow(id);
65  
66  	wf2.update();
67  	assertEquals("Wrong version number.", 2, wf2.getVersionNumber());
68  	assertEquals("Wrong description.", "watis", wf2.getDescription());
69      }
70  
71      private class Listener implements IStructureListenerExt {
72  	public void beginModifications(IRootObject root) {
73  	    logger.info("Begin of modifications sequence.");
74  	}
75  
76  	public void endModifications(IRootObject root) {
77  	    logger.info("End of modifications sequence.");
78  	}
79  
80  	public void exception(IRootObject root, Throwable x) {
81  	    logger.error("Exception", x);
82  	}
83  
84  	public void objectsAdded(IStructureObject parent, String ns, String n, List objects) {
85  	    if (ProtocolPlace.NAME.equals(n)) {
86  		final Place p = (Place)objects.get(0);
87  		parent.getRoot().getTaskQueue().add(new IStructureTask() {
88  			public void run() {
89  			    logger.info("Inner method call.");
90  			    p.getProperties().put("The prop", "the val");
91  			}
92  		    });
93  	    }
94  	}
95  
96  	public void objectsRemoved(IStructureObject parent, String ns, String n, List objects) {
97  	}
98  
99  	public void propertyChanged(IStructureObject parent, String ns, String n, Object newvalue) {
100 	}
101     }
102 
103     public void testClientExtWithInnerMethodCalls() throws Exception {
104 	// configure Protocol
105 	StructureService service = new StructureService();
106 	IMethodCallStrategy defaults = new DefaultMethodCallStrategy();
107 	IMethodCallStrategy protocol = new DefaultClientDelegateExt(service, System.getProperty("user.name"));
108 	IModificationHandler handler = new XUModificationHandler();
109 	IMethodCallEncoder encoder = new XUMethodCallEncoder();
110 	Protocol.setDefaultMethodCallStrategy(defaults);
111 	Protocol.setProtocolMethodCallStrategy(protocol);
112 	Protocol.setModificationHandler(handler);
113 	Protocol.setMethodCallEncoder(encoder);
114 
115 	Creator creator = new ClientCreator(new DefaultCreator());	
116 	Factory.setCreator(creator);
117 
118 	String id = service.instantiate();
119 
120 	ClientWorkflow wf = (ClientWorkflow)Factory.newWorkflow(id);
121 
122 	wf.addStructureListener(new Listener());
123 
124 	wf.update();
125 	
126 	assertEquals("Wrong version number.", 0, wf.getVersionNumber());
127 
128 	wf.setDescription("Ditte");
129 	assertEquals("Wrong version number.", 1, wf.getVersionNumber());
130 	assertEquals("Wrong description.", "Ditte", wf.getDescription());
131 
132 	wf.setDescription("watis");
133 	assertEquals("Wrong version number.", 2, wf.getVersionNumber());
134 	assertEquals("Wrong description.", "watis", wf.getDescription());
135 	
136 	wf.update();
137 	assertEquals("Wrong version number.", 2, wf.getVersionNumber());
138 	assertEquals("Wrong description.", "watis", wf.getDescription());
139 
140 	Place p = Factory.newPlace();
141 	p.setID("thePlace");
142 
143 	wf.addPlace(p);
144 
145 	assertEquals("Property in inner method call not set.", "the val", p.getProperties().get("The prop"));
146     }
147 }