コード例 #1
0
    @Test
    public void testEmptyProduces() throws Exception {
      final ResourceConfig rc = new ResourceConfig(EmptyProducesTestResource.class);
      rc.property(ServerProperties.WADL_FEATURE_DISABLE, false);

      final ApplicationHandler applicationHandler = new ApplicationHandler(rc);

      final ContainerResponse containerResponse =
          applicationHandler
              .apply(
                  new ContainerRequest(
                      URI.create("/"),
                      URI.create("/application.wadl"),
                      "GET",
                      null,
                      new MapPropertiesDelegate()))
              .get();

      assertEquals(200, containerResponse.getStatus());

      ((ByteArrayInputStream) containerResponse.getEntity()).reset();

      final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
      bf.setNamespaceAware(true);
      bf.setValidating(false);
      if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
      }
      final DocumentBuilder b = bf.newDocumentBuilder();
      final Document d = b.parse((InputStream) containerResponse.getEntity());
      printSource(new DOMSource(d));
      final XPath xp = XPathFactory.newInstance().newXPath();
      xp.setNamespaceContext(
          new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));

      final NodeList responseElements =
          (NodeList)
              xp.evaluate(
                  "/wadl:application/wadl:resources[@path!='application.wadl']//wadl:method/wadl:response",
                  d,
                  XPathConstants.NODESET);

      for (int i = 0; i < responseElements.getLength(); i++) {
        final Node item = responseElements.item(i);

        assertEquals(0, item.getChildNodes().getLength());
        assertEquals(0, item.getAttributes().getLength());
      }
    }
コード例 #2
0
  @Test
  public void testDefaultPathParamValueFoo() throws ExecutionException, InterruptedException {
    initiateWebApplication(FooResource.class);

    ContainerResponse response = getResponseContext("/foo");
    Assert.assertEquals("default-id", response.getEntity());
  }
コード例 #3
0
 private void _test(ApplicationHandler handler, String requestUri, String expected)
     throws InterruptedException, ExecutionException {
   final ContainerResponse response =
       handler.apply(RequestContextBuilder.from(requestUri, "GET").build()).get();
   assertEquals(200, response.getStatus());
   assertEquals(expected, response.getEntity());
 }
コード例 #4
0
  @Test
  public void testDefaultPathParamValueOnAnotherResource1()
      throws ExecutionException, InterruptedException {
    initiateWebApplication(AnotherResource.class);

    ContainerResponse response = getResponseContext("/foo/test/bar/barrr");
    Assert.assertEquals("test:barrr", response.getEntity());
  }
コード例 #5
0
  @Test
  public void testListOfStringReaderProvider() throws ExecutionException, InterruptedException {
    initiateWebApplication(ListOfStringResource.class, ListOfStringReaderProvider.class);
    final ContainerResponse responseContext =
        getResponseContext(
            UriBuilder.fromPath("/").queryParam("l", "1,2," + "3").build().toString());

    final String s = (String) responseContext.getEntity();

    assertEquals(Collections.singletonList(Arrays.asList("1", "2", "3")).toString(), s);
  }
コード例 #6
0
    /**
     * Test overriding WADL's /application/resources/@base attribute.
     *
     * @throws Exception in case of unexpected test failure.
     */
    @Test
    public void testCustomWadlResourcesBaseUri() throws Exception {
      final ResourceConfig rc = new ResourceConfig(WidgetsResource.class, ExtraResource.class);
      rc.property(ServerProperties.WADL_GENERATOR_CONFIG, MyWadlGeneratorConfig.class.getName());

      final ApplicationHandler applicationHandler = new ApplicationHandler(rc);

      final ContainerResponse containerResponse =
          applicationHandler
              .apply(
                  new ContainerRequest(
                      URI.create("/"),
                      URI.create("/application.wadl"),
                      "GET",
                      null,
                      new MapPropertiesDelegate()))
              .get();

      final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
      bf.setNamespaceAware(true);
      bf.setValidating(false);
      if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
      }
      final DocumentBuilder b = bf.newDocumentBuilder();

      ((ByteArrayInputStream) containerResponse.getEntity()).reset();

      final Document d = b.parse((InputStream) containerResponse.getEntity());
      printSource(new DOMSource(d));
      final XPath xp = XPathFactory.newInstance().newXPath();
      xp.setNamespaceContext(
          new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
      // check base URI
      final String val =
          (String) xp.evaluate("/wadl:application/wadl:resources/@base", d, XPathConstants.STRING);
      assertEquals(val, MyWadlGeneratorConfig.MyWadlGenerator.CUSTOM_RESOURCES_BASE_URI);
    }
コード例 #7
0
 public String test(HttpMethodOverrideFilter.Source... sources) {
   ResourceConfig rc = new ResourceConfig(Resource.class);
   HttpMethodOverrideFilter.enableFor(rc, sources);
   ApplicationHandler handler = new ApplicationHandler(rc);
   try {
     ContainerResponse response =
         handler
             .apply(
                 RequestContextBuilder.from("", "/?_method=DELETE", "POST")
                     .header("X-HTTP-Method-Override", "PUT")
                     .build())
             .get();
     if (Response.Status.OK.equals(response.getStatusInfo())) {
       return (String) response.getEntity();
     } else {
       return "" + response.getStatus();
     }
   } catch (Exception e) {
     e.printStackTrace();
     return e.getMessage();
   }
 }