📄 wadlresourcetest.java
字号:
/* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html * or jersey/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at jersey/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. *//* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.sun.jersey.impl.wadl;import com.sun.jersey.api.MediaTypes;import com.sun.jersey.impl.AbstractResourceTester;import com.sun.jersey.api.client.WebResource;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.Properties;import javax.ws.rs.ConsumeMime;import javax.ws.rs.DELETE;import javax.ws.rs.GET;import javax.ws.rs.POST;import javax.ws.rs.PUT;import javax.ws.rs.Path;import javax.ws.rs.ProduceMime;import javax.ws.rs.PathParam;import javax.xml.XMLConstants;import javax.xml.namespace.NamespaceContext;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.OutputKeys;import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;import org.w3c.dom.Document;import org.xml.sax.SAXException;/** * * @author mh124079 */public class WadlResourceTest extends AbstractResourceTester { public WadlResourceTest(String testName) { super(testName); } @Path("foo") public static class ExtraResource { @GET @ProduceMime("application/xml") public String getRep() { return null; } } @Path("widgets") public static class WidgetsResource { @GET @ProduceMime({"application/xml", "application/json"}) public String getWidgets() { return null; } @POST @ConsumeMime({"application/xml"}) @ProduceMime({"application/xml", "application/json"}) public String createWidget(String bar) { return bar; } @PUT @Path("{id}") @ConsumeMime("application/xml") public void updateWidget(String bar, @PathParam("id")int id) { } @GET @Path("{id}") @ProduceMime({"application/xml", "application/json"}) public String getWidget(@PathParam("id")int id) { return null; } @DELETE @Path("{id}") public void deleteWidget(@PathParam("id")int id) { } @Path("{id}/verbose") public Object getVerbose(@PathParam("id")int id) { return new ExtraResource(); } } /** * Test WADL generation */ public void testGetWadl() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { initiateWebApplication(WidgetsResource.class, ExtraResource.class); WebResource r = resource("/application.wadl"); File tmpFile = r.get(File.class); DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance(); bf.setNamespaceAware(true); bf.setValidating(false); bf.setXIncludeAware(false); DocumentBuilder b = bf.newDocumentBuilder(); Document d = b.parse(tmpFile); printSource(new DOMSource(d)); XPath xp = XPathFactory.newInstance().newXPath(); xp.setNamespaceContext(new NSResolver("wadl", "http://research.sun.com/wadl/2006/10")); // check base URI String val = (String)xp.evaluate("/wadl:application/wadl:resources/@base", d, XPathConstants.STRING); assertEquals(val,BASE_URI.toString()); // check total number of resources is 4 val = (String)xp.evaluate("count(//wadl:resource)", d, XPathConstants.STRING); assertEquals(val,"4"); // check only once resource with for {id} val = (String)xp.evaluate("count(//wadl:resource[@path='{id}'])", d, XPathConstants.STRING); assertEquals(val,"1"); // check only once resource with for {id}/verbose val = (String)xp.evaluate("count(//wadl:resource[@path='{id}/verbose'])", d, XPathConstants.STRING); assertEquals(val,"1"); // check only once resource with for widgets val = (String)xp.evaluate("count(//wadl:resource[@path='widgets'])", d, XPathConstants.STRING); assertEquals(val,"1"); // check 3 methods for {id} val = (String)xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method)", d, XPathConstants.STRING); assertEquals(val,"3"); // check 2 methods for widgets val = (String)xp.evaluate("count(//wadl:resource[@path='widgets']/wadl:method)", d, XPathConstants.STRING); assertEquals(val,"2"); // check type of {id} is int val = (String)xp.evaluate("//wadl:resource[@path='{id}']/wadl:param[@name='id']/@type", d, XPathConstants.STRING); assertEquals(val,"xs:int"); // check number of output representations is two val = (String)xp.evaluate("count(//wadl:resource[@path='widgets']/wadl:method[@name='GET']/wadl:response/wadl:representation)", d, XPathConstants.STRING); assertEquals(val,"2"); // check number of output representations is one val = (String)xp.evaluate("count(//wadl:resource[@path='widgets']/wadl:method[@name='POST']/wadl:request/wadl:representation)", d, XPathConstants.STRING); assertEquals(val,"1"); } public void testOptionsResourceWadl() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { initiateWebApplication(WidgetsResource.class, ExtraResource.class); WebResource r = resource("/widgets"); // test WidgetsResource File tmpFile = r.options(File.class); DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance(); bf.setNamespaceAware(true); bf.setValidating(false); bf.setXIncludeAware(false); DocumentBuilder b = bf.newDocumentBuilder(); Document d = b.parse(tmpFile); printSource(new DOMSource(d)); XPath xp = XPathFactory.newInstance().newXPath(); xp.setNamespaceContext(new NSResolver("wadl", "http://research.sun.com/wadl/2006/10")); // check base URI String val = (String)xp.evaluate("/wadl:application/wadl:resources/@base", d, XPathConstants.STRING); assertEquals(val,BASE_URI.toString()); // check total number of resources is 3 (no ExtraResource details included) val = (String)xp.evaluate("count(//wadl:resource)", d, XPathConstants.STRING); assertEquals(val,"3"); // check only once resource with for {id} val = (String)xp.evaluate("count(//wadl:resource[@path='{id}'])", d, XPathConstants.STRING); assertEquals(val,"1"); // check only once resource with for {id}/verbose val = (String)xp.evaluate("count(//wadl:resource[@path='{id}/verbose'])", d, XPathConstants.STRING); assertEquals(val,"1");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -