Package: ClientExecutor

ClientExecutor

nameinstructionbranchcomplexitylinemethod
ClientExecutor(DeploymentContext, TestClient)
M: 35 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getEndpoint(DeployedService, String)
M: 53 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
importPackage(NameSpace, String)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
injectResources(NameSpace, Interpreter)
M: 289 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 51 C: 0
0%
M: 1 C: 0
0%
invoke(Interpreter)
M: 96 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
runTest()
M: 184 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 35 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.ws.test.exec;
12:
13: import bsh.EvalError;
14: import bsh.Interpreter;
15: import bsh.NameSpace;
16: import bsh.TargetError;
17: import com.sun.xml.ws.test.client.InterpreterEx;
18: import com.sun.xml.ws.test.client.ScriptBaseClass;
19: import com.sun.xml.ws.test.client.XmlResource;
20: import com.sun.xml.ws.test.CodeGenerator;
21: import com.sun.xml.ws.test.container.DeployedService;
22: import com.sun.xml.ws.test.container.DeploymentContext;
23: import com.sun.xml.ws.test.model.TestClient;
24: import com.sun.xml.ws.test.model.TestEndpoint;
25: import com.sun.xml.ws.test.World;
26:
27: import java.beans.Introspector;
28: import java.io.InputStreamReader;
29: import java.io.Reader;
30: import java.io.StringReader;
31: import java.lang.annotation.Annotation;
32: import java.lang.reflect.InvocationTargetException;
33: import java.lang.reflect.Method;
34: import java.net.URL;
35: import java.net.URLClassLoader;
36: import java.util.ArrayList;
37: import java.util.HashMap;
38: import java.util.List;
39: import java.util.Map;
40: import java.util.Map.Entry;
41:
42: /**
43: * Executes {@link TestClient}.
44: *
45: * @author Kohsuke Kawaguchi
46: */
47: public class ClientExecutor extends Executor {
48: /**
49: * Client test scenario to execute.
50: */
51: private final TestClient client;
52: private List<String> pImports = new ArrayList<String>();
53: private StringBuilder pContents = new StringBuilder();
54: private Map<String, String> varMap = new HashMap<String, String>();
55:
56: public ClientExecutor(DeploymentContext context, TestClient client) {
57: super("client " + client.script.getName().replace('.', '_'), context);
58: this.client = client;
59: }
60:
61: public void runTest() throws Throwable {
62: CodeGenerator.testStarting(context.workDir);
63:• if (context.clientClassLoader == null) {
64:• context.clientClassLoader = context.getResources() != null
65: ? new URLClassLoader(new URL[]{context.getResources().toURL()}, World.runtime.getClassLoader())
66: : World.runtime.getClassLoader();
67: }
68:
69: Interpreter engine = new InterpreterEx(context.clientClassLoader);
70:
71: NameSpace ns = engine.getNameSpace();
72: // import namespaces. what are the other namespaces to be imported?
73: importPackage(ns, "jakarta.activation");
74: importPackage(ns, "jakarta.xml.ws");
75: importPackage(ns, "jakarta.xml.ws.soap");
76: importPackage(ns, "jakarta.xml.ws.handler");
77: importPackage(ns, "jakarta.xml.ws.handler.soap");
78: importPackage(ns, "jakarta.xml.bind");
79: importPackage(ns, "jakarta.xml.soap");
80: importPackage(ns, "javax.xml.namespace");
81: importPackage(ns, "javax.xml.transform");
82: importPackage(ns, "javax.xml.transform.sax");
83: importPackage(ns, "javax.xml.transform.dom");
84: importPackage(ns, "javax.xml.transform.stream");
85: importPackage(ns, "java.util");
86: importPackage(ns, "java.util.concurrent");
87: // if there's any client package, put them there
88: importPackage(ns, context.descriptor.name + ".client");
89:• if (context.descriptor.common != null && context.descriptor.common.exists()) {
90: importPackage(ns, context.descriptor.name + ".common");
91: }
92:
93: // this will make 'thisObject' available as 'this' in script
94: ns.importObject(new ScriptBaseClass(context, engine, client));
95:
96: // load additional helper methods
97: try {
98: engine.eval(new InputStreamReader(getClass().getResourceAsStream("util.bsh")));
99: } catch (EvalError evalError) {
100: throw new Error("Failed to evaluate util.bsh", evalError);
101: }
102:
103: // when invoking JAX-WS, we need to set the context classloader accordingly
104: // so that it can discover classes from the right places.
105: ClassLoader cl = Thread.currentThread().getContextClassLoader();
106: Thread.currentThread().setContextClassLoader(context.clientClassLoader);
107:
108: try {
109: injectResources(ns, engine);
110: invoke(engine);
111: } finally {
112: Thread.currentThread().setContextClassLoader(cl);
113: }
114: }
115:
116: protected void importPackage(NameSpace ns, String p) {
117: ns.importPackage(p);
118: pImports.add(p + ".*");
119: }
120:
121: /**
122: * Makes the test script invocation.
123: */
124: protected void invoke(Interpreter engine) throws Throwable {
125: // executes the script
126: Reader r = client.script.read();
127: try {
128:• if (client.parent.setUpScript != null) {
129: engine.eval(new StringReader(client.parent.setUpScript), engine.getNameSpace(), "pre-client script");
130: varMap.put("client_setUp_script", client.parent.setUpScript + "\n");
131: } else {
132: varMap.put("client_setUp_script", "");
133: }
134:
135: CodeGenerator.generateClientClass(
136: client.script.getName(), // test name
137: pImports,
138: client.script.getSource(),
139: varMap);
140:
141: engine.eval(r, engine.getNameSpace(), client.script.getName());
142:
143: } catch (TargetError e) {
144: throw e.getTarget();
145: } catch (Throwable t) {
146: System.err.println("Caught throwable while evaluating script: [\n" + client.script.getSource() + "\n]");
147: t.printStackTrace();
148: throw t;
149: } finally {
150: r.close();
151: }
152: }
153:
154: private void injectResources(NameSpace ns, Interpreter engine) throws Exception {
155: StringBuilder serviceList = new StringBuilder("injected services:");
156: StringBuilder portList = new StringBuilder("injected ports:");
157: StringBuilder addressList = new StringBuilder("injected addresses:");
158:
159: // inject test home directory
160: engine.set("home", client.parent.home);
161: varMap.put("home", client.parent.home.toString());
162:
163: // inject XML resources
164:• for (Entry<String, XmlResource> e : context.descriptor.xmlResources.entrySet())
165: engine.set(e.getKey(), e.getValue());
166:
167:• for (DeployedService svc : context.services.values()) {
168:• if (!svc.service.isSTS) {
169: // inject WSDL URLs
170: engine.set("wsdlUrls", svc.app.getWSDL());
171:
172: /*
173: TODO: some more work here
174: // Server side may be provider endpoint that doesn't expose WSDL
175: // So there are no generated services.
176: if (svc.serviceClass.size() == 0) {
177:
178: String portName = null;
179: for (TestEndpoint e : svc.service.endpoints) {
180: portName = e.portName;
181: break;
182: }
183: String varName = Introspector.decapitalize(portName);
184: engine.set(varName +"Address",svc.app.getEndpointAddress(getEndpoint(svc, portName)));
185: addressList.append(' ').append(varName).append("Address");
186: continue;
187: }
188: */
189:
190:• for (Class clazz : svc.serviceClass) {
191: String packageName = clazz.getPackage().getName();
192: // import the artifact package
193: ns.importPackage(packageName);
194: // use reflection to list up all methods with 'jakarta.xml.ws.WebEndpoint' annotations
195: // invoke that method via reflection to obtain the Port object.
196: // set the endpoint address to that port object
197: // inject it to the scripting engine
198: Method[] methods = clazz.getMethods();
199:
200: // annotation that serviceClass loads and annotation that this code
201: // uses might be different
202: Class<? extends Annotation> webendpointAnnotation = clazz.getClassLoader()
203: .loadClass("jakarta.xml.ws.WebEndpoint").asSubclass(Annotation.class);
204: Method nameMethod = webendpointAnnotation.getDeclaredMethod("name");
205:
206: Object serviceInstance = clazz.newInstance();
207:
208: //{// inject a service instance
209: String serviceVarName = Introspector.decapitalize(clazz.getSimpleName());
210: engine.set(serviceVarName, serviceInstance);
211: varMap.put("serviceClassName", clazz.getName().replaceAll("\\$", "."));
212: varMap.put("serviceVarName", serviceVarName);
213: serviceList.append(' ').append(serviceVarName);
214: //}
215:
216:• for (Method method : methods) {
217: Annotation endpoint = method.getAnnotation(webendpointAnnotation);
218: // don't inject variables for methods like getHelloPort(WebServiceFeatures)
219:• if (endpoint != null && method.getParameterTypes().length == 0) {
220:
221: //For multiple endpoints the convention for injecting the variables is
222: // portName obtained from the WebEndpoint annotation,
223: // which would be something like "addNumbersPort"
224: String portName = (String) nameMethod.invoke(endpoint);
225: String varName = Introspector.decapitalize(portName);
226:
227: try {
228: engine.set(varName, method.invoke(serviceInstance));
229: String portType = method.getReturnType().getName();
230:
231: varMap.put("portType", portType);
232: varMap.put("getPortMethod", method.getName());
233: varMap.put("varName", varName);
234: varMap.put("serviceName", serviceVarName);
235: varMap.put("portName", portName);
236: String endpointAddress = svc.app.getEndpointAddress(getEndpoint(svc, portName)).toString();
237: engine.set("" + varName + "Address", endpointAddress);
238: addressList.append(' ').append(varName).append("Address");
239: varMap.put("address", endpointAddress);
240: } catch (InvocationTargetException e) {
241:• if (e.getCause() instanceof Exception)
242: throw (Exception) e.getCause();
243: else
244: throw e;
245: }
246: portList.append(' ').append(varName);
247: }
248: }
249: }
250: }
251: }
252: System.out.println(serviceList);
253: System.out.println(portList);
254: System.out.println(addressList);
255: }
256:
257: private TestEndpoint getEndpoint(DeployedService svc, String portName) {
258: // first, look for the port name match
259:• for (TestEndpoint e : svc.service.endpoints) {
260:• if (e.portName != null && e.portName.equals(portName))
261: return e;
262: }
263: // nothing explicitly matched.
264:• if (svc.service.endpoints.size() != 1)
265: throw new Error("Multiple ports are defined on '" + svc.service.name + "', yet ports are ambiguous. Please use @WebService/Provider(portName=)");
266: // there's only one
267: return (TestEndpoint) svc.service.endpoints.toArray()[0];
268: }
269: }