Ejemplo n.º 1
0
  public void testEmptyRef() {
    Reference reference = new Reference();
    reference.setAuthority("testAuthority"); // must not produce NPE

    reference = new Reference();
    reference.setBaseRef("http://localhost"); // must not produce NPE

    reference = new Reference();
    reference.setFragment("fragment"); // must not produce NPE

    reference = new Reference();
    reference.setHostDomain("localhost"); // must not produce NPE
    assertEquals("localhost", reference.getAuthority());
    reference.setHostPort(new Integer(4711)); // must not produce NPE
    assertEquals("localhost:4711", reference.getAuthority());
    reference.setUserInfo("sdgj:skdfj"); // must not produce NPE
    assertEquals("sdgj:skdfj@localhost:4711", reference.getAuthority());

    reference = new Reference();
    reference.setIdentifier("http://host/abc/wkj"); // must not produce NPE

    reference = new Reference();
    reference.setPath("loc/alhost"); // must not produce NPE

    reference = new Reference();
    reference.setProtocol(Protocol.HTTPS); // must not produce NPE

    reference = new Reference();
    reference.setQuery("a=b&c=&g=1"); // must not produce NPE

    reference = new Reference();
    reference.setRelativePart("http://localhost"); // must not produce NPE

    reference = new Reference();
    reference.setScheme("skjf"); // must not produce NPE

    reference = new Reference();
    reference.setSchemeSpecificPart("host/afjhsd"); // must not produce NPE

    reference = new Reference();
    final List<String> segments = new ArrayList<String>();
    segments.add("skhf");
    segments.add("sgdfg");
    segments.add("xiz");
    reference.setSegments(segments); // must not produce NPE
  }
  @Test
  public void testJSON() throws IOException {
    JSONObject obj = new JSONObject();
    obj.put("title", "TITLE");
    JSONArray stuff = new JSONArray();
    for (int i = 0; i < 10; i++) {
      stuff.add(i);
    }
    obj.put("stuff", stuff);
    stuff = new JSONArray();
    for (int i = 0; i < 10; i++) {
      JSONObject s = new JSONObject();
      s.put("name", i);
      stuff.add(s);
    }
    obj.put("moreStuff", stuff);
    obj.put("missing", null);

    FreemarkerReaderTest.createTemplate(
        "farby.ftl",
        new StringReader(
            "${title}"
                + " <#list stuff as x>${x}</#list> "
                + " <#list moreStuff as s>${s.name}</#list>"
                + " ${missing!\"MISSING\"}"));

    FreemarkerReaderSource src = new FreemarkerReaderSource();
    Request req = new Request();
    Reference reference = new Reference();
    reference.setPath("/unused");
    reference.setQuery("");
    req.setResourceRef(reference);
    req.getAttributes().put("template", "farby");
    String json = obj.toString();
    req.setEntity(json, MediaType.APPLICATION_JSON);
    String text = new Scanner(src.printngReader(req).reader()).useDelimiter("\\Z").next();
    assertEquals("TITLE 0123456789  0123456789 MISSING", text);
  }