WebXml { public List<String> endpointsServiceClasses() { XPath xpath = XPathFactory.newInstance().newXPath(); String findService = "/web-app/servlet" + "[" + "servlet-class = 'com.google.api.server.spi.SystemServiceServlet'" + " or " + "servlet-class = '" + EndpointsServlet.class.getName() + "'" + "]/init-param[param-name = 'services']/param-value/text()"; try { String servicesString = (String) xpath.evaluate(findService, document, XPathConstants.STRING); List<String> services = Lists.newArrayList(); for (String service : servicesString.trim().split(",")) { if (!Strings.isNullOrEmpty(service.trim())) { services.add(service.trim()); } } return services; } catch (XPathExpressionException e) { throw new RuntimeException(e); } } WebXml(Document document); static WebXml parse(File webXml); List<String> endpointsServiceClasses();  }
@Test public void testServiceClasses_singleServiceSystemServiceServlet() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .systemServiceServlet("com.test.EndpointOne") .systemServiceServletMapping() .build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); List<String> expected = Collections.singletonList("com.test.EndpointOne"); Assert.assertEquals(expected, serviceClasses); }
@Test public void testServiceClasses_singleServiceEndpointsServlet() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .endpointsServlet("com.test.EndpointOne") .endpointsServletMapping() .build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); List<String> expected = Collections.singletonList("com.test.EndpointOne"); Assert.assertEquals(expected, serviceClasses); }
@Test public void testServiceClasses_multipleServicesSystemServiceServlet() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .systemServiceServlet("com.test.EndpointOne, com.test.EndpointTwo") .systemServiceServletMapping() .build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); List<String> expected = Arrays.asList("com.test.EndpointOne", "com.test.EndpointTwo"); Assert.assertEquals(expected, serviceClasses); }
@Test public void testServiceClasses_multipleServicesEndpointsServlet() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .endpointsServlet("com.test.EndpointOne, com.test.EndpointTwo") .endpointsServletMapping() .build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); List<String> expected = Arrays.asList("com.test.EndpointOne", "com.test.EndpointTwo"); Assert.assertEquals(expected, serviceClasses); }
@Test public void testServiceClasses_noServices() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .systemServiceServlet("") .systemServiceServletMapping() .build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); Assert.assertEquals(0, serviceClasses.size()); }
@Test public void testServiceClasses_badSpacing() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .systemServiceServlet(" com.test.EndpointOne, \n com.test.EndpointTwo ") .systemServiceServletMapping() .build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); List<String> expected = Arrays.asList("com.test.EndpointOne", "com.test.EndpointTwo"); Assert.assertEquals(expected, serviceClasses); }
@Test public void testServiceClasses_noneWithBadSpacing() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder().systemServiceServlet(" , \n, ") .systemServiceServletMapping().build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); Assert.assertEquals("Expecting nothing, found : " + serviceClasses, 0, serviceClasses.size()); }
@Test public void testServiceClasses_emptyParamValueTag() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .raw(" <servlet>" + " <servlet-name>SystemServiceServlet</servlet-name>" + " <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>" + " <init-param>" + " <param-name>services</param-name>" + " <param-value/>" + " </init-param>" + " </servlet>") .systemServiceServletMapping().build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); Assert.assertEquals("Expecting nothing, found : " + serviceClasses, 0, serviceClasses.size()); }
@Test public void testServiceClasses_noInitParams() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .raw(" <servlet>" + " <servlet-name>SystemServiceServlet</servlet-name>" + " <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>" + " </servlet>") .systemServiceServletMapping().build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); Assert.assertEquals("Expecting nothing, found : " + serviceClasses, 0, serviceClasses.size()); }
@Test public void testServiceClasses_noMatchingTag() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .servlet("HelloServlet", "com.example.Hello", "com.test.HelloService") .servletMapping("HelloServlet", "/hello") .systemServiceServletMapping().build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); Assert.assertEquals("Expecting nothing, found : " + serviceClasses, 0, serviceClasses.size()); }
@Test public void testServiceClasses_undefinedBehaviorMultipleSystemServiceServlets() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .servlet("SomeJunkServlet", "com.google.api.server.spi.SystemServiceServlet", "com.test.JunkEndpoint") .endpointsServlet("com.test.EndpointOne") .servletMapping("SomeJunkServlet", WebXmlBuilder.SPI_URL) .endpointsServletMapping() .build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); Assert.assertEquals(1, serviceClasses.size()); Assert.assertEquals(serviceClasses.get(0), "com.test.JunkEndpoint"); }
@Test public void testServiceClasses_undefinedBehaviorMultipleMatchingServlets() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .endpointsServlet("com.test.EndpointsServletEndpoint") .systemServiceServlet("com.test.SystemServiceServletEndpoint") .endpointsServletMapping() .systemServiceServletMapping() .build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); Assert.assertEquals(1, serviceClasses.size()); Assert.assertEquals(serviceClasses.get(0), "com.test.EndpointsServletEndpoint"); }
@Test public void testServiceClasses_undefinedBehaviorMultipleMatchingServlets2() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { WebXml webxml = new WebXmlBuilder() .systemServiceServlet("com.test.SystemServiceServletEndpoint") .endpointsServlet("com.test.EndpointsServletEndpoint") .endpointsServletMapping() .systemServiceServletMapping() .build(); List<String> serviceClasses = webxml.endpointsServiceClasses(); Assert.assertEquals(1, serviceClasses.size()); Assert.assertEquals(serviceClasses.get(0), "com.test.SystemServiceServletEndpoint"); }