Skip to content

Package: TestDescriptor$WSDLFilter

TestDescriptor$WSDLFilter

nameinstructionbranchcomplexitylinemethod
TestDescriptor.WSDLFilter(TestDescriptor, String)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
accept(File, String)
M: 13 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2019 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.model;
12:
13: import com.sun.istack.NotNull;
14: import com.sun.istack.Nullable;
15: import com.sun.istack.test.VersionProcessor;
16: import com.sun.istack.test.VersionNumber;
17: import com.sun.xml.ws.test.Main;
18: import com.sun.xml.ws.test.World;
19: import com.sun.xml.ws.test.client.InlineXmlResource;
20: import com.sun.xml.ws.test.client.ReferencedXmlResource;
21: import com.sun.xml.ws.test.client.ScriptBaseClass;
22: import com.sun.xml.ws.test.client.XmlResource;
23: import com.sun.xml.ws.test.container.ApplicationContainer;
24: import com.sun.xml.ws.test.container.DeployedService;
25: import com.sun.xml.ws.test.container.DeploymentContext;
26: import com.sun.xml.ws.test.exec.ClientCompileExecutor;
27: import com.sun.xml.ws.test.exec.ClientExecutor;
28: import com.sun.xml.ws.test.exec.ConcurrentClientExecutor;
29: import com.sun.xml.ws.test.exec.DeploymentExecutor;
30: import com.sun.xml.ws.test.exec.JavaClientExecutor;
31: import com.sun.xml.ws.test.exec.PrepareExecutor;
32: import com.sun.xml.ws.test.model.TransportSet.Singleton;
33: import com.sun.xml.ws.test.tool.WsTool;
34: import com.sun.xml.ws.test.util.XMLUtil;
35:
36: import junit.framework.TestSuite;
37: import org.apache.tools.ant.types.FileSet;
38:
39: import java.io.File;
40: import java.io.FilenameFilter;
41: import java.io.IOException;
42: import java.util.ArrayList;
43: import java.util.Arrays;
44: import java.util.HashMap;
45: import java.util.HashSet;
46: import java.util.LinkedList;
47: import java.util.List;
48: import java.util.Map;
49: import java.util.Set;
50: import java.util.SortedSet;
51: import java.util.StringTokenizer;
52: import java.util.TreeSet;
53: import javax.xml.parsers.ParserConfigurationException;
54: import org.w3c.dom.Document;
55: import org.w3c.dom.Element;
56: import org.xml.sax.SAXException;
57:
58: /**
59: * Root object of the test model. Describes one test.
60: * <p>
61: * TODO: Transaction needs some 'beforeTest'/'afterTest' hook to clean up database
62: *
63: * @author Kohsuke Kawaguchi
64: */
65: public class TestDescriptor {
66:
67: /**
68: * A Java identifier that represents a name of this test.
69: * <p>
70: * A test name needs to be unique among all the tests. It will be
71: * generated by the test harness dynamically from the test's partial
72: * directory path. Namely, from toplevel test case directory to the
73: * specific test case directory.
74: * Something like this 'testcases.policy.parsing.someSpecificTest'
75: * <p>
76: * This token is used by the harness to avoid collision when
77: * running multiple tests in parallel (for example, this can be
78: * used as a web application name so that multiple test services can be
79: * deployed on the same container without interference.)
80: */
81: @NotNull
82: public final String name;
83:
84: /**
85: * If non-null, this directory contains resources files used for tests.
86: * <p>
87: * To clients, this resource will be available from {@link ScriptBaseClass#resource(String)}.
88: */
89: @Nullable
90: public final File resources;
91:
92: /**
93: * If non-null, this directory contains Java files shared by service and client.
94: */
95: @Nullable
96: public final File common;
97:
98:
99: /**
100: * Versions of the program that this test applies.
101: */
102: @NotNull
103: public final VersionProcessor applicableVersions;
104:
105: /**
106: * Human readable description of this test.
107: * This could be long text that spans across multiple lines.
108: */
109: @Nullable
110: public final String description;
111:
112: /**
113: * Represents a set of transport that this test supports.
114: */
115: @NotNull
116: public final TransportSet supportedTransport;
117:
118: /**
119: * Represents a set of Use keywords that are used by this test
120: */
121: public final Set<String> useSet;
122:
123: /**
124: * Optional metadata files that describes this service.
125: */
126: public final List<String> metadatafiles = new ArrayList<String>();
127:
128: /**
129: * Bugster IDs that are related to this test.
130: * Can be empty set but not null.
131: */
132: @NotNull
133: public final SortedSet<Integer> bugsterIds = new TreeSet<Integer>();
134:
135: /**
136: * Client test scenarios that are to be executed.
137: * <p>
138: * When this field is empty, that means the test is just to make sure
139: * that the service deploys.
140: */
141: @NotNull
142: public final List<TestClient> clients = new ArrayList<TestClient>();
143:
144: /**
145: * Possibly empty list of JAXB/JAX-WS external binding customizations.
146: */
147: @NotNull
148: public final List<File> clientCustomizations = new ArrayList<File>();
149:
150: /**
151: * Optional "set up" script executed before each client script.
152: */
153: @Nullable
154: public final String setUpScript;
155:
156: /**
157: * Java client.
158: */
159: @NotNull
160: public final List<File> javaClients = new ArrayList<File>();
161:
162: /**
163: * Services to be deployed for this test.
164: */
165: @NotNull
166: public final List<TestService> services = new ArrayList<TestService>();
167:
168: /**
169: * <xml-resource>s.
170: */
171: public final Map<String, XmlResource> xmlResources = new HashMap<String, XmlResource>();
172:
173: /**
174: * Root of the test data directory.
175: */
176: @NotNull
177: public final File home;
178:
179: /**
180: * Additional arguments to configure the harness behavior per test.
181: */
182: public final List<String> testOptions = new ArrayList<String>();
183:
184:
185: /**
186: * Additional command-line arguments to wsimport for generating client artifacts.
187: */
188: public final List<String> wsimportClientOptions = new ArrayList<String>();
189: /**
190: * Additional command-line arguments to wsimport for generating server artifacts
191: * for Java-first case.
192: */
193: public final List<String> wsimportServerOptions = new ArrayList<String>();
194:
195: /**
196: * Additional command-line arguments to wsgen for generating artifacts.
197: */
198: public final List<String> wsgenOptions = new ArrayList<String>();
199:
200: /**
201: * Additional command-line arguments to javac
202: */
203: public final List<String> javacOptions = new ArrayList<String>();
204: /**
205: * Additional command-line arguments to javac
206: */
207: public final List<String> systemProperties = new ArrayList<String>();
208:
209: private boolean skip;
210:
211: private static final String JDK6_EXCLUDE_VERSION = "jdk6";
212:
213: /**
214: * If true, we don't want to package the result of wsgen so that
215: * we can test the generation of wrapper beans at the runtime.
216: * <p>
217: * False otherwise.
218: */
219: public final boolean disgardWsGenOutput;
220:
221: public final boolean jdk6;
222:
223: public TestDescriptor(String shortName, File home, File resources, File common, VersionProcessor applicableVersions, String description, boolean disgardWsGenOutput, boolean jdk6) {
224: this.name = shortName;
225: this.home = home;
226: this.resources = resources;
227: this.common = common;
228: this.applicableVersions = applicableVersions;
229: this.disgardWsGenOutput = disgardWsGenOutput;
230: this.supportedTransport = TransportSet.ALL;
231: this.description = description;
232: this.skip = false;
233: this.setUpScript = null;
234: this.jdk6 = jdk6;
235: this.useSet = new HashSet<String>();
236: }
237:
238: /**
239: * Parses a {@link TestDescriptor} from a test data directory.
240: *
241: * @param descriptor Test descriptor XML file.
242: */
243: public TestDescriptor(File descriptor, boolean disgardWsGenOutput, boolean jdk6) throws IOException, ParserConfigurationException, SAXException {
244: this.disgardWsGenOutput = disgardWsGenOutput;
245: this.jdk6 = jdk6;
246: File testDir = descriptor.getParentFile();
247: Element root = parse(descriptor).getDocumentElement();
248:
249: VersionProcessor versionProcessor;
250: List<Element> list = XMLUtil.getElements(root, "/descriptor/description");
251: this.description = list.isEmpty() ? "" : list.get(0).getTextContent().trim();
252:
253: /*
254: * Check if the resources folder exists in the dir where the
255: * test-descriptor.xml is present else it is null
256: */
257: File resourceDir = new File(testDir, "resources");
258: this.resources = resourceDir.exists() ? resourceDir : null;
259:
260: for (Element xre : XMLUtil.getElements(root, "/descriptor/xml-resource")) {
261: final XmlResource xr;
262: final String attr = xre.getAttribute("href");
263: if (attr == null || attr.trim().isEmpty()) {
264: xr = new InlineXmlResource((Element) xre.getFirstChild());
265: } else {
266: xr = new ReferencedXmlResource(new File(testDir, attr));
267: }
268: xmlResources.put(xre.getAttribute("name"), xr);
269: }
270:
271:
272: /*
273: * Check if the common folder exists in the dir where the
274: * test-descriptor.xml is present else it is null
275: */
276: File commonDir = new File(testDir, "common");
277: this.common = commonDir.exists() ? commonDir : null;
278: this.applicableVersions = getVersionProcessor(root);
279:
280: this.skip = Boolean.parseBoolean(root.getAttribute("skip"));
281:
282: parseArguments(XMLUtil.getTextFrom(root, "/descriptor/test-options"), testOptions);
283: parseArguments(XMLUtil.getTextFrom(root, "/descriptor/wsimport-client"), wsimportClientOptions);
284: parseArguments(XMLUtil.getTextFrom(root, "/descriptor/wsimport-server"), wsimportServerOptions);
285: parseArguments(XMLUtil.getTextFrom(root, "/descriptor/wsgen-options"), wsgenOptions);
286: parseArguments(XMLUtil.getTextFrom(root, "/descriptor/javac-options"), javacOptions);
287: parseArguments(XMLUtil.getTextFrom(root, "/descriptor/system-properties"), systemProperties);
288:
289: String transport = XMLUtil.getAttributeOrNull(root, "transport");
290: if (transport == null) {
291: this.supportedTransport = TransportSet.ALL;
292: } else {
293: this.supportedTransport = new Singleton(transport);
294: }
295:
296: this.useSet = new HashSet<String>();
297: String uses = XMLUtil.getAttributeOrNull(root, "uses");
298: if (uses != null) {
299: StringTokenizer st = new StringTokenizer(uses);
300: while (st.hasMoreTokens()) {
301: useSet.add(st.nextToken());
302: }
303: }
304:
305: String path = testDir.getCanonicalPath();
306: String testCasesPattern = "testcases" + File.separatorChar;
307: int testCaseIndex = path.lastIndexOf(testCasesPattern);
308: testCaseIndex += testCasesPattern.length();
309: /*
310: * For something like this 'testcases.policy.parsing.someSpecificTest'
311: * I think the shortName should be policy.parsing
312: * not testcases.policy.parsing as the above would conform to
313: * a valid package name too
314: */
315: this.name = path.substring(testCaseIndex).replace(File.separatorChar, '.');
316:
317: this.home = descriptor.getParentFile();
318:
319: list = XMLUtil.getElements(root, "/descriptor/pre-client");
320: // there is supposed to be at most 1 element
321: this.setUpScript = list.isEmpty() ? null : XMLUtil.getTextFrom(list.get(0));
322:
323: for (Element client : XMLUtil.getElements(root, "/descriptor/client")) {
324: versionProcessor = getVersionProcessor(client);
325:
326: boolean sideEffectFree = client.getAttribute("sideEffectFree") != null;
327: String clientTransport = XMLUtil.getAttributeOrNull(client, "transport");
328: TransportSet clientSupportedTransport = (clientTransport == null)
329: ? TransportSet.ALL : new Singleton(clientTransport);
330:
331: String href = XMLUtil.getAttributeOrNull(client, "href");
332: if (href != null) {
333: // reference to script files
334: FileSet fs = new FileSet();
335: fs.setDir(testDir);
336: fs.setIncludes(href);
337: for (String relPath : fs.getDirectoryScanner(World.project).getIncludedFiles()) {
338: TestClient testClient = new TestClient(this, versionProcessor, clientSupportedTransport,
339: new Script.File(new File(testDir, relPath)), sideEffectFree);
340: this.clients.add(testClient);
341: }
342: } else {
343: // literal text
344: TestClient testClient = new TestClient(this, versionProcessor, clientSupportedTransport,
345: new Script.Inline(client.getAttribute("name"), XMLUtil.getTextFrom(client)),
346: sideEffectFree);
347: this.clients.add(testClient);
348: }
349: }
350:
351: File customization = parseFile(testDir, "custom-client.xml");
352: if (customization.exists())
353: clientCustomizations.add(customization);
354: File schemaCustomization = parseFile(testDir, "custom-schema-client.xml");
355: if (schemaCustomization.exists())
356: clientCustomizations.add(schemaCustomization);
357:
358: findAllJavaClients(home);
359:
360:
361: List<Element> serviceList = XMLUtil.getElements(root, "/descriptor/service");
362: populateServices(serviceList, testDir, false);
363: List<Element> stsList = XMLUtil.getElements(root, "/descriptor/sts");
364: populateServices(stsList, testDir, true);
365:
366:
367: list = XMLUtil.getElements(root, "/descriptor/external-metadata");
368: for (Element element : list) {
369: String filepath = XMLUtil.getAttributeOrNull(element, "file");
370: if (filepath != null) {
371: metadatafiles.add(filepath);
372: }
373: }
374:
375: }
376:
377: /**
378: * Tokenize the given string and add them to the given list.
379: */
380: private void parseArguments(String s, List<String> result) {
381: if (s != null)
382: result.addAll(Arrays.asList(s.split("\\p{Space}+")));
383: }
384:
385: /**
386: * Recursively scans the test directory and finds all the Java test files.
387: */
388: private void findAllJavaClients(File dir) {
389: for (File child : dir.listFiles()) {
390: if (child.getName().equals("work"))
391: // don't look for the generated files, since often JAXB generates
392: // files that end with 'Test' from WSDL
393: continue;
394: if (child.isDirectory())
395: findAllJavaClients(child);
396: if (child.getName().endsWith("Test.java"))
397: javaClients.add(child);
398: }
399: }
400:
401:
402: /**
403: * Creates the execution plan of this test descriptor and adds them
404: * to {@link TestSuite} (so that when {@link TestSuite}
405: * is executed, you execute this test.
406: *
407: * @param container The container to host the services.
408: * @param clientScriptName See {@link Main#clientScriptName}
409: * @param concurrentSideEffectFree See {@link Main#concurrentSideEffectFree}
410: * @return {@link TestSuite} that contains test execution plan for this test.
411: */
412: public TestSuite build(ApplicationContainer container, WsTool wsimport, String clientScriptName,
413: boolean concurrentSideEffectFree, VersionNumber version) throws IOException {
414:
415: TestSuite suite = new TestSuite();
416:
417: if (skip) {
418: System.out.println("Skipping " + name + "; explictly marked to skip.");
419: return suite;
420: }
421:
422: if (!supportedTransport.contains(container.getTransport())) {
423: System.out.println("Skipping " + name + " as it's not applicable to " + container.getTransport());
424: return suite;
425: }
426:
427: Set<String> temp = new HashSet<String>(useSet);
428: temp.retainAll(container.getUnsupportedUses());
429: if (temp.size() > 0) {
430: System.out.println("Skipping " + name + " as the container " + container + " doesn't support " + temp);
431: return suite;
432: }
433:
434: DeploymentContext context = new DeploymentContext(this, container, wsimport);
435:
436: List<DeploymentExecutor> deployTests = new ArrayList<DeploymentExecutor>();
437:
438: // first prepare the working directories.
439: // we shouldn't do it after the run, or else developers won't be able to
440: // see what's generated to debug problems
441: // in the -skip mode, don't clean
442: suite.addTest(new PrepareExecutor(context, !wsimport.isNoop()));
443:
444: // deploy all services
445: for (DeployedService s : context.services.values()) {
446: DeploymentExecutor dt = new DeploymentExecutor(s);
447: deployTests.add(dt);
448: suite.addTest(dt);
449: }
450:
451: if (context.services.isEmpty() && new File(context.descriptor.home, "client").exists()) {
452: // no services. just run the clients as tests
453: suite.addTest(new ClientCompileExecutor(context));
454: }
455:
456: // run client test scripts
457: for (TestClient c : clients) {
458: if (clientScriptName != null && !c.script.getName().equals(clientScriptName))
459: continue; // skip
460: if (version != null && !c.applicableVersions.isApplicable(version)) {
461: System.err.println("Not applicable to current version=" + version + ". Skipping " + c.script.getName());
462: continue;
463: }
464: if (!c.supportedTransport.contains(container.getTransport())) {
465: System.out.println("Skipping " + c.script.getName() + " as it's not applicable to " + container.getTransport());
466: continue;
467: }
468: if (concurrentSideEffectFree && c.sideEffectFree) {
469: suite.addTest(new ConcurrentClientExecutor.Fixed(context, c));
470: suite.addTest(new ConcurrentClientExecutor.Cached(context, c));
471: } else
472: suite.addTest(new ClientExecutor(context, c));
473: }
474:
475: // run client Java tests
476: for (File f : javaClients)
477: suite.addTest(new JavaClientExecutor(context, f, version));
478:
479: // undeploy all services
480: for (DeploymentExecutor dt : deployTests) {
481: suite.addTest(dt.createUndeployer());
482: }
483:
484: return suite;
485: }
486:
487: /**
488: * Parses a potentially relative file path.
489: */
490: private File parseFile(File base, String href) {
491:
492: File f = new File(href);
493: if (f.isAbsolute())
494: return f;
495: else
496: return new File(base, href);
497: }
498:
499: /**
500: * Parses a test descriptor.
501: */
502: private Document parse(File descriptor) throws ParserConfigurationException, SAXException, IOException {
503: return XMLUtil.readXML(descriptor, World.class.getResource("test-descriptor.rnc"));
504: }
505:
506: /**
507: * Returns a human readable name that identifies the test,
508: * for better readability of the test result report.
509: */
510: public String toString() {
511: return name;
512: }
513:
514: class XSDFilter implements FilenameFilter {
515: public boolean accept(File dir, String name) {
516: return (name.endsWith(".xsd"));
517: }
518: }
519:
520: /**
521: * This filter gives all wsdls in the directory excluding primaryWsdl.
522: * This can be used to gather all imported wsdls.
523: */
524:
525: class WSDLFilter implements FilenameFilter {
526: String primaryWsdl;
527:
528: public WSDLFilter(String primaryWsdl) {
529: this.primaryWsdl = primaryWsdl;
530: }
531:
532: public boolean accept(File dir, String name) {
533:• return (name.endsWith(".wsdl") && (!name.equals(primaryWsdl)));
534: }
535: }
536:
537: private void populateServices(List<Element> serviceList, File testDir, boolean isSTS) throws IOException {
538: for (Element service : serviceList) {
539: String baseDir = XMLUtil.getAttributeOrDefault(service, "basedir", ".");
540:
541: String serviceName;
542: File serviceBaseDir;
543: if (!baseDir.equals(".")) {
544: serviceBaseDir = new File(testDir, baseDir);
545: serviceName = serviceBaseDir.getCanonicalFile().getName();
546: } else {
547: serviceName = "";
548: serviceBaseDir = testDir;
549: }
550:
551: List<WSDL> wsdlInfo = new LinkedList<WSDL>();
552: String xpath = isSTS ? "/descriptor/sts/wsdl" : "/descriptor/service/wsdl";
553: for (Element el : XMLUtil.getElements(service, xpath)) {
554: File serviceTargetDir = serviceBaseDir;
555: String wsdlAttribute = XMLUtil.getAttributeOrDefault(el, "href", "test.wsdl");
556: File wsdl = parseFile(serviceTargetDir, wsdlAttribute);
557: String relLoc = null;
558:
559: FileSet schemaSet = new FileSet();
560: if (wsdlAttribute.contains("/")) {
561: relLoc = wsdlAttribute.substring(0, wsdlAttribute.lastIndexOf("/"));
562: serviceTargetDir = new File(serviceTargetDir, relLoc);
563: }
564: schemaSet.setDir(serviceTargetDir);
565: schemaSet.setIncludes("**/*.xsd");
566: schemaSet.setExcludes("work/**");
567: List<File> schemaFiles = new ArrayList<File>();
568: for (String relPath : schemaSet.getDirectoryScanner(World.project).getIncludedFiles()) {
569: schemaFiles.add(new File(serviceTargetDir, relPath));
570: }
571:
572: FileSet wsdlSet = new FileSet();
573: wsdlSet.setDir(serviceTargetDir);
574: wsdlSet.setIncludes("**/*.wsdl");
575: wsdlSet.setExcludes("wsdlAttribute, work/**");
576:
577: List<File> importedWsdls = new ArrayList<File>();
578: for (String relPath : wsdlSet.getDirectoryScanner(World.project).getIncludedFiles()) {
579: importedWsdls.add(new File(serviceTargetDir, relPath));
580: }
581: // File[] schemas = serviceBaseDir.listFiles(new XSDFilter());
582: // File[] wsdls = serviceBaseDir.listFiles(new WSDLFilter(wsdlAttribute));
583: // List<File> importedWsdls = Arrays.asList(wsdls);
584: // List<File> schemaFiles = Arrays.asList(schemas);
585: wsdlInfo.add(new WSDL(wsdl, importedWsdls, schemaFiles, relLoc));
586:
587: }
588:
589: TestService testService = new TestService(this, serviceName, serviceBaseDir, wsdlInfo, isSTS,
590: XMLUtil.getAttributeOrNull(service, "class"));
591:
592: File customization = parseFile(serviceBaseDir, "custom-server.xml");
593: if (customization.exists()) {
594: testService.customizations.add(customization);
595: }
596: File schemaCustomization = parseFile(serviceBaseDir, "custom-schema-server.xml");
597: if (schemaCustomization.exists()) {
598: testService.customizations.add(schemaCustomization);
599: }
600:
601:
602: this.services.add(testService);
603:
604: }
605: }
606:
607: private VersionProcessor getVersionProcessor(Element e) {
608: // <client excludeFrom="jdk6" is excluded when run with -jdk6 flag
609: String excludeFrom = XMLUtil.getAttributeOrNull(e, "excludeFrom");
610: if (excludeFrom != null && excludeFrom.contains(JDK6_EXCLUDE_VERSION)) {
611: if (jdk6) {
612: excludeFrom = "all";
613: } else {
614: excludeFrom = excludeFrom.replace(JDK6_EXCLUDE_VERSION, "");
615: }
616: }
617: return new VersionProcessor(
618: XMLUtil.getAttributeOrNull(e, "since"),
619: XMLUtil.getAttributeOrNull(e, "until"),
620: excludeFrom);
621: }
622: }