⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wadlresourcetest.java

📁 resetful样式的ws样例,一种面向资源的webservices服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        // 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");        // test ExtraResource        r = resource("/foo");                tmpFile = r.options(File.class);        b = bf.newDocumentBuilder();        d = b.parse(tmpFile);        printSource(new DOMSource(d));        // check base URI        val = (String)xp.evaluate("/wadl:application/wadl:resources/@base", d, XPathConstants.STRING);        assertEquals(val,BASE_URI.toString());        // check total number of resources is 1 (no ExtraResource details included)        val = (String)xp.evaluate("count(//wadl:resource)", d, XPathConstants.STRING);        assertEquals(val,"1");        // check only once resource with path foo        val = (String)xp.evaluate("count(//wadl:resource[@path='foo'])", d, XPathConstants.STRING);        assertEquals(val,"1");        // check 1 methods for foo        val = (String)xp.evaluate("count(//wadl:resource[@path='foo']/wadl:method)", d, XPathConstants.STRING);        assertEquals(val,"1");    }        public void testOptionsLocatorWadl() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {        initiateWebApplication(WidgetsResource.class, ExtraResource.class);        WebResource r = resource("/widgets/3/verbose");                // test WidgetsResource        File tmpFile = r.accept(MediaTypes.WADL).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 1 (no ExtraResource details included)        val = (String)xp.evaluate("count(//wadl:resource)", d, XPathConstants.STRING);        assertEquals(val,"1");        // check only once resource with path         val = (String)xp.evaluate("count(//wadl:resource[@path='widgets/3/verbose'])", d, XPathConstants.STRING);        assertEquals(val,"1");        // check 1 methods        val = (String)xp.evaluate("count(//wadl:resource[@path='widgets/3/verbose']/wadl:method)", d, XPathConstants.STRING);        assertEquals(val,"1");    }        public void testOptionsSubResourceWadl() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {        initiateWebApplication(WidgetsResource.class, ExtraResource.class);        WebResource r = resource("/widgets/3");                // test WidgetsResource        File tmpFile = r.accept(MediaTypes.WADL).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"));        String val = (String)xp.evaluate("/wadl:application/wadl:resources/@base", d, XPathConstants.STRING);        assertEquals(val,BASE_URI.toString());        // check total number of resources is 1        val = (String)xp.evaluate("count(//wadl:resource)", d, XPathConstants.STRING);        assertEquals(val,"1");        // check only one resource with for {id}        val = (String)xp.evaluate("count(//wadl:resource[@path='widgets/3'])", d, XPathConstants.STRING);        assertEquals(val,"1");        // check 3 methods        val = (String)xp.evaluate("count(//wadl:resource[@path='widgets/3']/wadl:method)", d, XPathConstants.STRING);        assertEquals(val,"3");    }        @Path("root")    public static class RootResource {        @Path("loc")        public Object getSub() {            return new SubResource();        }    }        public static class SubResource {        @Path("loc")        public Object getSub() {            return new SubResource();        }                @GET        @ProduceMime("text/plain")        public String hello() {            return "Hello World !";        }                @GET        @Path("sub")        @ProduceMime("text/plain")        public String helloSub() {            return "Hello World !";        }    }        public void testRecursive() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {        initiateWebApplication(RootResource.class);        WebResource r = resource("/root/loc");                // test WidgetsResource        File tmpFile = r.accept(MediaTypes.WADL).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"));        String val = (String)xp.evaluate("/wadl:application/wadl:resources/@base", d, XPathConstants.STRING);        assertEquals(val,BASE_URI.toString());        // check only one resource with for 'root/loc'        val = (String)xp.evaluate("count(//wadl:resource[@path='root/loc'])", d, XPathConstants.STRING);        assertEquals(val,"1");                r = resource("/root/loc/loc");                // test WidgetsResource        tmpFile = r.accept(MediaTypes.WADL).options(File.class);        bf = DocumentBuilderFactory.newInstance();        bf.setNamespaceAware(true);        bf.setValidating(false);        bf.setXIncludeAware(false);        b = bf.newDocumentBuilder();        d = b.parse(tmpFile);        printSource(new DOMSource(d));        xp = XPathFactory.newInstance().newXPath();        xp.setNamespaceContext(new NSResolver("wadl", "http://research.sun.com/wadl/2006/10"));        val = (String)xp.evaluate("/wadl:application/wadl:resources/@base", d, XPathConstants.STRING);        assertEquals(val,BASE_URI.toString());        // check only one resource with for 'root/loc'        val = (String)xp.evaluate("count(//wadl:resource[@path='root/loc/loc'])", d, XPathConstants.STRING);        assertEquals(val,"1");            }        private static class NSResolver implements NamespaceContext {        private String prefix;        private String nsURI;                public NSResolver(String prefix, String nsURI) {            this.prefix = prefix;            this.nsURI = nsURI;        }                public String getNamespaceURI(String prefix) {             if (prefix.equals(this.prefix))                 return this.nsURI;             else                 return XMLConstants.NULL_NS_URI;        }        public String getPrefix(String namespaceURI) {            if (namespaceURI.equals(this.nsURI))                return this.prefix;            else                return null;        }        public Iterator getPrefixes(String namespaceURI) {            return null;        }    }        private static void printSource(Source source) {        try {            System.out.println("---------------------");            Transformer trans = TransformerFactory.newInstance().newTransformer();            Properties oprops = new Properties();            oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes");            oprops.put(OutputKeys.INDENT, "yes");            oprops.put(OutputKeys.METHOD, "xml");            trans.setOutputProperties(oprops);            trans.transform(source, new StreamResult(System.out));        } catch (Exception e) {            e.printStackTrace();        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -