aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/test/java/org/apache/velocity/test/provider/TestProvider.java
blob: 60eea81f3acfeb998592659f741906a2365ffaf9 (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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package org.apache.velocity.test.provider;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Stack;
import java.util.Vector;

/**
 * This class is used by the testbed. Instances of the class
 * are fed into the context that is set before the AST
 * is traversed and dynamic content generated.
 *
 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
 * @version $Id$
 */
public class TestProvider
{
    String title = "lunatic";
    boolean state;
    Object ob = null;

    public static String PUB_STAT_STRING = "Public Static String";

    int stateint = 0;


    public String getName()
    {
        return "jason";
    }

    public Stack getStack()
    {
        Stack stack = new Stack();
        stack.push("stack element 1");
        stack.push("stack element 2");
        stack.push("stack element 3");
        return stack;
    }

    public List getEmptyList()
    {
        return new ArrayList();
    }

    public List getList()
    {
        List list = new ArrayList();
        list.add("list element 1");
        list.add("list element 2");
        list.add("list element 3");

        return list;
    }

    public Hashtable getSearch()
    {
        Hashtable h = new Hashtable();
        h.put("Text", "this is some text");
        h.put("EscText", "this is escaped text");
        h.put("Title", "this is the title");
        h.put("Index", "this is the index");
        h.put("URL", "http://periapt.com");

        ArrayList al = new ArrayList();
        al.add(h);

        h.put("RelatedLinks", al);

        return h;
    }

    public Hashtable getHashtable()
    {
        Hashtable h = new Hashtable();
        h.put("key0", "value0");
        h.put("key1", "value1");
        h.put("key2", "value2");

        return h;
    }

    public ArrayList getRelSearches()
    {
        ArrayList al = new ArrayList();
        al.add(getSearch());

        return al;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public Object[] getMenu()
    {
        //ArrayList al = new ArrayList();
        Object[] menu = new Object[3];
        for (int i = 0; i < 3; i++)
        {
            Hashtable item = new Hashtable();
            item.put("id", "item" + (i+1));
            item.put("name", "name" + (i+1));
            item.put("label", "label" + (i+1));
            //al.add(item);
            menu[i] = item;
        }

        //return al;
        return menu;
    }

    public ArrayList getCustomers()
    {
        ArrayList list = new ArrayList();

        list.add("ArrayList element 1");
        list.add("ArrayList element 2");
        list.add("ArrayList element 3");
        list.add("ArrayList element 4");

        return list;
    }

    public ArrayList getCustomers2()
    {
        ArrayList list = new ArrayList();

        list.add(new TestProvider());
        list.add(new TestProvider());
        list.add(new TestProvider());
        list.add(new TestProvider());

        return list;
    }

    public Object me()
    {
        return this;
    }

    public String toString()
    {
        return ("test provider");
    }

    public Vector getVector()
    {
        Vector list = new Vector();

        list.addElement("vector element 1");
        list.addElement("vector element 2");

        return list;
    }

    public String[] getArray()
    {
        String[] strings = new String[2];
        strings[0] = "first element";
        strings[1] = "second element";
        return strings;
    }

    public boolean theAPLRules()
    {
        return true;
    }

    public boolean getStateTrue()
    {
        return true;
    }

    public boolean getStateFalse()
    {
        return false;
    }

    public String objectArrayMethod(Object[] o)
    {
        return "result of objectArrayMethod";
    }

    public String concat(Object[] strings)
    {
        StringBuilder result = new StringBuilder();

        for (Object string : strings)
        {
            result.append((String) string).append(' ');
        }

        return result.toString();
    }

    public String concat(List strings)
    {
        StringBuilder result = new StringBuilder();

        for (Object string : strings)
        {
            result.append((String) string).append(' ');
        }

        return result.toString();
    }

    public String objConcat(List objects)
    {
        StringBuilder result = new StringBuilder();

        for (Object object : objects)
        {
            result.append(object).append(' ');
        }

        return result.toString();
    }

    public String parse(String a, Object o, String c, String d)
    {
        return a + o.toString() + c + d;
    }

    public String concat(String a, String b)
    {
        return a + b;
    }

    // These two are for testing subclasses.

    public Person getPerson()
    {
        return new Person();
    }

    public Child getChild()
    {
        return new Child();
    }

    public String showPerson(Person person)
    {
        return person.getName();
    }

    /**
     * Chop i characters off the end of a string.
     *
     * @param string String to chop.
     * @param i Number of characters to chop.
     * @return String with processed answer.
     */
    public String chop(String string, int i)
    {
        return(string.substring(0, string.length() - i));
    }

    public boolean allEmpty(Object[] list)
    {
        int size = list.length;

        for (Object aList : list)
            if (aList.toString().length() > 0)
                return false;

        return true;
    }

    /*
     * This can't have the signature
     *
     *    public void setState(boolean state)
     *
     *    or dynamically invoking the method
     *    doesn't work ... you would have to
     *    put a wrapper around a method for a
     *    real boolean property that takes a
     *    Boolean object if you wanted this to
     *    work. Not really sure how useful it
     *    is anyway. Who cares about boolean
     *    values you can just set a variable.
     *
     */

    public void setState(Boolean state)
    {
    }

    public void setBangStart( Integer i )
    {
        System.out.println("SetBangStart() : called with val = " + i );
        stateint = i;
    }
    public Integer bang()
    {
        System.out.println("Bang! : " + stateint );
        Integer ret = stateint;
        stateint++;
        return ret;
    }

    /**
     * Test the ability of vel to use a get(key)
     * method for any object type, not just one
     * that implements the Map interface.
     */
    public String get(String key)
    {
        return key;
    }

    /**
     * Test the ability of vel to use a put(key)
     * method for any object type, not just one
     * that implements the Map interface.
     */
    public String put(String key, Object o)
    {
        ob = o;
        return key;
    }

    public String getFoo()
        throws Exception
    {
        throw new Exception("From getFoo()");
    }

    public String getThrow()
        throws Exception
    {
       throw new Exception("From getThrow()");
    }
}