KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > script > TestForeachToken


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.script;
16
17 import java.util.Arrays JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.easymock.MockControl;
24
25 /**
26  * Tests {@link org.apache.tapestry.script.ForeachToken}.
27  *
28  * @author Howard M. Lewis Ship
29  * @since 4.0
30  */

31 public class TestForeachToken extends HiveMindTestCase
32 {
33     private static class EchoToken extends AbstractToken
34     {
35         private String JavaDoc _key;
36
37         public EchoToken(String JavaDoc key)
38         {
39             super(null);
40
41             _key = key;
42         }
43
44         public void write(StringBuffer JavaDoc buffer, ScriptSession session)
45         {
46             Object JavaDoc value = session.getSymbols().get(_key);
47
48             buffer.append(value);
49             buffer.append("\n");
50         }
51     }
52
53     public void testFull()
54     {
55         Map JavaDoc symbols = new HashMap JavaDoc();
56
57         MockControl sc = newControl(ScriptSession.class);
58         ScriptSession s = (ScriptSession) sc.getMock();
59
60         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
61
62         Iterator JavaDoc i = Arrays.asList(new String JavaDoc[]
63         { "buffy", "angel" }).iterator();
64
65         s.evaluate("EXPRESSION", Iterator JavaDoc.class);
66         sc.setReturnValue(i);
67
68         s.getSymbols();
69         sc.setReturnValue(symbols, 5);
70
71         replayControls();
72
73         ForeachToken t = new ForeachToken("value", "index", "EXPRESSION", null);
74         t.addToken(new EchoToken("value"));
75         t.addToken(new EchoToken("index"));
76
77         t.write(buffer, s);
78
79         verifyControls();
80
81         assertEquals("buffy\n0\nangel\n1\n", buffer.toString());
82
83         assertEquals("angel", symbols.get("value"));
84         assertEquals("1", symbols.get("index"));
85     }
86
87     public void testNoIndex()
88     {
89         Map JavaDoc symbols = new HashMap JavaDoc();
90
91         symbols.put("index", "none");
92
93         MockControl sc = newControl(ScriptSession.class);
94         ScriptSession s = (ScriptSession) sc.getMock();
95
96         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
97
98         Iterator JavaDoc i = Arrays.asList(new String JavaDoc[]
99         { "buffy", "angel" }).iterator();
100
101         s.evaluate("EXPRESSION", Iterator JavaDoc.class);
102         sc.setReturnValue(i);
103
104         s.getSymbols();
105         sc.setReturnValue(symbols, 5);
106
107         replayControls();
108
109         ForeachToken t = new ForeachToken("value", null, "EXPRESSION", null);
110         t.addToken(new EchoToken("value"));
111         t.addToken(new EchoToken("index"));
112
113         t.write(buffer, s);
114
115         verifyControls();
116
117         assertEquals("buffy\nnone\nangel\nnone\n", buffer.toString());
118
119         assertEquals("angel", symbols.get("value"));
120         assertEquals("none", symbols.get("index"));
121     }
122
123     public void testNullIterator()
124     {
125         MockControl sc = newControl(ScriptSession.class);
126         ScriptSession s = (ScriptSession) sc.getMock();
127
128         s.evaluate("EXPRESSION", Iterator JavaDoc.class);
129         sc.setReturnValue(null);
130
131         IScriptToken inner = (IScriptToken) newMock(IScriptToken.class);
132
133         replayControls();
134         ForeachToken t = new ForeachToken("value", "index", "EXPRESSION", null);
135         t.addToken(inner);
136
137         t.write(null, s);
138
139         verifyControls();
140     }
141 }
Popular Tags