コード例 #1
0
  public void testStandardTemplate() throws Exception {
    // Create a temporary directory for the tests
    final File testDir = new File(System.getProperty("java.io.tmpdir"), "VelocityTestCase");
    testDir.mkdir();

    // Create a temporary template file
    final File testFile = File.createTempFile("test", ".vm", testDir);
    final FileWriter fw = new FileWriter(testFile);
    fw.write("Value=$value");
    fw.close();

    final Map<String, Object> map = new TreeMap<String, Object>();
    map.put("value", "myValue");

    // Standard approach
    final TemplateRepresentation tr =
        new TemplateRepresentation(testFile.getName(), map, MediaType.TEXT_PLAIN);
    tr.getEngine().setProperty("file.resource.loader.path", testDir.getAbsolutePath());
    final String result = tr.getText();
    assertEquals("Value=myValue", result);

    // Clean-up
    BioUtils.delete(testFile);
    BioUtils.delete(testDir, true);
  }
コード例 #2
0
  public void testRepresentationTemplate() throws Exception {
    // Create a temporary directory for the tests
    File testDir = new File(System.getProperty("java.io.tmpdir"), "VelocityTestCase");
    testDir.mkdir();

    // Create a temporary template file
    File testFile = File.createTempFile("test", ".vm", testDir);
    FileWriter fw = new FileWriter(testFile);
    fw.write("Value=$value");
    fw.close();

    Map<String, Object> map = new TreeMap<String, Object>();
    map.put("value", "myValue");

    // Representation approach
    Reference ref = LocalReference.createFileReference(testFile);
    ClientResource r = new ClientResource(ref);
    Representation templateFile = r.get();
    TemplateRepresentation tr = new TemplateRepresentation(templateFile, map, MediaType.TEXT_PLAIN);
    final String result = tr.getText();
    assertEquals("Value=myValue", result);

    // Clean-up
    BioUtils.delete(testFile);
    BioUtils.delete(testDir, true);
  }