File: CSTest.java

package info (click to toggle)
clearsilver 0.10.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: ansic: 24,586; python: 4,233; sh: 2,502; cs: 1,429; ruby: 819; java: 735; makefile: 589; perl: 120; lisp: 34; sql: 21
file content (271 lines) | stat: -rw-r--r-- 10,117 bytes parent folder | download | duplicates (8)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

import java.io.*;
import java.util.*;

import org.clearsilver.CS;
import org.clearsilver.CSFileLoader;
import org.clearsilver.HDF;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


public class CSTest {

    public static void main( String [] args ) throws IOException {
        org.clearsilver.HDF hdf = new HDF();

        System.out.println("Testing HDF set and dump\n");
        hdf.setValue("Foo.Bar","10");
        hdf.setValue("Foo.Baz","20");
        System.out.println( hdf.dump() );

        System.out.println("Testing HDF get\n");
        String foo = hdf.getValue("Foo.Bar", "30");
        System.out.println( foo );
        foo = hdf.getValue("Foo.Baz", "30");
        System.out.println( foo );

        System.out.println( "----" );

        System.out.println("Testing HDF setSymLink\n");
        hdf.setSymLink("Foo.Baz2","Foo.Baz");
        foo = hdf.getValue("Foo.Baz", "30");
        System.out.println( foo );

        System.out.println( "----" );

        System.out.println("Testing HDF get where default value is null\n");
        foo = hdf.getValue("Foo.Bar", null);
        System.out.println("foo = " + foo);
        foo = hdf.getValue("Foo.Nonexistent", null);
        System.out.println("foo = " + foo);

        System.out.println( "----" );

        int fooInt = hdf.getIntValue("Foo.Bar", 30);
        System.out.println("Testing HDF get int\n");
        System.out.println( fooInt );

        System.out.println( "----" );

        org.clearsilver.CS cs = new CS(hdf);

        System.out.println("Testing HDF parse/render\n");
        String tmplstr = "Foo.Bar:<?cs var:Foo.Bar ?>\nFoo.Baz:<?cs var:Foo.Baz ?>\n";
        System.out.println(tmplstr);
        System.out.println("----");

        cs.parseStr(tmplstr);
        System.out.println(cs.render());

        // test registered functions
        System.out.println("Testing registered string functions\n");
        hdf.setValue("Foo.EscapeTest","abc& 231<>/?");

        tmplstr = " <?cs var:url_escape(Foo.EscapeTest) ?> <?cs var:html_escape(Foo.EscapeTest) ?>";

        cs.parseStr(tmplstr);
        System.out.println(cs.render());

        cs = new CS(hdf);

        System.out.println("Testing white space stripping\n");
        // test white space stripping
        tmplstr = "      <?cs var:Foo.Bar ?> This is a       string     without whitespace stripped";
        cs.parseStr(tmplstr);
        System.out.println(cs.render());

        hdf.setValue("ClearSilver.WhiteSpaceStrip", "1");
        System.out.println(cs.render());

        // Now, test debug dump
        System.out.println("Testing debug dump\n");
        hdf.setValue("ClearSilver.DisplayDebug", "1");
        System.out.println(cs.render());

        System.out.println("Final HDF dump\n");
        System.out.println( hdf.dump() );

            // Now, test reading an HDF file from disk
        System.out.println("Testing HDF.readFile()\n");
        HDF file_hdf = new HDF();
        file_hdf.readFile("testdata/test1.hdf");
        System.out.println(file_hdf.dump());

        System.out.println("Testing HDF.readFile() for a file that doesn't exist");
        try {
          file_hdf.readFile("testdata/doesnt_exist.hdf");
        } catch (Exception e) {
          // The error message contains line numbers for functions in
          // neo_hdf.c, and I don't want this test to fail if the line numbers
          // change, so I'm not going to print out the exception message here.
          // The important thing to test here is that an exception is thrown
          // System.out.println(e + "\n");
          System.out.println("Caught exception of type " + e.getClass().getName() + "\n");
        }

	System.out.println("Testing HDF.writeFile()\n");
	file_hdf.writeFile("test1_out.hdf");
	file_hdf.writeFileAtomic("test1_out2.hdf");

	System.out.println("Testing HDF.writeString()\n");
	System.out.println(file_hdf.writeString());

        System.out.println("Testing HDF.getObj()");
        HDF foo_hdf = file_hdf.getObj("Foo");
        System.out.println(foo_hdf.dump());

        System.out.println("Testing HDF.objName()");
        System.out.println("Should be \"Foo\": " + foo_hdf.objName());
        System.out.println("Should be \"Bar\": "
                           + foo_hdf.getObj("Bar").objName());
        System.out.println("Should be null: " + file_hdf.objName() + "\n");

        System.out.println("Testing HDF.objValue()");
        System.out.println("Value of Foo.Bar: "
                           + foo_hdf.getObj("Bar").objValue());
        System.out.println("Value of root node: " + file_hdf.objValue() + "\n");

        System.out.println("Testing HDF.objChild()");
        HDF child_hdf = foo_hdf.objChild();
        System.out.println("First child name: " + child_hdf.objName() + "\n");

        System.out.println("Testing HDF.objNext()");
        HDF next_hdf = child_hdf.objNext();
        System.out.println("Next child name: " + next_hdf.objName());
        next_hdf = next_hdf.objNext();
        System.out.println("Next child (should be null): " + next_hdf + "\n");

	System.out.println("Testing HDF.copy()");
	HDF one = new HDF();
	one.setValue("name", "barneyb");
	one.setValue("age", "25");
	HDF two = new HDF();
	two.setValue("entity.type", "person");
	two.copy("entity.value", one);
	System.out.println("name should be barneyb: " +
	    two.getValue("entity.value.name", "--undefined--") +"\n");

	System.out.println("Testing HDF.exportDate()");
        HDF date_hdf = new HDF();
        date_hdf.exportDate("DatePST", "US/Pacific", 1142308218);
        date_hdf.exportDate("DateEST", "US/Eastern", 1142308218);
        System.out.println(date_hdf.dump());

        // Test default escaping mode: html
        HDF escape_hdf = new HDF();
        System.out.println("Testing escape mode: html");
        System.out.println("Config.VarEscapeMode = \"html\"");
        escape_hdf.setValue("Config.VarEscapeMode", "html");
        cs = new CS(escape_hdf);

        System.out.println("Some.HTML = " +
            "<script src=\"some.js\">alert('123');</script>");
        escape_hdf.setValue("Some.HTML",
            "<script src=\"some.js\">alert('123');</script>");
        tmplstr = "Default HTML escaping: <?cs var:Some.HTML ?>\n";
        System.out.println(tmplstr);
        System.out.println("----");
        cs.parseStr(tmplstr);
        System.out.println(cs.render());

        // Test default escaping mode: js
        escape_hdf = new HDF();
        System.out.println("Testing escape mode: js");
        System.out.println("Config.VarEscapeMode = \"js\"");
        escape_hdf.setValue("Config.VarEscapeMode", "js");
        cs = new CS(escape_hdf);

        System.out.println("Some.HTML = " +
            "<script src=\"some.js\">alert('123');</script>");
        escape_hdf.setValue("Some.HTML",
            "<script src=\"some.js\">alert('123');</script>");
        tmplstr = "Default JS escaping: <?cs var:Some.HTML ?>\n";
        System.out.println(tmplstr);
        System.out.println("----");
        cs.parseStr(tmplstr);
        System.out.println(cs.render());

        // Test default escaping mode: url
        escape_hdf = new HDF();
        System.out.println("Testing escape mode: url");
        System.out.println("Config.VarEscapeMode = \"url\"");
        escape_hdf.setValue("Config.VarEscapeMode", "url");
        cs = new CS(escape_hdf);

        System.out.println("Some.HTML = " +
            "<script src=\"some.js\">alert('123');</script>");
        escape_hdf.setValue("Some.HTML",
            "<script src=\"some.js\">alert('123');</script>");
        tmplstr = "Default URL escaping: <?cs var:Some.HTML ?>\n";
        System.out.println(tmplstr);
        System.out.println("----");
        cs.parseStr(tmplstr);
        System.out.println(cs.render());

        // Test escape blocks
        escape_hdf = new HDF();
        System.out.println("Testing escape blocks: none");
        System.out.println("Config.VarEscapeMode = \"none\"");
        escape_hdf.setValue("Config.VarEscapeMode", "none");
        cs = new CS(escape_hdf);

        System.out.println("Some.HTML = " +
            "<script src=\"some.js\">alert('123');</script>");
        escape_hdf.setValue("Some.HTML",
            "<script src=\"some.js\">alert('123');</script>");
        tmplstr = "url escape block: \n" +
          "<?cs escape: \"url\"?>" +
          "  <?cs var:Some.HTML ?>" +
          "<?cs /escape ?>\n" +
          "js escape block: \n" +
          "<?cs escape: \"js\"?>" +
          "  <?cs var:Some.HTML ?>" +
          "<?cs /escape ?>\n" +
          "html escape block: \n" +
          "<?cs escape: \"html\"?>" +
          "  <?cs var:Some.HTML ?>" +
          "<?cs /escape ?>\n";
        System.out.println(tmplstr);
        System.out.println("----");
        cs.parseStr(tmplstr);
        System.out.println(cs.render());

        System.out.println("Testing HDF.readFile() with callback\n");
        file_hdf = new HDF();
	file_hdf.setFileLoader(new CSTestLoader());
        file_hdf.readFile("testdata/test1.hdf");
        System.out.println(file_hdf.dump());

        System.out.println("Testing CS.parseFile() with callback\n");
        cs = new CS(file_hdf);
	cs.setFileLoader(new CSTestLoader());
	cs.parseFile("testdata/test.cs");
	System.out.println(cs.render());
    }
};

class CSTestLoader implements CSFileLoader {
  public String load(HDF hdf, String filename) throws IOException {
    System.out.println("CSTestLoader::Load " + filename + "\n");
    String data = readFile(new File(filename));
    System.out.println("---- file begin ----\n" + data +
	"\n---- file end ----\n");
    return data;
  }

  private String readFile(File file) throws IOException {
    InputStreamReader fin = new InputStreamReader(new FileInputStream(file),
        "UTF-8");
    StringBuilder sb = new StringBuilder(1024);
    char[] charbuf = new char[1024];
    int len = 0;
    while ((len = fin.read(charbuf)) != -1) {
      sb.append(charbuf, 0, len);
    }
    return sb.toString();
  }
};