public void testVariableNames() throws Exception {
    Template tpl = new Template("http://{userId}.noelios.com/invoices/{invoiceId}");
    tpl.setLogger(Engine.getAnonymousLogger());
    List<String> names = tpl.getVariableNames();

    assertEquals(2, names.size());
    assertEquals("userId", names.get(0));
    assertEquals("invoiceId", names.get(1));
  }
  public void testEncodedCharacters() {
    Template template = new Template("http://localhost/{token}/bookstore/{bookid}");
    String encodedToken =
        "FtDF91VSX%2F7AN6C39k51ZV510SW%2Fot6SIGstq8XGCcHfOfHbZOZLUD4u%2BGUNK0bBawVZ4GR5TgV7PtRbF%2Bnm9abYJN6AWycdj9J6CLyU4D7Zou36KEjkel%2B0LtlGGhFPVrCvpBuqPy8V8o5IZ9tDys0Py6sXXAtEVbXBYeRYzOvIBzOZkIviIyceVCU%2BlYv%2Fh9k7Fhlb1JGtKUCj3ZDg%2FvJ1Co7dOC1Ho3%2Fe0Fup7k9qgTuCvZRSHcpizaEFPNLp";
    String targetUri = "http://localhost/" + encodedToken + "/bookstore/1234";

    Map<String, Object> variables1 = new HashMap<String, Object>();
    int parsed1 = template.parse(targetUri, variables1);
    assertTrue("parsing of " + targetUri + " not successful, but it should be.", parsed1 >= 0);
    assertEquals(encodedToken, variables1.get("token"));
  }
  /**
   * Returns the reference of the target according to the a list of properties.
   *
   * @param resolver The resolver.
   * @return The target reference.
   * @throws ResourceException
   */
  protected Reference getTargetRef(Resolver<String> resolver) throws ResourceException {
    final Template targetTemplate = new Template(getTargetUri());
    Reference result = new Reference(targetTemplate.format(resolver));

    if (result.isRelative()) {
      result.setBaseRef(getMailboxUri());
      result = result.getTargetRef();
    }

    return result;
  }
  /**
   * Returns the reference of a mail according to its identifier.
   *
   * @param identifier The identifier of a mail.
   * @return The URI of the mail.
   * @throws ResourceException
   */
  protected Reference getMailRef(String identifier) throws ResourceException {
    final Template mailTemplate = new Template(getMailUriTemplate());
    Reference result = new Reference(mailTemplate.format(new MailResolver(identifier)));

    if (result.isRelative()) {
      result.setBaseRef(getMailboxUri());
      result = result.getTargetRef();
    }

    return result;
  }
  public void testWithPercentChars() {
    Template template = new Template("abc/{v1}");
    template.getDefaultVariable().setType(Variable.TYPE_URI_ALL);
    Map<String, Object> variables1 = new HashMap<String, Object>();
    String string1 = "abc/hff11kh";
    int parsed1 = template.parse(string1, variables1);
    assertTrue("parsing of " + string1 + " not successful, but it should be.", parsed1 >= 0);
    assertEquals("hff11kh", variables1.get("v1"));

    Map<String, Object> variables2 = new HashMap<String, Object>();
    String string2 = "abc/hf%20kh";
    int parsed2 = template.parse(string2, variables2);
    assertTrue("parsing of " + string2 + " not successful, but it should be.", parsed2 >= 0);
    assertEquals("hf%20kh", variables2.get("v1"));
  }
  private static Resource getResource(
      CollectInfo collectInfo, Object restlet, String basePath, ChallengeScheme scheme) {
    Resource resource = new Resource();
    resource.setResourcePath(basePath);

    if (restlet instanceof Directory) {
      Directory directory = (Directory) restlet;
      resource.setName(directory.getName());
      resource.setDescription(directory.getDescription());
    }
    if (restlet instanceof ServerResource) {
      ServerResource serverResource = (ServerResource) restlet;
      resource.setName(serverResource.getName());
      resource.setDescription(serverResource.getDescription());
    }
    if (restlet instanceof DocumentedResource) {
      DocumentedResource documentedServerResource = (DocumentedResource) restlet;
      resource.setSections(documentedServerResource.getSections());
    } else if (collectInfo.isUseSectionNamingPackageStrategy()) {
      String sectionName = restlet.getClass().getPackage().getName();
      resource.getSections().add(sectionName);
    }

    if (StringUtils.isNullOrEmpty(resource.getName())) {
      String name = restlet.getClass().getSimpleName();
      if (name.endsWith(SUFFIX_SERVER_RESOURCE)
          && name.length() > SUFFIX_SERVER_RESOURCE.length()) {
        name = name.substring(0, name.length() - SUFFIX_SERVER_RESOURCE.length());
      }
      if (name.endsWith(SUFFIX_RESOURCE) && name.length() > SUFFIX_RESOURCE.length()) {
        name = name.substring(0, name.length() - SUFFIX_RESOURCE.length());
      }
      resource.setName(name);
    }

    Template template = new Template(basePath);
    for (String variable : template.getVariableNames()) {
      PathVariable pathVariable = new PathVariable();
      pathVariable.setName(variable);
      resource.getPathVariables().add(pathVariable);
    }

    if (scheme != null) {
      resource.setAuthenticationProtocol(scheme.getName());
    }

    return resource;
  }
  public void testPathMatching() {
    Template template = new Template("http://www.mydomain.com/abc/{v1}");
    template.setMatchingMode(Template.MODE_STARTS_WITH);
    template.getDefaultVariable().setType(Variable.TYPE_URI_PATH);

    Map<String, Object> variables1 = new HashMap<String, Object>();
    String string1 = "http://www.mydomain.com/abc/123/456";
    int parsed1 = template.parse(string1, variables1);
    assertTrue("parsing of " + string1 + " not successful, but it should be.", parsed1 >= 0);
    assertEquals("123/456", variables1.get("v1"));

    Map<String, Object> variables2 = new HashMap<String, Object>();
    String string2 = "http://www.mydomain.com/abc/123/456?s=tuv";
    int parsed2 = template.parse(string2, variables2);
    assertTrue("parsing of " + string2 + " not successful, but it should be.", parsed2 >= 0);
    assertEquals("123/456", variables2.get("v1"));

    Map<String, Object> variables3 = new HashMap<String, Object>();
    String string3 = "http://www.mydomain.com/abc/123/456#tuv";
    int parsed3 = template.parse(string3, variables3);
    assertTrue("parsing of " + string3 + " not successful, but it should be.", parsed3 >= 0);
    assertEquals("123/456", variables3.get("v1"));
  }