summaryrefslogtreecommitdiff
path: root/java/pablotron/luigi/Test.java
blob: 7163c526f55bcdfb5c3fe81d5f1637bf302c7ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package pablotron.luigi;

import java.util.Map;
import java.util.HashMap;

import pablotron.luigi.LuigiError;
import pablotron.luigi.Filter;
import pablotron.luigi.Template;
import pablotron.luigi.Cache;

public final class Test {
  // test template
  private static final String TEMPLATE =
    "test basic: hello %{name}\n" +
    "test filter: hello %{name | uc}\n" +
    "test custom: %{name | custom | uc | lc}\n" +
    "test custom_with_arg: %{name | custom_with_arg hello}\n";

  // test template cache
  private static final Cache cache = new Cache(new HashMap<String, String>() {{
    put("test-template", TEMPLATE);
  }});

  // test arguments
  private static final Map<String, String> args = new HashMap<String, String>() {{
    put("name", "paul");
  }};

  // custom filters
  private static final Map<String, Filter.Handler> filters = new HashMap<String, Filter.Handler>() {{
    // add custom filter
    put("custom", new Filter.Handler() {
      public String filter(String val, String args[], Map<String, String> row) {
        return "custom";
      }
    });

    // add custom filter with argument
    put("custom_with_arg", new Filter.Handler() {
      public String filter(String val, String args[], Map<String, String> row) {
        return (args.length > 0) ? args[0] : "custom";
      }
    });
  }};

  public static void main(String params[]) throws Exception {
    // add custom filters
    Filter.FILTERS.putAll(filters);

    // create and run template
    final Template t = new Template(TEMPLATE);
    System.out.print(t.run(args));

    // run cache
    System.out.print(cache.run("test-template", args));
  }
};